MenuItem enablement doesn't work

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

MenuItem enablement doesn't work

Sylvain pralon
Hello,
I'am currently developping a canvas application with a menu bar
initially this menu bar contains some menu which are disabled.
after some actions this menu has to be enable but I can not do it, I
don't understand why please help !

here is the code I use to try to enable this menu :

#menu is the method i call to insert the menu in my application.

menu := ( ( self builder menuAt: #menu ) atNameKey:#menuTerminology )
enable.

after this the #menuTerminology is always disabled.

thanks

sylvain pralon

Reply | Threaded
Open this post in threaded view
|

Re: MenuItem enablement doesn't work

Reinout Heeck-2
Sylvain,

Could it be that the menu is dynamically (re)build after the enablement?

HTH,
Adriaan.

> Hello,
> I'am currently developping a canvas application with a menu bar
> initially this menu bar contains some menu which are disabled.
> after some actions this menu has to be enable but I can not do it, I
> don't understand why please help !
>
> here is the code I use to try to enable this menu :
>
> #menu is the method i call to insert the menu in my application.
>
> menu := ( ( self builder menuAt: #menu ) atNameKey:#menuTerminology )
> enable.
>
> after this the #menuTerminology is always disabled.
>
> thanks
>
> sylvain pralon
>
>


--
http://vdg38bis.xs4all.nl

Reply | Threaded
Open this post in threaded view
|

Re: MenuItem enablement doesn't work

Holger Kleinsorgen-4
In reply to this post by Sylvain pralon
Sylvain Pralon wrote:

> Hello,
> I'am currently developping a canvas application with a menu bar
> initially this menu bar contains some menu which are disabled.
> after some actions this menu has to be enable but I can not do it, I
> don't understand why please help !
>
> here is the code I use to try to enable this menu :
>
> #menu is the method i call to insert the menu in my application.
>
> menu := ( ( self builder menuAt: #menu ) atNameKey:#menuTerminology )
> enable.
>
> after this the #menuTerminology is always disabled.

Changing the menu item enablement does not affect menus that are already
opened. So if #menuTerminology is a menu item of the menu bar, changing
the enablement has no effect. You'll either need to enable/disable all
menuitems of the terminology menu, or set the enablement of the menu
button view directly.


Reply | Threaded
Open this post in threaded view
|

Re: MenuItem enablement doesn't work

Holger Kleinsorgen-4
oops, forgot something:

> You'll either need to enable/disable all
> menuitems of the terminology menu
... and enable the terminology menu item itself all the time.

btw, there's a notification mechanism (addEnablementDependent:), that is
unfortunately only used for toolbars, not for menu bar buttons.


Reply | Threaded
Open this post in threaded view
|

Re: MenuItem enablement doesn't work

Ladislav Lenart
In reply to this post by Sylvain pralon
Sylvain Pralon wrote:

> Hello,
> I'am currently developping a canvas application with a menu bar
> initially this menu bar contains some menu which are disabled.
> after some actions this menu has to be enable but I can not do it, I
> don't understand why please help !
>
> here is the code I use to try to enable this menu :
>
> #menu is the method i call to insert the menu in my application.
>
> menu := ( ( self builder menuAt: #menu ) atNameKey:#menuTerminology )
> enable.
Hello,

I think you don't have to mess with UIBuilder stuff to achieve what you
need...

I wrote a very basic example of how we do dynamic main menus in UIs (see
the attachment).

Few comments to the example...
It is just one class, MenuTestUI. It has main menu (menu bar) #menuAspect
and contains two buttons (#enableMenu and #disableMenu). One enables main
menu and the other one disables it. Main menu itself has the following
structure:
      Menu
          Action

'Action' item does nothing. The example demonstrates enabling and disabling of
'Menu' item but extending it, you can dynamically enable/disable and add/remove
any items you want or modify their appearance (labels and co.). To test it,
just file it in and open the UI: UI.MenuTestUI open.

The whole trick is that #menuAspect does not return aMenu but rather aValueHolder
on aMenu. Each time you want to refresh the menu, evaluate something like this:
      self menuAspect value: self createMenu    "see MenuTestUI>>menuEnabled:"
where #createMenu returns brand new menu with enabled disabled items according
to actual needs.

Just a note: To achieve this kind of dynamicity in context menus, just return
block building the menu instead of the menu itsef. This way, the block will be
evaluated each time the context menu is to be opened.

Hope this helps,

Ladislav Lenart


<?xml version="1.0"?>

<st-source>
<time-stamp>From VisualWorks®, 7.4 of 5. prosinec 2005 on 17. říjen 2006 at 12:34:13</time-stamp>


<class>
<name>MenuTestUI</name>
<environment>UI</environment>
<super>UI.ApplicationModel</super>
<private>false</private>
<indexed-type>none</indexed-type>
<inst-vars>menuAspect enabled </inst-vars>
<class-inst-vars></class-inst-vars>
<imports></imports>
<category>_runtime_additions_</category>
<attributes>
<package>_runtime_additions_</package>
</attributes>
</class>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<methods>
<class-id>UI.MenuTestUI class</class-id> <category>interface specs</category>

<body package="_runtime_additions_" selector="windowSpec">windowSpec
        "Tools.UIPainter new openOnClass: self andSelector: #windowSpec"

        &lt;resource: #canvas&gt;
        ^#(#{UI.FullSpec}
                #window:
                #(#{UI.WindowSpec}
                        #label: 'Menu Test'
                        #bounds: #(#{Graphics.Rectangle} 446 61 738 321 )
                        #flags: 4
                        #menu: #menuAspect )
                #component:
                #(#{UI.SpecCollection}
                        #collection: #(
                                #(#{UI.ActionButtonSpec}
                                        #layout: #(#{Graphics.LayoutFrame} 3 0 3 0 -3 0.5 26 0 )
                                        #name: #enableMenu
                                        #model: #enableMenu
                                        #label: 'Enable'
                                        #defaultable: true )
                                #(#{UI.ActionButtonSpec}
                                        #layout: #(#{Graphics.LayoutFrame} 3 0.5 3 0 -3 1 26 0 )
                                        #name: #disableMenu
                                        #model: #disableMenu
                                        #label: 'Disable'
                                        #defaultable: true ) ) ) )</body>
</methods>

<!-- -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -   -->


<methods>
<class-id>UI.MenuTestUI</class-id> <category>accessing</category>

<body package="_runtime_additions_" selector="menuAspect">menuAspect

        menuAspect ifNil: [menuAspect := self createMenu asValue].
        ^menuAspect.</body>
</methods>

<methods>
<class-id>UI.MenuTestUI</class-id> <category>private-accessing</category>

<body package="_runtime_additions_" selector="createMenu">createMenu

        | submenu item |
        submenu := Menu new.
        submenu addItemLabel: 'Action'.
        item :=
                (MenuItem labeled: 'Menu')
                        submenu: submenu;
                        enabled: enabled;
                        yourself.
        ^Menu new
                addItem: item;
                yourself.</body>
</methods>

<methods>
<class-id>UI.MenuTestUI</class-id> <category>actions</category>

<body package="_runtime_additions_" selector="disableMenu">disableMenu

        self menuEnabled: false.</body>

<body package="_runtime_additions_" selector="enableMenu">enableMenu

        self menuEnabled: true.</body>

<body package="_runtime_additions_" selector="menuEnabled:">menuEnabled: aBoolean

        enabled = aBoolean ifTrue: [^self].
        enabled := aBoolean.
        self menuAspect value: self createMenu.</body>
</methods>

<methods>
<class-id>UI.MenuTestUI</class-id> <category>initialize-release</category>

<body package="_runtime_additions_" selector="initialize">initialize

        super initialize.
       
        enabled := true.</body>
</methods>

</st-source>