wxRuby Documentation Home

# Wx::ToolBar

A ToolBar is a collection of graphical buttons and/or controls which is attached
to the edge of a Wx::Frame. It provides quick access to
one-step application functions, such as opening or saving a file, or
cutting and pasting text.

It has a native style on all platforms; note that on OS X, native
toolbars may only be placed at the top of a Frame. Use
Frame#set_tool_bar to set a toolbar to be managed by a Frame and ensure
native appearance.

## Derived from

Control

Window

EvtHandler

Object

## Remarks

You may also create a toolbar that is managed by the frame, by
calling Frame#create_tool_bar. Under Pocket PC,
you should always use this function for creating the toolbar
to be managed by the frame, so that Widgets can use a combined
menubar and toolbar. Where you manage your own toolbars, create a ToolBar
as usual.

The meaning of a “separator” is a vertical line under Windows and simple
space under GTK+.

## Window styles

`TB_FLAT` | Gives the toolbar a flat look (Windows and GTK only).
`TB_DOCKABLE` | Makes the toolbar floatable and dockable (GTK only).
`TB_HORIZONTAL` | Specifies horizontal layout (default).
`TB_VERTICAL` | Specifies vertical layout.
`TB_TEXT` | Shows the text in the toolbar buttons; by default only icons are shown.
`TB_NOICONS` | Specifies no icons in the toolbar buttons; by default they are shown.
`TB_NODIVIDER` | Specifies no divider (border) above the toolbar (Windows only).
`TB_NOALIGN` | Specifies no alignment with the parent window (Windows only, not very useful).
`TB_HORZ_LAYOUT` | Shows the text and the icons alongside, not vertically stacked (Windows and GTK2 only). This style must be used with TB_TEXT.
`TB_HORZ_TEXT` | Combination of TB_HORZ_LAYOUT and TB_TEXT.
`TB_NO_TOOLTIPS` | Don’t show the short help tooltips for the tools when the mouse hovers over them.
`TB_BOTTOM` | Align the toolbar at the bottom of parent window.
`TB_RIGHT` | Align the toolbar at the right side of parent window.

See also window styles overview. Note that the Win32
native toolbar ignores `TB_NOICONS` style. Also, toggling the
`TB_TEXT` works only if the style was initially on.

## Adding and inserting items

Adding and inserting an item (a tool, a check tool, or a radio tool) to a toolbar may be done respectively through ToolBar#add_tool and ToolBar#insert_tool.
A more flexible way is provided by ToolBar#add_item that supports :

- both the addition and the insertion of an item,
- both positional and keyword arguments (an argument may be specified by its position or by its keyword).

## Event handling

The toolbar class emits menu commands in the same way that a frame menubar does,
so you can use one EVT_MENU macro for both a menu item and a toolbar button.
The event handler functions take a CommandEvent argument. For most event macros,
the identifier of the tool is passed, but for EVT_TOOL_ENTER the toolbar
window identifier is passed and the tool identifier is retrieved from the CommandEvent.
This is because the identifier may be -1 when the mouse moves off a tool, and -1 is not
allowed as an identifier in the event system.

evt_tool(id) { | event | … } | Process a EVT_COMMAND_TOOL_CLICKED event(a synonym for EVT_COMMAND_MENU_SELECTED). Pass the id of the tool.
evt_menu(id) { | event | … } | The same as EVT_TOOL.
EVT_TOOL_RANGE(id1, id2, func) | Process a EVT_COMMAND_TOOL_CLICKED eventfor a range of identifiers. Pass the ids of the tools.
EVT_MENU_RANGE(id1, id2, func) | The same as EVT_TOOL_RANGE.
evt_tool_rclicked(id) { | event | … } | Process a EVT_COMMAND_TOOL_RCLICKED event.Pass the id of the tool.
EVT_TOOL_RCLICKED_RANGE(id1, id2, func) | Process a EVT_COMMAND_TOOL_RCLICKED eventfor a range of ids. Pass the ids of the tools.
evt_tool_enter(id) { | event | … } | Process a EVT_COMMAND_TOOL_ENTER event.Pass the id of the toolbar itself. The value of CommandEvent::GetSelection is the tool id, or -1 if the mouse cursor has moved off a tool.

