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