On multiple button toolbar, to keep only 1 MF_GRAYED at a time

Folks, I’m creating a toolbar (or a menu item also can) to set a specific STRING value to a variable.
But even I added the set_validation_proc to control the switch MF_GRAYED and MF_ENABLED, I can’t keep only 1 MF_GRAYED is allowed at one time.
Any suggestions? should I carry on with UI::Toolbar or rather just go with action_call_back instead?

Thanks a ton.

module LeeJayHom
  module OnesieTwosey

    @@loaded = false unless defined?(@@loaded)
    
    unless @@loaded

      @var ||= 'one'

      CMD ||= {}

      CMD[:one]= UI::Command.new('Command One') {
        @var = 'one'
        puts '@var changed to "one".'
      }.set_validation_proc {
        @var == 'two' ? MF_ENABLED : MF_DISABLED | MF_GRAYED
      }
      CMD[:one].small_icon= 'one.png'
      CMD[:one].large_icon= 'one.png'

      CMD[:two]= UI::Command.new('Command Two') {
        @var = 'two'
        puts '@var changed to "two".'
      }.set_validation_proc {
        @var == 'one' ? MF_ENABLED : MF_DISABLED | MF_GRAYED
      }
      CMD[:two].small_icon= 'two.png'
      CMD[:two].large_icon= 'two.png'

      TOOLBAR ||= UI::Toolbar.new('Onesie-Twosey')

      CMD.each_value do |cmd|
        TOOLBAR.add_item(cmd)
      end

      TOOLBAR.show

      @@loaded = true
    end

  end
end

OMG… I love the module names! really thanks Dan!

1 Like

Beware that validation procs trigger very frequently on Windows. And string comparisons are slow.
Consider using symbols or integer constants instead of strings for state.

4 Likes

Thanks Tom! Integer control is faster… I’ll post you another question regarding your component replacer haha super love it!

1 Like