## See also

Toolbar overviewwxtoolbaroverview, ScrolledWindow

## Methods

- ToolBar.new
- ToolBar#add_control
- ToolBar#add_separator
- ToolBar#add_item
- ToolBar#add_tool
- ToolBar#add_check_tool
- ToolBar#add_radio_tool
- ToolBar#clear_tools
- ToolBar#delete_tool
- ToolBar#delete_tool_by_pos
- ToolBar#enable_tool
- ToolBar#find_by_id
- ToolBar#find_control
- ToolBar#find_tool_for_position
- ToolBar#get_tools_count
- ToolBar#get_tool_size
- ToolBar#get_tool_bitmap_size
- ToolBar#get_margins
- ToolBar#get_tool_client_data
- ToolBar#get_tool_enabled
- ToolBar#get_tool_long_help
- ToolBar#get_tool_packing
- ToolBar#get_tool_pos
- ToolBar#get_tool_separation
- ToolBar#get_tool_short_help
- ToolBar#get_tool_state
- ToolBar#insert_control
- ToolBar#insert_separator
- ToolBar#insert_tool
- ToolBar#on_left_click
- ToolBar#on_mouse_enter
- ToolBar#on_right_click
- ToolBar#realize
- ToolBar#remove_tool
- ToolBar#set_bitmap_resource
- ToolBar#set_margins
- ToolBar#set_tool_bitmap_size
- ToolBar#set_tool_client_data
- ToolBar#set_tool_disabled_bitmap
- ToolBar#set_tool_long_help
- ToolBar#set_tool_packing
- ToolBar#set_tool_short_help
- ToolBar#set_tool_normal_bitmap
- ToolBar#set_tool_separation
- ToolBar#toggle_tool

### ToolBar.new

ToolBar.new(%(arg-type)Window% parent, Integer id, Point pos = DEFAULT_POSITION, Size size = DEFAULT_SIZE, Integer style = TB_HORIZONTAL NO_BORDER, String name = PanelNameStr)

Constructs a toolbar.

#### Parameters

- parent Pointer to a parent window.
- id Window identifier. If -1, will automatically create an identifier.
- pos Window position. DefaultPosition is (-1, -1) which indicates that Widgets
  should generate a default position for the window. If using the Window class directly, supply
  an actual position.
- size Window size. DefaultSize is (-1, -1) which indicates that Widgets
  should generate a default size for the window.
- style Window style. See ToolBar for details.
- name Window name.

#### Remarks

After a toolbar is created, you use ToolBar#add_tool and
perhaps ToolBar#add_separator, and then you
must call ToolBar#realize to construct and display the toolbar
tools.

You may also create a toolbar that is managed by the frame, by
calling Frame#create_tool_bar.

destructor()

Toolbar destructor.

### ToolBar#add_control

Boolean add_control(%(arg-type)Control% control)

Adds any control to the toolbar, typically e.g. a combobox.

- control The control to be added.

### ToolBar#add_separator

add_separator()

Adds a separator for spacing groups of tools.

#### See also

ToolBar#add_tool, ToolBar#set_tool_separation

### ToolBar#add_item

ToolBarTool add_item(%(arg-type)Bitmap% bitmap1, Bitmap bitmap2 = NULL_BITMAP, Integer position = -1, Integer id = -1, String label =’’, ItemKind kind = ITEM_NORMAL, String short_help = ’’, String long_help = ’’, Object client_data = nil)

Adds or inserts a tool to the toolbar. Returns a ToolBarTool object with
information about the newly added item.
Optional parameters may also be specified by keywords in any order as following :
tool = toolbar.add_item(a_bitmap, :position => 1, :label => “My Button”)

#### Parameters

