Page 1 of 1

[ToDo ?] SetMenuItemImage

Posted: Tue Feb 11, 2020 3:41 pm
by ar-s
Hey,
I didn't find any command to change the image of a MenuItem in a PopupMenu. Jump in destroying and then recreating the menu, which is a bit cumbersome.
A SetMenuItemImage would be welcome.

Code: Select all

SetMenuItemImage(#Menu, Element, ImageID(image))

Re: [ToDo ?] SetMenuItemImage

Posted: Tue Feb 23, 2021 7:44 pm
by Regenduft
Hi there,

I would also prefer a proper "SetMenuItemImage()", but the following code shows how to at least get the the job done without destroying and recreating the whole menu:

Code: Select all

CreateImage(0, 16, 16, 32, #PB_Image_Transparent)

CreateImage(1, 16, 16, 32)
StartDrawing(ImageOutput(1))
For y = 0 To 15
  For x = 0 To 15
    r.a = Random(1)-1
    g.a = Random(1)-1
    b.a = Random(1)-1
    Plot(x, y, RGB(r,g,b))
  Next
Next
StopDrawing()

OpenWindow(0, 200, 200, 200, 120, "Change Popup-Menu Image")

CreatePopupImageMenu(0, #PB_Menu_ModernLook)
MenuItem(1, "get red"  , ImageID(0))
MenuItem(2, "get green", ImageID(0))
MenuItem(3, "get blue" , ImageID(0))
MenuItem(4, "get funky", ImageID(0))
MenuItem(5, "get lost" , ImageID(0))

Repeat
  
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      End
      
    Case #PB_Event_RightClick
      DisplayPopupMenu(0, WindowID(0))
      
    Case #PB_Event_Menu
      StartDrawing(ImageOutput(0))
      DrawingMode(#PB_2DDrawing_AllChannels)
      Select EventMenu()
        Case 1 : Box(0,0,16,16,$FF0000FF)
        Case 2 : Box(0,0,16,16,$FF00FF00)
        Case 3 : Box(0,0,16,16,$FFFF0000)
        Case 4 : DrawImage(ImageID(1),0,0)
        Case 5 : Box(0,0,16,16,0)
      EndSelect
      StopDrawing()
      
  EndSelect
  
ForEver
...and sorry for the bad jokes inside the code... ;)

Re: [ToDo ?] SetMenuItemImage

Posted: Sun Mar 07, 2021 9:53 am
by DoubleDutch
I hadn't thought of that, good workaround. :)

Re: [ToDo ?] SetMenuItemImage

Posted: Sun Mar 07, 2021 11:53 am
by BarryG
ar-s wrote:destroying and then recreating the menu
...is never the answer. Menus are complicated and are often created on-the-fly from dynamic data, and such data can be gone or unavailable after the menu is created, thus making re-creation impossible. Not to mention you also lose all other properties of each menu item, such as checked and disabled status.

A command to simply change the image is the correct answer.

Re: [ToDo ?] SetMenuItemImage

Posted: Sun Mar 07, 2021 12:22 pm
by jacdelad
Just speaking out the obvious: The code above alters the assigned image, which may be not wanted. E.g. if I have a play and pause button in the menu and want to flip between them I would need a third picture, which is assigned to the menu and is repeatedly painted on.

Re: [ToDo ?] SetMenuItemImage

Posted: Sun Mar 07, 2021 3:00 pm
by Marc56us
Hi

Knowing that users don't look at the icons much, it is simpler and faster to change the full text.
This can be done with a single command: SetMenuItemText()

Code: Select all

OpenWindow(0, 200, 200, 200, 120, "", #PB_Window_SystemMenu  |#PB_Window_ScreenCentered)

CreatePopupImageMenu(0, #PB_Menu_ModernLook)
MenuItem(1, "Music OFF")

Repeat
    Select WaitWindowEvent()
            
        Case #PB_Event_CloseWindow
            End
            
        Case #PB_Event_RightClick
            DisplayPopupMenu(0, WindowID(0))
            
        Case #PB_Event_Menu
            Select EventMenu()
                Case 1 
                    Music_State + 1
                    Select Music_State
                        Case 1 : SetMenuItemText(0, 1, "Music ON")
                        Case 2 : SetMenuItemText(0, 1, "Music PAUSE")
                        Case 3 : SetMenuItemText(0, 1, "Music OFF")
                    EndSelect
            EndSelect
            
    EndSelect
ForEver
:wink:

Re: [ToDo ?] SetMenuItemImage

Posted: Sun Mar 07, 2021 3:24 pm
by skywalk
Actually, I've found users see pictures before words. So I don't swap images. I enable/disable them per context. This way, muscle memory is enforced.