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