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