The datatype ListStore is provided to store a list of choosable data items. Objects of type ListStore are used as data models for the ComboBox as well as for simple uses of the TreeView component.
ListStore is a generic datatype. To derive an usable type, you have to specify the type of the list elements to be stored in the ListStore object.
A ListStore for string-valued data items has type
(ListStore String)
An empty instance of that type can be created with the statement
store <- listStoreNew([]::[String])
Note that the function listStoreNew is called with a typed empty list.
Technically, a ListStore is a simplified interface to a specialized TreeModel.
import "gtk3" Graphics.UI.Gtk import Graphics.Rendering.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 -- function parameters: -- w width of the rectangle to be drawn -- h height of the rectangle to be drawn -- (r, g, b) fill color of the rectangle to be drawn drawMethod :: Double -> Double -> (Double, Double, Double) -> Render () drawMethod w h (r, g, b) = do setSourceRGB r g b rectangle 5.0 5.0 (w - 10.0) (h - 10.0) fill -- 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) refreshDrawing :: DrawingArea -> IO () refreshDrawing paintArea = do widgetQueueDraw paintArea -- The data for the list store of the combo box are provided here. setupListStore :: (ListStore String) -> IO () setupListStore store = do listStoreAppend store "white" listStoreAppend store "red" listStoreAppend store "green" listStoreAppend store "yellow" listStoreAppend store "blue" return () -- setAttributesOfRenderer :: String -> [AttrOp CellRendererText] setAttributesOfRenderer a = [cellText := a] -- A combo box is equipped with a list store and a text cell renderer. -- The font family, font weight and font size are individually configured. setupComboBox :: ComboBox -> IO () setupComboBox comboBox = do cellLayoutClear comboBox store <- listStoreNew([]::[String]) comboBoxSetModel comboBox (Just store) setupListStore store -- must not precede the comboBoxSetModel statement let colId = makeColumnIdString 0 customStoreSetColumn store colId id comboBoxSetEntryTextColumn comboBox colId ren <- cellRendererTextNew fnt <- fontDescriptionNew fontDescriptionSetFamily fnt "Serif" fontDescriptionSetWeight fnt WeightBold fontDescriptionSetSize fnt 14 set ren [cellTextFontDesc := fnt] cellLayoutPackStart comboBox ren True cellLayoutSetAttributes comboBox ren store setAttributesOfRenderer comboBoxSetActive comboBox 0 widgetSetSizeRequest comboBox (-1) 38 -- 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 setupComboBox comboBox -- modified -- the properties of the main window are set set window [windowDefaultWidth := 310, windowDefaultHeight := 280, windowWindowPosition := WinPosCenter, containerChild := vBox, windowTitle := "ComboBox Demo"] widgetModifyBg paintArea StateNormal (Color 65535 65535 65535) -- all needed callback functions are provided on window objectDestroy $ destroyEventHandler on comboBox changed $ (refreshDrawing paintArea) on paintArea draw $ (paintHandler paintArea comboBox) 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
Calling cellLayoutSetAttributes with a function name
.... This variant makes the type of the function explicit.