wxHaskell multi-thread & custom event example


/ Published in: Haskell
Save to your folder(s)

This examples shows how to handle user-defined events in wxHaskell via menu events.

If the thread issues an event is not the main thread, use evtHandlerAddPendingEvent instead of evtHandlerProcessEvent.


Copy this code and paste it in your HTML
  1. import EnableGUI -- from http://wxhaskell.sourceforge.net/download/EnableGUI.hs
  2.  
  3. import Graphics.UI.WXCore hiding (Var)
  4. import Graphics.UI.WX hiding (Var, enter)
  5. import Data.Bits ((.&.), complement)
  6.  
  7. myEventId = wxID_HIGHEST+1 -- the custom event ID
  8. -- the custom event is registered as a menu event
  9. createMyEvent = commandEventCreate wxEVT_COMMAND_MENU_SELECTED myEventId
  10. registerMyEvent win io = evtHandlerOnMenuCommand win myEventId io
  11.  
  12. gui
  13. = do f <- frame [text := "custom event sample"]
  14. bt <- button f [text := "click to invoke a custom event"]
  15. set f [layout := column 1 [hfill (widget bt)]]
  16. set bt [on command := onClick f]
  17. registerMyEvent f (putStrLn "The custom event is fired!!")
  18. return ()
  19. where
  20. onClick f
  21. = do
  22. ev <- createMyEvent
  23. evtHandlerProcessEvent f ev
  24. return ()
  25.  
  26.  
  27. main = enableGUI >> start gui

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.