Disable or Hide a menu title (not subitem)?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Disable or Hide a menu title (not subitem)?

Post by Keya »

Is it possible to disable/gray out a main menu? While it's easy enough to use DisableMenuItem for a child i cant figure out a solution for a menu title

In the IDE I tried setting a #Constant and also Event Procedure for it, but the switch from Design->Code->Design forgets and overwrites it

To demonstrate, the following code makes 3 Title menus (Parent1/2/3), each with a Child1 & Child2 submenu ...
The code then DisableMenuItems 1 through 6 ... which correctly disables each submenu ... but how to disable a title menu?

Code: Select all

If OpenWindow(0,200,200,200,100,"Menu Test")
  If CreateMenu(0, WindowID(0))

   MenuTitle("Parent1")
    MenuItem(1, "P1Child1")
    MenuItem(2, "P1Child2")      <-- Easy to disable this one
   MenuTitle("Parent2")          <-- But how to disable this one??  docs for MenuTitle says it only accepts a text, no identifier constant/#PB_Any
    MenuItem(3, "P2Child1")
    MenuItem(4, "P2Child2")
   MenuTitle("Parent3")
    MenuItem(5, "P3Child1")
    MenuItem(6, "P3Child2")    
    
    For i = 1 To 6
      DisableMenuItem(0,i,1)  ;disable menus 1-6, but that only disables the submenus
    Next i
    
  EndIf
  Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Disable or Hide a menu title (not subitem)?

Post by RSBasic »

Image
Image
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Disable or Hide a menu title (not subitem)?

Post by Keya »

Thankyou, but that uses Windows API so doesn't work in Linux or Mac
User avatar
JackWebb
Enthusiast
Enthusiast
Posts: 109
Joined: Wed Dec 16, 2009 1:42 pm
Location: Tampa Florida

Re: Disable or Hide a menu title (not subitem)?

Post by JackWebb »

Keya,

You might try something along these lines.

Code: Select all

EnableExplicit

Declare ShowMenu(MenuToShow)

