haskellgtkgtk2hshaskell-diagrams

Diagrams and gtk2hs


I've been working on my summer research project, and I've been using the Diagrams library to draw graphs with edges, nodes. I'm simulating the spread of information throughout the graph, so my diagram is constantly updating discretely. I have all of the infrastructure for updating the graph's state data structures, drawing the diagrams etc.

For testing purposes, I've just been using the SVG backend with a feedback loop of getLine, updating the diagram, then rendering it to the same '.svg' file. This is obviously quite shoddy and relies on the fact that my .svg viewer updates when the file does.

I've looked into using the GTK backend for this, so I can just render the diagram to a drawingArea in a GTK window. I've got all of this installed and have been messing around with a few small programs.

However I cannot wrap my head around how I can implement my program. I've seen quite a few examples of how to draw a diagram in a GTK window, but none of them seem to include updating of that image based on a button press, or even something from the command line.

I've heard a bit about multithreading with GTK. I'm considering writing a function like:

renderDiagram :: DrawingArea -> Diagram B -> IO ()
renderDiagram c d = do
 c `on` exposeEvent $ do
   liftIO (defaultRender c d)
   return True
 widgetQueueDraw c

To change what diagram is rendered when the canvas is exposed, then forcing an expose event. If I then run my main program execution of updating the graph, and running renderDiagram on a separate thread to mainGUI, will I get my desired results? Is there a cleaner way to do this?

If anyone could point me in the right direction, or offer some external reading, that would be great. Thanks.


Solution

  • Have you checked this article? I think the article explains just what you are trying to do.

    You can use buttonPressEvent instead of motionNotifyEvent, if you want to do something upon mouse clicking.