Page 1 of 1
How to retrieve text from dynamically created menu
Posted: Fri Aug 15, 2008 2:39 pm
by superjacent
Hope someone can help. I have the following function which builds a menu structure and adds from zero 'recent folders' (directory paths) to many items. The directory paths are retrieved from a linked list.
Code: Select all
Procedure BuildMenu()
If CreateMenu(#MENU_BAR, WindowID(#WIN_MAIN))
MenuTitle("File")
OpenSubMenu("Recent Folders")
ForEach strFolder()
MenuItem(#PB_Any, strFolder()\strPath)
Next
CloseSubMenu()
MenuItem(#M_QUIT, "Quit")
MenuTitle("Help")
MenuItem(#M_ABOUT, "About")
EndIf
EndProcedure
In the main program code, the menu trapping section is as follows:
Code: Select all
Case #PB_Event_Menu
Select EventMenu()
Case #M_ABOUT
MessageRequester("About","A program by Steve")
Case #M_QUIT
Quit = #True
My question is, how do I detect if a 'recent folder' menu item has been clicked and then retrieve the value. I'm a little confused as there could be anything from zero to say 10, 20 or 30 items.
Any help appreciated.
Posted: Fri Aug 15, 2008 2:47 pm
by Fluid Byte
You need a reserved ID range, e. g. 33 to 43 or somthing. To don't interfere with other Menu ID's you need to assign the ID's after all other menu items.
Code: Select all
Enumeration
#IDM_MenuItem1
#IDM_MenuItem2
#IDM_MenuItem3
EndEnumeration
#IDM_RecentStart = #PB_Compiler_EnumerationValue
#IDM_RecentMax = 10
Then you can check events like this:
Code: Select all
If EventID = #PB_Event_Menu
Select EventMenu()
Case #IDM_MenuItem1
Case #IDM_MenuItem2
Case #IDM_MenuItem3
Case #IDM_RecentStart To #IDM_RecentStart + #IDM_RecentMax
; do stuff with your recent menu ...
EndSelect
EndIf
Posted: Fri Aug 15, 2008 3:01 pm
by superjacent
Thank you, looks like that makes sense, after visiting the Enumeration help page. I'll fiddle with this over the weekend.
Posted: Fri Aug 15, 2008 3:46 pm
by Kale
Fluid Byte wrote:You need a reserved ID range, e. g. 33 to 43 or somthing. To don't interfere with other Menu ID's you need to assign the ID's after all other menu items.
Code: Select all
Enumeration
#IDM_MenuItem1
#IDM_MenuItem2
#IDM_MenuItem3
EndEnumeration
#IDM_RecentStart = #PB_Compiler_EnumerationValue
#IDM_RecentMax = 10
Then you can check events like this:
Code: Select all
If EventID = #PB_Event_Menu
Select EventMenu()
Case #IDM_MenuItem1
Case #IDM_MenuItem2
Case #IDM_MenuItem3
Case #IDM_RecentStart To #IDM_RecentStart + #IDM_RecentMax
; do stuff with your recent menu ...
EndSelect
EndIf
But thats not dynamic.
Posted: Fri Aug 15, 2008 4:10 pm
by eddy
Code: Select all
If EventID = #PB_Event_Menu
Select EventMenu()
Case #M_ABOUT
MessageRequester("About","A program by Steve")
Case #M_QUIT
Quit = #True
Default
RecentFilePath.s=GetMenuItemText(#MEMU_BAR, EventMenu())
EndSelect
EndIf
Posted: Fri Aug 15, 2008 5:03 pm
by Fluid Byte
But thats not dynamic.
What's not dynamic about it? You have to reserve ID's anyway.
Posted: Fri Aug 15, 2008 6:31 pm
by Kale
Fluid Byte wrote:But thats not dynamic.
What's not dynamic about it? You have to reserve ID's anyway.
Using predefined constants make it static.
A dynamic way would be (thanks eddy):
Code: Select all
;======================================================================
;-CONSTANTS
;======================================================================
#APP_NAME = "Dynamic Menu Test"
Enumeration
#WINDOW_ROOT
#MENUBAR_ROOT
#MENU_QUIT
EndEnumeration
#WINDOW_FLAGS = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
;======================================================================
;-GLOBAL FLAGS / VARIABLES / STRUCTURES / ARRAYS
;======================================================================
NewList MenuItems.s()
AddElement(MenuItems()) : MenuItems() = "Menu Item 1"
AddElement(MenuItems()) : MenuItems() = "Menu Item 2"
AddElement(MenuItems()) : MenuItems() = "Menu Item 3"
;======================================================================
;-PROCEDURES
;======================================================================
;Handle an error
Procedure HandleError(Result.l, Text.s)
If Result = 0
MessageRequester("Error", Text, #PB_MessageRequester_Ok)
End
EndIf
EndProcedure
;======================================================================
;-GEOMETRY
;======================================================================
HandleError(OpenWindow(#WINDOW_ROOT, 0, 0, 400, 300, #APP_NAME, #WINDOW_FLAGS), "Main window could not be created.")
HandleError(CreateGadgetList(WindowID(#WINDOW_ROOT)), "Gadget list for the main window could not be created.")
HandleError(CreateMenu(#MENUBAR_ROOT, WindowID(#WINDOW_ROOT)), "Menu for the main window could not be created.")
MenuTitle("File")
ForEach MenuItems()
MenuItem(#PB_Any, MenuItems())
Next
MenuBar()
MenuItem(#MENU_QUIT, "Quit")
;======================================================================
;-MAIN LOOP
;======================================================================
Repeat
EventID.l = WaitWindowEvent()
Select EventID
Case #PB_Event_Menu
MenuItemID.l = EventMenu()
Select MenuItemID.l
Case #MENU_QUIT
Debug GetMenuItemText(#MENUBAR_ROOT, MenuItemID.l)
End
Default
Debug GetMenuItemText(#MENUBAR_ROOT, MenuItemID.l)
EndSelect
EndSelect
Until EventID = #PB_Event_CloseWindow
End
;======================================================================
;-END
;======================================================================
Except it doesn't work. For some reason using #PB_Any as a MenuID makes GetMenuItemText() not work!!! Bug? Me too drunk?

Posted: Fri Aug 15, 2008 8:19 pm
by Fluid Byte
Err, MenuItem() doesn't support #PB_Any ...
Posted: Fri Aug 15, 2008 9:31 pm
by Kale
Fluid Byte wrote:Err, MenuItem() doesn't support #PB_Any ...
Really? Then why is there no error? and how then are true dynamic menus created? Maybe it should be added?

Posted: Fri Aug 15, 2008 9:49 pm
by Fluid Byte
Really? Then why is there no error?
You are simply passing -1 as the ID and since MenuItem() doesn't support handles negative indexes are valid. Some commands support handles, some don't. And if a certain lib does, it's stated in the manual.
Posted: Sat Aug 16, 2008 1:22 am
by Kale
Fluid Byte wrote:Really? Then why is there no error?
You are simply passing -1 as the ID and since MenuItem() doesn't support handles negative indexes are valid. Some commands support handles, some don't. And if a certain lib does, it's stated in the manual.
It should also be stated that it doesn't then.
Also, how then are true dynamic menus created? Maybe it should be added?
Posted: Sat Aug 16, 2008 2:32 am
by superjacent
Thanks all, there's enough info here to enable me to progress my first PB application. I like Eddy's one-liner but that's not dismissing the other info put forward, I'll have to study it to make sense of it.