Skip to content

Commit

Permalink
Add drag-scrolling functionality (middle mouse button).
Browse files Browse the repository at this point in the history
This feature was requested in #2.
  • Loading branch information
leftaroundabout committed Apr 25, 2016
1 parent 7c23c69 commit 3b307c5
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Graphics/Dynamic/Plot/R2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,8 @@ plotWindow graphs' = do

GTK.initGUI
window <- GTK.windowNew

mouseAnchor <- newIORef Nothing

refreshDraw <- do
drawA <- GTK.drawingAreaNew
Expand All @@ -775,6 +777,33 @@ plotWindow graphs' = do
BGTK.renderToGtk drawWindow $ scaledDia
return True

GTK.on drawA GTK.buttonPressEvent . Event.tryEvent $ do
Event.eventButton >>= guard.(==defaultDragButton)
anchXY <- Event.eventCoordinates
liftIO . writeIORef mouseAnchor $ Just anchXY
GTK.on drawA GTK.buttonReleaseEvent . Event.tryEvent $ do
Event.eventButton >>= guard.(==defaultDragButton)
liftIO . writeIORef mouseAnchor $ Nothing

GTK.on drawA GTK.motionNotifyEvent . Event.tryEvent $ do
liftIO (readIORef mouseAnchor) >>= \case
Just (oldX,oldY) -> do
(mvX,mvY) <- Event.eventCoordinates
(canvasX,canvasY) <- liftIO $ GTK.widgetGetSize drawA
let ηX = (oldX-mvX) / fromIntegral canvasX
ηY = (mvY-oldY) / fromIntegral canvasY
liftIO . modifyIORef viewTgt $ \view@GraphWindowSpecR2{..} ->
let w = rBound - lBound
h = tBound - bBound
in view{ lBound = lBound + w * ηX
, rBound = rBound + w * ηX
, tBound = tBound + h * ηY
, bBound = bBound + h * ηY
}
liftIO . modifyIORef mouseAnchor . fmap $ const (mvX,mvY)
Nothing -> mzero
GTK.widgetAddEvents drawA [GTK.ButtonMotionMask]

GTK.on drawA GTK.scrollEvent . Event.tryEvent $ do
(canvasX,canvasY) <- liftIO $ GTK.widgetGetSize drawA
(scrollX,scrollY) <- Event.eventCoordinates
Expand Down Expand Up @@ -958,6 +987,9 @@ defaultScrollBehaviour :: Event.ScrollDirection -> ScrollAction
defaultScrollBehaviour Event.ScrollUp = ScrollZoomIn
defaultScrollBehaviour Event.ScrollDown = ScrollZoomOut

defaultDragButton :: Event.MouseButton
defaultDragButton = Event.MiddleButton

scrollZoomStrength :: Double
scrollZoomStrength = 1/20

Expand Down

0 comments on commit 3b307c5

Please sign in to comment.