X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/06393630145526eefff697d2fc61164530d7da46..0fd078b7fc55d5bfa7e3d8e82955dc3341de8a8f:/samples/toolbar/toolbar.cpp diff --git a/samples/toolbar/toolbar.cpp b/samples/toolbar/toolbar.cpp index c66b6cf428..8f8da745bc 100644 --- a/samples/toolbar/toolbar.cpp +++ b/samples/toolbar/toolbar.cpp @@ -37,7 +37,7 @@ // define this to use XPMs everywhere (by default, BMPs are used under Win) // BMPs use less space, but aren't compiled into the executable on other platforms -#ifdef __WXMSW__ +#ifdef __WINDOWS__ #define USE_XPM_BITMAPS 0 #else #define USE_XPM_BITMAPS 1 @@ -60,8 +60,8 @@ // resources // ---------------------------------------------------------------------------- -#if !defined(__WXMSW__) && !defined(__WXPM__) - #include "mondrian.xpm" +#ifndef wxHAS_IMAGES_IN_RESOURCES + #include "../sample.xpm" #endif #if USE_XPM_BITMAPS @@ -134,7 +134,6 @@ public: void OnToolbarStyle(wxCommandEvent& event); void OnToolbarBgCol(wxCommandEvent& event); - void OnToolbarCustomBg(wxCommandEvent& event); void OnToolbarCustomBitmap(wxCommandEvent& event); void OnToolLeftClick(wxCommandEvent& event); @@ -149,8 +148,6 @@ public: { event.Enable( m_tbar != NULL ); } private: - void OnEraseToolBarBackground(wxEraseEvent& event); - void DoEnablePrint(); void DoDeletePrint(); void DoToggleHelp(); @@ -209,7 +206,6 @@ enum IDM_TOOLBAR_SHOW_ICONS, IDM_TOOLBAR_SHOW_BOTH, IDM_TOOLBAR_BG_COL, - IDM_TOOLBAR_CUSTOM_BG, IDM_TOOLBAR_CUSTOM_PATH, IDM_TOOLBAR_TOP_ORIENTATION, IDM_TOOLBAR_LEFT_ORIENTATION, @@ -268,7 +264,6 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH, MyFrame::OnToolbarStyle) EVT_MENU(IDM_TOOLBAR_BG_COL, MyFrame::OnToolbarBgCol) - EVT_MENU(IDM_TOOLBAR_CUSTOM_BG, MyFrame::OnToolbarCustomBg) EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH, MyFrame::OnToolbarCustomBitmap) @@ -310,7 +305,7 @@ bool MyApp::OnInit() // Create the main frame window MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY, wxT("wxToolBar Sample"), - wxPoint(100, 100), wxSize(550, 300)); + wxPoint(100, 100), wxSize(650, 300)); frame->Show(true); @@ -320,8 +315,6 @@ bool MyApp::OnInit() wxInitAllImageHandlers(); - SetTopWindow(frame); - return true; } @@ -376,17 +369,6 @@ void MyFrame::RecreateToolbar() toolBar = CreateToolBar(style, ID_TOOLBAR); #endif - if ( GetMenuBar()->IsChecked(IDM_TOOLBAR_CUSTOM_BG) ) - { - toolBar->Connect - ( - wxEVT_ERASE_BACKGROUND, - wxEraseEventHandler(MyFrame::OnEraseToolBarBackground), - NULL, - this - ); - } - PopulateToolbar(toolBar); } @@ -474,9 +456,12 @@ void MyFrame::PopulateToolbar(wxToolBarBase* toolBar) #endif // USE_CONTROLS_IN_TOOLBAR toolBar->AddTool(wxID_SAVE, wxT("Save"), toolBarBitmaps[Tool_save], wxT("Toggle button 1"), wxITEM_CHECK); + + toolBar->AddSeparator(); toolBar->AddTool(wxID_COPY, wxT("Copy"), toolBarBitmaps[Tool_copy], wxT("Toggle button 2"), wxITEM_CHECK); toolBar->AddTool(wxID_CUT, wxT("Cut"), toolBarBitmaps[Tool_cut], wxT("Toggle/Untoggle help button")); toolBar->AddTool(wxID_PASTE, wxT("Paste"), toolBarBitmaps[Tool_paste], wxT("Paste")); + toolBar->AddSeparator(); if ( m_useCustomDisabled ) { @@ -500,14 +485,16 @@ void MyFrame::PopulateToolbar(wxToolBarBase* toolBar) wxT("Delete this tool. This is a very long tooltip to test whether it does the right thing when the tooltip is more than Windows can cope with.")); } - toolBar->AddSeparator(); + // add a stretchable space before the "Help" button to make it + // right-aligned + toolBar->AddStretchableSpace(); toolBar->AddTool(wxID_HELP, wxT("Help"), toolBarBitmaps[Tool_help], wxT("Help button"), wxITEM_CHECK); if ( !m_pathBmp.empty() ) { // create a tool with a custom bitmap for testing wxImage img(m_pathBmp); - if ( img.Ok() ) + if ( img.IsOk() ) { if ( img.GetWidth() > w && img.GetHeight() > h ) img = img.GetSubImage(wxRect(0, 0, w, h)); @@ -521,7 +508,8 @@ void MyFrame::PopulateToolbar(wxToolBarBase* toolBar) // the changes toolBar->Realize(); - toolBar->SetRows(!(toolBar->IsVertical()) ? m_rows : 10 / m_rows); + toolBar->SetRows(toolBar->IsVertical() ? toolBar->GetToolsCount() / m_rows + : m_rows); } // ---------------------------------------------------------------------------- @@ -554,7 +542,7 @@ MyFrame::MyFrame(wxFrame* parent, #endif // Give it an icon - SetIcon(wxICON(mondrian)); + SetIcon(wxICON(sample)); // Make a menubar wxMenu *tbarMenu = new wxMenu; @@ -607,7 +595,6 @@ MyFrame::MyFrame(wxFrame* parent, tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, wxT("Show &both\tCtrl-Alt-B")); tbarMenu->AppendSeparator(); tbarMenu->Append(IDM_TOOLBAR_BG_COL, wxT("Choose bac&kground colour...")); - tbarMenu->AppendCheckItem(IDM_TOOLBAR_CUSTOM_BG, wxT("Draw custom back&ground")); tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, wxT("Custom &bitmap...\tCtrl-B")); wxMenu *toolMenu = new wxMenu; @@ -639,6 +626,7 @@ MyFrame::MyFrame(wxFrame* parent, // Associate the menu bar with the frame SetMenuBar(menuBar); + menuBar->Check(IDM_TOOLBAR_TOGGLE_TOOLBAR, true); menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true); menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true); @@ -653,7 +641,7 @@ MyFrame::MyFrame(wxFrame* parent, m_extraToolBar = new wxToolBar(m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_TEXT|wxTB_FLAT|wxTB_TOP); PopulateToolbar(m_extraToolBar); #endif - + m_textWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); @@ -707,15 +695,6 @@ void MyFrame::OnSize(wxSizeEvent& event) } } -void MyFrame::OnEraseToolBarBackground(wxEraseEvent& event) -{ - wxDC& dc = *event.GetDC(); - const wxSize size = dc.GetSize(); - dc.SetPen(*wxRED_PEN); - dc.DrawLine(0, 0, size.x, size.y); - dc.DrawLine(0, size.y, size.x, 0); -} - void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event)) { wxToolBar *tbar = GetToolBar(); @@ -726,9 +705,11 @@ void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event)) } else { + // notice that there is no need to call SetToolBar(NULL) here (although + // this it is harmless to do and it must be called if you do not delete + // the toolbar but keep it for later reuse), just delete the toolbar + // directly and it will reset the associated frame toolbar pointer delete tbar; - - SetToolBar(NULL); } } @@ -743,8 +724,7 @@ void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event)) { if ( m_tbar ) { - delete m_tbar; - m_tbar = NULL; + wxDELETE(m_tbar); } else { @@ -783,7 +763,9 @@ void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event)) // m_rows may be only 1 or 2 m_rows = 3 - m_rows; - GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows : 10 / m_rows); + wxToolBar* const toolBar = GetToolBar(); + toolBar->SetRows(toolBar->IsVertical() ? toolBar->GetToolsCount() / m_rows + : m_rows); //RecreateToolbar(); -- this is unneeded } @@ -899,14 +881,12 @@ void MyFrame::DoToggleHelp() void MyFrame::OnToggleSearch(wxCommandEvent& WXUNUSED(event)) { - static const int searchPos = 3; - wxToolBarBase * const tb = GetToolBar(); if ( !m_searchTool ) { wxSearchCtrl * const srch = new wxSearchCtrl(tb, wxID_ANY, "needle"); srch->SetMinSize(wxSize(80, -1)); - m_searchTool = tb->InsertControl(searchPos, srch); + m_searchTool = tb->AddControl(srch); } else // tool already exists { @@ -919,7 +899,7 @@ void MyFrame::OnToggleSearch(wxCommandEvent& WXUNUSED(event)) } else // tool exists in detached state, attach it back { - tb->InsertTool(searchPos, m_searchTool); + tb->AddTool(m_searchTool); win->Show(); } } @@ -948,12 +928,12 @@ void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnToolbarStyle(wxCommandEvent& event) { long style = GetToolBar()->GetWindowStyle(); - style &= ~(wxTB_NOICONS | wxTB_TEXT); + style &= ~(wxTB_NOICONS | wxTB_HORZ_TEXT); switch ( event.GetId() ) { case IDM_TOOLBAR_SHOW_TEXT: - style |= wxTB_NOICONS | wxTB_TEXT; + style |= wxTB_NOICONS | (m_horzText ? wxTB_HORZ_TEXT : wxTB_TEXT); break; case IDM_TOOLBAR_SHOW_ICONS: @@ -961,7 +941,7 @@ void MyFrame::OnToolbarStyle(wxCommandEvent& event) break; case IDM_TOOLBAR_SHOW_BOTH: - style |= wxTB_TEXT; + style |= (m_horzText ? wxTB_HORZ_TEXT : wxTB_TEXT); } GetToolBar()->SetWindowStyle(style); @@ -982,34 +962,6 @@ void MyFrame::OnToolbarBgCol(wxCommandEvent& WXUNUSED(event)) } } -void MyFrame::OnToolbarCustomBg(wxCommandEvent& event) -{ - wxToolBarBase *tb = GetToolBar(); - - if ( event.IsChecked() ) - { - tb->Connect - ( - wxEVT_ERASE_BACKGROUND, - wxEraseEventHandler(MyFrame::OnEraseToolBarBackground), - NULL, - this - ); - } - else - { - tb->Disconnect - ( - wxEVT_ERASE_BACKGROUND, - wxEraseEventHandler(MyFrame::OnEraseToolBarBackground), - NULL, - this - ); - } - - tb->Refresh(); -} - void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event)) { m_pathBmp = wxLoadFileSelector("custom bitmap", "");