- bitmap1 The primary tool bitmap.
- bitmap2 The bitmap used when the tool is disabled. If it is equal to
  NULL_BITMAP, the disabled bitmap is automatically generated by greing the
  normal one.
- position If equals to -1, the tool is added at the end of the toolbar.
  Otherwise, it is inserted at the specified position.
- id An integer by which
  the tool may be identified in subsequent operations.
- label The label of the tool.
- kind May be ITEM_NORMAL for a normal button (default),
  ITEM_CHECK for a checkable tool (such tool stays pressed after it had been
  toggled) or ITEM_RADIO for a checkable tool which makes part of a radio
  group of tools each of which is automatically unchecked whenever another button
  in the group is checked
- short_help This string is used for the tools tooltip
- long_help This string is shown in the statusbar (if any) of the
  parent frame when the mouse pointer is inside the tool
- client_data An optional pointer to client data which can be
  retrieved later using ToolBar#get_tool_client_data.

#### Remarks

After you have added tools to a toolbar, you must call ToolBar#realize in
order to have the tools appear.

#### See also

ToolBar#add_separator, ToolBar#add_tool, ToolBar#add_check_tool, ToolBar#add_radio_tool, ToolBar#insert_tool, ToolBar#delete_tool, ToolBar#realize

### ToolBar#add_tool

ToolBarTool add_tool(%(arg-type)Integer% toolId, String label, Bitmap bitmap1, String shortHelpString = "", ItemKind kind = ITEM_NORMAL) ToolBarTool add_tool(%(arg-type)Integer% toolId, String label, Bitmap bitmap1, Bitmap bitmap2 = NullBitmap, ItemKind kind = ITEM_NORMAL, String shortHelpString = "", String longHelpString = "", Object clientData = nil)

Adds a tool to the toolbar. Returns a ToolBarTool object with
information about the newly added item.

The first (short and most commonly used) version has fewer parameters
than the full version at the price of not being able to specify some of
the more rarely used button features.

#### Parameters

- toolId An integer by which
  the tool may be identified in subsequent operations.
- kind May be ITEM_NORMAL for a normal button (default),
  ITEM_CHECK for a checkable tool (such tool stays pressed after it had been
  toggled) or ITEM_RADIO for a checkable tool which makes part of a radio
  group of tools each of which is automatically unchecked whenever another button
  in the group is checked
- bitmap1 The primary tool bitmap.
- bitmap2 The bitmap used when the tool is disabled. If it is equal to
  NullBitmap, the disabled bitmap is automatically generated by greing the
  normal one.
- shortHelpString This string is used for the tools tooltip
- longHelpString This string is shown in the statusbar (if any) of the
  parent frame when the mouse pointer is inside the tool
- clientData An optional pointer to client data which can be
  retrieved later using ToolBar#get_tool_client_data.

#### Remarks

After you have added tools to a toolbar, you must call ToolBar#realize in
order to have the tools appear.

#### See also

ToolBar#add_separator, ToolBar#add_check_tool, ToolBar#add_radio_tool, ToolBar#insert_tool, ToolBar#delete_tool, ToolBar#realize

### ToolBar#add_check_tool

ToolBarTool add_check_tool(%(arg-type)Integer% toolId, String label, Bitmap bitmap1, Bitmap bitmap2, String shortHelp = "", String longHelp = "", Object clientData = nil)

Adds a new check (or toggle) tool to the toolbar. The parameters are the same
as in ToolBar#add_tool.

#### See also

ToolBar#add_tool

### ToolBar#add_radio_tool

ToolBarTool add_radio_tool(%(arg-type)Integer% toolId, String label, Bitmap bitmap1, Bitmap bitmap2, String shortHelpString = "", String longHelpString = "", Object clientData = nil)

Adds a new radio tool to the toolbar. Consecutive radio tools form a radio
group such that exactly one button in the group is pressed at any moment, in
other words whenever a button in the group is pressed the previously pressed
button is automatically released. You should avoid having the radio groups of
only one element as it would be impossible for the user to use such button.

