Note: The following items created for a window are also automatically freed when the window is closed: Gadgets, Keyboard shortcuts, Menus, StatusBars, Timers, Toolbars and bound events (with BindEvent()).
I thought I understood this, but maybe not...?
Run my issue-demo program below, click on a main menu item and notice that nothing happens (as expected, since no event is hooked up to the main menu as the relevant line is commented out).
Now open the Stuff window, invoke its popupmenu and see it generate debug messages as expected upon menuitems being clicked. Close the Stuff window and again click on any of the main menu items. It now invokes the popupmenu from the Stuff window...despite the main menu not being hooked up to an event and despite the Stuff window having been closed.
Can someone enlighten me?
Code: Select all
DeclareModule Stuff
Declare StuffMain()
EndDeclareModule
Module Stuff
EnableExplicit
Declare CreatePopupMenuStuff()
Declare PopupMenuStuffClick()
Declare ShowPopupMenuStuff()
Declare WindowClose()
Global StuffWindow, PopupMenuStuff
Procedure CreatePopupMenuStuff()
PopupMenuStuff = CreatePopupMenu(#PB_Any)
If PopupMenuStuff
MenuItem(0, "Stuff 0")
MenuItem(1, "Stuff 1")
MenuItem(2, "Stuff 2")
EndIf
EndProcedure
Procedure ShowPopupmenuStuff()
If IsMenu(PopupMenuStuff)
DisplayPopupMenu(PopupMenuStuff, WindowID(StuffWindow))
EndIf
EndProcedure
Procedure PopupMenuStuffClick()
Select EventMenu()
Case 0: Debug "Menuitem 0"
Case 1: Debug "Menuitem 1"
Case 2: Debug "Menuitem 2"
EndSelect
EndProcedure
Procedure WindowClose()
CloseWindow(StuffWindow)
EndProcedure
Procedure StuffMain()
StuffWindow = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 300, 300, "Stuff window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If StuffWindow
CreatePopupMenuStuff()
Protected button = ButtonGadget(#PB_Any, 100, 100, 60, 25, "Menu")
;
BindEvent(#PB_Event_CloseWindow, @WindowClose(), StuffWindow)
BindEvent(#PB_Event_Menu, @PopupMenuStuffClick())
;
BindGadgetEvent(button, @ShowPopupmenuStuff(), #PB_EventType_LeftClick)
EndIf
EndProcedure
EndModule
; ----------------------------------------------------------------------------------------------------------------------------
DeclareModule Demo
;
EndDeclareModule
Module Demo
Declare WindowClose()
Declare DisplayStuffWindow()
Declare CreateMainMenu()
Declare MainMenuClick()
Global MainWindow, MainMenu
Procedure WindowClose()
CloseWindow(MainWindow)
EndProcedure
Procedure DisplayStuffWindow()
Stuff::StuffMain()
EndProcedure
Procedure CreateMainMenu()
MainMenu = CreateMenu(#PB_Any, WindowID(MainWindow))
If MainMenu
MenuTitle("Main Menu")
MenuItem(0, "Main Menuitem 0")
MenuItem(1, "Main Menuitem 1")
MenuItem(2, "Main Menuitem 2")
EndIf
EndProcedure
Procedure MainMenuClick()
Select EventMenu()
Case 0: Debug "Main Menuitem 0"
Case 1: Debug "Main Menuitem 1"
Case 2: Debug "Main Menuitem 2"
EndSelect
EndProcedure
Procedure DemoMain()
MainWindow = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 400, 400, "Demo window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If MainWindow
CreateMainMenu()
Protected button = ButtonGadget(#PB_Any, 100, 100, 120, 25, "Open Stuff window")
;
BindEvent(#PB_Event_CloseWindow, @WindowClose(), MainWindow)
;;; BindEvent(#PB_Event_Menu, @MainMenuClick()) ; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
;
BindGadgetEvent(button, @DisplayStuffWindow(), #PB_EventType_LeftClick)
EndIf
EndProcedure
DemoMain()
Repeat: Until WaitWindowEvent() = #PB_Event_CloseWindow
EndModule