changed menu accels to not clash with menubar ones
[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
35 // define this to 1 to use wxToolBarSimple instead of the native one
36 #define USE_GENERIC_TBAR 0
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_GENERIC_TBAR
47 #if !wxUSE_TOOLBAR_SIMPLE
48 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
49 to 1 in setup.h and recompile the library.
50 #else
51 #include "wx/tbarsmpl.h"
52 #endif
53 #endif // USE_GENERIC_TBAR
54
55 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
56 #error You need to enable XPM support to use XPM bitmaps with toolbar!
57 #endif // USE_XPM_BITMAPS
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 // ----------------------------------------------------------------------------
79 // classes
80 // ----------------------------------------------------------------------------
81
82 // Define a new application
83 class MyApp : public wxApp
84 {
85 public:
86 bool OnInit();
87 };
88
89 // Define a new frame
90 class MyFrame: public wxFrame
91 {
92 public:
93 MyFrame(wxFrame *parent,
94 wxWindowID id = wxID_ANY,
95 const wxString& title = _T("wxToolBar Sample"),
96 const wxPoint& pos = wxDefaultPosition,
97 const wxSize& size = wxDefaultSize,
98 long style = wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
99
100 void RecreateToolbar();
101
102 void OnQuit(wxCommandEvent& event);
103 void OnAbout(wxCommandEvent& event);
104
105 void OnSize(wxSizeEvent& event);
106
107 void OnToggleToolbar(wxCommandEvent& event);
108 void OnToggleAnotherToolbar(wxCommandEvent& event);
109 void OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event));
110
111 void OnToggleToolbarSize(wxCommandEvent& event);
112 void OnToggleToolbarOrient(wxCommandEvent& event);
113 void OnToggleToolbarRows(wxCommandEvent& event);
114 void OnToggleTooltips(wxCommandEvent& event);
115 void OnToggleCustomDisabled(wxCommandEvent& event);
116
117 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
118 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
119 void OnInsertPrint(wxCommandEvent& event);
120 void OnChangeToolTip(wxCommandEvent& event);
121 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
122 void OnToggleRadioBtn(wxCommandEvent& event);
123
124 void OnToolbarStyle(wxCommandEvent& event);
125
126 void OnToolLeftClick(wxCommandEvent& event);
127 void OnToolRightClick(wxCommandEvent& event);
128 void OnToolEnter(wxCommandEvent& event);
129
130 void OnCombo(wxCommandEvent& event);
131
132 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
133 void OnUpdateToggleHorzText(wxUpdateUIEvent& event);
134 void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event)
135 { event.Enable( m_tbar != NULL ); }
136
137 #if USE_GENERIC_TBAR
138 virtual wxToolBar *OnCreateToolBar(long style,
139 wxWindowID id,
140 const wxString& name );
141 #endif // USE_GENERIC_TBAR
142
143 private:
144 void DoEnablePrint();
145 void DoDeletePrint();
146 void DoToggleHelp();
147
148 void LayoutChildren();
149
150 bool m_smallToolbar,
151 m_horzToolbar,
152 m_horzText,
153 m_useCustomDisabled,
154 m_showTooltips;
155 size_t m_rows; // 1 or 2 only
156
157 // the number of print buttons we have (they're added/removed dynamically)
158 size_t m_nPrint;
159
160 wxTextCtrl *m_textWindow;
161
162 wxToolBar *m_tbar;
163
164 DECLARE_EVENT_TABLE()
165 };
166
167 // ----------------------------------------------------------------------------
168 // constants
169 // ----------------------------------------------------------------------------
170
171 const int ID_TOOLBAR = 500;
172
173 static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT;
174
175 enum
176 {
177 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
178 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
179 IDM_TOOLBAR_TOGGLETOOLBARROWS,
180 IDM_TOOLBAR_TOGGLETOOLTIPS,
181 IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
182 IDM_TOOLBAR_ENABLEPRINT,
183 IDM_TOOLBAR_DELETEPRINT,
184 IDM_TOOLBAR_INSERTPRINT,
185 IDM_TOOLBAR_TOGGLEHELP,
186 IDM_TOOLBAR_TOGGLERADIOBTN1,
187 IDM_TOOLBAR_TOGGLERADIOBTN2,
188 IDM_TOOLBAR_TOGGLERADIOBTN3,
189 IDM_TOOLBAR_TOGGLE_TOOLBAR,
190 IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
191 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
192 IDM_TOOLBAR_CHANGE_TOOLTIP,
193 IDM_TOOLBAR_SHOW_TEXT,
194 IDM_TOOLBAR_SHOW_ICONS,
195 IDM_TOOLBAR_SHOW_BOTH,
196
197 IDM_TOOLBAR_OTHER_1,
198 IDM_TOOLBAR_OTHER_2,
199 IDM_TOOLBAR_OTHER_3,
200
201 ID_COMBO = 1000
202 };
203
204 // ----------------------------------------------------------------------------
205 // event tables
206 // ----------------------------------------------------------------------------
207
208 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
209 // help button.
210
211 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
212 EVT_SIZE(MyFrame::OnSize)
213
214 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
215 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
216
217 EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR, MyFrame::OnToggleToolbar)
218 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar)
219 EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, MyFrame::OnToggleHorizontalText)
220
221 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
222 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
223 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
224 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS, MyFrame::OnToggleTooltips)
225 EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, MyFrame::OnToggleCustomDisabled)
226
227 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
228 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
229 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
230 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
231 EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3,
232 MyFrame::OnToggleRadioBtn)
233 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip)
234
235 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH,
236 MyFrame::OnToolbarStyle)
237
238 EVT_MENU(wxID_ANY, MyFrame::OnToolLeftClick)
239
240 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
241
242 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
243 EVT_TOOL_RCLICKED(wxID_ANY, MyFrame::OnToolRightClick)
244
245 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
246 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
247
248 EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1,
249 IDM_TOOLBAR_TOGGLERADIOBTN3,
250 MyFrame::OnUpdateToggleRadioBtn)
251 EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
252 MyFrame::OnUpdateToggleHorzText)
253 END_EVENT_TABLE()
254
255 // ============================================================================
256 // implementation
257 // ============================================================================
258
259 // ----------------------------------------------------------------------------
260 // MyApp
261 // ----------------------------------------------------------------------------
262
263 IMPLEMENT_APP(MyApp)
264
265 // The `main program' equivalent, creating the windows and returning the
266 // main frame
267 bool MyApp::OnInit()
268 {
269 // Create the main frame window
270 MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY,
271 _T("wxToolBar Sample"),
272 #ifdef __WXWINCE__
273 wxDefaultPosition, wxDefaultSize
274 #else
275 wxPoint(100, 100), wxSize(550, 300)
276 #endif
277 );
278
279 frame->Show(true);
280
281 #if wxUSE_STATUSBAR
282 frame->SetStatusText(_T("Hello, wxWidgets"));
283 #endif
284
285 SetTopWindow(frame);
286
287 return true;
288 }
289
290 void MyFrame::RecreateToolbar()
291 {
292 #ifdef __WXWINCE__
293 // On Windows CE, we should not delete the
294 // previous toolbar in case it contains the menubar.
295 // We'll try to accommodate this usage in due course.
296 wxToolBar* toolBar = CreateToolBar();
297 #else
298 // delete and recreate the toolbar
299 wxToolBarBase *toolBar = GetToolBar();
300 long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE;
301
302 delete toolBar;
303
304 SetToolBar(NULL);
305
306 style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL | wxTB_HORZ_LAYOUT);
307 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
308
309 if ( m_showTooltips )
310 style &= ~wxTB_NO_TOOLTIPS;
311 else
312 style |= wxTB_NO_TOOLTIPS;
313
314 if ( style & wxTB_TEXT && !(style & wxTB_NOICONS) && m_horzText )
315 style |= wxTB_HORZ_LAYOUT;
316
317 toolBar = CreateToolBar(style, ID_TOOLBAR);
318 #endif
319
320 // Set up toolbar
321 enum
322 {
323 Tool_new,
324 Tool_open,
325 Tool_save,
326 Tool_copy,
327 Tool_cut,
328 Tool_paste,
329 Tool_print,
330 Tool_help,
331 Tool_Max
332 };
333
334 wxBitmap toolBarBitmaps[Tool_Max];
335
336 #if USE_XPM_BITMAPS
337 #define INIT_TOOL_BMP(bmp) \
338 toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm)
339 #else // !USE_XPM_BITMAPS
340 #define INIT_TOOL_BMP(bmp) \
341 toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp)
342 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
343
344 INIT_TOOL_BMP(new);
345 INIT_TOOL_BMP(open);
346 INIT_TOOL_BMP(save);
347 INIT_TOOL_BMP(copy);
348 INIT_TOOL_BMP(cut);
349 INIT_TOOL_BMP(paste);
350 INIT_TOOL_BMP(print);
351 INIT_TOOL_BMP(help);
352
353 int w = toolBarBitmaps[Tool_new].GetWidth(),
354 h = toolBarBitmaps[Tool_new].GetHeight();
355
356 if ( !m_smallToolbar )
357 {
358 w *= 2;
359 h *= 2;
360
361 for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ )
362 {
363 toolBarBitmaps[n] =
364 wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h));
365 }
366 }
367
368 toolBar->SetToolBitmapSize(wxSize(w, h));
369
370 toolBar->AddTool(wxID_NEW, _T("New"), toolBarBitmaps[Tool_new], _T("New file"));
371 toolBar->AddTool(wxID_OPEN, _T("Open"), toolBarBitmaps[Tool_open], _T("Open file"));
372
373 // the generic toolbar doesn't really support this
374 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXX11__) || defined(__WXUNIVERSAL__)
375 // adding a combo to a vertical toolbar is not very smart
376 if ( m_horzToolbar )
377 {
378 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, wxEmptyString, wxDefaultPosition, wxSize(200,wxDefaultCoord) );
379 combo->Append(_T("This"));
380 combo->Append(_T("is a"));
381 combo->Append(_T("combobox"));
382 combo->Append(_T("in a"));
383 combo->Append(_T("toolbar"));
384 toolBar->AddControl(combo);
385 }
386 #endif // toolbars which don't support controls
387
388 toolBar->AddTool(wxID_SAVE, _T("Save"), toolBarBitmaps[Tool_save], _T("Toggle button 1"), wxITEM_CHECK);
389 toolBar->AddTool(wxID_COPY, _T("Copy"), toolBarBitmaps[Tool_copy], _T("Toggle button 2"), wxITEM_CHECK);
390 toolBar->AddTool(wxID_CUT, _T("Cut"), toolBarBitmaps[Tool_cut], _T("Toggle/Untoggle help button"));
391 toolBar->AddTool(wxID_PASTE, _T("Paste"), toolBarBitmaps[Tool_paste], _T("Paste"));
392
393 if ( m_useCustomDisabled )
394 {
395 wxBitmap bmpDisabled(w, h);
396 {
397 wxMemoryDC dc;
398 dc.SelectObject(bmpDisabled);
399 dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0);
400
401 wxPen pen(*wxRED, 5);
402 dc.SetPen(pen);
403 dc.DrawLine(0, 0, w, h);
404 }
405
406 toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print],
407 bmpDisabled);
408 }
409 else
410 {
411 toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print],
412 _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."));
413 }
414
415 toolBar->AddSeparator();
416 toolBar->AddTool(wxID_HELP, _T("Help"), toolBarBitmaps[Tool_help], _T("Help button"), wxITEM_CHECK);
417
418 // after adding the buttons to the toolbar, must call Realize() to reflect
419 // the changes
420 toolBar->Realize();
421
422 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
423 }
424
425 // ----------------------------------------------------------------------------
426 // MyFrame
427 // ----------------------------------------------------------------------------
428
429 // Define my frame constructor
430 MyFrame::MyFrame(wxFrame* parent,
431 wxWindowID id,
432 const wxString& title,
433 const wxPoint& pos,
434 const wxSize& size,
435 long style)
436 : wxFrame(parent, id, title, pos, size, style)
437 {
438 m_tbar = NULL;
439
440 m_smallToolbar = true;
441 m_horzToolbar = true;
442 m_horzText = false;
443 m_useCustomDisabled = false;
444 m_showTooltips = true;
445
446 m_rows = 1;
447 m_nPrint = 1;
448
449 #if wxUSE_STATUSBAR
450 // Give it a status line
451 CreateStatusBar();
452 #endif
453
454 // Give it an icon
455 SetIcon(wxICON(mondrian));
456
457 // Make a menubar
458 wxMenu *tbarMenu = new wxMenu;
459 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR,
460 _T("Toggle &toolbar\tCtrl-Z"),
461 _T("Show or hide the toolbar"));
462
463 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
464 _T("Toggle &another toolbar\tCtrl-A"),
465 _T("Show/hide another test toolbar"));
466
467 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
468 _T("Toggle hori&zontal text\tCtrl-H"),
469 _T("Show text under/alongside the icon"));
470
471 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
472 _T("&Toggle toolbar size\tCtrl-S"),
473 _T("Toggle between big/small toolbar"));
474
475 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
476 _T("Toggle toolbar &orientation\tCtrl-O"),
477 _T("Toggle toolbar orientation"));
478
479 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS,
480 _T("Toggle number of &rows\tCtrl-R"),
481 _T("Toggle number of toolbar rows between 1 and 2"));
482
483 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS,
484 _T("Show &tooltips\tCtrl-L"),
485 _T("Show tooltips for the toolbar tools"));
486
487 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
488 _T("Use c&ustom disabled images\tCtrl-U"),
489 _T("Switch between using system-generated and custom disabled images"));
490
491
492 tbarMenu->AppendSeparator();
493
494 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, _T("&Enable print button\tCtrl-E"), wxEmptyString);
495 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, _T("&Delete print button\tCtrl-D"), wxEmptyString);
496 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, _T("&Insert print button\tCtrl-I"), wxEmptyString);
497 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, _T("Toggle &help button\tCtrl-T"), wxEmptyString);
498 tbarMenu->AppendSeparator();
499 tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, _T("Toggle &1st radio button\tCtrl-1"), wxEmptyString);
500 tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, _T("Toggle &2nd radio button\tCtrl-2"), wxEmptyString);
501 tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, _T("Toggle &3rd radio button\tCtrl-3"), wxEmptyString);
502 tbarMenu->AppendSeparator();
503 tbarMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, _T("Change tool tip"), wxEmptyString);
504 tbarMenu->AppendSeparator();
505 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, _T("Show &text\tCtrl-Alt-T"));
506 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, _T("Show &icons\tCtrl-Alt-I"));
507 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, _T("Show &both\tCtrl-Alt-B"));
508
509 wxMenu *fileMenu = new wxMenu;
510 fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") );
511
512 wxMenu *helpMenu = new wxMenu;
513 helpMenu->Append(wxID_HELP, _T("&About"), _T("About toolbar sample"));
514
515 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
516
517 menuBar->Append(fileMenu, _T("&File"));
518 menuBar->Append(tbarMenu, _T("&Toolbar"));
519 menuBar->Append(helpMenu, _T("&Help"));
520
521 // Associate the menu bar with the frame
522 SetMenuBar(menuBar);
523
524 menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true);
525 menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true);
526
527 // Create the toolbar
528 RecreateToolbar();
529
530 m_textWindow = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
531 }
532
533 #if USE_GENERIC_TBAR
534
535 wxToolBar* MyFrame::OnCreateToolBar(long style,
536 wxWindowID id,
537 const wxString& name)
538 {
539 return (wxToolBar *)new wxToolBarSimple(this, id,
540 wxDefaultPosition, wxDefaultSize,
541 style, name);
542 }
543
544 #endif // USE_GENERIC_TBAR
545
546 void MyFrame::LayoutChildren()
547 {
548 wxSize size = GetClientSize();
549
550 int offset;
551 if ( m_tbar )
552 {
553 m_tbar->SetSize(0, 0, wxDefaultCoord, size.y);
554
555 offset = m_tbar->GetSize().x;
556 }
557 else
558 {
559 offset = 0;
560 }
561
562 m_textWindow->SetSize(offset, 0, size.x - offset, size.y);
563 }
564
565 void MyFrame::OnSize(wxSizeEvent& event)
566 {
567 if ( m_tbar )
568 {
569 LayoutChildren();
570 }
571 else
572 {
573 event.Skip();
574 }
575 }
576
577 void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
578 {
579 wxToolBar *tbar = GetToolBar();
580
581 if ( !tbar )
582 {
583 RecreateToolbar();
584 }
585 else
586 {
587 delete tbar;
588
589 SetToolBar(NULL);
590 }
591 }
592
593 void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event))
594 {
595 m_horzText = !m_horzText;
596
597 RecreateToolbar();
598 }
599
600 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
601 {
602 if ( m_tbar )
603 {
604 delete m_tbar;
605 m_tbar = NULL;
606 }
607 else
608 {
609 long style = GetToolBar() ? GetToolBar()->GetWindowStyle()
610 : TOOLBAR_STYLE;
611 style &= ~wxTB_HORIZONTAL;
612 style |= wxTB_VERTICAL;
613
614 m_tbar = new wxToolBar(this, wxID_ANY,
615 wxDefaultPosition, wxDefaultSize,
616 style);
617
618 m_tbar->SetMargins(4, 4);
619
620 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, _T("First"), wxBITMAP(new));
621 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, _T("Second"), wxBITMAP(open));
622 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, _T("Third"), wxBITMAP(save));
623 m_tbar->AddSeparator();
624 m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help));
625
626 m_tbar->Realize();
627 }
628
629 LayoutChildren();
630 }
631
632 void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
633 {
634 m_smallToolbar = !m_smallToolbar;
635
636 RecreateToolbar();
637 }
638
639 void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
640 {
641 // m_rows may be only 1 or 2
642 m_rows = 3 - m_rows;
643
644 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
645
646 //RecreateToolbar(); -- this is unneeded
647 }
648
649 void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event))
650 {
651 m_showTooltips = !m_showTooltips;
652
653 RecreateToolbar();
654 }
655
656 void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event))
657 {
658 m_useCustomDisabled = !m_useCustomDisabled;
659
660 RecreateToolbar();
661 }
662
663 void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
664 {
665 m_horzToolbar = !m_horzToolbar;
666
667 RecreateToolbar();
668 }
669
670 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
671 {
672 Close(true);
673 }
674
675 void MyFrame::OnAbout(wxCommandEvent& event)
676 {
677 if ( event.IsChecked() )
678 m_textWindow->WriteText( _T("Help button down now.\n") );
679 else
680 m_textWindow->WriteText( _T("Help button up now.\n") );
681
682 (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar"));
683 }
684
685 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
686 {
687 wxString str;
688 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
689 m_textWindow->WriteText( str );
690
691 if (event.GetId() == wxID_COPY)
692 {
693 DoEnablePrint();
694 }
695
696 if (event.GetId() == wxID_CUT)
697 {
698 DoToggleHelp();
699 }
700
701 if (event.GetId() == wxID_PRINT)
702 {
703 DoDeletePrint();
704 }
705 }
706
707 void MyFrame::OnToolRightClick(wxCommandEvent& event)
708 {
709 m_textWindow->AppendText(
710 wxString::Format(_T("Tool %d right clicked.\n"),
711 (int) event.GetInt()));
712 }
713
714 void MyFrame::OnCombo(wxCommandEvent& event)
715 {
716 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
717 }
718
719 void MyFrame::DoEnablePrint()
720 {
721 if ( !m_nPrint )
722 return;
723
724 wxToolBarBase *tb = GetToolBar();
725 tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT));
726 }
727
728 void MyFrame::DoDeletePrint()
729 {
730 if ( !m_nPrint )
731 return;
732
733 wxToolBarBase *tb = GetToolBar();
734 tb->DeleteTool( wxID_PRINT );
735
736 m_nPrint--;
737 }
738
739 void MyFrame::DoToggleHelp()
740 {
741 wxToolBarBase *tb = GetToolBar();
742 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
743 }
744
745 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
746 {
747 event.Enable( m_textWindow->CanCopy() );
748 }
749
750 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event)
751 {
752 wxToolBar *tbar = GetToolBar();
753 event.Enable( tbar &&
754 tbar->HasFlag(wxTB_TEXT) &&
755 !tbar->HasFlag(wxTB_NOICONS) );
756 }
757
758 void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event))
759 {
760 GetToolBar()->SetToolShortHelp(wxID_NEW, _T("New toolbar button"));
761 }
762
763 void MyFrame::OnToolbarStyle(wxCommandEvent& event)
764 {
765 long style = GetToolBar()->GetWindowStyle();
766 style &= ~(wxTB_NOICONS | wxTB_TEXT);
767
768 switch ( event.GetId() )
769 {
770 case IDM_TOOLBAR_SHOW_TEXT:
771 style |= wxTB_NOICONS | wxTB_TEXT;
772 break;
773
774 case IDM_TOOLBAR_SHOW_ICONS:
775 // nothing to do
776 break;
777
778 case IDM_TOOLBAR_SHOW_BOTH:
779 style |= wxTB_TEXT;
780 }
781
782 GetToolBar()->SetWindowStyle(style);
783 }
784
785 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
786 {
787 m_nPrint++;
788
789 wxToolBarBase *tb = GetToolBar();
790 tb->InsertTool(0, wxID_PRINT, _T("New print"),
791 wxBITMAP(print), wxNullBitmap,
792 wxITEM_NORMAL,
793 _T("Delete this tool"),
794 _T("This button was inserted into the toolbar"));
795
796 // must call Realize() after adding a new button
797 tb->Realize();
798 }
799
800 void MyFrame::OnToolEnter(wxCommandEvent& event)
801 {
802 #if wxUSE_STATUSBAR
803 if (event.GetSelection() > -1)
804 {
805 wxString str;
806 str.Printf(_T("This is tool number %d"), event.GetSelection());
807 SetStatusText(str);
808 }
809 else
810 SetStatusText(wxEmptyString);
811 #else
812 wxUnusedVar(event);
813 #endif // wxUSE_STATUSBAR
814 }
815
816 void MyFrame::OnToggleRadioBtn(wxCommandEvent& event)
817 {
818 if ( m_tbar )
819 {
820 m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 +
821 event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true);
822 }
823 }