How to change properties of DrawingArea in Gtk2Hs

977 Views Asked by At

Can someone please point me in the right direction when it comes to changing properties of an element in Gtk2Hs.

For example, how do I change the background-color of a DrawingArea?

1

There are 1 best solutions below

2
On BEST ANSWER

There are various methods for modifying a widget's style. For example to modify the background style you can use widgetModifyBg (corresponding to the C function gtk_widget_modify_bg()). In principle, if you change the style for one state (e.g. StateNormal) then you should also change it for the others.

Y would suggest you describe the styles you want in an RC file, and then load that file from your application, but it seems that functions like gtk_rc_parse() are not bound in gtk2hs.

Here's an example:

import Graphics.UI.Gtk

main = do
    initGUI
    window <- windowNew
    window `onDestroy` mainQuit
    drawingArea <- drawingAreaNew
    window `containerAdd` drawingArea
    widgetModifyBg drawingArea StateNormal (Color 0xffff 0 0)
    widgetShowAll window
    mainGUI

If you need to do custom drawing based on a widget's styles, you can do that using widgetGetState, the widgetStyle property and the styleGet* family of functions (e.g. styleGetText). Here's an example of that:

import Graphics.Rendering.Cairo
import Graphics.UI.Gtk hiding (fill)
import Graphics.UI.Gtk.Gdk.Events (Event(Expose))

expose widget rect = do
    state <- widgetGetState widget
    style <- widget `get` widgetStyle
    (Color red green blue) <- styleGetText style state
    drawWindow <- widgetGetDrawWindow widget

    renderWithDrawable drawWindow $ do
        moveTo 50 50
        setFontSize 20
        setSourceRGB (fromIntegral red / 0xffff)
                     (fromIntegral green / 0xffff)
                     (fromIntegral blue / 0xffff)
        showText "O HAI"
        fill

    return False

main = do
    initGUI
    window <- windowNew
    window `onDestroy` mainQuit
    drawingArea <- drawingAreaNew
    drawingArea `onExpose` \(Expose sent area region count) ->
        expose drawingArea area
    window `containerAdd` drawingArea
    widgetShowAll window
    mainGUI