If Not OpenWindow(0,200,200,200,100,"Menu Test",#PB_Window_SystemMenu)
  End
EndIf

CreateMenu(0, WindowID(0))
ShowMenu(1)
ShowMenu(2)
ShowMenu(3)

MessageRequester("Menu Test", "Continue?", #PB_MessageRequester_Ok)
FreeMenu(0)
CreateMenu(0, WindowID(0))
ShowMenu(1)
ShowMenu(3)

MessageRequester("Menu Test", "Continue?", #PB_MessageRequester_Ok)
FreeMenu(0)
CreateMenu(0, WindowID(0))
ShowMenu(3)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure ShowMenu(MenuToShow)
  Select MenuToSHow
  Case 1
    MenuTitle("Parent1")
    MenuItem(1, "P1Child1")
    MenuItem(2, "P1Child2") 
  Case 2
    MenuTitle("Parent2")  
    MenuItem(3, "P2Child1")
    MenuItem(4, "P2Child2")
  Case 3
    MenuTitle("Parent3")
    MenuItem(5, "P3Child1")
    MenuItem(6, "P3Child2")    
  EndSelect
EndProcedure
Good Luck!
Jack 8)
Make everything as simple as possible, but not simpler. ~Albert Einstein
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Disable or Hide a menu title (not subitem)?

Post by Keya »

Hi Jack, thankyou for the demo, it was a little confusing at first but I think i understand it now... when we want to disable a menu you're recreating the whole menu but not creating the one we want to disable. So, it hides not disables, but all the messages/events still end up at the correct place at least :)

It's an interesting approach! I might have to go that route if nobody knows how to actually disable a title menu
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Disable or Hide a menu title (not subitem)?

Post by Shardik »

Keya wrote:Thankyou, but that uses Windows API so doesn't work in Linux or Mac
It's not necessarily evil to use Windows API, but in that case you also have to use GTK API for Linux and the Cocoa framework API for MacOS X... :wink:

Please try the follwing API example for Windows and Linux that allows you to toggle a menu title between disabled and enabled (successfully tested with PB 5.31 on Windows XP SP3, Windows 8.1 x64, Kubuntu 9.04 x86 and Kubuntu 14.04 x86). A solution for MacOS is more complicated and will take me some time (or Cocoa master Wilbert beats me because he only will need some minutes while it takes me always several hours... :lol:).

Code: Select all

EnableExplicit

Procedure DisableMenuTitle(WindowID.I, MenuID.I, MenuTitleIndex.I, DisableTitle.I = #True)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected MenuTitle.I
      Protected MenuTitleList.I = gtk_container_get_children_(MenuID(MenuID))

      If g_list_length_(MenuTitleList) > MenuTitleIndex
        MenuTitle = g_list_nth_data_(MenuTitleList, MenuTitleIndex)

        If DisableTitle
          gtk_widget_set_sensitive_(MenuTitle, #False)
        Else
          gtk_widget_set_sensitive_(MenuTitle, #True)
        EndIf

        gtk_widget_show_all_(WindowID(WindowID))
      EndIf

      g_list_free_(MenuTitleList)
    CompilerCase #PB_OS_MacOS
    CompilerCase #PB_OS_Windows
      Protected Flags.I

      If DisableTitle
        Flags = #MF_BYPOSITION | #MF_GRAYED
      Else
        Flags = #MF_BYPOSITION
      EndIf

      EnableMenuItem_(GetMenu_(WindowID(WindowID)), MenuTitleIndex, Flags)
      DrawMenuBar_(WindowID(WindowID))
   CompilerEndSelect
EndProcedure

Define MenuTitleState.I

If OpenWindow(0, 100, 100, 200, 100, "Menu demo")
  If CreateMenu(0, WindowID(0))
    MenuTitle("Menu1")
    MenuItem(0, "Item 1.1")
    MenuTitle("Menu2")
    MenuItem(0, "Item 2.1")
  EndIf

  ButtonGadget(0, 30, 30, 140, 25, "Disable Menu2")

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
          MenuTitleState ! 1

          If MenuTitleState
            SetGadgetText(0, "Enable Menu2")
          Else
            SetGadgetText(0, "Disable Menu2")
          EndIf

          DisableMenuTitle(0, 0, 1, MenuTitleState)
        EndIf
    EndSelect
  ForEver
EndIf
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Disable or Hide a menu title (not subitem)?

Post by wilbert »

Shardik wrote:A solution for MacOS is more complicated and will take me some time (or Cocoa master Wilbert beats me because he only will need some minutes while it takes me always several hours... :lol:).
It takes me longer than minutes Shardik and quite often you have come up with solutions I wouldn't have thought about. :)
It seems indeed a bit complicated on OSX. This is as far as I could get but I don't know if it is what you are looking for

Code: Select all

    Menu = CocoaMessage(0, MenuID(0), "objectAtIndex:", 1); hide second title from menu 0
    SuperMenu = CocoaMessage(0, Menu, "supermenu")
    If SuperMenu
      MenuIndex = CocoaMessage(0, SuperMenu, "indexOfItemWithSubmenu:", Menu)    
      CocoaMessage(0, CocoaMessage(0, SuperMenu, "itemAtIndex:", MenuIndex), "setHidden:", #YES)
    EndIf
Maybe there's an easier way.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Disable or Hide a menu title (not subitem)?

Post by Shardik »

Wilbert,

thank you for your example. But it only hides the menu title while the original poster Keya would prefer to disable the title by changing it to grey which is demonstrated in my example above (with Windows API code from RSBasic and Linux GTK2 API code from me).

Unfortunately this doesn't seem to be possible on MacOS without major hacks, particularly as Apple explicitly states in its OS X Human Interface Guidelines:
[color=#0040FF]OS X Human Interface Guidelines[/color] wrote:Ensure that a submenu’s title is undimmed even when all its commands are unavailable. A submenu’s title is the title of the menu item to which the submenu is attached. As with menu titles, it’s important for users to be able to view a submenu’s contents, even if none of them are available in the current context.
And as far as I have seen in Apple's NSMenu and NSMenuItem references it's only possible to disable (grey out) NSMenuItems (using setEnabled: NO) but not the NSMenu (menu title) entries... :evil:
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Disable or Hide a menu title (not subitem)?

Post by wilbert »

Shardik wrote:And as far as I have seen in Apple's NSMenu and NSMenuItem references it's only possible to disable (grey out) NSMenuItems (using setEnabled: NO) but not the NSMenu (menu title) entries... :evil:
It might be possible to use setAttributedTitle: from the NSMenuItem class to make it gray but I haven't tried that.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply