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