title: How to deal with application state in Gtk2Hs
body: <p>Trying to learn to write applications with Gtk2Hs I'm getting difficulties bridging the gap between the event driven Gtk2HS and the persistent state of my model. So to simplify, lets say that I have this simple application</p>
  
  <pre><code>module Main where
  
  import Graphics.UI.Gtk
  import Control.Monad.State
  
  main = do
      initGUI
      window &lt;- windowNew
      button &lt;- buttonNew
      set button [buttonLabel := "Press me"]
      containerAdd window button
  
      -- Events
      onDestroy window mainQuit
      onClicked button (putStrLn ---PUT MEANINGFUL CODE HERE---)
  
      widgetShowAll window
      mainGUI
  </code></pre>
  
  <p>and the state of my application is how many times the button has been pressed. Seeing other posts like <a href="https://stackoverflow.com/questions/5293898/how-to-pass-state-between-event-handlers-in-gtk2hs">this</a> they rely on MVars or IORefs which do not seem satisfactory to me, because in the future maybe I will want to refactor the code so the state lives on its own context. </p>
  
  <p>I think that the solution should use the State monad using a step function like:</p>
  
  <pre><code>State $ \s -&gt; ((),s+1)
  </code></pre>
  
  <p>but I'm not sure about the implications, how to do that in the above code or even if that monad is the right solution to my problem.</p>