By default, the first button in the radio group is initially pressed, the
others are not.

#### See also

ToolBar#add_tool

### ToolBar#clear_tools

clear_tools()

Deletes all the tools in the toolbar.

### ToolBar#delete_tool

Boolean delete_tool(%(arg-type)Integer% toolId)

Removes the specified tool from the toolbar and deletes it. If you don’t want
to delete the tool, but just to remove it from the toolbar (to possibly add it
back later), you may use remove_tool instead.

Note that it is unnecessary to call Realize for the
change to take place, it will happen immediately.

Returns true if the tool was deleted, false otherwise.

#### See also

delete_tool_by_pos

### ToolBar#delete_tool_by_pos

Boolean delete_tool_by_pos(%(arg-type)Integer% pos)

This function behaves like delete_tool but it
deletes the tool at the specified position and not the one with the given id.

### ToolBar#enable_tool

enable_tool(%(arg-type)Integer% toolId, Boolean enable)

Enables or disables the tool.

#### Parameters

- toolId Tool to enable or disable.
- enable If true, enables the tool, otherwise disables it.

NB: This function should only be called after
Realize.

#### Remarks

Some implementations will change the visible state of the tool to indicate that it is disabled.

#### See also

ToolBar#get_tool_enabled, ToolBar#toggle_tool

### ToolBar#find_by_id

ToolBarTool find_by_id(%(arg-type)Integer% id)

Returns information about the item with the passed-in id.

### ToolBar#find_control

Control find_control(%(arg-type)Integer% id)

Returns a pointer to the Wx::Control identified by id or nil if no
corresponding control is found.

### ToolBar#get_tools_count

Integer get_tools_count()

Returns the number of tools in the toolbar.

### ToolBar#get_tool_size

Size get_tool_size()

Returns the size of a whole button, which is usually larger than a tool bitmap because
of added 3D effects.

#### See also

ToolBar#set_tool_bitmap_size, ToolBar#get_tool_bitmap_size

### ToolBar#get_tool_bitmap_size

Size get_tool_bitmap_size()

Returns the size of bitmap that the toolbar expects to have. The default bitmap size is 16 by 15 pixels.

#### Remarks

Note that this is the size of the bitmap you pass to ToolBar#add_tool,
and not the eventual size of the tool button.

#### See also

ToolBar#set_tool_bitmap_size, ToolBar#get_tool_size

### ToolBar#get_margins

Size get_margins()

Returns the left/right and top/bottom margins, which are also used for inter-toolspacing.

#### See also

ToolBar#set_margins

### ToolBar#get_tool_client_data

Object get_tool_client_data(%(arg-type)Integer% toolId)

Get any client data associated with the tool.

#### Parameters

- toolId Id of the tool, as passed to ToolBar#add_tool.

#### Return value

Client data, or NULL if there is none.

### ToolBar#get_tool_enabled

Boolean get_tool_enabled(%(arg-type)Integer% toolId)

Called to determine whether a tool is enabled (responds to user input).

#### Parameters

- toolId Id of the tool in question.

#### Return value

true if the tool is enabled, false otherwise.

#### See also

ToolBar#enable_tool

### ToolBar#get_tool_long_help

String get_tool_long_help(%(arg-type)Integer% toolId)

Returns the long help for the given tool.

#### Parameters

- toolId The tool in question.

#### See also

ToolBar#set_tool_long_help, ToolBar#set_tool_short_help

### ToolBar#get_tool_packing

Integer get_tool_packing()

Returns the value used for packing tools.

#### See also

ToolBar#set_tool_packing

### ToolBar#get_tool_pos

Integer get_tool_pos(%(arg-type)Integer% toolId)

Returns the tool position in the toolbar, or `NOT_FOUND` if the tool is not found.

### ToolBar#get_tool_separation

Integer get_tool_separation()

Returns the default separator size.

#### See also

ToolBar#set_tool_separation

### ToolBar#get_tool_short_help

String get_tool_short_help(%(arg-type)Integer% toolId)

