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