3

I have a Window with three Entry widgets and one Button. I use the button to remove one of the widgets programmatically. The problem is that the main window doesn't change it's size to fit the new layout after it's been removed.

I can imagine that I need to send some Signal or Event to the main loop which would cause the recalculation but I've been unable to find such functionality.

Here is some example code:

import Graphics.UI.Gtk
import Data.IORef
import qualified Graphics.UI.Gtk as G hiding (Point)
import qualified Graphics.UI.Gtk.Gdk.EventM as E
import qualified Graphics.UI.Gtk.Abstract.Widget as W
import qualified Graphics.Rendering.Cairo as C


makeEntry :: String -> IO Entry
makeEntry str = do e <- entryNew
                   entrySetText e str
                   return e

main :: IO ()
main = do
  initGUI
  window <- windowNew
  box <- vBoxNew False 0
  G.on window G.keyPressEvent $ E.tryEvent $ do
    "Escape" <- E.eventKeyName
    C.liftIO $ G.widgetDestroy window

  set window [ containerChild := box ]

  e1 <- makeEntry "e1"
  boxPackStart box e1 PackNatural 0

  e2 <- makeEntry "e2"
  boxPackStart box e2 PackNatural 0

  e3 <- makeEntry "e3"
  boxPackStart box e3 PackNatural 0

  button <- buttonNew
  set button [ buttonLabel := "Remove" ]
  boxPackStart box button PackNatural 0

  onClicked button (containerRemove box e2)
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI
Peter Jankuliak
  • 3,464
  • 1
  • 29
  • 40

1 Answers1

3

You can ask your top-level window how big it wants to be, and make it be that big:

refresh window = do
    Requisition w h <- widgetSizeRequest window
    windowResize window w h

To use this, stick it in the button's click-handler:

onClicked button (containerRemove box e2 >> refresh window)
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380