zu www.bildungsgueter.de next page Table of Section Contents previous page

Use of the ComboBox Widget

Introductory Example


Specification of a Font and of Colours

The combo box uses a cell renderer to display the selected item and the selectable items in the pop up list. When a combo box is prepared with the comboBoxSetModelText statement, the cell renderer is created and furnished with a default font and with default colours.

To use a specific font or individually choosen colours, you have to modify the cell renderer.

Archive file with this example: ComboBox-Example001Fnt.tar.gz. Be sure to read the file README.txt for further instructions.

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
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

installFont :: ComboBox -> IO () --  inserted
installFont cb =
  do
    fnt <- fontDescriptionNew
    fontDescriptionSetFamily fnt "Monospace"
    fontDescriptionSetWeight fnt WeightBold
    fontDescriptionSetSize fnt 16
    layouts <- cellLayoutGetCells (toComboBox cb)
    let layout : ls  = layouts
    set (castToCellRendererText layout) [cellTextFontDesc := fnt]
 --  end of insertion
    
--  main program
main :: IO ()
main = do
    initGUI
    window <- windowNew
    vBox <- vBoxNew False 2   -- non-homogeneous packing
    paintArea <- drawingAreaNew
    comboBox <- comboBoxNew
    comboBoxSetModelText comboBox
    installFont comboBox              --  one-line insertion
    comboBoxAppendText comboBox "white"
    comboBoxAppendText comboBox "red"
    comboBoxAppendText comboBox "green"
    comboBoxAppendText comboBox "yellow"
    comboBoxAppendText comboBox "blue"
    comboBoxSetActive comboBox 0
    widgetSetSizeRequest comboBox (-1) 38
    set window [windowDefaultWidth := 310, windowDefaultHeight := 280,
                containerChild := vBox, windowTitle := "ComboBox Demo"]
    windowSetPosition window WinPosCenter
    widgetModifyBg paintArea StateNormal (Color 65535 65535 65535)
    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
    widgetShow comboBox
    widgetShow paintArea
    widgetShow vBox
    widgetShow window
    mainGUI

previous page Table of Section Contents next page