2016-03-30 8 views
2

Ich möchte ein Popup GTK Menu erstellen und fügen Sie dann einige MenuItems zu ihm hinzu. Ich fand dieses bit of sample code for popup menus, aber es war bittrotted. Die feste Version ist unten. Suchen Sie nach dem Kommentar "Dies ist das Bit, das nicht funktioniert!".Hinzufügen eines MenuItem zu einem Menü in Gtk2hs

Das Problem besteht darin, dass das Menü Item I hinzufügen nicht angezeigt wird, wenn das Menü angezeigt wird. Die mit dem UI-Manager erstellten Aktionselemente sind dort, aber nicht das Element "Test", das ich manuell erstellt habe.

Gibt es einen Schritt fehlt mir? Oder mache ich das falsch?

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) 

Antwort

1

Ich habe es herausgefunden. Ich muss "widgetShow" auf dem neuen Menüpunkt aufrufen.

 testItem <- menuItemNewWithLabel "Test" 
    widgetShow testItem 
    testItem `on` menuItemActivated $ putStrLn "Test clicked" 
    menuShellAppend (castToMenu pop) testItem 
Verwandte Themen