The wxWidgets programming tutorial (3. Menus and Toolbars).
Translations of this material:
- into Russian: Руководство по программированию с wxWidgets (3. Меню и панели инструментов).. Translation complete.
-
Submitted for translation by ber113 14.03.2010
Published 2 years, 2 months ago.
Text
Menus and Toolbars
Menubar
A menubar is one of the most visible parts of the GUI application. It is a group of commands located in various menus. While in console applications you had to remember all those arcane commands, here we have most of the commands grouped into logical parts. There are accepted standards that further reduce the amount of time spending to learn a new application. To implement a menubar in wxWidgets we need to have three things. A wxMenuBar, a wxMenu and a wxMenuItem.
Menubar
Figure: Menubar
Simple menu example
Creating a menubar in wxWidgets is very simple. Just a few lines of code.
menu.h
#include <wx/wx.h>
#include <wx/menu.h>
class SimpleMenu : public wxFrame
{
public:
SimpleMenu(const wxString& title);
void OnQuit(wxCommandEvent& event);
wxMenuBar *menubar;
wxMenu *file;
};
menu.cpp
#include "menu.h"
SimpleMenu::SimpleMenu(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{
menubar = new wxMenuBar;
file = new wxMenu;
file->Append(wxID_EXIT, wxT("&Quit"));
menubar->Append(file, wxT("&File"));
SetMenuBar(menubar);
Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(SimpleMenu::OnQuit));
Centre();
}
void SimpleMenu::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(true);
}
main.h
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
main.cpp
#include "main.h"
#include "menu.h"
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
SimpleMenu *menu = new SimpleMenu(wxT("Simple Menu"));
menu->Show(true);
return true;
}
menubar = new wxMenuBar;
First we create a menubar object.
file = new wxMenu;
Next we create a menu object.
We add a tool to the toolbar.
toolbar->Realize();
After we have added the tools, we call the Realize() method.
Toolbar
Figure: Toolbar
Toolbars
If we want to have more than one toolbar, we must create them in a different way. e.g. other than calling the CreateToolbar() method.
toolbars.h
#include <wx/wx.h>
class Toolbar : public wxFrame
{
public:
Toolbar(const wxString& title);
void OnQuit(wxCommandEvent& event);
wxToolBar *toolbar1;
wxToolBar *toolbar2;
};
toolbars.cpp
#include "toolbars.h"
Toolbar::Toolbar(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(280, 180))
{
wxImage::AddHandler( new wxPNGHandler );
wxBitmap exit(wxT("exit.png"), wxBITMAP_TYPE_PNG);
wxBitmap newb(wxT("new.png"), wxBITMAP_TYPE_PNG);
wxBitmap open(wxT("open.png"), wxBITMAP_TYPE_PNG);
wxBitmap save(wxT("save.png"), wxBITMAP_TYPE_PNG);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
toolbar1 = new wxToolBar(this, wxID_ANY);
toolbar1->AddTool(wxID_ANY, newb, wxT(""));
toolbar1->AddTool(wxID_ANY, open, wxT(""));
toolbar1->AddTool(wxID_ANY, save, wxT(""));
toolbar1->Realize();
toolbar2 = new wxToolBar(this, wxID_ANY);
toolbar2->AddTool(wxID_EXIT, exit, wxT("Exit application"));
toolbar2->Realize();
vbox->Add(toolbar1, 0, wxEXPAND);
vbox->Add(toolbar2, 0, wxEXPAND);
SetSizer(vbox);
Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED,
wxCommandEventHandler(Toolbar::OnQuit));
Centre();
}
