开发者

gtk2hs: Request recalculation of windows size after removing a widget

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


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