[code] Menu Validation (MF_DISABLED bugged on PC?)
-
Re: Menu and Toolbar validation.
It seems on SU 8.0M1 the validation flag MF_DISABLED has no effect. The user can still select the item, and the comand is run.
Is this a new bug?
How does this compare to Mac ?
# Valid integer values for PC; # # ** MF_BITMAP not used by Sketchup # # 0 = MF_UNCHECKED | MF_ENABLED # 1 = MF_UNCHECKED | MF_GRAYED # 2 = MF_UNCHECKED | MF_DISABLED # 3 = MF_UNCHECKED | MF_GRAYED | MF_DISABLED # # 4 = MF_UNCHECKED | MF_BITMAP | MF_ENABLED # 5 = MF_UNCHECKED | MF_BITMAP | MF_GRAYED # 6 = MF_UNCHECKED | MF_BITMAP | MF_DISABLED # 7 = MF_UNCHECKED | MF_BITMAP | MF_GRAYED | MF_DISABLED # # 8 = MF_CHECKED | MF_ENABLED # 9 = MF_CHECKED | MF_GRAYED # 10 = MF_CHECKED | MF_DISABLED # 11 = MF_CHECKED | MF_GRAYED | MF_DISABLED # # 12 = MF_CHECKED | MF_BITMAP | MF_ENABLED # 13 = MF_CHECKED | MF_BITMAP | MF_GRAYED # 14 = MF_CHECKED | MF_BITMAP | MF_DISABLED # 15 = MF_CHECKED | MF_BITMAP | MF_GRAYED | MF_DISABLED # # note 16 = MF_POPUP
If
@@validation
is a class var holding the validation integer for a menu item or toolbar item. You could use an instance var@validation
. Or you could pass the integer in, by putting avalidation
argument into the()
on thedef
method line, and changing all the@@validation
to justvalidation
:Produce it's Bitwise OR string:
<span class="syntaxdefault">def valid_string</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault"> <br /> valid</span><span class="syntaxkeyword">=</span><span class="syntaxstring">''<br /></span><span class="syntaxdefault"> <br /> </span><span class="syntaxcomment"># Test the 4th bit; MF_CHECKED==1<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1<br /> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_CHECKED | '<br /></span><span class="syntaxdefault"> else<br /> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_UNCHECKED | '<br /></span><span class="syntaxdefault"> end<br /> <br /> </span><span class="syntaxcomment"># Test the 3rd bit; MF_BITMAP==1<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># valid << 'MF_BITMAP | ' if @@validation[2]==1 <br /></span><span class="syntaxdefault"> <br /> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1 or </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1<br /> </span><span class="syntaxcomment"># Test the 1st bit; MF_GRAYED==1<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1<br /> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_GRAYED'</span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">0<br /> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_GRAYED | '</span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1<br /> end<br /> </span><span class="syntaxcomment"># Test the 2nd bit; MF_DISABLED==1<br /></span><span class="syntaxdefault"> if </span><span class="syntaxkeyword">@@</span><span class="syntaxdefault">validation</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]==</span><span class="syntaxdefault">1<br /> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_DISABLED'<br /></span><span class="syntaxdefault"> end<br /> else </span><span class="syntaxcomment">#<br /></span><span class="syntaxdefault"> valid </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> </span><span class="syntaxstring">'MF_ENABLED'<br /></span><span class="syntaxdefault"> end<br /> return valid<br />end </span><span class="syntaxcomment"># def<br /></span><span class="syntaxdefault"> </span>
checked?()
method<span class="syntaxdefault"></span><span class="syntaxcomment"># checked?()<br />#<br /># Returns Boolean state of MF_CHECKED.<br />#<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">checked</span><span class="syntaxkeyword">?()<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#@@validation >= 8 # on PC<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_CHECKED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br />end<br /></span>
enabled?()
method<span class="syntaxdefault"></span><span class="syntaxcomment"># enabled?()<br />#<br /># Returns Boolean state of MF_ENABLED.<br /># !! NOT EXACT same as MF_UNCHECKED !!<br />#<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">enabled</span><span class="syntaxkeyword">?()<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># [0,4,8,12].include?(@@validation) # on PC<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># on PC MF_DISABLED has no effect. Use MF_GRAYED instead.<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">((@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_GRAYED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_DISABLED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> true<br />end<br /></span>
disabled?()
method<span class="syntaxdefault"></span><span class="syntaxcomment"># disabled?()<br />#<br /># Returns Boolean state of MF_DISABLED.<br />#<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">disabled</span><span class="syntaxkeyword">?()<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment"># [2,3,6,7,10,11,14,15].include?(@@validation) # PC<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_DISABLED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br />end<br /></span>
grayed?()
method<span class="syntaxdefault"></span><span class="syntaxcomment"># grayed?()<br />#<br /># Returns Boolean state of MF_GRAYED.<br />#<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">grayed</span><span class="syntaxkeyword">?()<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_GRAYED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> true </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> false<br />end<br /></span>
unchecked?()
method<span class="syntaxdefault"></span><span class="syntaxcomment"># unchecked?()<br />#<br /># Returns Boolean state of MF_UNCHECKED.<br /># !! NOT EXACT same as MF_ENABLED !!<br />#<br /></span><span class="syntaxdefault">def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">unchecked</span><span class="syntaxkeyword">?()<br /></span><span class="syntaxdefault"> </span><span class="syntaxcomment">#@@validation < 8 # PC<br /></span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(@@</span><span class="syntaxdefault">validation </span><span class="syntaxkeyword">&</span><span class="syntaxdefault"> MF_CHECKED</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">nonzero</span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> false </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> true<br />end<br /></span>
EDIT: made Boolean methods use constants and bitwise comparison for cross-platform use, (which greatly simplifys the check for MF_GRAYED.)
-
This is what I always use for validation procs:
def self.can_paste_proc if self.can_paste? MF_ENABLED else MF_DISABLED | MF_GRAYED end end
I though
MF_DISABLED
prevented the action from triggering. And thatMF_GRAYED
where used only for the visual effect. -
hmm... testing in SU8M1, you're right.
MF_DISABLED
seem to do nothing. OnlyMF_GRAYED
seems to affect anything.
Though I imagine there is a reason I useMF_DISABLED | MF_GRAYED
- just can't remember why. Can't find any notes on it. -
Same behaviour on SU6, 7.1 Windows.
-
@thomthom said:
Though I imagine there is a reason I use
MF_DISABLED | MF_GRAYED
- just can't remember why. Can't find any notes on it.I'll bet it's a cross-platform thangie... it does not seem to matter to Windows if the DISABLED bit is set, so no harm is done.
Need a Mac guy to test it and let us know.
-
@thomthom said:
This is what I always use for validation procs:
Just being clear that the methods above are not the actual validation_proc. I try not to put conditional evaluations into the menu or toolbar procs, if I can do the decision in another place in the code. In most cases, the validation state does not change until the user actually clicks on the item (or button,) so it's better to change the menu item's (or button's) valid state within the command proc, or some other method that gets called. So when the change is made I just change the value of a
@@validation
class var (or@validation
instance var,) and let Sketchup just pick up the value of that var when it must do a UI draw.So creating a menu item, would look like:
item = menu.add_item('Some Menu Label') { ..execute_code.. } menu.set_validation_proc(item) { @@validation }
-
Yea, that'd probably be better.
IN my case I'd need an observer, but it'd still be more efficient.
-
Tested on OSX SU8.0M1 - Same behaviour,
MF_DISABLED
does nothing. -
@thomthom said:
hmm... testing in SU8M1, you're right.
MF_DISABLED
seem to do nothing. OnlyMF_GRAYED
seems to affect anything.@thomthom said:
Same behaviour on SU6, 7.1 Windows.
@thomthom said:
Tested on OSX SU8.0M1 - Same behaviour,
MF_DISABLED
does nothing.Thanks go to ThomThom for filing a Google BugReport on this, 30 JAN 2011.
-
I still wonder why I've been using
MF_DISABLED
... -
Covering your butt.. just in case the API changes in the future?
-
Possibly. Maybe it was in another language where you had to gray and disable. ...Visual Basic...? Can't think of anything else where I've dealt with menus...
Advertisement