HowTo : ToolBar widget - mcorino/wxRuby3 GitHub Wiki

     About      FAQ      User Guide      Reference documentation

wxRuby ToolBar Widget

In this tutorial we will explore how to create a simple toolbar in wxRuby. A toolbar is a useful menu-like control that appears on your Window with a bunch of icons which act as buttons. These icons are linked to methods, such as a Save icon triggering a #save method.

Overall the Wx::ToolBar is an important widget, which will add a lot of flavor and user-friendliness to your wxRuby app.

wxRuby ToolBar Syntax

There are two ways of creating a ToolBar in wxRuby. You can either use the #create_tool_bar method on the frame or create the ToolBar using its constructor.

class Wx::ToolBar
  def initialize(parent, id, pos = Wx::DEFAULT_POSITION, size = Wx::DEFAULT_SIZE, style = Wx::TB_HORIZONTAL, name = Wx::TOOL_BAR_NAME_STR)
end

The first method is preferred as it helps shorten your code and looks simpler. We’ll be using this method later in our examples.

wxRuby ToolBar Styles

A list of available styles for the wxRuby Toolbar widget:

ToolBar Style Description
Wx::TB_FLAT Makes the ToolBar look Flat
Wx::TB_HORIZONTAL Makes the layout horizontal (default)
Wx::TB_VERTICAL Makes the layout Vertical
Wx::TB_TEXT Shows Text on the Toolbar (only icons shown by default)
Wx::TB_NOICONS Disables Icons on the ToolBar
Wx::TB_HORZ_LAYOUT Puts Icons and Text side by side, instead of vertically
Wx::TB_HORZ_TEXT A combination of the TB_HORZ_LAYOUT and TB_TEXT styles
Wx::TB_NO_TOOLTIPS Disables Tooltips which appear on mouse hover
Wx::TB_BOTTOM Aligns ToolBar at the bottom
Wx::TB_RIGHT Aligns ToolBar to the Right

wxRuby ToolBar Methods

A list of useful methods for the wxRuby ToolBar:

Method Description
add_tool(toolId, label, bitmap, shortHelp='', kind=Wx::ItemKind::ITEM_NORMAL)
add_tool(toolId, label, bitmap, bmpDisabled, kind=Wx::ItemKind::ITEM_NORMAL, shortHelp='', longHelp='', clientData=nil)
Adds a Tool to the Toolbar
add_check_tool(toolId, label, bitmap1, bmpDisabled=Wx::NULL_BITMAP, shortHelp='', longHelp='', clientData=nil) Adds a Check Tool to the ToolBar
add_control(control, label='') Adds as a Tool a widget which inherits from wx.Control (e.g: ComboBox)
add_radio_tool(toolId, label, bitmap1, bmpDisabled=Wx::NULL_BITMAP, shortHelp='', longHelp='', clientData=nil) Adds a Radio Tool to the ToolBar
add_separator Adds a Separator to the ToolBar (for grouping of tools)
clear_tools Deletes all Tools in the ToolBar
delete_tool(toolid) Deletes the Tool completely
enable_tool(toolid, enable) Used to Enable/Disable the Tool
insert_tool(pos, toolId, label, bitmap1, bmpDisabled=Wx::NULL_BITMAP, shortHelp='', longHelp='', clientData=nil) Inserts a tool at the specified index position in the Toolbar
insert_control(pos, control, label='') Inserts a Control tool at the specified index position in the Toolbar
insert_separator(pos) Inserts a separator at the specified index position in the Toolbar
realize Must be called after you have added tools into the Toolbar
remove_tool Removes the Tool from the ToolBar, but doesn’t delete it (can be re-used)
toggle_tool(toolid, toggle) Toggles the Tool On/Off

Example Code

A small example showing how to create a ToolBar, add in some icons and an image showing what it all looks like.

We’ve used the #create_tool_bar method to create a Toolbar, to which we add a few "Tools". We’ve added two simple tools using the #add_tool method.

require 'wx'

class MyWindow < Wx::Frame
  def initialize(title)
    super(nil, title: title)
    @panel = Wx::Panel.new(self)

    @toolbar = create_tool_bar
 
    # Tool 1
    @toolbar.add_tool(Wx::ID_ANY, 'Open', Wx::Bitmap(:open))
 
    # Tool 2
    @toolbar.add_tool(Wx::ID_ANY, 'Save', Wx::Bitmap(:save))

    @toolbar.realize
    
    centre
  end
end

Wx::App.run do
  window = MyWindow.new("wxRuby ToolBar Guide")
  window.show
end

The output of the above code:

screenshot1

Note: To run this example you will need the bitmap files too. Download those here and here.

Adding Check Tools

In this example we will add a Check tool in our ToolBar.

The #add_check_tool method is used the same way as #add_tool, but it adds some extra functionality. The Tool that is created behaves like a Wx::ToggleButton. When you click it once, it will be highlighted with a blue color and turn "on". Clicking it again will turn it off.

Remember to call the #realize method in the end, otherwise the tools will not show up on the ToolBar.

require 'wx'

class MyWindow < Wx::Frame
  def initialize(title)
    super(nil, title: title)
    @panel = Wx::Panel.new(self)

    @toolbar = create_tool_bar
 
    # Tool 1
    @toolbar.add_tool(Wx::ID_ANY, 'Open', Wx::Bitmap(:open))
 
    # Tool 2
    @toolbar.add_tool(Wx::ID_ANY, 'Save', Wx::Bitmap(:save))

    # Tool 3
    @toolbar.add_check_tool(Wx::ID_ANY, 'Center', Wx::Bitmap(:center))
    
    @toolbar.realize
    
    centre
  end
end

Wx::App.run do
  window = MyWindow.new("wxRuby ToolBar Guide")
  window.show
end

The output, where we have selected the Center tool option:

screenshot2

Note: To run this example you will need the bitmap files too. Download those here, here and here.

Adding Control Tools

In this example we will add a Control tool in our ToolBar.

The control widget must first be created (such as a Wx::ComboBox), and parented to the Toolbar. It can then be added to the ToolBar using the #add_cControl method.

require 'wx'

class MyWindow < Wx::Frame
  def initialize(title)
    super(nil, title: title)
    @panel = Wx::Panel.new(self)

    @toolbar = create_tool_bar
 
    # Tool 1
    @toolbar.add_tool(Wx::ID_ANY, 'Open', Wx::Bitmap(:open))
 
    # Tool 2
    @toolbar.add_tool(Wx::ID_ANY, 'Save', Wx::Bitmap(:save))

    # Tool 3
    @toolbar.add_check_tool(Wx::ID_ANY, 'Center', Wx::Bitmap(:center))

    # Tool 4
    combo = Wx::ComboBox.new(@toolbar, choices: %w[Option1 Option2])
    @toolbar.add_control(combo)
    
    @toolbar.realize
    
    centre
  end
end

Wx::App.run do
  window = MyWindow.new("wxRuby ToolBar Guide")
  window.show
end

The output of the above code:

screenshot3

Note: To run this example you will need the bitmap files too. Download those here, here and here.

⚠️ **GitHub.com Fallback** ⚠️