X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/14d1ccd89decb9d84f394fb2218eddbfdba56baf..217e13fa85b151b701e19f0e345f977231ec0b16:/samples/toolbar/test.cpp diff --git a/samples/toolbar/test.cpp b/samples/toolbar/test.cpp index 80aaeff9c1..0274aefac2 100644 --- a/samples/toolbar/test.cpp +++ b/samples/toolbar/test.cpp @@ -23,18 +23,29 @@ #include "wx/toolbar.h" #include "test.h" +#ifdef __WXGTK__ +#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/paste.xpm" +#include "bitmaps/print.xpm" +#include "bitmaps/preview.xpm" +#include "bitmaps/help.xpm" +#endif + IMPLEMENT_APP(MyApp) -#ifdef __X__ -// TODO: include XBM or XPM icons for X apps -#endif // 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(NULL, -1, "wxToolBar Sample", wxPoint(100, 100), wxSize(450, 300)); + MyFrame* frame = new MyFrame((wxFrame *) NULL, -1, (const wxString) "wxToolBar Sample", + wxPoint(100, 100), wxSize(450, 300)); // Give it a status line frame->CreateStatusBar(); @@ -42,17 +53,16 @@ bool MyApp::OnInit(void) // Give it an icon #ifdef __WXMSW__ frame->SetIcon(wxIcon("mondrian")); -#endif -#ifdef __X__ - frame->SetIcon(wxIcon("mondrian.xbm")); +#else + frame->SetIcon( wxIcon(mondrian_xpm) ); #endif // 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,15 +73,14 @@ bool MyApp::OnInit(void) frame->SetMenuBar(menuBar); // Create the toolbar - TestToolBar* toolRibbon = new TestToolBar(frame, -1, wxPoint(0, 0), wxSize(100, 30), - wxNO_BORDER|wxTB_FLAT, wxVERTICAL, 1); - toolRibbon->SetMargins(5, 5); + frame->CreateToolBar(wxNO_BORDER|wxHORIZONTAL|wxTB_FLAT, ID_TOOLBAR); - // Tell the frame about it - frame->SetToolBar(toolRibbon); + InitToolbar(frame->GetToolBar()); - // Force a resize, just in case. - frame->OnSize(wxSizeEvent(wxSize(-1, -1), frame->GetId())); + // 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, wxWindows"); @@ -81,63 +90,10 @@ 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) +bool MyApp::InitToolbar(wxToolBar* toolBar) { - 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() + toolBar->SetMargins(5, 5); -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) -{ // Set up toolbar wxBitmap* toolBarBitmaps[8]; @@ -150,18 +106,16 @@ TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, con toolBarBitmaps[5] = new wxBitmap("icon6"); toolBarBitmaps[6] = new wxBitmap("icon7"); toolBarBitmaps[7] = new wxBitmap("icon8"); -#endif -#ifdef __X__ - // TODO - toolBarBitmaps[0] = new wxBitmap(...); - toolBarBitmaps[1] = new wxBitmap(...); - toolBarBitmaps[2] = new wxBitmap(...); - toolBarBitmaps[3] = new wxBitmap(...); - toolBarBitmaps[4] = new wxBitmap(...); - toolBarBitmaps[5] = new wxBitmap(...); - toolBarBitmaps[6] = new wxBitmap(...); - toolBarBitmaps[7] = new wxBitmap(...); - +#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( paste_xpm ); + toolBarBitmaps[5] = new wxBitmap( preview_xpm ); + toolBarBitmaps[6] = new wxBitmap( print_xpm ); + toolBarBitmaps[7] = new wxBitmap( help_xpm ); #endif #ifdef __WXMSW__ @@ -169,66 +123,88 @@ TestToolBar::TestToolBar(wxFrame* parent, wxWindowID id, const wxPoint& pos, con #else int width = 16; #endif - 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) 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, (wxObject *) NULL, "Help"); - CreateTools(); + toolBar->Realize(); // Can delete the bitmaps since they're reference counted int i; for (i = 0; i < 8; i++) delete toolBarBitmaps[i]; + + return TRUE; } -bool TestToolBar::OnLeftClick(int toolIndex, bool toggled) +// 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) { - char buf[200]; - sprintf(buf, "Clicked on tool %d", toolIndex); - ((wxFrame*) GetParent())->SetStatusText(buf); - return TRUE; + m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE); } -void TestToolBar::OnMouseEnter(int toolIndex) +void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) { - char buf[200]; - if (toolIndex > -1) - { - sprintf(buf, "This is tool number %d", toolIndex); - ((wxFrame*)GetParent())->SetStatusText(buf); - } - else ((wxFrame*)GetParent())->SetStatusText(""); + Close(TRUE); } -void TestToolBar::OnPaint(wxPaintEvent& event) +void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { - wxToolBar::OnPaint(event); + (void)wxMessageBox("wxWindows wxToolBar demo\n", "About wxToolBar"); +} - 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); +// 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(""); +}