The ComboBox is a widget that allows the user to choose an item from a list of valid choices. The current choice is displayed. To make a choice, the user clicks into the display field to open a popup which shows a scrollable list of all possible choices.
There are several kinds of combo boxes that show choosable data items in different ways. Individually programmed components have to be used for some kinds of combo-boxes. The text-only combo box is particularly easy to use; it does not require individually programmed supportive components.
The image shows a screen shot of a window that contains a combo box placed above a drawing field. The Combo box shows the name of the currently selected drawing colour. Upon a left-click into the combo box, a list with the names of the selectable colours occurs.
The reason that colour names and not colours are displayed is simply that it is more easy to display colour names. To display colours, one has to program what is called a dedicated cell renderer. This will be done in one of the more advanced examples.
This introductory example uses a text-only combo box. That kind of a combo box is easily created and filled with data.
Archive file with this example: ComboBox-Example001.tar.gz. Be sure to read the file README.txt for further instructions.
import "gtk3" Graphics.UI.Gtk import Graphics.Rendering.Cairo as Cairo -- action to be performed after the window was closed destroyEventHandler :: IO () destroyEventHandler = do mainQuit -- function to translate a selection index into a color setting getColorToUse :: Int -> (Double, Double, Double) getColorToUse 0 = (1.0, 1.0, 1.0) -- white getColorToUse 1 = (1.0, 0.0, 0.0) -- red getColorToUse 2 = (0.0, 1.0, 0.0) -- green getColorToUse 3 = (1.0, 1.0, 0.0) -- yellow getColorToUse 4 = (0.0, 0.0, 1.0) -- blue -- function to draw a filled rectangle drawMethod :: Double -> Double -> (Double, Double, Double) -> Render () drawMethod w h (r, g, b) = do setSourceRGB r g b Cairo.rectangle 5.0 5.0 (w - 10.0) (h - 10.0) fill -- This function handles the draw event of the drawing area. -- Update the content of the drawing area paintHandler :: DrawingArea -> ComboBox -> Render() paintHandler drawingArea cb = do w <- liftIO $ widgetGetAllocatedWidth drawingArea h <- liftIO $ widgetGetAllocatedHeight drawingArea selIdx <- liftIO $ comboBoxGetActive cb drawMethod (fromIntegral w) (fromIntegral h) (getColorToUse selIdx) -- This function handles the changed event of the combo box. refreshDrawing :: DrawingArea -> IO () refreshDrawing paintArea = do widgetQueueDraw paintArea -- main program main :: IO () main = do initGUI -- all needed widgets are created and configured window <- windowNew vBox <- vBoxNew False 2 -- non-homogeneous packing paintArea <- drawingAreaNew comboBox <- comboBoxNew -- content for the combo box is provided here comboBoxSetModelText comboBox comboBoxAppendText comboBox "white" comboBoxAppendText comboBox "red" comboBoxAppendText comboBox "green" comboBoxAppendText comboBox "yellow" comboBoxAppendText comboBox "blue" comboBoxSetActive comboBox 0 widgetSetSizeRequest comboBox (-1) 38 -- the properties of the main window are set set window [windowDefaultWidth := 310, windowDefaultHeight := 280, widgetHeightRequest := 120, windowTitle := "ComboBox Demo", containerChild := vBox] windowSetPosition window WinPosCenter -- all needed callback functions are provided on window objectDestroy destroyEventHandler on comboBox changed (refreshDrawing paintArea) on paintArea draw (paintHandler paintArea comboBox) -- the placement of the widgets is done here boxPackStart vBox comboBox PackNatural 0 boxPackStart vBox paintArea PackGrow 0 -- all widgets are made visible widgetShow comboBox widgetShow paintArea widgetShow vBox widgetShow window -- the event processing is started mainGUI