Page 1 of 1

Disable or Hide a menu title (not subitem)?

Posted: Tue Aug 18, 2015 3:42 pm
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

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

Posted: Tue Aug 18, 2015 3:50 pm
by RSBasic

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

Posted: Tue Aug 18, 2015 4:43 pm
by Keya
Thankyou, but that uses Windows API so doesn't work in Linux or Mac

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

Posted: Wed Aug 19, 2015 3:30 am
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)

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

Posted: Wed Aug 19, 2015 5:37 am
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

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

Posted: Wed Aug 19, 2015 11:12 am
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

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

Posted: Wed Aug 19, 2015 12:43 pm
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.

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

Posted: Wed Aug 19, 2015 9:19 pm
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:

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

Posted: Wed Aug 19, 2015 9:28 pm
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.