Add tool button to toolbar

edited September 2013 in Ruby Scripting

I'd like to create a custom button and add it to the top toolbar. By top toolbar I mean the one with "select", "move", etc tools.

For instance the button could allow the user to draw a box which, as soon as it is drawn is instantly converted to a ROUND_CORNERS PCell with some default radius. So in effect you are drawing a box with rounded corners. Or equivalently you click the button then draw the "guiding shape" (box or whatever) for a PCell, again in effect drawing a box with rounded corners.

The box with rounded corners is a simple example but you could imagine more complicated functions.

It looks to me like you can do this by creating a AbstractMenu object and something to do with the @toolbar path, but I can't seem to figure out the syntax. Can you perhaps provide an example?

Thanks!

Comments

  • edited November -1

    Hallo,

    the easiest way to do so is by binding a macro to a toolbar button. Open the macro's properties and enter a toolbar menu path into the "Menu path" edit box. This will usually be "@toolbar.end" which means a new tool button is created at the right end of the toolbar. Enter a description - this will be shown as the text of the tool button.

    If you want to configure other properties such as icon, you'll need some more code. Here's some example:

    a = RBA::Action.new
    a.title = "Push Me!"
    # more, i.e.
    # a.icon = "file path"
    a.on_triggered do
      # put your code here ...
      puts "I was pushed!"
    end
    
    # install the action at the right end of the toolbar with a separator
    menu = RBA::Application.instance.main_window.menu
    menu.insert_separator("@toolbar.end", "name")
    menu.insert_item("@toolbar.end", "my_action", a)
    

    Please note that the menu entry will be shown when that code is executed. If you put that code into a macro, mark it as "run on startup" to install the tool button automatically.

    Matthias

  • edited November -1

    Thank you, exactly what I was looking for. I was unfortunately trying to figure out how to use "@toolbar" in the ruby code, however now I see it can be entered into the macro's properties box.

    One small follow-on: I can't get the a.icon = "file path" to work. I saved an icon in various types (jpg, gif, tif, png) and sizes (very small to larger) and change the line to

    a.icon = "C:\Users\image.gif"
    

    or .jpg or .tif or .png, but I can't get any of them to show up on the toolbar. (The "Push Me!" text loads fine however.) Is there some certain image format or size or aspect ratio required? Couldn't see it noted in the docs.

    Thanks!

  • edited October 2013

    Just a rough guess: you came across the Windows-backslash-double quoted string-incompatibility issue. In double-quoted strings, the backslash starts an escape sequence and will insert control characters (or something else), but not the backslash character.

    Use a double backslashes or single quotes.

    Matthias

Sign In or Register to comment.