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