[ToDo ?] SetMenuItemImage

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

[ToDo ?] SetMenuItemImage

Post 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))
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Regenduft
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Mar 02, 2009 9:20 pm
Location: Germany

Re: [ToDo ?] SetMenuItemImage

Post 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... ;)
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Re: [ToDo ?] SetMenuItemImage

Post by DoubleDutch »

I hadn't thought of that, good workaround. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: [ToDo ?] SetMenuItemImage

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1488
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: [ToDo ?] SetMenuItemImage

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: [ToDo ?] SetMenuItemImage

Post 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:
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: [ToDo ?] SetMenuItemImage

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply