[Solved] Duplicate a second menu from a first one?

Everything else that doesn't fall into one of the other PB categories.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

[Solved] Duplicate a second menu from a first one?

Post by Dude »

SOLVED. Just having a brain fart couple of days. Too much happening. :lol:

I have a menu in my app that is accessed via a button on the window.

I have a second different menu that is accessed from the system tray icon.

Now, I'd like to have that first menu also show up as a sub-menu of the second menu.

When the user selects either menu for an item, my app should handle the same procedure call for the item.

Do I really have to create two sets of menus to do this? :(
Last edited by Dude on Thu Feb 23, 2017 2:05 am, edited 1 time in total.
User avatar
TI-994A
Addict
Addict
Posts: 2770
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Duplicate a second menu from a first one?

Post by TI-994A »

Dude wrote:...my app should handle the same procedure call for the item.
One common menu with a single handler, accessible from a window menu, button gadget, and systray icon:

Code: Select all

;PureBasic v5.60 b4 x64

#mainMenuItem = 0
#sysTrayMenuItem = 1

Procedure menuHandler()
  Shared mainWin, commonMenu, btnMenu
  Protected displayMenu
  
  Select Event()
    Case #PB_Event_SysTray
      Select EventType()
        Case #PB_EventType_LeftClick
          displayMenu = #True
      EndSelect
    Case #PB_Event_Menu
      Select EventMenu()
        Case #mainMenuItem
          displayMenu = #True
        Case #sysTrayMenuItem
          Debug "common handler..."
      EndSelect
    Case #PB_Event_Gadget
      Select EventGadget()
        Case btnMenu
          displayMenu = #True
      EndSelect
  EndSelect
  
  If displayMenu
    DisplayPopupMenu(commonMenu, WindowID(mainWin))
  EndIf
  
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered 
sysTrayIcon = LoadImage(#PB_Any, #PB_Compiler_Home + 
                                 "Examples\Sources\Data\CdPlayer.ico")

mainWin = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore, 
                     300, 200, "Common Menus", wFlags)
btnMenu = ButtonGadget(#PB_Any, 190, 10, 100, 30, "Button Menu")

mainMenu = CreateMenu(#PB_Any, WindowID(mainWin))
MenuTitle("Main Menu")
MenuItem(#mainMenuItem , "Menu Item")

sysTrayWin = AddSysTrayIcon(#PB_Any, WindowID(mainWin), ImageID(sysTrayIcon))
SysTrayIconToolTip(sysTrayWin, "Systray Menu")

commonMenu = CreatePopupMenu(#PB_Any)
MenuItem(#sysTrayMenuItem, "Common Menu Item")

BindGadgetEvent(btnMenu, @menuHandler())
BindMenuEvent(mainMenu, #mainMenuItem, @menuHandler())
BindMenuEvent(commonMenu, #sysTrayMenuItem, @menuHandler())
BindEvent(#PB_Event_SysTray, @menuHandler(), 
          mainWin, sysTrayWin, #PB_EventType_LeftClick)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Hope it helps. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Duplicate a second menu from a first one?

Post by Dude »

TI-994A wrote:Hope it helps.
Not really (sorry). When clicking the Main Menu, the Common Menu Item doesn't appear in the drop-down menu; and clicking the button would need to show both menus, too. That's what I'm trying to do -- have both menus show at once, no matter which way is chosen. But the second menu must show the first menu as a sub-menu of the second menu.

See in this example, how I've got 2 x Cut/Copy/Paste menus? How can I have those menu definitions created ONCE in my code, instead of DUPLICATED like this? Both sets of menus will call the same procedure when selected.

Reason for this help: I'm applying the DRY coding principle (https://en.wikipedia.org/wiki/Don't_repeat_yourself). :mrgreen:

Code: Select all

OpenWindow(0, 100, 150, 195, 200, "PureBasic - Menu")

If CreateMenu(0, WindowID(0))

  MenuTitle("File")
    MenuItem( 1, "&Load...")
    MenuItem( 2, "Save")
    MenuItem( 3, "Save As...")
    MenuBar()
    OpenSubMenu("Recents")
      MenuItem( 4, "Pure.png")
      MenuItem( 5, "Basic.jpg")
      OpenSubMenu("Even more !")
          MenuItem( 6, "Cut")
          MenuItem( 7, "Copy")
          MenuItem(8, "Paste")
      CloseSubMenu()
      MenuItem(9, "Rocks.tga")
    CloseSubMenu()
    MenuBar()
    MenuItem(10, "&Quit")

  MenuTitle("Clipboard")
    MenuItem(11, "Cut")
    MenuItem(12, "Copy")
    MenuItem(13, "Paste")

  MenuTitle("?")
    MenuItem(14, "About")

EndIf

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Duplicate a second menu from a first one?

Post by #NULL »

just create parts of the menu in procedures and call them where you want them to appear.

Code: Select all

OpenWindow(0, 100, 150, 195, 200, "PureBasic - Menu")

Procedure commonMenu()
  OpenSubMenu("Common Menu Item")
    MenuItem(10, "common 1")
    MenuItem(11, "common 2")
    MenuItem(12, "common 3")
  CloseSubMenu()
EndProcedure

Procedure moreMenu()
  OpenSubMenu("More")
    MenuItem(20, "more1")
    MenuItem(21, "more2")
    commonMenu()
  CloseSubMenu()
EndProcedure

mainMenu = CreateMenu(#PB_Any, WindowID(0))
  MenuTitle("File")
    MenuItem( 1, "&Load...")
    MenuItem( 2, "Save")
    MenuItem( 3, "Save As...")
    MenuBar()
    moreMenu()
    MenuBar()
    MenuItem(30, "&Quit")

commonMenu = CreatePopupMenu(#PB_Any)
moreMenu()
commonMenu()

sysTrayWin = AddSysTrayIcon(#PB_Any, WindowID(mainWin), ImageID(LoadImage(#PB_Any, #PB_Compiler_Home + "examples/sources/Data/CdPlayer.ico")))

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_RightClick
      DisplayPopupMenu(commonMenu, WindowID(0))
    Case #PB_Event_SysTray
      DisplayPopupMenu(commonMenu, WindowID(0))
    Case #PB_Event_Menu
      Debug EventMenu()
  EndSelect
Until Event = #PB_Event_CloseWindow
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Duplicate a second menu from a first one?

Post by Dude »

Thanks #NULL! :) That's what I was looking for. Don't know why I couldn't get my head around it. Too much going on in my life at the moment (it's all good, but hectic).
Post Reply