Hi
Does anybody have experience enabling and disabling popup menu items (either triggered by a right-click or the system or menu bar menus) dynamically in response to the event aboutToShowMenu: ? I have had success connecting the event handler and locating the menu item to be disabled with no problems. However, I found no elegant way of disabling a menu item using the existing class hierarchy. I believe there is a solution to be had by populating a MENUITEMINFO class with the proper flags, but I have had no success with this solution. The following method is my attempted solution. setMenuItem: aPopupMenu byCommand: aCommand enabled: bEnabled | anInfo index item | index := aPopupMenu indexOfCommand: aCommand. item := aPopupMenu itemAt: index. anInfo := MENUITEMINFOA new. item populateItemInfo: anInfo. anInfo fMask: MIIM_STATE; fState: MFS_DISABLED. aPopupMenu setItem: index info: anInfo. According to all the docs I could get my hands on between dolphin and microsoft concerning this issue, the above code should disable a menu item, however it does not work. Any ideas? Am I working at the wrong level of abstraction here? I spent a good amount of time searching through the Menu, MenuItem, CommandMenuItem, MENUITEMINFOA classes. I thought one of these classes would have methods to enable/disable, check/uncheck menu items? Jon |
Jonathon,
> Any ideas? Am I working at the wrong level of abstraction here? I spent a > good amount of time searching through the Menu, MenuItem, CommandMenuItem, > MENUITEMINFOA classes. I thought one of these classes would have methods to > enable/disable, check/uncheck menu items? Nope. Dolphin makes it a lot easier ;-) Have a look in your image for definitions of the #queryCommand: method - there should be lots. Rather than enabling/disabling or checking/unchecking specific menu items Dolphin enables or disables the _command_ sent by the menu item. This has the big advantage that if the command is sent from a number of sources (multiple menus and a toolbar button for example) the UI for all of them can be updated in one operation. For example. Say you have a menu item that sends the #trigger command. The #queryCommand: method in the Shell could then read queryCommand: aCommandQuery aCommandQuery command == #trigger ifTrue: [ aCommandQuery isEnabled: self canTrigger. ^true]. ^super queryCommand: aCommandQuery If the #canTrigger method answers true then the menu item is enabled, if it answers false the menu item is disabled. The same block can also set/clear the checked state of the menu item using the #isChecked: method. There is a bit more to it (to do with which object in a nested Shell performs and enables a command) but that should be enough to get you going. One other point. Dolphin calls the above method when the UI is free (for toolbars) and when a menu is displayed so, normally, you don't have to take any extra action to update the UI, it's done for you. There are also a number of longer, and better, explanations in the newsgroup archive - search for queryCommand. Regards Ian |
In reply to this post by Jonathon Spaeth
Jonathon Spaeth wrote:
> Hi > > Does anybody have experience enabling and disabling popup menu items > (either triggered by a right-click or the system or menu bar menus) > dynamically in > response to the event aboutToShowMenu: ? I have had success connecting I think #queryCommand: might help you. Dolphin does not enable or disable the menu item itself. Instead you enable/disable the command .... and all widgets issuing this command will be disabled/enabled. Everytime a menu item or button is display, #queryCommand: is called with a CommandQuery. This CommandQuery includes the the command to be executed (the message you choose). Based on this you can choose to enable/disable the command or check/uncheck it. #queryCommand: is also called when no widget is to be displayed. Therefore this is a very comfortable way to control the commands without having to deal with how to disable/enable all the differnet kind of widgets. As long as a widget sends the same command it will be enabled/disabled like all the others. Take a look at some #queryCommand: methods in the system to see some possible uses. CU, Udo |
In reply to this post by Ian Bartholomew-17
Ian
Thanks for the pointer. After a while of trying to make the MENUITEMINFO class do what I wanted, I knew that there had to have been a higher-level API that I was just missing. After about 15 minutes of investigation into the queryCommand hook, things became much more obvious. Thanks! Jon "Ian Bartholomew" <[hidden email]> wrote in message news:t8jg9.4991$571.489773@wards... > Jonathon, > > > Any ideas? Am I working at the wrong level of abstraction here? I spent > a > > good amount of time searching through the Menu, MenuItem, CommandMenuItem, > > MENUITEMINFOA classes. I thought one of these classes would have methods > to > > enable/disable, check/uncheck menu items? > > Nope. Dolphin makes it a lot easier ;-) > > Have a look in your image for definitions of the #queryCommand: method - > there should be lots. Rather than enabling/disabling or checking/unchecking > specific menu items Dolphin enables or disables the _command_ sent by the > menu item. This has the big advantage that if the command is sent from a > number of sources (multiple menus and a toolbar button for example) the UI > for all of them can be updated in one operation. > > For example. Say you have a menu item that sends the #trigger command. The > #queryCommand: method in the Shell could then read > > queryCommand: aCommandQuery > aCommandQuery command == #trigger > ifTrue: [ > aCommandQuery isEnabled: self canTrigger. > ^true]. > ^super queryCommand: aCommandQuery > > If the #canTrigger method answers true then the menu item is enabled, if > answers false the menu item is disabled. The same block can also set/clear > the checked state of the menu item using the #isChecked: method. There is a > bit more to it (to do with which object in a nested Shell performs and > enables a command) but that should be enough to get you going. > > One other point. Dolphin calls the above method when the UI is free (for > toolbars) and when a menu is displayed so, normally, you don't have to take > any extra action to update the UI, it's done for you. > > There are also a number of longer, and better, explanations in the newsgroup > archive - search for queryCommand. > > Regards > Ian > > |
Free forum by Nabble | Edit this page |