]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/toolbar.cpp
use a slightly less ugly way to conditionally suppress unused parameter warnings
[wxWidgets.git] / samples / toolbar / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
3 // Purpose: wxToolBar sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #include "wx/toolbar.h"
32 #include "wx/log.h"
33 #include "wx/image.h"
34 #include "wx/filedlg.h"
35 #include "wx/colordlg.h"
36 #include "wx/srchctrl.h"
37
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
40 #ifdef __WXMSW__
41 #define USE_XPM_BITMAPS 0
42 #else
43 #define USE_XPM_BITMAPS 1
44 #endif
45
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
49
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
55
56 // Define this as 0 for the platforms not supporting controls in toolbars
57 #define USE_CONTROLS_IN_TOOLBAR 1
58
59 // ----------------------------------------------------------------------------
60 // resources
61 // ----------------------------------------------------------------------------
62
63 #if !defined(__WXMSW__) && !defined(__WXPM__)
64 #include "mondrian.xpm"
65 #endif
66
67 #if USE_XPM_BITMAPS
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
77
78 enum Positions
79 {
80 TOOLBAR_LEFT,
81 TOOLBAR_TOP,
82 TOOLBAR_RIGHT,
83 TOOLBAR_BOTTOM
84 };
85
86 // ----------------------------------------------------------------------------
87 // classes
88 // ----------------------------------------------------------------------------
89
90 // Define a new application
91 class MyApp : public wxApp
92 {
93 public:
94 bool OnInit();
95 };
96
97 // Define a new frame
98 class MyFrame: public wxFrame
99 {
100 public:
101 MyFrame(wxFrame *parent,
102 wxWindowID id = wxID_ANY,
103 const wxString& title = _T("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);
107 virtual ~MyFrame();
108
109 void PopulateToolbar(wxToolBarBase* toolBar);
110 void RecreateToolbar();
111
112 void OnQuit(wxCommandEvent& event);
113 void OnAbout(wxCommandEvent& event);
114
115 void OnSize(wxSizeEvent& event);
116
117 void OnToggleToolbar(wxCommandEvent& event);
118 void OnToggleAnotherToolbar(wxCommandEvent& event);
119 void OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event));
120
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);
126
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);
134
135 void OnToolbarStyle(wxCommandEvent& event);
136 void OnToolbarBgCol(wxCommandEvent& event);
137 void OnToolbarCustomBitmap(wxCommandEvent& event);
138
139 void OnToolLeftClick(wxCommandEvent& event);
140 void OnToolRightClick(wxCommandEvent& event);
141 void OnToolDropdown(wxCommandEvent& event);
142
143 void OnCombo(wxCommandEvent& event);
144
145 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
146 void OnUpdateToggleHorzText(wxUpdateUIEvent& event);
147 void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event)
148 { event.Enable( m_tbar != NULL ); }
149
150 private:
151 void DoEnablePrint();
152 void DoDeletePrint();
153 void DoToggleHelp();
154
155 void LayoutChildren();
156
157 bool m_smallToolbar,
158 m_horzText,
159 m_useCustomDisabled,
160 m_showTooltips;
161 size_t m_rows; // 1 or 2 only
162
163 // the number of print buttons we have (they're added/removed dynamically)
164 size_t m_nPrint;
165
166 // store toolbar position for future use
167 Positions m_toolbarPosition;
168
169 wxTextCtrl *m_textWindow;
170
171 wxPanel *m_panel;
172 #if USE_UNMANAGED_TOOLBAR
173 wxToolBar *m_extraToolBar;
174 #endif
175
176 wxToolBar *m_tbar;
177
178 // the path to the custom bitmap for the test toolbar tool
179 wxString m_pathBmp;
180
181 // the search tool, initially NULL
182 wxToolBarToolBase *m_searchTool;
183
184 DECLARE_EVENT_TABLE()
185 };
186
187 // ----------------------------------------------------------------------------
188 // constants
189 // ----------------------------------------------------------------------------
190
191 const int ID_TOOLBAR = 500;
192
193 static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT;
194
195 enum
196 {
197 // toolbar menu items
198 IDM_TOOLBAR_TOGGLE_TOOLBAR = 200,
199 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
200 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
201 IDM_TOOLBAR_TOGGLETOOLBARSIZE,
202 IDM_TOOLBAR_TOGGLETOOLBARROWS,
203 IDM_TOOLBAR_TOGGLETOOLTIPS,
204 IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
205 IDM_TOOLBAR_SHOW_TEXT,
206 IDM_TOOLBAR_SHOW_ICONS,
207 IDM_TOOLBAR_SHOW_BOTH,
208 IDM_TOOLBAR_BG_COL,
209 IDM_TOOLBAR_CUSTOM_PATH,
210 IDM_TOOLBAR_TOP_ORIENTATION,
211 IDM_TOOLBAR_LEFT_ORIENTATION,
212 IDM_TOOLBAR_BOTTOM_ORIENTATION,
213 IDM_TOOLBAR_RIGHT_ORIENTATION,
214 IDM_TOOLBAR_OTHER_1,
215 IDM_TOOLBAR_OTHER_2,
216 IDM_TOOLBAR_OTHER_3,
217
218 // tools menu items
219 IDM_TOOLBAR_ENABLEPRINT,
220 IDM_TOOLBAR_DELETEPRINT,
221 IDM_TOOLBAR_INSERTPRINT,
222 IDM_TOOLBAR_TOGGLEHELP,
223 IDM_TOOLBAR_TOGGLESEARCH,
224 IDM_TOOLBAR_TOGGLERADIOBTN1,
225 IDM_TOOLBAR_TOGGLERADIOBTN2,
226 IDM_TOOLBAR_TOGGLERADIOBTN3,
227 IDM_TOOLBAR_CHANGE_TOOLTIP,
228
229 ID_COMBO = 1000
230 };
231
232 // ----------------------------------------------------------------------------
233 // event tables
234 // ----------------------------------------------------------------------------
235
236 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
237 // help button.
238
239 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
240 EVT_SIZE(MyFrame::OnSize)
241
242 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
243 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
244
245 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR, MyFrame::OnToggleToolbar)
246 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar)
247 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, MyFrame::OnToggleHorizontalText)
248
249 EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION, IDM_TOOLBAR_RIGHT_ORIENTATION, MyFrame::OnChangeOrientation)
250 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
251 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
252 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS, MyFrame::OnToggleTooltips)
253 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, MyFrame::OnToggleCustomDisabled)
254
255 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
256 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
257 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
258 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
259 EVT_MENU(IDM_TOOLBAR_TOGGLESEARCH, MyFrame::OnToggleSearch)
260 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3,
261 MyFrame::OnToggleRadioBtn)
262 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip)
263
264 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH,
265 MyFrame::OnToolbarStyle)
266 EVT_MENU(IDM_TOOLBAR_BG_COL, MyFrame::OnToolbarBgCol)
267
268 EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH, MyFrame::OnToolbarCustomBitmap)
269
270 EVT_MENU(wxID_ANY, MyFrame::OnToolLeftClick)
271
272 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
273
274 EVT_TOOL_RCLICKED(wxID_ANY, MyFrame::OnToolRightClick)
275
276 EVT_TOOL_DROPDOWN(wxID_ANY, MyFrame::OnToolDropdown)
277
278 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
279 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
280
281 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1,
282 IDM_TOOLBAR_TOGGLERADIOBTN3,
283 MyFrame::OnUpdateToggleRadioBtn)
284 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
285 MyFrame::OnUpdateToggleHorzText)
286 END_EVENT_TABLE()
287
288 // ============================================================================
289 // implementation
290 // ============================================================================
291
292 // ----------------------------------------------------------------------------
293 // MyApp
294 // ----------------------------------------------------------------------------
295
296 IMPLEMENT_APP(MyApp)
297
298 // The `main program' equivalent, creating the windows and returning the
299 // main frame
300 bool MyApp::OnInit()
301 {
302 if ( !wxApp::OnInit() )
303 return false;
304
305 // Create the main frame window
306 MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY,
307 _T("wxToolBar Sample"),
308 wxPoint(100, 100), wxSize(550, 300));
309
310 frame->Show(true);
311
312 #if wxUSE_STATUSBAR
313 frame->SetStatusText(_T("Hello, wxWidgets"));
314 #endif
315
316 wxInitAllImageHandlers();
317
318 SetTopWindow(frame);
319
320 return true;
321 }
322
323 void MyFrame::RecreateToolbar()
324 {
325 #ifdef __WXWINCE__
326 // On Windows CE, we should not delete the
327 // previous toolbar in case it contains the menubar.
328 // We'll try to accommodate this usage in due course.
329 wxToolBar* toolBar = CreateToolBar();
330 #else
331 // delete and recreate the toolbar
332 wxToolBarBase *toolBar = GetToolBar();
333 long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE;
334
335 if (toolBar && m_searchTool && m_searchTool->GetToolBar() == NULL)
336 {
337 // see ~MyFrame()
338 toolBar->AddTool(m_searchTool);
339 }
340 m_searchTool = NULL;
341
342 delete toolBar;
343
344 SetToolBar(NULL);
345
346 style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL | wxTB_BOTTOM | wxTB_RIGHT | wxTB_HORZ_LAYOUT);
347 switch( m_toolbarPosition )
348 {
349 case TOOLBAR_LEFT:
350 style |= wxTB_LEFT;
351 break;
352 case TOOLBAR_TOP:
353 style |= wxTB_TOP;
354 break;
355 case TOOLBAR_RIGHT:
356 style |= wxTB_RIGHT;
357 break;
358 case TOOLBAR_BOTTOM:
359 style |= wxTB_BOTTOM;
360 break;
361 }
362
363 if ( m_showTooltips )
364 style &= ~wxTB_NO_TOOLTIPS;
365 else
366 style |= wxTB_NO_TOOLTIPS;
367
368 if ( style & wxTB_TEXT && !(style & wxTB_NOICONS) && m_horzText )
369 style |= wxTB_HORZ_LAYOUT;
370
371 toolBar = CreateToolBar(style, ID_TOOLBAR);
372 #endif
373
374 PopulateToolbar(toolBar);
375 }
376
377 void MyFrame::PopulateToolbar(wxToolBarBase* toolBar)
378 {
379 // Set up toolbar
380 enum
381 {
382 Tool_new,
383 Tool_open,
384 Tool_save,
385 Tool_copy,
386 Tool_cut,
387 Tool_paste,
388 Tool_print,
389 Tool_help,
390 Tool_Max
391 };
392
393 wxBitmap toolBarBitmaps[Tool_Max];
394
395 #if USE_XPM_BITMAPS
396 #define INIT_TOOL_BMP(bmp) \
397 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
398 #else // !USE_XPM_BITMAPS
399 #define INIT_TOOL_BMP(bmp) \
400 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
401 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
402
403 INIT_TOOL_BMP(new);
404 INIT_TOOL_BMP(open);
405 INIT_TOOL_BMP(save);
406 INIT_TOOL_BMP(copy);
407 INIT_TOOL_BMP(cut);
408 INIT_TOOL_BMP(paste);
409 INIT_TOOL_BMP(print);
410 INIT_TOOL_BMP(help);
411
412 int w = toolBarBitmaps[Tool_new].GetWidth(),
413 h = toolBarBitmaps[Tool_new].GetHeight();
414
415 if ( !m_smallToolbar )
416 {
417 w *= 2;
418 h *= 2;
419
420 for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ )
421 {
422 toolBarBitmaps[n] =
423 wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h));
424 }
425 }
426
427 toolBar->SetToolBitmapSize(wxSize(w, h));
428
429 toolBar->AddTool(wxID_NEW, _T("New"),
430 toolBarBitmaps[Tool_new], wxNullBitmap, wxITEM_DROPDOWN,
431 _T("New file"), _T("This is help for new file tool"));
432
433 wxMenu* menu = new wxMenu;
434 menu->Append(wxID_ANY, _T("&First dummy item"));
435 menu->Append(wxID_ANY, _T("&Second dummy item"));
436 menu->AppendSeparator();
437 menu->Append(wxID_EXIT, _T("Exit"));
438 toolBar->SetDropdownMenu(wxID_NEW, menu);
439
440 toolBar->AddTool(wxID_OPEN, _T("Open"),
441 toolBarBitmaps[Tool_open], wxNullBitmap, wxITEM_NORMAL,
442 _T("Open file"), _T("This is help for open file tool"));
443
444 #if USE_CONTROLS_IN_TOOLBAR
445 // adding a combo to a vertical toolbar is not very smart
446 if ( !toolBar->IsVertical() )
447 {
448 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, wxEmptyString, wxDefaultPosition, wxSize(100,-1) );
449 combo->Append(_T("This"));
450 combo->Append(_T("is a"));
451 combo->Append(_T("combobox"));
452 combo->Append(_T("in a"));
453 combo->Append(_T("toolbar"));
454 toolBar->AddControl(combo, _T("Combo Label"));
455 }
456 #endif // USE_CONTROLS_IN_TOOLBAR
457
458 toolBar->AddTool(wxID_SAVE, _T("Save"), toolBarBitmaps[Tool_save], _T("Toggle button 1"), wxITEM_CHECK);
459 toolBar->AddTool(wxID_COPY, _T("Copy"), toolBarBitmaps[Tool_copy], _T("Toggle button 2"), wxITEM_CHECK);
460 toolBar->AddTool(wxID_CUT, _T("Cut"), toolBarBitmaps[Tool_cut], _T("Toggle/Untoggle help button"));
461 toolBar->AddTool(wxID_PASTE, _T("Paste"), toolBarBitmaps[Tool_paste], _T("Paste"));
462
463 if ( m_useCustomDisabled )
464 {
465 wxBitmap bmpDisabled(w, h);
466 {
467 wxMemoryDC dc;
468 dc.SelectObject(bmpDisabled);
469 dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0);
470
471 wxPen pen(*wxRED, 5);
472 dc.SetPen(pen);
473 dc.DrawLine(0, 0, w, h);
474 }
475
476 toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print],
477 bmpDisabled);
478 }
479 else
480 {
481 toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print],
482 _T("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."));
483 }
484
485 toolBar->AddSeparator();
486 toolBar->AddTool(wxID_HELP, _T("Help"), toolBarBitmaps[Tool_help], _T("Help button"), wxITEM_CHECK);
487
488 if ( !m_pathBmp.empty() )
489 {
490 // create a tool with a custom bitmap for testing
491 wxImage img(m_pathBmp);
492 if ( img.Ok() )
493 {
494 if ( img.GetWidth() > w && img.GetHeight() > h )
495 img = img.GetSubImage(wxRect(0, 0, w, h));
496
497 toolBar->AddSeparator();
498 toolBar->AddTool(wxID_ANY, _T("Custom"), img);
499 }
500 }
501
502 // after adding the buttons to the toolbar, must call Realize() to reflect
503 // the changes
504 toolBar->Realize();
505
506 toolBar->SetRows(!(toolBar->IsVertical()) ? m_rows : 10 / m_rows);
507 }
508
509 // ----------------------------------------------------------------------------
510 // MyFrame
511 // ----------------------------------------------------------------------------
512
513 // Define my frame constructor
514 MyFrame::MyFrame(wxFrame* parent,
515 wxWindowID id,
516 const wxString& title,
517 const wxPoint& pos,
518 const wxSize& size,
519 long style)
520 : wxFrame(parent, id, title, pos, size, style)
521 {
522 m_tbar = NULL;
523
524 m_smallToolbar = true;
525 m_horzText = false;
526 m_useCustomDisabled = false;
527 m_showTooltips = true;
528 m_searchTool = NULL;
529
530 m_rows = 1;
531 m_nPrint = 1;
532
533 #if wxUSE_STATUSBAR
534 // Give it a status line
535 CreateStatusBar();
536 #endif
537
538 // Give it an icon
539 SetIcon(wxICON(mondrian));
540
541 // Make a menubar
542 wxMenu *tbarMenu = new wxMenu;
543 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR,
544 _T("Toggle &toolbar\tCtrl-Z"),
545 _T("Show or hide the toolbar"));
546
547 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
548 _T("Toggle &another toolbar\tCtrl-A"),
549 _T("Show/hide another test toolbar"));
550
551 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
552 _T("Toggle hori&zontal text\tCtrl-H"),
553 _T("Show text under/alongside the icon"));
554
555 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
556 _T("&Toggle toolbar size\tCtrl-S"),
557 _T("Toggle between big/small toolbar"));
558
559 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS,
560 _T("Toggle number of &rows\tCtrl-R"),
561 _T("Toggle number of toolbar rows between 1 and 2"));
562
563 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS,
564 _T("Show &tooltips\tCtrl-L"),
565 _T("Show tooltips for the toolbar tools"));
566
567 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
568 _T("Use c&ustom disabled images\tCtrl-U"),
569 _T("Switch between using system-generated and custom disabled images"));
570
571
572 tbarMenu->AppendSeparator();
573 tbarMenu->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION,
574 _T("Set toolbar at the top of the window"),
575 _T("Set toolbar at the top of the window"));
576 tbarMenu->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION,
577 _T("Set toolbar at the left of the window"),
578 _T("Set toolbar at the left of the window"));
579 tbarMenu->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION,
580 _T("Set toolbar at the bottom of the window"),
581 _T("Set toolbar at the bottom of the window"));
582 tbarMenu->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION,
583 _T("Set toolbar at the right edge of the window"),
584 _T("Set toolbar at the right edge of the window"));
585 tbarMenu->AppendSeparator();
586
587 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, _T("Show &text\tCtrl-Alt-T"));
588 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, _T("Show &icons\tCtrl-Alt-I"));
589 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, _T("Show &both\tCtrl-Alt-B"));
590 tbarMenu->AppendSeparator();
591 tbarMenu->Append(IDM_TOOLBAR_BG_COL, _T("Choose bac&kground colour..."));
592 tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, _T("Custom &bitmap...\tCtrl-B"));
593
594 wxMenu *toolMenu = new wxMenu;
595 toolMenu->Append(IDM_TOOLBAR_ENABLEPRINT, _T("&Enable print button\tCtrl-E"));
596 toolMenu->Append(IDM_TOOLBAR_DELETEPRINT, _T("&Delete print button\tCtrl-D"));
597 toolMenu->Append(IDM_TOOLBAR_INSERTPRINT, _T("&Insert print button\tCtrl-I"));
598 toolMenu->Append(IDM_TOOLBAR_TOGGLEHELP, _T("Toggle &help button\tCtrl-T"));
599 toolMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH, _T("Toggle &search field\tCtrl-F"));
600 toolMenu->AppendSeparator();
601 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, _T("Toggle &1st radio button\tCtrl-1"));
602 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, _T("Toggle &2nd radio button\tCtrl-2"));
603 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, _T("Toggle &3rd radio button\tCtrl-3"));
604 toolMenu->AppendSeparator();
605 toolMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, _T("Change tooltip of \"New\""));
606
607 wxMenu *fileMenu = new wxMenu;
608 fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
609
610 wxMenu *helpMenu = new wxMenu;
611 helpMenu->Append(wxID_HELP, _T("&About"), _T("About toolbar sample"));
612
613 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
614
615 menuBar->Append(fileMenu, _T("&File"));
616 menuBar->Append(tbarMenu, _T("&Toolbar"));
617 menuBar->Append(toolMenu, _T("Tool&s"));
618 menuBar->Append(helpMenu, _T("&Help"));
619
620 // Associate the menu bar with the frame
621 SetMenuBar(menuBar);
622
623 menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true);
624 menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true);
625
626 menuBar->Check(IDM_TOOLBAR_TOP_ORIENTATION, true );
627 m_toolbarPosition = TOOLBAR_TOP;
628
629 // Create the toolbar
630 RecreateToolbar();
631
632 m_panel = new wxPanel(this, wxID_ANY);
633 #if USE_UNMANAGED_TOOLBAR
634 m_extraToolBar = new wxToolBar(m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_TEXT|wxTB_FLAT|wxTB_TOP);
635 PopulateToolbar(m_extraToolBar);
636 #endif
637
638 m_textWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
639
640 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
641 m_panel->SetSizer(sizer);
642 #if USE_UNMANAGED_TOOLBAR
643 if (m_extraToolBar)
644 sizer->Add(m_extraToolBar, 0, wxEXPAND, 0);
645 #endif
646 sizer->Add(m_textWindow, 1, wxEXPAND, 0);
647 }
648
649 MyFrame::~MyFrame()
650 {
651 if ( m_searchTool && !m_searchTool->GetToolBar() )
652 {
653 // we currently can't delete a toolbar tool ourselves, so we have to
654 // attach it to the toolbar just for it to be deleted, this is pretty
655 // ugly and will need to be changed
656 GetToolBar()->AddTool(m_searchTool);
657 }
658 }
659
660 void MyFrame::LayoutChildren()
661 {
662 wxSize size = GetClientSize();
663
664 int offset;
665 if ( m_tbar )
666 {
667 m_tbar->SetSize(0, 0, wxDefaultCoord, size.y);
668
669 offset = m_tbar->GetSize().x;
670 }
671 else
672 {
673 offset = 0;
674 }
675
676 m_panel->SetSize(offset, 0, size.x - offset, size.y);
677 }
678
679 void MyFrame::OnSize(wxSizeEvent& event)
680 {
681 if ( m_tbar )
682 {
683 LayoutChildren();
684 }
685 else
686 {
687 event.Skip();
688 }
689 }
690
691 void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
692 {
693 wxToolBar *tbar = GetToolBar();
694
695 if ( !tbar )
696 {
697 RecreateToolbar();
698 }
699 else
700 {
701 delete tbar;
702
703 SetToolBar(NULL);
704 }
705 }
706
707 void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event))
708 {
709 m_horzText = !m_horzText;
710
711 RecreateToolbar();
712 }
713
714 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
715 {
716 if ( m_tbar )
717 {
718 delete m_tbar;
719 m_tbar = NULL;
720 }
721 else
722 {
723 long style = GetToolBar() ? GetToolBar()->GetWindowStyle()
724 : TOOLBAR_STYLE;
725 style &= ~wxTB_HORIZONTAL;
726 style |= wxTB_VERTICAL;
727
728 m_tbar = new wxToolBar(this, wxID_ANY,
729 wxDefaultPosition, wxDefaultSize,
730 style);
731
732 m_tbar->SetMargins(4, 4);
733
734 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, _T("First"), wxBITMAP(new));
735 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, _T("Second"), wxBITMAP(open));
736 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, _T("Third"), wxBITMAP(save));
737 m_tbar->AddSeparator();
738 m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help));
739
740 m_tbar->Realize();
741 }
742
743 LayoutChildren();
744 }
745
746 void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
747 {
748 m_smallToolbar = !m_smallToolbar;
749
750 RecreateToolbar();
751 }
752
753 void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
754 {
755 // m_rows may be only 1 or 2
756 m_rows = 3 - m_rows;
757
758 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows : 10 / m_rows);
759
760 //RecreateToolbar(); -- this is unneeded
761 }
762
763 void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event))
764 {
765 m_showTooltips = !m_showTooltips;
766
767 RecreateToolbar();
768 }
769
770 void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event))
771 {
772 m_useCustomDisabled = !m_useCustomDisabled;
773
774 RecreateToolbar();
775 }
776
777 void MyFrame::OnChangeOrientation(wxCommandEvent& event)
778 {
779 switch( event.GetId() )
780 {
781 case IDM_TOOLBAR_LEFT_ORIENTATION:
782 m_toolbarPosition = TOOLBAR_LEFT;
783 break;
784 case IDM_TOOLBAR_TOP_ORIENTATION:
785 m_toolbarPosition = TOOLBAR_TOP;
786 break;
787 case IDM_TOOLBAR_RIGHT_ORIENTATION:
788 m_toolbarPosition = TOOLBAR_RIGHT;
789 break;
790 case IDM_TOOLBAR_BOTTOM_ORIENTATION:
791 m_toolbarPosition = TOOLBAR_BOTTOM;
792 break;
793 }
794 RecreateToolbar();
795 }
796
797 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
798 {
799 Close(true);
800 }
801
802 void MyFrame::OnAbout(wxCommandEvent& event)
803 {
804 if ( event.IsChecked() )
805 m_textWindow->WriteText( _T("Help button down now.\n") );
806 else
807 m_textWindow->WriteText( _T("Help button up now.\n") );
808
809 (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar"));
810 }
811
812 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
813 {
814 wxString str;
815 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
816 m_textWindow->WriteText( str );
817
818 if (event.GetId() == wxID_COPY)
819 {
820 DoEnablePrint();
821 }
822
823 if (event.GetId() == wxID_CUT)
824 {
825 DoToggleHelp();
826 }
827
828 if (event.GetId() == wxID_PRINT)
829 {
830 DoDeletePrint();
831 }
832 }
833
834 void MyFrame::OnToolRightClick(wxCommandEvent& event)
835 {
836 m_textWindow->AppendText(
837 wxString::Format(_T("Tool %d right clicked.\n"),
838 (int) event.GetInt()));
839 }
840
841 void MyFrame::OnCombo(wxCommandEvent& event)
842 {
843 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
844 }
845
846 void MyFrame::DoEnablePrint()
847 {
848 if ( !m_nPrint )
849 return;
850
851 wxToolBarBase *tb = GetToolBar();
852 tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT));
853 }
854
855 void MyFrame::DoDeletePrint()
856 {
857 if ( !m_nPrint )
858 return;
859
860 wxToolBarBase *tb = GetToolBar();
861 tb->DeleteTool( wxID_PRINT );
862
863 m_nPrint--;
864 }
865
866 void MyFrame::DoToggleHelp()
867 {
868 wxToolBarBase *tb = GetToolBar();
869 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
870 }
871
872 void MyFrame::OnToggleSearch(wxCommandEvent& WXUNUSED(event))
873 {
874 static const int searchPos = 3;
875
876 wxToolBarBase * const tb = GetToolBar();
877 if ( !m_searchTool )
878 {
879 wxSearchCtrl * const srch = new wxSearchCtrl(tb, wxID_ANY, "needle");
880 srch->SetMinSize(wxSize(80, -1));
881 m_searchTool = tb->InsertControl(searchPos, srch);
882 }
883 else // tool already exists
884 {
885 wxControl * const win = m_searchTool->GetControl();
886 if ( m_searchTool->GetToolBar() )
887 {
888 // attached now, remove it
889 win->Hide();
890 tb->RemoveTool(m_searchTool->GetId());
891 }
892 else // tool exists in detached state, attach it back
893 {
894 tb->InsertTool(searchPos, m_searchTool);
895 win->Show();
896 }
897 }
898
899 tb->Realize();
900 }
901
902 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
903 {
904 event.Enable( m_textWindow->CanCopy() );
905 }
906
907 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event)
908 {
909 wxToolBar *tbar = GetToolBar();
910 event.Enable( tbar &&
911 tbar->HasFlag(wxTB_TEXT) &&
912 !tbar->HasFlag(wxTB_NOICONS) );
913 }
914
915 void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event))
916 {
917 GetToolBar()->SetToolShortHelp(wxID_NEW, _T("New toolbar button"));
918 }
919
920 void MyFrame::OnToolbarStyle(wxCommandEvent& event)
921 {
922 long style = GetToolBar()->GetWindowStyle();
923 style &= ~(wxTB_NOICONS | wxTB_TEXT);
924
925 switch ( event.GetId() )
926 {
927 case IDM_TOOLBAR_SHOW_TEXT:
928 style |= wxTB_NOICONS | wxTB_TEXT;
929 break;
930
931 case IDM_TOOLBAR_SHOW_ICONS:
932 // nothing to do
933 break;
934
935 case IDM_TOOLBAR_SHOW_BOTH:
936 style |= wxTB_TEXT;
937 }
938
939 GetToolBar()->SetWindowStyle(style);
940 }
941
942 void MyFrame::OnToolbarBgCol(wxCommandEvent& WXUNUSED(event))
943 {
944 wxColour col = wxGetColourFromUser
945 (
946 this,
947 GetToolBar()->GetBackgroundColour(),
948 "Toolbar background colour"
949 );
950 if ( col.IsOk() )
951 {
952 GetToolBar()->SetBackgroundColour(col);
953 GetToolBar()->Refresh();
954 }
955 }
956
957 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event))
958 {
959 m_pathBmp = wxFileSelector(_T("Custom bitmap path"));
960
961 RecreateToolbar();
962 }
963
964 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
965 {
966 m_nPrint++;
967
968 wxToolBarBase *tb = GetToolBar();
969 tb->InsertTool(0, wxID_PRINT, _T("New print"),
970 wxBITMAP(print), wxNullBitmap,
971 wxITEM_NORMAL,
972 _T("Delete this tool"),
973 _T("This button was inserted into the toolbar"));
974
975 // must call Realize() after adding a new button
976 tb->Realize();
977 }
978
979 void MyFrame::OnToggleRadioBtn(wxCommandEvent& event)
980 {
981 if ( m_tbar )
982 {
983 m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 +
984 event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true);
985 }
986 }
987
988 void MyFrame::OnToolDropdown(wxCommandEvent& event)
989 {
990 wxString str;
991 str.Printf( _T("Dropdown on tool %d\n"), event.GetId());
992 m_textWindow->WriteText( str );
993
994 event.Skip();
995 }