4d. wxWidgets – Multiple Toolbars

OVERVIEW

We extend the code from the previous section to have two toolbars and divide the existing icons between the two. We use a wxBoxSizer in this case so that we can put the toolbars one below the other. We look at using sizers later in this series.

SAMPLE CODE

toolbars2.h
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
	#include <wx/wx.h>
#endif

class Toolbar: public wxFrame {

	public:
		Toolbar(const wxString& title);
		void OnToolItem(wxCommandEvent &evt);
		void OnToolItem2(wxCommandEvent &evt);

};

const int ID_NEW = 101;
const int ID_SAVE = 102;
const int ID_OPEN = 103;
const int ID_EXPORT = 104;


toolbars2.cpp
#include "toolbars2.h"

Toolbar::Toolbar(const wxString& title):
	wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,300)) {

	SetBackgroundColour(wxColour(255,255,255));
	wxImage::AddHandler(new wxPNGHandler());	
	wxBitmap bmpAdd (wxT("bulb.png"), wxBITMAP_TYPE_PNG);
	wxBitmap bmpSave (wxT("folder.png"), wxBITMAP_TYPE_PNG);
	wxBitmap bmpOpen (wxT("edit.png"), wxBITMAP_TYPE_PNG);
	wxBitmap bmpExport (wxT("export.png"), wxBITMAP_TYPE_PNG);
	
	wxToolBar *tbr = new wxToolBar(this, -1);
	tbr->AddTool(ID_NEW, wxT("Add"), bmpAdd);
	tbr->AddTool(ID_SAVE, wxT("Save"), bmpSave);
	tbr->Realize();
	tbr->Bind(wxEVT_TOOL, &Toolbar::OnToolItem, this);

	wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);

	wxToolBar *tbr2 = new wxToolBar(this, -1, wxDefaultPosition, wxDefaultSize, wxTB_BOTTOM);
	tbr2->AddTool(ID_OPEN, wxT("Open"), bmpOpen);
	tbr2->AddTool(ID_EXPORT, wxT("Export"), bmpExport);
	tbr2->Bind(wxEVT_TOOL, &Toolbar::OnToolItem2, this);
	tbr2->Realize();
	vbox->Add(tbr, 0, wxEXPAND | wxALL);
	vbox->Add(tbr2, 0, wxEXPAND | wxALL);
	SetSizer(vbox);

}

void Toolbar::OnToolItem(wxCommandEvent &evt) {

	int eventId= evt.GetId();
	wxString strId = wxT("");
	strId = wxString::Format(wxT("%d"), evt.GetId());
	wxPuts(strId);
	if (eventId == ID_NEW) 
		wxLogMessage("New file item");
	else if (eventId == ID_SAVE )
		wxLogMessage("Save file item");
	else if (eventId == wxID_EXIT)
		Close(true);
}

void Toolbar::OnToolItem2(wxCommandEvent &evt) {

	int eventId= evt.GetId();
	wxString strId = wxT("");
	strId = wxString::Format(wxT("%d"), evt.GetId());
	wxPuts(strId);
	if (eventId == ID_OPEN) 
		wxLogMessage("Open file item");
	else if (eventId == ID_EXPORT) 
		wxLogMessage("Export file item");

	else if (eventId == wxID_EXIT)
		Close(true);
}



The output is given below

1 Trackback / Pingback

  1. 1.wxWidgets – Introduction – Truelogic Blog

Leave a Reply

Your email address will not be published.


*