Search code examples
rubyuser-interfacecheckboxmenusketchup

SketchUp API: How to add a check box to a menu item


I don't see it anywhere in the Ruby API documentation but just in case I'm missing something...

I'm writing a plugin for SketchUp and I'm trying to add some options to the menu bar. One of my options would work best as a checkbox, but right now I have to have two separate buttons. Is there a way to create a checkbox menu item with the Ruby API?

Here's what I had to do instead:

foo = true
UI.menu("Plugins").add_item("Turn foo_option on") { @foo = true }
UI.menu("Plugins").add_item("Turn foo_option off") { @foo = false }

...and then I just use foo to change the options. Is there a cleaner way to do this?


Solution

  • SketchUp can have check marks in menu items. Both menu items and commands can have a validation proc. The documentation for set_validation_proc gives this example:

    plugins_menu = UI.menu("Plugins")
    item = plugins_menu.add_item("Test") { UI.messagebox "My Test Item"}
    status = plugins_menu.set_validation_proc(item)  {
      if Sketchup.is_pro?
       MF_ENABLED
      else
       MF_GRAYED
      end
    }
    

    Although for checkmarks you would use the constants MF_CHECKED and MF_UNCHECKED

    http://www.sketchup.com/intl/en/developer/docs/ourdoc/menu#set_validation_proc

    http://www.sketchup.com/intl/en/developer/docs/ourdoc/command#set_validation_proc