enabling, disabling, checking popup menus 
Author Message
 enabling, disabling, checking popup menus

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



Tue, 01 Mar 2005 17:42:41 GMT  
 enabling, disabling, checking popup menus
Jonathon,

Quote:
> 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



Tue, 01 Mar 2005 18:24:18 GMT  
 enabling, disabling, checking popup menus
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


Quote:
> 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



Wed, 02 Mar 2005 01:45:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamic menu-rights/Enable and Disable menu-items

2. Disable Right Mouse Button Popup Menu

3. Disable Right Mouse Button Popup Menu

4. desktop popup menu disabling

5. menu(bar) separator design and popup disable ?

6. Why does NumLock disable popup menus?

7. Enable/Disable Menu Items...

8. menu disable/enable problem

9. Disable/Enable menu from MDI child?

10. Disabling/Enable Menu Items

11. Disabling/Enable Menu Items

12. Syntax for enabling/disabling menu items

 

 
Powered by phpBB® Forum Software