The tracking of the mouse pointer and the handling of mouse button clicks are needed in graphics applications.
Gtk distinguishes the following mouse events:
PointerMotion
The event is sent every time the mouse pointer changes its position.
Enter
The event is sent whenever the mouse pointer enters a widget.
Leave
The event is sent whenever the mouse pointer leaves a widget.
ButtonPress
The event is sent whenever a mouse button is pressed.
ButtonRelease
The event is sent whenever a pressed mouse button is released.
These events are only sent when Gtk is asked to send them. It is possible to ask Gtk to send only some of these events.
This section presents examples that handle all mouse events. To illustrate different programming styles, several solutions for the handling of mouse events are shown.
Archive file with this example: MouseMovement-Example001.tar.gz. Be sure to read the file README.txt for further instructions.
import "gtk3" Graphics.UI.Gtk import "gtk3" Graphics.UI.Gtk.Gdk.Events import Control.Monad.IO.Class -- for liftIO import Graphics.Rendering.Cairo import Control.Applicative -- for <$> -- This function draws a diagonal line from the top left corner -- to the bottom right corner of the drawing area. -- The function arguments are: -- w width of the drawing area -- h height of thedrawing area -- strokewidth the width of the stroke to be drawn drawMethod :: Double -> Double -> Double -> Render () drawMethod w h strokeWidth = do setSourceRGB 1.0 0 0 setLineWidth strokeWidth setLineCap LineCapRound setAntialias AntialiasNone moveTo 20 20 lineTo (w - 20) (h - 20) strokePreserve setSourceRGB 1.0 1.0 1.0 setLineWidth 1.0 stroke -- This function returns a string, the description of a mouse button identifyButton :: MouseButton -> String identifyButton LeftButton = "left Button" identifyButton MiddleButton = "middle Button" identifyButton RightButton = "right Button" identifyButton _ = "other Button" -- This function has the responsibility to handle the objectDestroy -- event of the window. The function exits the event handler loop. -- The program can then terminate. destroyEventHandler :: IO () destroyEventHandler = do mainQuit main :: IO () main = do initGUI -- all needed widgets are created window <- windowNew vBox <- vBoxNew False 2 -- non-homogeneous packing paintArea <- drawingAreaNew displayField <- labelNew (Just " ") -- attributes of the notebook are set set window [windowDefaultWidth := 450, windowDefaultHeight := 480, windowWindowPosition := WinPosCenter, containerChild := vBox, windowTitle := "Mouse Movement Demo"] widgetAddEvents paintArea [PointerMotionMask, EnterNotifyMask, LeaveNotifyMask, ButtonPressMask, ButtonReleaseMask] -- all needed callback functions are provided on paintArea enterNotifyEvent $ do liftIO $ labelSetText displayField "drawingArea entered" return False on paintArea leaveNotifyEvent $ do liftIO $ labelSetText displayField "drawingArea left" return False on paintArea buttonPressEvent $ do bttnId <- Graphics.UI.Gtk.eventButton liftIO $ labelSetText displayField (identifyButton bttnId ++ " pressed") return False on paintArea buttonReleaseEvent $ do bttnId <- Graphics.UI.Gtk.eventButton liftIO $ labelSetText displayField (identifyButton bttnId ++ " released") return False on paintArea motionNotifyEvent $ do (x, y) <- eventCoordinates liftIO $ labelSetText displayField ("mouse pointer moved: " ++ show (round x) ++ ", " ++ show (round y)) return False on paintArea draw $ do w <- liftIO $ (fromIntegral <$> widgetGetAllocatedWidth paintArea) h <- liftIO $ (fromIntegral <$> widgetGetAllocatedHeight paintArea) drawMethod w h 5.0 on window configureEvent $ do (w, h) <- eventSize liftIO (putStrLn (show w ++ "x" ++ show h)) return False on window objectDestroy destroyEventHandler -- the placement of the widgets is done here boxPackStart vBox displayField PackNatural 0 boxPackStart vBox paintArea PackGrow 0 -- all widgets are made visible widgetShow displayField widgetShow paintArea widgetShow vBox widgetShow window -- the event processing is started mainGUI
Archive file with this example: MouseMovement-Example002.tar.gz. Be sure to read the file README.txt for further instructions.
This solution uses named functions with explicitly declared types. It is hoped that this solution gives a better understanding ....
Archive file with this example: MouseMovement-Example003.tar.gz. Be sure to read the file README.txt for further instructions.