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