toolbar overview
classes: wxtoolbar
the toolbar family of classes allows an application to use toolbars
in a variety of configurations and styles.
the toolbar is a popular user interface component and contains a set of bitmap
buttons or toggles. a toolbar gives faster access to an application's facilities than
menus, which have to be popped up and selected rather laboriously.
instead of supplying one toolbar class with a number
of different implementations depending on platform, wxwidgets separates
out the classes. this is because there are a number of different toolbar
styles that you may wish to use simultaneously, and also, future
toolbar implementations will emerge which
cannot all be shoe-horned into the one class.
for each platform, the symbol wxtoolbar is defined to be one of the
specific toolbar classes.
the following is a summary of the toolbar classes and their differences.
- wxtoolbarbase. this is a base class with pure virtual functions,
and should not be used directly.
- wxtoolbarsimple. a simple toolbar class written entirely with generic wxwidgets
functionality. a simple 3d effect for buttons is possible, but it is not consistent
with the windows look and feel. this toolbar can scroll, and you can have arbitrary
numbers of rows and columns.
- wxtoolbarmsw. this class implements an old-style windows toolbar, only on
windows. there are small, three-dimensional buttons, which do not (currently) reflect
the current windows colour settings: the buttons are grey. this is the default wxtoolbar
on 16-bit windows.
- wxtoolbar95. uses the native windows 95 toolbar class. it dynamically adjusts its
background and button colours according to user colour settings.
createtools must be called after the tools have been added.
no absolute positioning is supported but you can specify the number
of rows, and add tool separators with addseparator.
tooltips are supported. onrightclick is not supported. this is the default wxtoolbar
on windows 95, windows nt 4 and above. with the style wxtb_flat, the flat toolbar
look is used, with a border that is highlighted when the cursor moves over the buttons.
a toolbar might appear as a single row of images under
the menubar, or it might be in a separate frame layout in several rows
and columns. the class handles the layout of the images, unless explicit
positioning is requested.
a tool is a bitmap which can either be a button (there is no 'state',
it just generates an event when clicked) or it can be a toggle. if a
toggle, a second bitmap can be provided to depict the 'on' state; if
the second bitmap is omitted, either the inverse of the first bitmap
will be used (for monochrome displays) or a thick border is drawn
around the bitmap (for colour displays where inverting will not have
the desired result).
the windows-specific toolbar classes expect 16-colour bitmaps that are 16 pixels wide and 15 pixels
high. if you want to use a different size, call settoolbitmapsize
as the demo shows, before adding tools to the button bar. don't supply more than
one bitmap for each tool, because the toolbar generates all three images (normal,
depressed and checked) from the single bitmap you give it.
using the toolbar library
using the toolbar library
include "wx/toolbar.h", or if using a class directly, one of:
- "wx/msw/tbarmsw.h for wxtoolbarmsw
- "wx/msw/tbar95.h for wxtoolbar95
- "wx/tbarsmpl.h for wxtoolbarsimple
example of toolbar use are given in the sample program "toolbar''. the
source is given below. in fact it is out of date because recommended
practise is to use event handlers (using evt_menu or evt_tool) instead of
overriding onleftclick.
/////////////////////////////////////////////////////////////////////////////
// name: test.cpp
// purpose: wxtoolbar sample
// author: julian smart
// modified by:
// created: 04/01/98
// rcs-id: $id: ttoolbar.tex 32309 2005-02-22 15:09:56z abx $
// copyright: (c) julian smart
// license: wxwindows license
/////////////////////////////////////////////////////////////////////////////
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __borlandc__
#pragma hdrstop
#endif
#ifndef wx_precomp
#include "wx/wx.h"
#endif
#include "wx/toolbar.h"
#include <wx/log.h>
#include "test.h"
#if defined(__wxgtk__) || defined(__wxmotif__)
#include "mondrian.xpm"
#include "bitmaps/new.xpm"
#include "bitmaps/open.xpm"
#include "bitmaps/save.xpm"
#include "bitmaps/copy.xpm"
#include "bitmaps/cut.xpm"
#include "bitmaps/print.xpm"
#include "bitmaps/preview.xpm"
#include "bitmaps/help.xpm"
#endif
implement_app(myapp)
// the `main program' equivalent, creating the windows and returning the
// main frame
bool myapp::oninit(void)
{
// create the main frame window
myframe* frame = new myframe((wxframe *) null, -1, (const wxstring) "wxtoolbar sample",
wxpoint(100, 100), wxsize(450, 300));
// give it a status line
frame->createstatusbar();
// give it an icon
frame->seticon(wxicon(mondrian));
// make a menubar
wxmenu *filemenu = new wxmenu;
filemenu->append(wxid_exit, "e&xit", "quit toolbar sample" );
wxmenu *helpmenu = new wxmenu;
helpmenu->append(wxid_help, "&about", "about toolbar sample");
wxmenubar* menubar = new wxmenubar;
menubar->append(filemenu, "&file");
menubar->append(helpmenu, "&help");
// associate the menu bar with the frame
frame->setmenubar(menubar);
// create the toolbar
frame->createtoolbar(wxno_border|wxhorizontal|wxtb_flat, id_toolbar);
frame->gettoolbar()->setmargins( 2, 2 );
inittoolbar(frame->gettoolbar());
// force a resize. this should probably be replaced by a call to a wxframe
// function that lays out default decorations and the remaining content window.
wxsizeevent event(wxsize(-1, -1), frame->getid());
frame->onsize(event);
frame->show(true);
frame->setstatustext("hello, wxwidgets");
settopwindow(frame);
return true;
}
bool myapp::inittoolbar(wxtoolbar* toolbar)
{
// set up toolbar
wxbitmap* toolbarbitmaps[8];
#ifdef __wxmsw__
toolbarbitmaps[0] = new wxbitmap("icon1");
toolbarbitmaps[1] = new wxbitmap("icon2");
toolbarbitmaps[2] = new wxbitmap("icon3");
toolbarbitmaps[3] = new wxbitmap("icon4");
toolbarbitmaps[4] = new wxbitmap("icon5");
toolbarbitmaps[5] = new wxbitmap("icon6");
toolbarbitmaps[6] = new wxbitmap("icon7");
toolbarbitmaps[7] = new wxbitmap("icon8");
#else
toolbarbitmaps[0] = new wxbitmap( new_xpm );
toolbarbitmaps[1] = new wxbitmap( open_xpm );
toolbarbitmaps[2] = new wxbitmap( save_xpm );
toolbarbitmaps[3] = new wxbitmap( copy_xpm );
toolbarbitmaps[4] = new wxbitmap( cut_xpm );
toolbarbitmaps[5] = new wxbitmap( preview_xpm );
toolbarbitmaps[6] = new wxbitmap( print_xpm );
toolbarbitmaps[7] = new wxbitmap( help_xpm );
#endif
#ifdef __wxmsw__
int width = 24;
#else
int width = 16;
#endif
int currentx = 5;
toolbar->addtool(wxid_new, *(toolbarbitmaps[0]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "new file");
currentx += width + 5;
toolbar->addtool(wxid_open, *(toolbarbitmaps[1]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "open file");
currentx += width + 5;
toolbar->addtool(wxid_save, *(toolbarbitmaps[2]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "save file");
currentx += width + 5;
toolbar->addseparator();
toolbar->addtool(wxid_copy, *(toolbarbitmaps[3]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "copy");
currentx += width + 5;
toolbar->addtool(wxid_cut, *(toolbarbitmaps[4]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "cut");
currentx += width + 5;
toolbar->addtool(wxid_paste, *(toolbarbitmaps[5]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "paste");
currentx += width + 5;
toolbar->addseparator();
toolbar->addtool(wxid_print, *(toolbarbitmaps[6]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "print");
currentx += width + 5;
toolbar->addseparator();
toolbar->addtool(wxid_help, *(toolbarbitmaps[7]), wxnullbitmap, false, currentx, -1, (wxobject *) null, "help");
toolbar->realize();
// can delete the bitmaps since they're reference counted
int i;
for (i = 0; i < 8; i++)
delete toolbarbitmaps[i];
return true;
}
// wxid_help will be processed for the 'about' menu and the toolbar help button.
begin_event_table(myframe, wxframe)
evt_menu(wxid_exit, myframe::onquit)
evt_menu(wxid_help, myframe::onabout)
evt_close(myframe::onclosewindow)
evt_tool_range(wxid_open, wxid_paste, myframe::ontoolleftclick)
evt_tool_enter(wxid_open, myframe::ontoolenter)
end_event_table()
// define my frame constructor
myframe::myframe(wxframe* parent, wxwindowid id, const wxstring& title, const wxpoint& pos,
const wxsize& size, long style):
wxframe(parent, id, title, pos, size, style)
{
m_textwindow = new wxtextctrl(this, -1, "", wxpoint(0, 0), wxsize(-1, -1), wxte_multiline);
}
void myframe::onquit(wxcommandevent& wxunused(event))
{
close(true);
}
void myframe::onabout(wxcommandevent& wxunused(event))
{
(void)wxmessagebox("wxwidgets toolbar sample", "about wxtoolbar");
}
// define the behaviour for the frame closing
// - must delete all frames except for the main one.
void myframe::onclosewindow(wxcloseevent& wxunused(event))
{
destroy();
}
void myframe::ontoolleftclick(wxcommandevent& event)
{
wxstring str;
str.printf("clicked on tool %d", event.getid());
setstatustext(str);
}
void myframe::ontoolenter(wxcommandevent& event)
{
if (event.getselection() > -1)
{
wxstring str;
str.printf("this is tool number %d", event.getselection());
setstatustext(str);
}
else
setstatustext("");
}
|