1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
31 #include "wx/toolbar.h"
34 #include "wx/filedlg.h"
35 #include "wx/colordlg.h"
36 #include "wx/srchctrl.h"
38 // define this to use XPMs everywhere (by default, BMPs are used under Win)
39 // BMPs use less space, but aren't compiled into the executable on other platforms
41 #define USE_XPM_BITMAPS 0
43 #define USE_XPM_BITMAPS 1
46 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
47 #error You need to enable XPM support to use XPM bitmaps with toolbar!
48 #endif // USE_XPM_BITMAPS
50 // If this is 1, the sample will test an extra toolbar identical to the
51 // main one, but not managed by the frame. This can test subtle differences
52 // in the way toolbars are handled, especially on Mac where there is one
53 // native, 'installed' toolbar.
54 #define USE_UNMANAGED_TOOLBAR 0
56 // Define this as 0 for the platforms not supporting controls in toolbars
57 #define USE_CONTROLS_IN_TOOLBAR 1
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #if !defined(__WXMSW__) && !defined(__WXPM__)
64 #include "mondrian.xpm"
68 #include "bitmaps/new.xpm"
69 #include "bitmaps/open.xpm"
70 #include "bitmaps/save.xpm"
71 #include "bitmaps/copy.xpm"
72 #include "bitmaps/cut.xpm"
73 #include "bitmaps/preview.xpm" // paste XPM
74 #include "bitmaps/print.xpm"
75 #include "bitmaps/help.xpm"
76 #endif // USE_XPM_BITMAPS
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // Define a new application
91 class MyApp
: public wxApp
98 class MyFrame
: public wxFrame
101 MyFrame(wxFrame
*parent
,
102 wxWindowID id
= wxID_ANY
,
103 const wxString
& title
= wxT("wxToolBar Sample"),
104 const wxPoint
& pos
= wxDefaultPosition
,
105 const wxSize
& size
= wxDefaultSize
,
106 long style
= wxDEFAULT_FRAME_STYLE
|wxCLIP_CHILDREN
|wxNO_FULL_REPAINT_ON_RESIZE
);
109 void PopulateToolbar(wxToolBarBase
* toolBar
);
110 void RecreateToolbar();
112 void OnQuit(wxCommandEvent
& event
);
113 void OnAbout(wxCommandEvent
& event
);
115 void OnSize(wxSizeEvent
& event
);
117 void OnToggleToolbar(wxCommandEvent
& event
);
118 void OnToggleAnotherToolbar(wxCommandEvent
& event
);
119 void OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
));
121 void OnToggleToolbarSize(wxCommandEvent
& event
);
122 void OnChangeOrientation(wxCommandEvent
& event
);
123 void OnToggleToolbarRows(wxCommandEvent
& event
);
124 void OnToggleTooltips(wxCommandEvent
& event
);
125 void OnToggleCustomDisabled(wxCommandEvent
& event
);
127 void OnEnablePrint(wxCommandEvent
& WXUNUSED(event
)) { DoEnablePrint(); }
128 void OnDeletePrint(wxCommandEvent
& WXUNUSED(event
)) { DoDeletePrint(); }
129 void OnInsertPrint(wxCommandEvent
& event
);
130 void OnChangeToolTip(wxCommandEvent
& event
);
131 void OnToggleHelp(wxCommandEvent
& WXUNUSED(event
)) { DoToggleHelp(); }
132 void OnToggleSearch(wxCommandEvent
& event
);
133 void OnToggleRadioBtn(wxCommandEvent
& event
);
135 void OnToolbarStyle(wxCommandEvent
& event
);
136 void OnToolbarBgCol(wxCommandEvent
& event
);
137 void OnToolbarCustomBg(wxCommandEvent
& event
);
138 void OnToolbarCustomBitmap(wxCommandEvent
& event
);
140 void OnToolLeftClick(wxCommandEvent
& event
);
141 void OnToolRightClick(wxCommandEvent
& event
);
142 void OnToolDropdown(wxCommandEvent
& event
);
144 void OnCombo(wxCommandEvent
& event
);
146 void OnUpdateCopyAndCut(wxUpdateUIEvent
& event
);
147 void OnUpdateToggleHorzText(wxUpdateUIEvent
& event
);
148 void OnUpdateToggleRadioBtn(wxUpdateUIEvent
& event
)
149 { event
.Enable( m_tbar
!= NULL
); }
152 void OnEraseToolBarBackground(wxEraseEvent
& event
);
154 void DoEnablePrint();
155 void DoDeletePrint();
158 void LayoutChildren();
164 size_t m_rows
; // 1 or 2 only
166 // the number of print buttons we have (they're added/removed dynamically)
169 // store toolbar position for future use
170 Positions m_toolbarPosition
;
172 wxTextCtrl
*m_textWindow
;
175 #if USE_UNMANAGED_TOOLBAR
176 wxToolBar
*m_extraToolBar
;
181 // the path to the custom bitmap for the test toolbar tool
184 // the search tool, initially NULL
185 wxToolBarToolBase
*m_searchTool
;
187 DECLARE_EVENT_TABLE()
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 const int ID_TOOLBAR
= 500;
196 static const long TOOLBAR_STYLE
= wxTB_FLAT
| wxTB_DOCKABLE
| wxTB_TEXT
;
200 // toolbar menu items
201 IDM_TOOLBAR_TOGGLE_TOOLBAR
= 200,
202 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
203 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
204 IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
205 IDM_TOOLBAR_TOGGLETOOLBARROWS
,
206 IDM_TOOLBAR_TOGGLETOOLTIPS
,
207 IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
208 IDM_TOOLBAR_SHOW_TEXT
,
209 IDM_TOOLBAR_SHOW_ICONS
,
210 IDM_TOOLBAR_SHOW_BOTH
,
212 IDM_TOOLBAR_CUSTOM_BG
,
213 IDM_TOOLBAR_CUSTOM_PATH
,
214 IDM_TOOLBAR_TOP_ORIENTATION
,
215 IDM_TOOLBAR_LEFT_ORIENTATION
,
216 IDM_TOOLBAR_BOTTOM_ORIENTATION
,
217 IDM_TOOLBAR_RIGHT_ORIENTATION
,
223 IDM_TOOLBAR_ENABLEPRINT
,
224 IDM_TOOLBAR_DELETEPRINT
,
225 IDM_TOOLBAR_INSERTPRINT
,
226 IDM_TOOLBAR_TOGGLEHELP
,
227 IDM_TOOLBAR_TOGGLESEARCH
,
228 IDM_TOOLBAR_TOGGLERADIOBTN1
,
229 IDM_TOOLBAR_TOGGLERADIOBTN2
,
230 IDM_TOOLBAR_TOGGLERADIOBTN3
,
231 IDM_TOOLBAR_CHANGE_TOOLTIP
,
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
243 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
244 EVT_SIZE(MyFrame::OnSize
)
246 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
247 EVT_MENU(wxID_HELP
, MyFrame::OnAbout
)
249 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR
, MyFrame::OnToggleToolbar
)
250 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
, MyFrame::OnToggleAnotherToolbar
)
251 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
, MyFrame::OnToggleHorizontalText
)
253 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION
, IDM_TOOLBAR_RIGHT_ORIENTATION
, MyFrame::OnChangeOrientation
)
254 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE
, MyFrame::OnToggleToolbarSize
)
255 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS
, MyFrame::OnToggleToolbarRows
)
256 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS
, MyFrame::OnToggleTooltips
)
257 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
, MyFrame::OnToggleCustomDisabled
)
259 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT
, MyFrame::OnEnablePrint
)
260 EVT_MENU(IDM_TOOLBAR_DELETEPRINT
, MyFrame::OnDeletePrint
)
261 EVT_MENU(IDM_TOOLBAR_INSERTPRINT
, MyFrame::OnInsertPrint
)
262 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP
, MyFrame::OnToggleHelp
)
263 EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH
, MyFrame::OnToggleSearch
)
264 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
, IDM_TOOLBAR_TOGGLERADIOBTN3
,
265 MyFrame::OnToggleRadioBtn
)
266 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP
, MyFrame::OnChangeToolTip
)
268 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT
, IDM_TOOLBAR_SHOW_BOTH
,
269 MyFrame::OnToolbarStyle
)
270 EVT_MENU(IDM_TOOLBAR_BG_COL
, MyFrame::OnToolbarBgCol
)
271 EVT_MENU(IDM_TOOLBAR_CUSTOM_BG
, MyFrame::OnToolbarCustomBg
)
273 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH
, MyFrame::OnToolbarCustomBitmap
)
275 EVT_MENU(wxID_ANY
, MyFrame::OnToolLeftClick
)
277 EVT_COMBOBOX(ID_COMBO
, MyFrame::OnCombo
)
279 EVT_TOOL_RCLICKED(wxID_ANY
, MyFrame::OnToolRightClick
)
281 EVT_TOOL_DROPDOWN(wxID_ANY
, MyFrame::OnToolDropdown
)
283 EVT_UPDATE_UI(wxID_COPY
, MyFrame::OnUpdateCopyAndCut
)
284 EVT_UPDATE_UI(wxID_CUT
, MyFrame::OnUpdateCopyAndCut
)
286 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1
,
287 IDM_TOOLBAR_TOGGLERADIOBTN3
,
288 MyFrame::OnUpdateToggleRadioBtn
)
289 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
290 MyFrame::OnUpdateToggleHorzText
)
293 // ============================================================================
295 // ============================================================================
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
303 // The `main program' equivalent, creating the windows and returning the
307 if ( !wxApp::OnInit() )
310 // Create the main frame window
311 MyFrame
* frame
= new MyFrame((wxFrame
*) NULL
, wxID_ANY
,
312 wxT("wxToolBar Sample"),
313 wxPoint(100, 100), wxSize(650, 300));
318 frame
->SetStatusText(wxT("Hello, wxWidgets"));
321 wxInitAllImageHandlers();
328 void MyFrame::RecreateToolbar()
331 // On Windows CE, we should not delete the
332 // previous toolbar in case it contains the menubar.
333 // We'll try to accommodate this usage in due course.
334 wxToolBar
* toolBar
= CreateToolBar();
336 // delete and recreate the toolbar
337 wxToolBarBase
*toolBar
= GetToolBar();
338 long style
= toolBar
? toolBar
->GetWindowStyle() : TOOLBAR_STYLE
;
340 if (toolBar
&& m_searchTool
&& m_searchTool
->GetToolBar() == NULL
)
343 toolBar
->AddTool(m_searchTool
);
351 style
&= ~(wxTB_HORIZONTAL
| wxTB_VERTICAL
| wxTB_BOTTOM
| wxTB_RIGHT
| wxTB_HORZ_LAYOUT
);
352 switch( m_toolbarPosition
)
364 style
|= wxTB_BOTTOM
;
368 if ( m_showTooltips
)
369 style
&= ~wxTB_NO_TOOLTIPS
;
371 style
|= wxTB_NO_TOOLTIPS
;
373 if ( style
& wxTB_TEXT
&& !(style
& wxTB_NOICONS
) && m_horzText
)
374 style
|= wxTB_HORZ_LAYOUT
;
376 toolBar
= CreateToolBar(style
, ID_TOOLBAR
);
379 if ( GetMenuBar()->IsChecked(IDM_TOOLBAR_CUSTOM_BG
) )
383 wxEVT_ERASE_BACKGROUND
,
384 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
390 PopulateToolbar(toolBar
);
393 void MyFrame::PopulateToolbar(wxToolBarBase
* toolBar
)
409 wxBitmap toolBarBitmaps
[Tool_Max
];
412 #define INIT_TOOL_BMP(bmp) \
413 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
414 #else // !USE_XPM_BITMAPS
415 #define INIT_TOOL_BMP(bmp) \
416 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
417 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
424 INIT_TOOL_BMP(paste
);
425 INIT_TOOL_BMP(print
);
428 int w
= toolBarBitmaps
[Tool_new
].GetWidth(),
429 h
= toolBarBitmaps
[Tool_new
].GetHeight();
431 if ( !m_smallToolbar
)
436 for ( size_t n
= Tool_new
; n
< WXSIZEOF(toolBarBitmaps
); n
++ )
439 wxBitmap(toolBarBitmaps
[n
].ConvertToImage().Scale(w
, h
));
443 // this call is actually unnecessary as the toolbar will adjust its tools
444 // size to fit the biggest icon used anyhow but it doesn't hurt neither
445 toolBar
->SetToolBitmapSize(wxSize(w
, h
));
447 toolBar
->AddTool(wxID_NEW
, wxT("New"),
448 toolBarBitmaps
[Tool_new
], wxNullBitmap
, wxITEM_DROPDOWN
,
449 wxT("New file"), wxT("This is help for new file tool"));
451 wxMenu
* menu
= new wxMenu
;
452 menu
->Append(wxID_ANY
, wxT("&First dummy item"));
453 menu
->Append(wxID_ANY
, wxT("&Second dummy item"));
454 menu
->AppendSeparator();
455 menu
->Append(wxID_EXIT
, wxT("Exit"));
456 toolBar
->SetDropdownMenu(wxID_NEW
, menu
);
458 toolBar
->AddTool(wxID_OPEN
, wxT("Open"),
459 toolBarBitmaps
[Tool_open
], wxNullBitmap
, wxITEM_NORMAL
,
460 wxT("Open file"), wxT("This is help for open file tool"));
462 #if USE_CONTROLS_IN_TOOLBAR
463 // adding a combo to a vertical toolbar is not very smart
464 if ( !toolBar
->IsVertical() )
466 wxComboBox
*combo
= new wxComboBox(toolBar
, ID_COMBO
, wxEmptyString
, wxDefaultPosition
, wxSize(100,-1) );
467 combo
->Append(wxT("This"));
468 combo
->Append(wxT("is a"));
469 combo
->Append(wxT("combobox"));
470 combo
->Append(wxT("in a"));
471 combo
->Append(wxT("toolbar"));
472 toolBar
->AddControl(combo
, wxT("Combo Label"));
474 #endif // USE_CONTROLS_IN_TOOLBAR
476 toolBar
->AddTool(wxID_SAVE
, wxT("Save"), toolBarBitmaps
[Tool_save
], wxT("Toggle button 1"), wxITEM_CHECK
);
478 toolBar
->AddSeparator();
479 toolBar
->AddTool(wxID_COPY
, wxT("Copy"), toolBarBitmaps
[Tool_copy
], wxT("Toggle button 2"), wxITEM_CHECK
);
480 toolBar
->AddTool(wxID_CUT
, wxT("Cut"), toolBarBitmaps
[Tool_cut
], wxT("Toggle/Untoggle help button"));
481 toolBar
->AddTool(wxID_PASTE
, wxT("Paste"), toolBarBitmaps
[Tool_paste
], wxT("Paste"));
482 toolBar
->AddSeparator();
484 if ( m_useCustomDisabled
)
486 wxBitmap
bmpDisabled(w
, h
);
489 dc
.SelectObject(bmpDisabled
);
490 dc
.DrawBitmap(toolBarBitmaps
[Tool_print
], 0, 0);
492 wxPen
pen(*wxRED
, 5);
494 dc
.DrawLine(0, 0, w
, h
);
497 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
502 toolBar
->AddTool(wxID_PRINT
, wxT("Print"), toolBarBitmaps
[Tool_print
],
503 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."));
506 // add a stretchable space before the "Help" button to make it
508 toolBar
->AddStretchableSpace();
509 toolBar
->AddTool(wxID_HELP
, wxT("Help"), toolBarBitmaps
[Tool_help
], wxT("Help button"), wxITEM_CHECK
);
511 if ( !m_pathBmp
.empty() )
513 // create a tool with a custom bitmap for testing
514 wxImage
img(m_pathBmp
);
517 if ( img
.GetWidth() > w
&& img
.GetHeight() > h
)
518 img
= img
.GetSubImage(wxRect(0, 0, w
, h
));
520 toolBar
->AddSeparator();
521 toolBar
->AddTool(wxID_ANY
, wxT("Custom"), img
);
525 // after adding the buttons to the toolbar, must call Realize() to reflect
529 toolBar
->SetRows(!(toolBar
->IsVertical()) ? m_rows
: 10 / m_rows
);
532 // ----------------------------------------------------------------------------
534 // ----------------------------------------------------------------------------
536 // Define my frame constructor
537 MyFrame::MyFrame(wxFrame
* parent
,
539 const wxString
& title
,
543 : wxFrame(parent
, id
, title
, pos
, size
, style
)
547 m_smallToolbar
= true;
549 m_useCustomDisabled
= false;
550 m_showTooltips
= true;
557 // Give it a status line
562 SetIcon(wxICON(mondrian
));
565 wxMenu
*tbarMenu
= new wxMenu
;
566 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR
,
567 wxT("Toggle &toolbar\tCtrl-Z"),
568 wxT("Show or hide the toolbar"));
570 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR
,
571 wxT("Toggle &another toolbar\tCtrl-A"),
572 wxT("Show/hide another test toolbar"));
574 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT
,
575 wxT("Toggle hori&zontal text\tCtrl-H"),
576 wxT("Show text under/alongside the icon"));
578 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE
,
579 wxT("&Toggle toolbar size\tCtrl-S"),
580 wxT("Toggle between big/small toolbar"));
582 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS
,
583 wxT("Toggle number of &rows\tCtrl-R"),
584 wxT("Toggle number of toolbar rows between 1 and 2"));
586 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS
,
587 wxT("Show &tooltips\tCtrl-L"),
588 wxT("Show tooltips for the toolbar tools"));
590 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED
,
591 wxT("Use c&ustom disabled images\tCtrl-U"),
592 wxT("Switch between using system-generated and custom disabled images"));
595 tbarMenu
->AppendSeparator();
596 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION
,
597 wxT("Set toolbar at the top of the window"),
598 wxT("Set toolbar at the top of the window"));
599 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION
,
600 wxT("Set toolbar at the left of the window"),
601 wxT("Set toolbar at the left of the window"));
602 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION
,
603 wxT("Set toolbar at the bottom of the window"),
604 wxT("Set toolbar at the bottom of the window"));
605 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION
,
606 wxT("Set toolbar at the right edge of the window"),
607 wxT("Set toolbar at the right edge of the window"));
608 tbarMenu
->AppendSeparator();
610 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT
, wxT("Show &text\tCtrl-Alt-T"));
611 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS
, wxT("Show &icons\tCtrl-Alt-I"));
612 tbarMenu
->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH
, wxT("Show &both\tCtrl-Alt-B"));
613 tbarMenu
->AppendSeparator();
614 tbarMenu
->Append(IDM_TOOLBAR_BG_COL
, wxT("Choose bac&kground colour..."));
615 tbarMenu
->AppendCheckItem(IDM_TOOLBAR_CUSTOM_BG
, wxT("Draw custom back&ground"));
616 tbarMenu
->Append(IDM_TOOLBAR_CUSTOM_PATH
, wxT("Custom &bitmap...\tCtrl-B"));
618 wxMenu
*toolMenu
= new wxMenu
;
619 toolMenu
->Append(IDM_TOOLBAR_ENABLEPRINT
, wxT("&Enable print button\tCtrl-E"));
620 toolMenu
->Append(IDM_TOOLBAR_DELETEPRINT
, wxT("&Delete print button\tCtrl-D"));
621 toolMenu
->Append(IDM_TOOLBAR_INSERTPRINT
, wxT("&Insert print button\tCtrl-I"));
622 toolMenu
->Append(IDM_TOOLBAR_TOGGLEHELP
, wxT("Toggle &help button\tCtrl-T"));
623 toolMenu
->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH
, wxT("Toggle &search field\tCtrl-F"));
624 toolMenu
->AppendSeparator();
625 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN1
, wxT("Toggle &1st radio button\tCtrl-1"));
626 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN2
, wxT("Toggle &2nd radio button\tCtrl-2"));
627 toolMenu
->Append(IDM_TOOLBAR_TOGGLERADIOBTN3
, wxT("Toggle &3rd radio button\tCtrl-3"));
628 toolMenu
->AppendSeparator();
629 toolMenu
->Append(IDM_TOOLBAR_CHANGE_TOOLTIP
, wxT("Change tooltip of \"New\""));
631 wxMenu
*fileMenu
= new wxMenu
;
632 fileMenu
->Append(wxID_EXIT
, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
634 wxMenu
*helpMenu
= new wxMenu
;
635 helpMenu
->Append(wxID_HELP
, wxT("&About"), wxT("About toolbar sample"));
637 wxMenuBar
* menuBar
= new wxMenuBar( wxMB_DOCKABLE
);
639 menuBar
->Append(fileMenu
, wxT("&File"));
640 menuBar
->Append(tbarMenu
, wxT("&Toolbar"));
641 menuBar
->Append(toolMenu
, wxT("Tool&s"));
642 menuBar
->Append(helpMenu
, wxT("&Help"));
644 // Associate the menu bar with the frame
647 menuBar
->Check(IDM_TOOLBAR_SHOW_BOTH
, true);
648 menuBar
->Check(IDM_TOOLBAR_TOGGLETOOLTIPS
, true);
650 menuBar
->Check(IDM_TOOLBAR_TOP_ORIENTATION
, true );
651 m_toolbarPosition
= TOOLBAR_TOP
;
653 // Create the toolbar
656 m_panel
= new wxPanel(this, wxID_ANY
);
657 #if USE_UNMANAGED_TOOLBAR
658 m_extraToolBar
= new wxToolBar(m_panel
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTB_TEXT
|wxTB_FLAT
|wxTB_TOP
);
659 PopulateToolbar(m_extraToolBar
);
662 m_textWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
664 wxBoxSizer
* sizer
= new wxBoxSizer(wxVERTICAL
);
665 m_panel
->SetSizer(sizer
);
666 #if USE_UNMANAGED_TOOLBAR
668 sizer
->Add(m_extraToolBar
, 0, wxEXPAND
, 0);
670 sizer
->Add(m_textWindow
, 1, wxEXPAND
, 0);
675 if ( m_searchTool
&& !m_searchTool
->GetToolBar() )
677 // we currently can't delete a toolbar tool ourselves, so we have to
678 // attach it to the toolbar just for it to be deleted, this is pretty
679 // ugly and will need to be changed
680 GetToolBar()->AddTool(m_searchTool
);
684 void MyFrame::LayoutChildren()
686 wxSize size
= GetClientSize();
691 m_tbar
->SetSize(0, 0, wxDefaultCoord
, size
.y
);
693 offset
= m_tbar
->GetSize().x
;
700 m_panel
->SetSize(offset
, 0, size
.x
- offset
, size
.y
);
703 void MyFrame::OnSize(wxSizeEvent
& event
)
715 void MyFrame::OnEraseToolBarBackground(wxEraseEvent
& event
)
717 wxDC
& dc
= *event
.GetDC();
718 const wxSize size
= dc
.GetSize();
719 dc
.SetPen(*wxRED_PEN
);
720 dc
.DrawLine(0, 0, size
.x
, size
.y
);
721 dc
.DrawLine(0, size
.y
, size
.x
, 0);
724 void MyFrame::OnToggleToolbar(wxCommandEvent
& WXUNUSED(event
))
726 wxToolBar
*tbar
= GetToolBar();
740 void MyFrame::OnToggleHorizontalText(wxCommandEvent
& WXUNUSED(event
))
742 m_horzText
= !m_horzText
;
747 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent
& WXUNUSED(event
))
756 long style
= GetToolBar() ? GetToolBar()->GetWindowStyle()
758 style
&= ~wxTB_HORIZONTAL
;
759 style
|= wxTB_VERTICAL
;
761 m_tbar
= new wxToolBar(this, wxID_ANY
,
762 wxDefaultPosition
, wxDefaultSize
,
765 m_tbar
->SetMargins(4, 4);
767 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_1
, wxT("First"), wxBITMAP(new));
768 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_2
, wxT("Second"), wxBITMAP(open
));
769 m_tbar
->AddRadioTool(IDM_TOOLBAR_OTHER_3
, wxT("Third"), wxBITMAP(save
));
770 m_tbar
->AddSeparator();
771 m_tbar
->AddTool(wxID_HELP
, wxT("Help"), wxBITMAP(help
));
779 void MyFrame::OnToggleToolbarSize(wxCommandEvent
& WXUNUSED(event
))
781 m_smallToolbar
= !m_smallToolbar
;
786 void MyFrame::OnToggleToolbarRows(wxCommandEvent
& WXUNUSED(event
))
788 // m_rows may be only 1 or 2
791 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows
: 10 / m_rows
);
793 //RecreateToolbar(); -- this is unneeded
796 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
798 m_showTooltips
= !m_showTooltips
;
803 void MyFrame::OnToggleCustomDisabled(wxCommandEvent
& WXUNUSED(event
))
805 m_useCustomDisabled
= !m_useCustomDisabled
;
810 void MyFrame::OnChangeOrientation(wxCommandEvent
& event
)
812 switch( event
.GetId() )
814 case IDM_TOOLBAR_LEFT_ORIENTATION
:
815 m_toolbarPosition
= TOOLBAR_LEFT
;
817 case IDM_TOOLBAR_TOP_ORIENTATION
:
818 m_toolbarPosition
= TOOLBAR_TOP
;
820 case IDM_TOOLBAR_RIGHT_ORIENTATION
:
821 m_toolbarPosition
= TOOLBAR_RIGHT
;
823 case IDM_TOOLBAR_BOTTOM_ORIENTATION
:
824 m_toolbarPosition
= TOOLBAR_BOTTOM
;
830 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
835 void MyFrame::OnAbout(wxCommandEvent
& event
)
837 if ( event
.IsChecked() )
838 m_textWindow
->WriteText( wxT("Help button down now.\n") );
840 m_textWindow
->WriteText( wxT("Help button up now.\n") );
842 (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
845 void MyFrame::OnToolLeftClick(wxCommandEvent
& event
)
848 str
.Printf( wxT("Clicked on tool %d\n"), event
.GetId());
849 m_textWindow
->WriteText( str
);
851 if (event
.GetId() == wxID_COPY
)
856 if (event
.GetId() == wxID_CUT
)
861 if (event
.GetId() == wxID_PRINT
)
867 void MyFrame::OnToolRightClick(wxCommandEvent
& event
)
869 m_textWindow
->AppendText(
870 wxString::Format(wxT("Tool %d right clicked.\n"),
871 (int) event
.GetInt()));
874 void MyFrame::OnCombo(wxCommandEvent
& event
)
876 wxLogStatus(wxT("Combobox string '%s' selected"), event
.GetString().c_str());
879 void MyFrame::DoEnablePrint()
884 wxToolBarBase
*tb
= GetToolBar();
885 tb
->EnableTool(wxID_PRINT
, !tb
->GetToolEnabled(wxID_PRINT
));
888 void MyFrame::DoDeletePrint()
893 wxToolBarBase
*tb
= GetToolBar();
894 tb
->DeleteTool( wxID_PRINT
);
899 void MyFrame::DoToggleHelp()
901 wxToolBarBase
*tb
= GetToolBar();
902 tb
->ToggleTool( wxID_HELP
, !tb
->GetToolState( wxID_HELP
) );
905 void MyFrame::OnToggleSearch(wxCommandEvent
& WXUNUSED(event
))
907 wxToolBarBase
* const tb
= GetToolBar();
910 wxSearchCtrl
* const srch
= new wxSearchCtrl(tb
, wxID_ANY
, "needle");
911 srch
->SetMinSize(wxSize(80, -1));
912 m_searchTool
= tb
->AddControl(srch
);
914 else // tool already exists
916 wxControl
* const win
= m_searchTool
->GetControl();
917 if ( m_searchTool
->GetToolBar() )
919 // attached now, remove it
921 tb
->RemoveTool(m_searchTool
->GetId());
923 else // tool exists in detached state, attach it back
925 tb
->AddTool(m_searchTool
);
933 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent
& event
)
935 event
.Enable( m_textWindow
->CanCopy() );
938 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent
& event
)
940 wxToolBar
*tbar
= GetToolBar();
941 event
.Enable( tbar
&&
942 tbar
->HasFlag(wxTB_TEXT
) &&
943 !tbar
->HasFlag(wxTB_NOICONS
) );
946 void MyFrame::OnChangeToolTip(wxCommandEvent
& WXUNUSED(event
))
948 GetToolBar()->SetToolShortHelp(wxID_NEW
, wxT("New toolbar button"));
951 void MyFrame::OnToolbarStyle(wxCommandEvent
& event
)
953 long style
= GetToolBar()->GetWindowStyle();
954 style
&= ~(wxTB_NOICONS
| wxTB_TEXT
);
956 switch ( event
.GetId() )
958 case IDM_TOOLBAR_SHOW_TEXT
:
959 style
|= wxTB_NOICONS
| wxTB_TEXT
;
962 case IDM_TOOLBAR_SHOW_ICONS
:
966 case IDM_TOOLBAR_SHOW_BOTH
:
970 GetToolBar()->SetWindowStyle(style
);
973 void MyFrame::OnToolbarBgCol(wxCommandEvent
& WXUNUSED(event
))
975 wxColour col
= wxGetColourFromUser
978 GetToolBar()->GetBackgroundColour(),
979 "Toolbar background colour"
983 GetToolBar()->SetBackgroundColour(col
);
984 GetToolBar()->Refresh();
988 void MyFrame::OnToolbarCustomBg(wxCommandEvent
& event
)
990 wxToolBarBase
*tb
= GetToolBar();
992 if ( event
.IsChecked() )
996 wxEVT_ERASE_BACKGROUND
,
997 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
1006 wxEVT_ERASE_BACKGROUND
,
1007 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground
),
1016 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent
& WXUNUSED(event
))
1018 m_pathBmp
= wxLoadFileSelector("custom bitmap", "");
1023 void MyFrame::OnInsertPrint(wxCommandEvent
& WXUNUSED(event
))
1027 wxToolBarBase
*tb
= GetToolBar();
1028 tb
->InsertTool(0, wxID_PRINT
, wxT("New print"),
1029 wxBITMAP(print
), wxNullBitmap
,
1031 wxT("Delete this tool"),
1032 wxT("This button was inserted into the toolbar"));
1034 // must call Realize() after adding a new button
1038 void MyFrame::OnToggleRadioBtn(wxCommandEvent
& event
)
1042 m_tbar
->ToggleTool(IDM_TOOLBAR_OTHER_1
+
1043 event
.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1
, true);
1047 void MyFrame::OnToolDropdown(wxCommandEvent
& event
)
1050 str
.Printf( wxT("Dropdown on tool %d\n"), event
.GetId());
1051 m_textWindow
->WriteText( str
);