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