Updated the toolbar sample to reflect updated event handling.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@353 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -49,10 +49,10 @@ bool MyApp::OnInit(void)
|
||||
|
||||
// Make a menubar
|
||||
wxMenu *fileMenu = new wxMenu;
|
||||
fileMenu->Append(TEST_QUIT, "E&xit");
|
||||
fileMenu->Append(wxID_EXIT, "E&xit");
|
||||
|
||||
wxMenu *helpMenu = new wxMenu;
|
||||
helpMenu->Append(TEST_ABOUT, "&About");
|
||||
helpMenu->Append(wxID_HELP, "&About");
|
||||
|
||||
wxMenuBar* menuBar = new wxMenuBar;
|
||||
|
||||
@@ -63,14 +63,17 @@ bool MyApp::OnInit(void)
|
||||
frame->SetMenuBar(menuBar);
|
||||
|
||||
// Create the toolbar
|
||||
TestToolBar* toolRibbon = new TestToolBar(frame, -1, wxPoint(0, 0), wxSize(100, 30),
|
||||
wxToolBar* toolBar = new wxToolBar(frame, -1, wxPoint(0, 0), wxSize(100, 30),
|
||||
wxNO_BORDER|wxTB_FLAT, wxVERTICAL, 1);
|
||||
toolRibbon->SetMargins(5, 5);
|
||||
toolBar->SetMargins(5, 5);
|
||||
|
||||
InitToolbar(toolBar);
|
||||
|
||||
// Tell the frame about it
|
||||
frame->SetToolBar(toolRibbon);
|
||||
frame->SetToolBar(toolBar);
|
||||
|
||||
// Force a resize, just in case.
|
||||
// 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.
|
||||
frame->OnSize(wxSizeEvent(wxSize(-1, -1), frame->GetId()));
|
||||
frame->Show(TRUE);
|
||||
|
||||
@@ -81,62 +84,7 @@ bool MyApp::OnInit(void)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
||||
EVT_MENU(TEST_QUIT, MyFrame::OnQuit)
|
||||
EVT_MENU(TEST_ABOUT, MyFrame::OnAbout)
|
||||
EVT_MENU_HIGHLIGHT_ALL(MyFrame::OnMenuHighlight)
|
||||
EVT_CLOSE(MyFrame::OnCloseWindow)
|
||||
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& event)
|
||||
{
|
||||
Close(TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnAbout(wxCommandEvent& event)
|
||||
{
|
||||
(void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar");
|
||||
}
|
||||
|
||||
// Intercept menu item selection - only has an effect in Windows
|
||||
void MyFrame::OnMenuHighlight(wxMenuEvent& event)
|
||||
{
|
||||
char *msg = NULL;
|
||||
switch (event.GetMenuId())
|
||||
{
|
||||
case TEST_QUIT:
|
||||
msg = "Quit program";
|
||||
break;
|
||||
case -1:
|
||||
msg = "";
|
||||
break;
|
||||
}
|
||||
if (msg)
|
||||
SetStatusText(msg);
|
||||
}
|
||||
|
||||
// Define the behaviour for the frame closing
|
||||
// - must delete all frames except for the main one.
|
||||
void MyFrame::OnCloseWindow(wxCloseEvent& event)
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
BEGIN_EVENT_TABLE(TestToolBar, wxToolBar95)
|
||||
EVT_PAINT(TestToolBar::OnPaint)
|
||||
END_EVENT_TABLE()
|
||||
|
||||
TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
|
||||
long style, int direction, int RowsOrColumns):
|
||||
wxToolBar(parent, id, pos, size, style, direction, RowsOrColumns)
|
||||
bool MyApp::InitToolbar(wxToolBar* toolBar)
|
||||
{
|
||||
// Set up toolbar
|
||||
wxBitmap* toolBarBitmaps[8];
|
||||
@@ -161,7 +109,6 @@ TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, con
|
||||
toolBarBitmaps[5] = new wxBitmap(...);
|
||||
toolBarBitmaps[6] = new wxBitmap(...);
|
||||
toolBarBitmaps[7] = new wxBitmap(...);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __WXMSW__
|
||||
@@ -172,63 +119,86 @@ TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, con
|
||||
int offX = 5;
|
||||
int currentX = 5;
|
||||
|
||||
AddTool(0, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "New file");
|
||||
toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "New file");
|
||||
currentX += width + 5;
|
||||
AddTool(1, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Open file");
|
||||
toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Open file");
|
||||
currentX += width + 5;
|
||||
AddTool(2, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Save file");
|
||||
toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Save file");
|
||||
currentX += width + 5;
|
||||
AddSeparator();
|
||||
AddTool(3, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Copy");
|
||||
toolBar->AddSeparator();
|
||||
toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Copy");
|
||||
currentX += width + 5;
|
||||
AddTool(4, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Cut");
|
||||
toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Cut");
|
||||
currentX += width + 5;
|
||||
AddTool(5, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Paste");
|
||||
toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Paste");
|
||||
currentX += width + 5;
|
||||
AddSeparator();
|
||||
AddTool(6, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Print");
|
||||
toolBar->AddSeparator();
|
||||
toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, (float)currentX, -1, NULL, "Print");
|
||||
currentX += width + 5;
|
||||
AddSeparator();
|
||||
AddTool(7, *(toolBarBitmaps[7]), wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
|
||||
toolBar->AddSeparator();
|
||||
toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, FALSE, currentX, -1, NULL, "Help");
|
||||
|
||||
CreateTools();
|
||||
toolBar->CreateTools();
|
||||
|
||||
// Can delete the bitmaps since they're reference counted
|
||||
int i;
|
||||
for (i = 0; i < 8; i++)
|
||||
delete toolBarBitmaps[i];
|
||||
}
|
||||
|
||||
bool TestToolBar::OnLeftClick(int toolIndex, bool toggled)
|
||||
{
|
||||
char buf[200];
|
||||
sprintf(buf, "Clicked on tool %d", toolIndex);
|
||||
((wxFrame*) GetParent())->SetStatusText(buf);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
void TestToolBar::OnMouseEnter(int toolIndex)
|
||||
// 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_RANGE(wxID_OPEN, wxID_PASTE, 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)
|
||||
{
|
||||
char buf[200];
|
||||
if (toolIndex > -1)
|
||||
m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
|
||||
}
|
||||
|
||||
void MyFrame::OnQuit(wxCommandEvent& event)
|
||||
{
|
||||
Close(TRUE);
|
||||
}
|
||||
|
||||
void MyFrame::OnAbout(wxCommandEvent& event)
|
||||
{
|
||||
(void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar");
|
||||
}
|
||||
|
||||
// Define the behaviour for the frame closing
|
||||
// - must delete all frames except for the main one.
|
||||
void MyFrame::OnCloseWindow(wxCloseEvent& 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.GetId() > -1)
|
||||
{
|
||||
sprintf(buf, "This is tool number %d", toolIndex);
|
||||
((wxFrame*)GetParent())->SetStatusText(buf);
|
||||
wxString str;
|
||||
str.Printf("This is tool number %d", event.GetId());
|
||||
SetStatusText(str);
|
||||
}
|
||||
else ((wxFrame*)GetParent())->SetStatusText("");
|
||||
else
|
||||
SetStatusText("");
|
||||
}
|
||||
|
||||
void TestToolBar::OnPaint(wxPaintEvent& event)
|
||||
{
|
||||
wxToolBar::OnPaint(event);
|
||||
|
||||
wxPaintDC dc(this);
|
||||
|
||||
int w, h;
|
||||
GetSize(&w, &h);
|
||||
dc.SetPen(wxBLACK_PEN);
|
||||
dc.SetBrush(wxTRANSPARENT_BRUSH);
|
||||
dc.DrawLine(0, h-1, w, h-1);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -14,6 +14,7 @@ class MyApp: public wxApp
|
||||
{
|
||||
public:
|
||||
bool OnInit(void);
|
||||
bool InitToolbar(wxToolBar* toolBar);
|
||||
};
|
||||
|
||||
// Define a new frame
|
||||
@@ -25,9 +26,10 @@ public:
|
||||
long style = wxDEFAULT_FRAME_STYLE);
|
||||
|
||||
void OnCloseWindow(wxCloseEvent& event);
|
||||
void OnMenuHighlight(wxMenuEvent& event);
|
||||
void OnQuit(wxCommandEvent& event);
|
||||
void OnAbout(wxCommandEvent& event);
|
||||
void OnToolLeftClick(wxCommandEvent& event);
|
||||
void OnToolEnter(wxCommandEvent& event);
|
||||
|
||||
private:
|
||||
wxTextCtrl* m_textWindow;
|
||||
@@ -35,19 +37,3 @@ private:
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
class TestToolBar: public wxToolBar
|
||||
{
|
||||
public:
|
||||
TestToolBar(wxFrame *parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition,
|
||||
const wxSize& size = wxDefaultSize,
|
||||
long style = wxNO_BORDER, int direction = wxVERTICAL, int RowsOrColumns = 2);
|
||||
bool OnLeftClick(int toolIndex, bool toggled);
|
||||
void OnMouseEnter(int toolIndex);
|
||||
void OnPaint(wxPaintEvent& event);
|
||||
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
||||
#define TEST_QUIT 100
|
||||
#define TEST_ABOUT 101
|
||||
|
||||
|
Reference in New Issue
Block a user