Reputation: 17786
I want to create a popup GTK Menu and then add some MenuItems to it. I found this bit of sample code for popup menus but it was bitrotted. The fixed version is below. Look for the comment "This is the bit that doesn't work!".
The trouble is that the MenuItem I add doesn't appear when the menu is displayed. The action items created with the UI Manager are there, but not the "Test" item I created manually.
Is there some step I'm missing? Or am I going about this the wrong way?
import Control.Monad.IO.Class
import Graphics.UI.Gtk
main :: IO ()
main= do
initGUI
window <- windowNew
set window [windowTitle := "Click Right Popup",
windowDefaultWidth := 250,
windowDefaultHeight := 150 ]
eda <- actionNew "EDA" "Edit" Nothing Nothing
pra <- actionNew "PRA" "Process" Nothing Nothing
rma <- actionNew "RMA" "Remove" Nothing Nothing
saa <- actionNew "SAA" "Save" Nothing Nothing
agr <- actionGroupNew "AGR1"
mapM_ (actionGroupAddAction agr) [eda,pra,rma,saa]
uiman <- uiManagerNew
uiManagerAddUiFromString uiman uiDecl
uiManagerInsertActionGroup uiman agr 0
maybePopup <- uiManagerGetWidget uiman "/ui/popup"
let pop = case maybePopup of
(Just x) -> x
Nothing -> error "Cannot get popup from string"
window `on` buttonPressEvent $ do
b <- eventButton
if b == RightButton
then do
liftIO $ menuPopup (castToMenu pop) Nothing
return True
else return True
-- This is the bit that doesn't work!
testItem <- menuItemNewWithLabel "Test"
testItem `on` menuItemActivated $ putStrLn "Test clicked"
menuShellAppend (castToMenu pop) testItem
mapM_ prAct [eda,pra,rma,saa]
widgetShowAll window
window `on` objectDestroy $ mainQuit
mainGUI
uiDecl = "<ui> \
\ <popup>\
\ <menuitem action=\"EDA\" />\
\ <menuitem action=\"PRA\" />\
\ <menuitem action=\"RMA\" />\
\ <separator />\
\ <menuitem action=\"SAA\" />\
\ </popup>\
\ </ui>"
prAct :: ActionClass self => self -> IO (ConnectId self)
prAct a = a `on` actionActivated $ do
name <- actionGetName a
putStrLn ("Action Name: " ++ name)
Upvotes: 2
Views: 75
Reputation: 17786
I've figured it out. I need to call "widgetShow" on the new menu item.
testItem <- menuItemNewWithLabel "Test"
widgetShow testItem
testItem `on` menuItemActivated $ putStrLn "Test clicked"
menuShellAppend (castToMenu pop) testItem
Upvotes: 1