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