module GHC.Vis.View.Graph (
export,
redraw,
click,
move,
updateObjects
)
where
#if __GLASGOW_HASKELL__ < 706
import Prelude hiding (catch)
#endif
import Graphics.UI.Gtk hiding (Box, Signal, Rectangle, Object)
import qualified Graphics.UI.Gtk as Gtk
import Graphics.Rendering.Cairo
import Control.Concurrent
import Control.Monad
import Control.Exception
import Data.IORef
import System.IO.Unsafe
import GHC.Vis.View.Graph.Parser
import GHC.Vis.Types hiding (State, View(..))
import GHC.Vis.View.Common
import GHC.HeapView hiding (size)
import Graphics.XDot.Viewer
import Graphics.XDot.Types hiding (size, w, h)
data State = State
{ boxes :: [Box]
, operations :: [(Object Int, Operation)]
, totalSize :: (Double, Double, Double, Double)
, bounds :: [(Object Int, (Double, Double, Double, Double))]
, hover :: Object Int
}
state :: IORef State
state = unsafePerformIO $ newIORef $ State [] [] (0, 0, 1, 1) [] None
redraw :: WidgetClass w => w -> IO ()
redraw canvas = do
s <- readIORef state
Gtk.Rectangle _ _ rw2 rh2 <- widgetGetAllocation canvas
boundingBoxes <- render canvas (draw s rw2 rh2)
modifyIORef state (\s' -> s' {bounds = boundingBoxes})
export :: DrawFunction -> String -> IO ()
export drawFn file = do
s <- readIORef state
let (_, _, xSize, ySize) = totalSize s
drawFn file xSize ySize
(\surface -> renderWith surface (draw s (round xSize) (round ySize)))
return ()
draw :: State -> Int -> Int -> Render [(Object Int, Rectangle)]
draw s rw2 rh2 = do
vS <- liftIO $ readIORef visState
let rw = 0.97 * fromIntegral rw2
rh = 0.97 * fromIntegral rh2
ops = operations s
size@(_,_,sw,sh) = totalSize s
(sx,sy) = (zoomRatio vS * min (rw / sw) (rh / sh), sx)
(ox1,oy1) = (0.5 * fromIntegral rw2, 0.5 * fromIntegral rh2)
(ox2,oy2) = position vS
(ox,oy) = (ox1 + ox2, oy1 + oy2)
translate ox oy
scale sx sy
result <- drawAll (hover s) size ops
return $ map (\(o, (x,y,w,h)) -> (o,
( x * sx + ox
, y * sy + oy
, w * sx
, h * sy
))) result
render :: WidgetClass w => w -> Render b -> IO b
render canvas r = do
win <- widgetGetDrawWindow canvas
renderWithDrawable win r
click :: IO ()
click = do
s <- readIORef state
case hover s of
Node t -> unless (length (boxes s) <= t) $ do
evaluate2 $ boxes s !! t
void $ forkIO $ putMVar visSignal UpdateSignal
_ -> return ()
evaluate2 :: Box -> IO ()
evaluate2 b@(Box a) = do
c <- getBoxedClosureData b
case c of
IndClosure{} -> a `seq` return ()
BlackholeClosure{} -> a `seq` return ()
FunClosure{} -> a `seq` return ()
ThunkClosure{} -> a `seq` return ()
APClosure{} -> a `seq` return ()
PAPClosure{} -> a `seq` return ()
_ -> return ()
`catch`
\(e :: SomeException) -> putStrLn $ "Caught exception while evaluating: " ++ show e
move :: WidgetClass w => w -> IO ()
move canvas = do
vs <- readIORef visState
oldS <- readIORef state
let oldHover = hover oldS
modifyIORef state $ \s' -> (
let (mx, my) = mousePos vs
check (o, (x,y,w,h)) =
if x <= mx && mx <= x + w &&
y <= my && my <= y + h
then o else None
validOne (None:xs) = validOne xs
validOne (x:_) = x
validOne _ = None
in s' {hover = validOne $ map check (bounds oldS)}
)
s <- readIORef state
unless (oldHover == hover s) $ widgetQueueDraw canvas
updateObjects :: [(Box, String)] -> IO ()
updateObjects bs = do
(ops, bs', size) <- xDotParse bs
modifyIORef state (\s -> s {operations = ops, boxes = bs', totalSize = size, hover = None})