Returns the short help for the given tool.

#### Parameters

- toolId The tool in question.

#### See also

ToolBar#get_tool_long_help, ToolBar#set_tool_short_help
h3(#ToolBar_gettoolstate). ToolBar#get_tool_state

Boolean get_tool_state(%(arg-type)Integer% toolId)

Gets the on/off state of a toggle tool.

#### Parameters

- toolId The tool in question.

#### Return value

true if the tool is toggled on, false otherwise.

#### See also

ToolBar#toggle_tool

### ToolBar#insert_control

ToolBarTool insert_control(%(arg-type)Integer% pos, Control control)

Inserts the control into the toolbar at the given position.

You must call Realize for the change to take place.

#### See also

add_control,

insert_tool

### ToolBar#insert_separator

ToolBarTool insert_separator(%(arg-type)Integer% pos)

Inserts the separator into the toolbar at the given position.

You must call Realize for the change to take place.

#### See also

add_separator,

insert_tool

### ToolBar#insert_tool

ToolBarTool insert_tool(%(arg-type)Integer% pos, Integer toolId, String label, Bitmap bitmap1, Bitmap bitmap2 = NullBitmap, Integer type = Wx::ITEM_NORMAL, String shortHelp = "", String longHelp = "", Object clientData = nil)

Inserts the tool with the specified attributes into the toolbar at the given
position. Returns a ToolBarTool object with
information about the newly inserted item.

You must call Realize for the change to take place.

#### See also

add_tool,

insert_control,

insert_separator

### ToolBar#on_left_click

Boolean on_left_click(%(arg-type)Integer% toolId, Boolean toggleDown)

Called when the user clicks on a tool with the left mouse button.

This is the old way of detecting tool clicks; although it will still work,
you should use the EVT_MENU or EVT_TOOL macro instead.

#### Parameters

- toolId The identifier passed to ToolBar#add_tool.
- toggleDown true if the tool is a toggle and the toggle is down, otherwise is false.

#### Return value

If the tool is a toggle and this function returns false, the toggle
toggle state (internal and visual) will not be changed. This provides a way of
specifying that toggle operations are not permitted in some circumstances.

#### See also

ToolBar#on_mouse_enter, ToolBar#on_right_click

### ToolBar#on_mouse_enter

on_mouse_enter(%(arg-type)Integer% toolId)

This is called when the mouse cursor moves into a tool or out of
the toolbar.

This is the old way of detecting mouse enter events; although it will still work,
you should use the EVT_TOOL_ENTER macro instead.

#### Parameters

- toolId Greater than -1 if the mouse cursor has moved into the tool,
  or -1 if the mouse cursor has moved. The
  programmer can override this to provide extra information about the tool,
  such as a short description on the status line.

#### Remarks

With some derived toolbar classes, if the mouse moves quickly out of the toolbar, Widgets may not be able to
detect it. Therefore this function may not always be called when expected.

### ToolBar#on_right_click

on_right_click(%(arg-type)Integer% toolId, Float x, Float y)

Called when the user clicks on a tool with the right mouse button. The
programmer should override this function to detect right tool clicks.

This is the old way of detecting tool right clicks; although it will still work,
you should use the EVT_TOOL_RCLICKED macro instead.

#### Parameters

- toolId The identifier passed to ToolBar#add_tool.
- x The x position of the mouse cursor.
- y The y position of the mouse cursor.

#### Remarks

A typical use of this member might be to pop up a menu.

#### See also

ToolBar#on_mouse_enter, ToolBar#on_left_click

### ToolBar#realize

Boolean realize()

This function should be called after you have added tools.

### ToolBar#remove_tool

ToolBarTool remove_tool(%(arg-type)Integer% id)

Removes the given tool from the toolbar but doesn’t delete it. This allows to
insert/add this tool back to this (or another) toolbar later.

Note that it is unnecessary to call Realize for the
change to take place, it will happen immediately.

#### See also

delete_tool

### ToolBar#set_bitmap_resource

set_bitmap_resource(%(arg-type)Integer% resourceId)

Sets the bitmap resource identifier for specifying tool bitmaps as indices
into a custom bitmap. Windows CE only.

### ToolBar#set_margins

set_margins(%(arg-type)Size% size) set_margins(%(arg-type)Integer% x, Integer y)

Set the values to be used as margins for the toolbar.

#### Parameters

- size Margin size.
- x Left margin, right margin and inter-tool separation value.
- y Top margin, bottom margin and inter-tool separation value.

#### Remarks

This must be called before the tools are added if absolute positioning is to be used, and the
default (zero-size) margins are to be overridden.

#### See also

ToolBar#get_margins, Size

### ToolBar#set_tool_bitmap_size

set_tool_bitmap_size(%(arg-type)Size% size)

Sets the default size of each tool bitmap. The default bitmap size is 16 by 15 pixels.

#### Parameters

- size The size of the bitmaps in the toolbar.

#### Remarks

This should be called to tell the toolbar what the tool bitmap size is. Call
it before you add tools.

Note that this is the size of the bitmap you pass to ToolBar#add_tool,
and not the eventual size of the tool button.

#### See also

ToolBar#get_tool_bitmap_size, ToolBar#get_tool_size

### ToolBar#set_tool_client_data

set_tool_client_data(%(arg-type)Integer% id, Object clientData)

Sets the client data associated with the tool.

### ToolBar#set_tool_disabled_bitmap

set_tool_disabled_bitmap(%(arg-type)Integer% id, Bitmap bitmap)

Sets the bitmap to be used by the tool with the given ID when the tool
is in a disabled state. This can only be used on Button tools, not
controls. NOTE: The native toolbar classes on the main platforms all
synthesize the disabled bitmap from the normal bitmap, so this function
will have no effect on those platforms.

### ToolBar#set_tool_long_help

set_tool_long_help(%(arg-type)Integer% toolId, String helpString)

Sets the long help for the given tool.

#### Parameters

- toolId The tool in question.
- helpString A string for the long help.

#### Remarks

You might use the long help for displaying the tool purpose on the status line.

### ToolBar#set_tool_normal_bitmap

set_tool_normal_bitmap(%(arg-type)Integer% id, Bitmap bitmap)

Sets the bitmap to be used by the tool with the given ID. This can only
be used on Button tools, not controls.

#### See also

ToolBar#get_tool_long_help, ToolBar#set_tool_short_help,
h3(#ToolBar_settoolpacking). ToolBar#set_tool_packing

set_tool_packing(%(arg-type)Integer% packing)

Sets the value used for spacing tools. The default value is 1.

#### Parameters

- packing The value for packing.

#### Remarks

The packing is used for spacing in the vertical direction if the toolbar is horizontal,
and for spacing in the horizontal direction if the toolbar is vertical.

#### See also

ToolBar#get_tool_packing

### ToolBar#set_tool_short_help

set_tool_short_help(%(arg-type)Integer% toolId, String helpString)

Sets the short help for the given tool.

#### Parameters

- toolId The tool in question.
- helpString The string for the short help.

#### Remarks

An application might use short help for identifying the tool purpose in a tooltip.

#### See also

ToolBar#get_tool_short_help, ToolBar#set_tool_long_help

### ToolBar#set_tool_separation

set_tool_separation(%(arg-type)Integer% separation)

Sets the default separator size. The default value is 5.

#### Parameters

- separation The separator size.

#### See also

ToolBar#add_separator

### ToolBar#toggle_tool

toggle_tool(%(arg-type)Integer% toolId, Boolean toggle)

Toggles a tool on or off. This does not cause any event to get emitted.

#### Parameters

- toolId Tool in question.
- toggle If true, toggles the tool on, otherwise toggles it off.

#### Remarks

Only applies to a tool that has been specified as a toggle tool.

#### See also

ToolBar#get_tool_state

[This page automatically generated from the Textile source at 2023-06-09 00:45:27 +0000]