I am quite new to haskell, and would like to write a GUI with gtk. The goal of the program is to periodically poll a UART interface (which already works), and update the values of a chart. I use the "Chart" library. I have already come as far as drawing a single chart in a window. Now I currently use "G.timeoutAdd" http://hackage.haskell.org/package/gtk-0.15.0/docs/Graphics-UI-Gtk-General-General.html, to which I pass the function that draws some values.
Then i get the following GTK warning:
(:24592): Gtk-WARNING **: 12:06:59.996: Attempting to add a widget with type GtkDrawingArea to a container of type GtkWindow, but the widget is already inside a container of type GtkWindow, the GTK+ FAQ at http://library.gnome.org/devel/gtk-faq/stable/ explains how to reparent a widget
I don't know what kind of pointer Magic Haskell does under the hood. And It seems, that i would have to use a "forever" with some kind of delay ? Is there a standart procedure for recurrent calls with a timedelay ?
The Current code:
module Main where
import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Gtk
import qualified Graphics.UI.Gtk as G
import qualified Graphics.Rendering.Cairo as C
import qualified Graphics.UI.Gtk.Gdk.Events as GE
import Graphics.Rendering.Chart.Renderable
import Graphics.Rendering.Chart.Geometry
import Graphics.Rendering.Chart.Drawing
import Graphics.Rendering.Chart.Backend.Cairo
import Data.IORef
import Control.Monad(when)
import System.IO.Unsafe(unsafePerformIO)
-- 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
linechart list = toRenderable layout
where
lineplot = plot_lines_values .~ list
$ plot_lines_style . line_color .~ opaque blue
$ plot_lines_title .~ "Plot"
$ def
layout = layout_title .~ "Amplitude Modulation"
$ layout_plots .~ [toPlot lineplot]
$ def
func :: Double -> Double
func x = (sin (x*3.14159/45) + 1) / 2 * (sin (x*3.14159/5))
createWindowAndCanvas :: Renderable a -> Int -> Int -> IO (G.Window, G.DrawingArea)
createWindowAndCanvas chart windowWidth windowHeight = do
window <- G.windowNew
canvas <- G.drawingAreaNew
G.widgetSetSizeRequest window windowWidth windowHeight
G.onExpose canvas $ const (updateCanvas chart canvas)
G.set window [G.containerChild G.:= canvas]
return (window, canvas)
getSerialDataAndUpdateCanvas :: G.DrawingArea ->IO(Bool)
getSerialDataAndUpdateCanvas canvas = do
-- I Thought that here, i could do the fetching of the data from
-- the UART/Serialport and then give the data to "x"
umpdateCanvas (linechart [[ (x, func x) | x <- [0,0.5 .. 40.0 ]]]) canvas
return True
umpdateCanvas :: Renderable a -> G.DrawingArea -> IO Bool
umpdateCanvas chart canvas = do
win <- G.widgetGetDrawWindow canvas
(width, height) <- G.widgetGetSize canvas
regio <- G.regionRectangle $ GE.Rectangle 0 0 width height
let sz = (fromIntegral width, fromIntegral height)
G.drawWindowBeginPaintRegion win regio
G.renderWithDrawable win $ runBackend (defaultEnv bitmapAlignmentFns) (render chart sz)
G.drawWindowEndPaint win
return True
main :: IO ()
main = do
let emptyList = [[]] :: [[(Double, Double)]]
emptyChart = linechart emptyList
initGuiOnce
(window, canvas) <- createWindowAndCanvas emptyChart 400 400
G.set window [G.containerChild G.:= canvas]
window `G.on` G.keyPressEvent $ do
C.liftIO (G.widgetDestroy window)
return True
window `G.on` G.objectDestroy $ G.mainQuit
-- Calls function with the timeout in ms
G.timeoutAdd (getSerialDataAndUpdateCanvas canvas) 20
G.widgetShowAll window
G.mainGUI
I use the newest version of stack, with the following extra deps in stack.yaml
extra-deps:
- gtk-0.14.10
- gio-0.13.5.0
- SVGFonts-1.6.0.3
- diagrams-core-1.4.1.1
- diagrams-lib-1.4.2.3
- diagrams-postscript-1.4.1
- diagrams-svg-1.4.2
- diagrams-solve-0.1.1
- dual-tree-0.2.2
- Chart-1.9
- Chart-cairo-1.9
- Chart-diagrams-1.9
- Chart-gtk-1.9
and dependencies in package.yaml
dependencies:
- base >= 4.7 && < 5
- gtk
- Chart
- cairo
- Chart-diagrams
- Chart-gtk
- Chart-cairo
G.set window [G.containerChild G.:= canvas]
both increateWindowAndCanvas
and inmain
. Perhaps that's related. – Daniel Wagner