Chart 0.13 → 0.13.1
raw patch · 3 files changed
+44/−15 lines, 3 files
Files
- Chart.cabal +1/−1
- Graphics/Rendering/Chart/Gtk.hs +41/−12
- tests/TestParametric.hs +2/−2
Chart.cabal view
@@ -1,5 +1,5 @@ Name: Chart-Version: 0.13+Version: 0.13.1 License: BSD3 License-file: LICENSE Copyright: Tim Docker, 2006-2009
Graphics/Rendering/Chart/Gtk.hs view
@@ -9,37 +9,66 @@ ) where import qualified Graphics.UI.Gtk as G-import qualified Graphics.UI.Gtk.Gdk.Events as G+import qualified Graphics.UI.Gtk.Gdk.Events as GE import qualified Graphics.Rendering.Cairo as C import Graphics.Rendering.Chart import Graphics.Rendering.Chart.Renderable import Graphics.Rendering.Chart.Types import Data.List (isPrefixOf)+import Data.IORef+import Control.Monad(when)+import System.IO.Unsafe(unsafePerformIO) -- do action m for any keypress (except meta keys)-anyKey :: (Monad m) => m a -> G.Event -> m Bool-anyKey m (G.Key {G.eventKeyName=key})+anyKey :: (Monad m) => m a -> GE.Event -> m Bool+anyKey m (GE.Key {GE.eventKeyName=key}) | any (`isPrefixOf` key) ignores = return True | otherwise = m >> return True where ignores = ["Shift","Control","Alt", "Super","Meta","Hyper"] +-- Yuck. But we really want the convenience function+-- renderableToWindow as to be callable without requiring+-- initGUI to be called first. But newer versions of+-- gtk insist that initGUI is only called once+guiInitVar :: IORef Bool+{-# NOINLINE guiInitVar #-}+guiInitVar = unsafePerformIO (newIORef False)++initGuiOnce :: IO ()+initGuiOnce = do+ v <- readIORef guiInitVar+ when (not v) $ do+ -- G.initGUI+ G.unsafeInitGUIForThreadedRTS+ writeIORef guiInitVar True++-- | Display a renderable in a gtk window.+--+-- Note that this is a convenience function that initialises GTK on+-- it's first call, but not subsequent calls. Hence it's +-- unlikely to be compatible with other code using gtk. In +-- that case use createRenderableWindow. renderableToWindow :: Renderable a -> Int -> Int -> IO () renderableToWindow chart windowWidth windowHeight = do- G.unsafeInitGUIForThreadedRTS- -- G.initGUI+ initGuiOnce+ window <- createRenderableWindow chart windowWidth windowHeight+ -- press any key to exit the loop+ G.onKeyPress window $ anyKey (G.widgetDestroy window)+ G.onDestroy window G.mainQuit+ G.widgetShowAll window+ G.mainGUI++-- | Create a new GTK window displaying a renderable.+createRenderableWindow :: Renderable a -> Int -> Int -> IO G.Window+createRenderableWindow chart windowWidth windowHeight = do window <- G.windowNew canvas <- G.drawingAreaNew- -- fix size- -- G.windowSetResizable window False G.widgetSetSizeRequest window windowWidth windowHeight- -- press any key to quit- G.onKeyPress window $ anyKey (G.widgetDestroy window)- G.onDestroy window G.mainQuit G.onExpose canvas $ const (updateCanvas chart canvas) G.set window [G.containerChild G.:= canvas]- G.widgetShowAll window- G.mainGUI+ return window+ updateCanvas :: Renderable a -> G.DrawingArea -> IO Bool updateCanvas chart canvas = do
tests/TestParametric.hs view
@@ -23,8 +23,8 @@ $ defaultLayout1 main1 :: [String] -> IO()-main1 ["small"] = renderableToPNGFile (chart 0.25) 320 240 "test_parametric_small.png"-main1 ["big"] = renderableToPNGFile (chart 0.25) 800 600 "test_parametric_big.png"+main1 ["small"] = renderableToPNGFile (chart 1.00) 320 240 "test_parametric_small.png"+main1 ["big"] = renderableToPNGFile (chart 1.00) 800 600 "test_parametric_big.png" main1 _ = renderableToWindow (chart 1.00) 640 480 main = getArgs >>= main1