Advertisement
Guest User

panningMinimal

a guest
May 4th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .cabal
  2. -- Initial panningMinimal.cabal generated by cabal init.  For further
  3. -- documentation, see http://haskell.org/cabal/users-guide/
  4.  
  5. name:                panningMinimal
  6. version:             0.1.0.0
  7. -- synopsis:            
  8. -- description:        
  9. -- license:            
  10. license-file:        LICENSE
  11. author:              .
  12. maintainer:          .
  13. -- copyright:          
  14. -- category:            
  15. build-type:          Simple
  16. -- extra-source-files:  
  17. cabal-version:       >=1.10
  18.  
  19. executable panningMinimal
  20.   main-is:             Main.hs
  21.   -- other-modules:      
  22.   other-extensions:    PackageImports, MultiParamTypeClasses, GeneralizedNewtypeDeriving, FlexibleInstances
  23.   build-depends:       base >=4.6 && <4.7, gtk3, cgi >= 3001.2.2.0, cairo      >= 0.13.0.2, transformers >= 0.4.2.0, mtl >= 2.2.1
  24.   -- hs-source-dirs:      
  25.   default-language:    Haskell2010
  26.  
  27.  
  28.  
  29.  
  30. Main.hs
  31.  
  32. {-# LANGUAGE PackageImports #-}
  33. module Main where
  34.  
  35. import Control.Monad
  36. import "gtk3" Graphics.UI.Gtk
  37. import "gtk3" Graphics.UI.Gtk.Buttons.Button
  38. import "gtk3" Graphics.UI.Gtk.General.Enums
  39. import Network.CGI(liftIO)
  40. import WidgetBehavior
  41.  
  42. main = do
  43.         initGUI
  44.  
  45.         builder <- builderNew
  46.         builderAddFromFile builder "main.ui"
  47.  
  48.         window <- builderGetObject builder castToWindow "mainWindow"
  49.  
  50.         overlay <- builderGetObject builder castToOverlay "overlay"
  51.         viewport <- builderGetObject builder castToViewport "viewport"
  52.         scrolledWindow <- builderGetObject builder castToScrolledWindow "scrolledWindow"
  53.  
  54.         initViewportPanning viewport
  55.         image <- imageNewFromFile "redCat.jpg"
  56.  
  57.         containerAdd overlay image
  58.         set overlay [widgetOpacity := 0.9]
  59.  
  60.         window `on` deleteEvent $ liftIO mainQuit >> return False
  61.  
  62.         -- Display the window
  63.         widgetShowAll window
  64.         mainGUI
  65.  
  66. WidgetBehavior.hs
  67.  
  68. {-# LANGUAGE MultiParamTypeClasses      #-}
  69. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  70. {-# LANGUAGE PackageImports #-}
  71. {-# LANGUAGE FlexibleInstances #-}
  72. {-# LANGUAGE PackageImports #-}
  73.  
  74. module WidgetBehavior where
  75.  
  76. import Control.Monad
  77. import "gtk3" Graphics.UI.Gtk
  78. import "gtk3" Graphics.UI.Gtk.Buttons.Button
  79. import "gtk3" Graphics.UI.Gtk.General.Enums
  80. import "gtk3" Graphics.UI.Gtk.Gdk.EventM
  81. import Network.CGI(liftIO, MonadIO)
  82. import Control.Monad.State.Class
  83. import Control.Monad.Trans.Reader
  84. import Data.IORef
  85. import Control.Applicative
  86. import Control.Monad.Trans.Class
  87.  
  88. initViewportPanning :: (WidgetClass target, ViewportClass target) => target -> IO (ConnectId target)
  89. initViewportPanning target = do
  90.     widgetAddEvents target [Button1MotionMask]
  91.     initialCursorPosition <-newIORef (0, 0)
  92.     initialAdjustment <-newIORef (0, 0)
  93.     on target buttonPressEvent $ do
  94.         newPos <- eventCoordinates
  95.         liftIO $ do
  96.             writeIORef initialCursorPosition newPos
  97.             hAdj <- viewportGetHAdjustment target
  98.             hVal <- adjustmentGetValue hAdj
  99.             vAdj <- viewportGetVAdjustment target
  100.             vVal <- adjustmentGetValue vAdj
  101.             writeIORef initialAdjustment (hVal, vVal)
  102.         liftIO $ putStrLn "pressed"
  103.         return True
  104.     on target motionNotifyEvent $ do
  105.         (newH, newV) <- eventCoordinates
  106.         liftIO $ do
  107.             putStrLn ("motion at " ++ show newH ++ " , "++ show newV)
  108.             hAdj <- viewportGetHAdjustment target
  109.             vAdj <- viewportGetVAdjustment target
  110.             (initAdjH, initAdjV) <- readIORef initialAdjustment
  111.             (initCH, initCV) <- readIORef initialCursorPosition
  112.  
  113.             adjustmentSetValue hAdj (initAdjH - (newH - initCH) )
  114.             adjustmentSetValue vAdj (initAdjV - (newV - initCV) )
  115.  
  116.             adjustmentValueChanged hAdj
  117.             adjustmentValueChanged vAdj
  118.             return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement