Search code examples
haskellgtkgtk2hs

Using haskell-gi GTK signals


Haskell Gi-GTK noob here. And GTK noob in general.

I have an image, which I've wrapped in an Eventbox. Now, I want to detect when the user presses on the event box (mousedown). Some Googling points me to using button-press-event. My code is below.

drag <- imageNewFromFile "rszh.png"
dragevents <- eventBoxNew
containerAdd dragevents drag
set dragevents [widgetHalign := AlignEnd, widgetValign := AlignEnd]
onWidgetButtonPressEvent dragevents (print "Hello world")

And GHC fails to compile this with the following cryptic error message:

panedraggin.hs:30:42: error:
    • Couldn't match type ‘IO ()’
                     with ‘GI.Gdk.Structs.EventButton.EventButton -> IO Bool’
      Expected type: GI.Gtk.Objects.Widget.WidgetButtonPressEventCallback
        Actual type: IO ()
    • Possible cause: ‘print’ is applied to too many arguments
      In the second argument of ‘onWidgetButtonPressEvent’, namely
        ‘(print "Hello world")’
      In a stmt of a 'do' block:
        onWidgetButtonPressEvent dragevents (print "Hello world")
      In the expression:
        do { Gtk.init Nothing;
             window <- windowNew WindowTypeToplevel;
             onWidgetDestroy window mainQuit;
             windowMaximize window;
             .... }

What am I doing incorrectly?


Solution

  • Well the error message already says it: it expects a function of the type EventButton -> IO Bool, and print "Hello world" has a type IO ().

    You can however easily transform it into one, with:

    onWidgetButtonPressEvent dragevents (const $ print "Hello world" >> return True)

    So by using const $ we ignore the EventButton parameter for now (later you can decide to take the event parameters into account), and by using >> return True, we make sure that after printing, we return True (meaning the callback succeeded).