]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/toolbar.cpp
Add support for stretchable spaces to wxToolBar.
[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(650, 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
478 toolBar->AddSeparator();
479 toolBar->AddTool(wxID_COPY, wxT("Copy"), toolBarBitmaps[Tool_copy], wxT("Toggle button 2"), wxITEM_CHECK);
480 toolBar->AddTool(wxID_CUT, wxT("Cut"), toolBarBitmaps[Tool_cut], wxT("Toggle/Untoggle help button"));
481 toolBar->AddTool(wxID_PASTE, wxT("Paste"), toolBarBitmaps[Tool_paste], wxT("Paste"));
482 toolBar->AddSeparator();
483
484 if ( m_useCustomDisabled )
485 {
486 wxBitmap bmpDisabled(w, h);
487 {
488 wxMemoryDC dc;
489 dc.SelectObject(bmpDisabled);
490 dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0);
491
492 wxPen pen(*wxRED, 5);
493 dc.SetPen(pen);
494 dc.DrawLine(0, 0, w, h);
495 }
496
497 toolBar->AddTool(wxID_PRINT, wxT("Print"), toolBarBitmaps[Tool_print],
498 bmpDisabled);
499 }
500 else
501 {
502 toolBar->AddTool(wxID_PRINT, wxT("Print"), toolBarBitmaps[Tool_print],
503 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."));
504 }
505
506 // add a stretchable space before the "Help" button to make it
507 // right-aligned
508 toolBar->AddStretchableSpace();
509 toolBar->AddTool(wxID_HELP, wxT("Help"), toolBarBitmaps[Tool_help], wxT("Help button"), wxITEM_CHECK);
510
511 if ( !m_pathBmp.empty() )
512 {
513 // create a tool with a custom bitmap for testing
514 wxImage img(m_pathBmp);
515 if ( img.Ok() )
516 {
517 if ( img.GetWidth() > w && img.GetHeight() > h )
518 img = img.GetSubImage(wxRect(0, 0, w, h));
519
520 toolBar->AddSeparator();
521 toolBar->AddTool(wxID_ANY, wxT("Custom"), img);
522 }
523 }
524
525 // after adding the buttons to the toolbar, must call Realize() to reflect
526 // the changes
527 toolBar->Realize();
528
529 toolBar->SetRows(!(toolBar->IsVertical()) ? m_rows : 10 / m_rows);
530 }
531
532 // ----------------------------------------------------------------------------
533 // MyFrame
534 // ----------------------------------------------------------------------------
535
536 // Define my frame constructor
537 MyFrame::MyFrame(wxFrame* parent,
538 wxWindowID id,
539 const wxString& title,
540 const wxPoint& pos,
541 const wxSize& size,
542 long style)
543 : wxFrame(parent, id, title, pos, size, style)
544 {
545 m_tbar = NULL;
546
547 m_smallToolbar = true;
548 m_horzText = false;
549 m_useCustomDisabled = false;
550 m_showTooltips = true;
551 m_searchTool = NULL;
552
553 m_rows = 1;
554 m_nPrint = 1;
555
556 #if wxUSE_STATUSBAR
557 // Give it a status line
558 CreateStatusBar();
559 #endif
560
561 // Give it an icon
562 SetIcon(wxICON(mondrian));
563
564 // Make a menubar
565 wxMenu *tbarMenu = new wxMenu;
566 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR,
567 wxT("Toggle &toolbar\tCtrl-Z"),
568 wxT("Show or hide the toolbar"));
569
570 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
571 wxT("Toggle &another toolbar\tCtrl-A"),
572 wxT("Show/hide another test toolbar"));
573
574 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT,
575 wxT("Toggle hori&zontal text\tCtrl-H"),
576 wxT("Show text under/alongside the icon"));
577
578 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
579 wxT("&Toggle toolbar size\tCtrl-S"),
580 wxT("Toggle between big/small toolbar"));
581
582 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS,
583 wxT("Toggle number of &rows\tCtrl-R"),
584 wxT("Toggle number of toolbar rows between 1 and 2"));
585
586 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS,
587 wxT("Show &tooltips\tCtrl-L"),
588 wxT("Show tooltips for the toolbar tools"));
589
590 tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED,
591 wxT("Use c&ustom disabled images\tCtrl-U"),
592 wxT("Switch between using system-generated and custom disabled images"));
593
594
595 tbarMenu->AppendSeparator();
596 tbarMenu->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION,
597 wxT("Set toolbar at the top of the window"),
598 wxT("Set toolbar at the top of the window"));
599 tbarMenu->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION,
600 wxT("Set toolbar at the left of the window"),
601 wxT("Set toolbar at the left of the window"));
602 tbarMenu->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION,
603 wxT("Set toolbar at the bottom of the window"),
604 wxT("Set toolbar at the bottom of the window"));
605 tbarMenu->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION,
606 wxT("Set toolbar at the right edge of the window"),
607 wxT("Set toolbar at the right edge of the window"));
608 tbarMenu->AppendSeparator();
609
610 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, wxT("Show &text\tCtrl-Alt-T"));
611 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, wxT("Show &icons\tCtrl-Alt-I"));
612 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, wxT("Show &both\tCtrl-Alt-B"));
613 tbarMenu->AppendSeparator();
614 tbarMenu->Append(IDM_TOOLBAR_BG_COL, wxT("Choose bac&kground colour..."));
615 tbarMenu->AppendCheckItem(IDM_TOOLBAR_CUSTOM_BG, wxT("Draw custom back&ground"));
616 tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, wxT("Custom &bitmap...\tCtrl-B"));
617
618 wxMenu *toolMenu = new wxMenu;
619 toolMenu->Append(IDM_TOOLBAR_ENABLEPRINT, wxT("&Enable print button\tCtrl-E"));
620 toolMenu->Append(IDM_TOOLBAR_DELETEPRINT, wxT("&Delete print button\tCtrl-D"));
621 toolMenu->Append(IDM_TOOLBAR_INSERTPRINT, wxT("&Insert print button\tCtrl-I"));
622 toolMenu->Append(IDM_TOOLBAR_TOGGLEHELP, wxT("Toggle &help button\tCtrl-T"));
623 toolMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLESEARCH, wxT("Toggle &search field\tCtrl-F"));
624 toolMenu->AppendSeparator();
625 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, wxT("Toggle &1st radio button\tCtrl-1"));
626 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, wxT("Toggle &2nd radio button\tCtrl-2"));
627 toolMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, wxT("Toggle &3rd radio button\tCtrl-3"));
628 toolMenu->AppendSeparator();
629 toolMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, wxT("Change tooltip of \"New\""));
630
631 wxMenu *fileMenu = new wxMenu;
632 fileMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Quit toolbar sample") );
633
634 wxMenu *helpMenu = new wxMenu;
635 helpMenu->Append(wxID_HELP, wxT("&About"), wxT("About toolbar sample"));
636
637 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
638
639 menuBar->Append(fileMenu, wxT("&File"));
640 menuBar->Append(tbarMenu, wxT("&Toolbar"));
641 menuBar->Append(toolMenu, wxT("Tool&s"));
642 menuBar->Append(helpMenu, wxT("&Help"));
643
644 // Associate the menu bar with the frame
645 SetMenuBar(menuBar);
646
647 menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true);
648 menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true);
649
650 menuBar->Check(IDM_TOOLBAR_TOP_ORIENTATION, true );
651 m_toolbarPosition = TOOLBAR_TOP;
652
653 // Create the toolbar
654 RecreateToolbar();
655
656 m_panel = new wxPanel(this, wxID_ANY);
657 #if USE_UNMANAGED_TOOLBAR
658 m_extraToolBar = new wxToolBar(m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_TEXT|wxTB_FLAT|wxTB_TOP);
659 PopulateToolbar(m_extraToolBar);
660 #endif
661
662 m_textWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
663
664 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
665 m_panel->SetSizer(sizer);
666 #if USE_UNMANAGED_TOOLBAR
667 if (m_extraToolBar)
668 sizer->Add(m_extraToolBar, 0, wxEXPAND, 0);
669 #endif
670 sizer->Add(m_textWindow, 1, wxEXPAND, 0);
671 }
672
673 MyFrame::~MyFrame()
674 {
675 if ( m_searchTool && !m_searchTool->GetToolBar() )
676 {
677 // we currently can't delete a toolbar tool ourselves, so we have to
678 // attach it to the toolbar just for it to be deleted, this is pretty
679 // ugly and will need to be changed
680 GetToolBar()->AddTool(m_searchTool);
681 }
682 }
683
684 void MyFrame::LayoutChildren()
685 {
686 wxSize size = GetClientSize();
687
688 int offset;
689 if ( m_tbar )
690 {
691 m_tbar->SetSize(0, 0, wxDefaultCoord, size.y);
692
693 offset = m_tbar->GetSize().x;
694 }
695 else
696 {
697 offset = 0;
698 }
699
700 m_panel->SetSize(offset, 0, size.x - offset, size.y);
701 }
702
703 void MyFrame::OnSize(wxSizeEvent& event)
704 {
705 if ( m_tbar )
706 {
707 LayoutChildren();
708 }
709 else
710 {
711 event.Skip();
712 }
713 }
714
715 void MyFrame::OnEraseToolBarBackground(wxEraseEvent& event)
716 {
717 wxDC& dc = *event.GetDC();
718 const wxSize size = dc.GetSize();
719 dc.SetPen(*wxRED_PEN);
720 dc.DrawLine(0, 0, size.x, size.y);
721 dc.DrawLine(0, size.y, size.x, 0);
722 }
723
724 void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
725 {
726 wxToolBar *tbar = GetToolBar();
727
728 if ( !tbar )
729 {
730 RecreateToolbar();
731 }
732 else
733 {
734 delete tbar;
735
736 SetToolBar(NULL);
737 }
738 }
739
740 void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event))
741 {
742 m_horzText = !m_horzText;
743
744 RecreateToolbar();
745 }
746
747 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
748 {
749 if ( m_tbar )
750 {
751 delete m_tbar;
752 m_tbar = NULL;
753 }
754 else
755 {
756 long style = GetToolBar() ? GetToolBar()->GetWindowStyle()
757 : TOOLBAR_STYLE;
758 style &= ~wxTB_HORIZONTAL;
759 style |= wxTB_VERTICAL;
760
761 m_tbar = new wxToolBar(this, wxID_ANY,
762 wxDefaultPosition, wxDefaultSize,
763 style);
764
765 m_tbar->SetMargins(4, 4);
766
767 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, wxT("First"), wxBITMAP(new));
768 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, wxT("Second"), wxBITMAP(open));
769 m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, wxT("Third"), wxBITMAP(save));
770 m_tbar->AddSeparator();
771 m_tbar->AddTool(wxID_HELP, wxT("Help"), wxBITMAP(help));
772
773 m_tbar->Realize();
774 }
775
776 LayoutChildren();
777 }
778
779 void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
780 {
781 m_smallToolbar = !m_smallToolbar;
782
783 RecreateToolbar();
784 }
785
786 void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
787 {
788 // m_rows may be only 1 or 2
789 m_rows = 3 - m_rows;
790
791 GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows : 10 / m_rows);
792
793 //RecreateToolbar(); -- this is unneeded
794 }
795
796 void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event))
797 {
798 m_showTooltips = !m_showTooltips;
799
800 RecreateToolbar();
801 }
802
803 void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event))
804 {
805 m_useCustomDisabled = !m_useCustomDisabled;
806
807 RecreateToolbar();
808 }
809
810 void MyFrame::OnChangeOrientation(wxCommandEvent& event)
811 {
812 switch( event.GetId() )
813 {
814 case IDM_TOOLBAR_LEFT_ORIENTATION:
815 m_toolbarPosition = TOOLBAR_LEFT;
816 break;
817 case IDM_TOOLBAR_TOP_ORIENTATION:
818 m_toolbarPosition = TOOLBAR_TOP;
819 break;
820 case IDM_TOOLBAR_RIGHT_ORIENTATION:
821 m_toolbarPosition = TOOLBAR_RIGHT;
822 break;
823 case IDM_TOOLBAR_BOTTOM_ORIENTATION:
824 m_toolbarPosition = TOOLBAR_BOTTOM;
825 break;
826 }
827 RecreateToolbar();
828 }
829
830 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
831 {
832 Close(true);
833 }
834
835 void MyFrame::OnAbout(wxCommandEvent& event)
836 {
837 if ( event.IsChecked() )
838 m_textWindow->WriteText( wxT("Help button down now.\n") );
839 else
840 m_textWindow->WriteText( wxT("Help button up now.\n") );
841
842 (void)wxMessageBox(wxT("wxWidgets toolbar sample"), wxT("About wxToolBar"));
843 }
844
845 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
846 {
847 wxString str;
848 str.Printf( wxT("Clicked on tool %d\n"), event.GetId());
849 m_textWindow->WriteText( str );
850
851 if (event.GetId() == wxID_COPY)
852 {
853 DoEnablePrint();
854 }
855
856 if (event.GetId() == wxID_CUT)
857 {
858 DoToggleHelp();
859 }
860
861 if (event.GetId() == wxID_PRINT)
862 {
863 DoDeletePrint();
864 }
865 }
866
867 void MyFrame::OnToolRightClick(wxCommandEvent& event)
868 {
869 m_textWindow->AppendText(
870 wxString::Format(wxT("Tool %d right clicked.\n"),
871 (int) event.GetInt()));
872 }
873
874 void MyFrame::OnCombo(wxCommandEvent& event)
875 {
876 wxLogStatus(wxT("Combobox string '%s' selected"), event.GetString().c_str());
877 }
878
879 void MyFrame::DoEnablePrint()
880 {
881 if ( !m_nPrint )
882 return;
883
884 wxToolBarBase *tb = GetToolBar();
885 tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT));
886 }
887
888 void MyFrame::DoDeletePrint()
889 {
890 if ( !m_nPrint )
891 return;
892
893 wxToolBarBase *tb = GetToolBar();
894 tb->DeleteTool( wxID_PRINT );
895
896 m_nPrint--;
897 }
898
899 void MyFrame::DoToggleHelp()
900 {
901 wxToolBarBase *tb = GetToolBar();
902 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
903 }
904
905 void MyFrame::OnToggleSearch(wxCommandEvent& WXUNUSED(event))
906 {
907 wxToolBarBase * const tb = GetToolBar();
908 if ( !m_searchTool )
909 {
910 wxSearchCtrl * const srch = new wxSearchCtrl(tb, wxID_ANY, "needle");
911 srch->SetMinSize(wxSize(80, -1));
912 m_searchTool = tb->AddControl(srch);
913 }
914 else // tool already exists
915 {
916 wxControl * const win = m_searchTool->GetControl();
917 if ( m_searchTool->GetToolBar() )
918 {
919 // attached now, remove it
920 win->Hide();
921 tb->RemoveTool(m_searchTool->GetId());
922 }
923 else // tool exists in detached state, attach it back
924 {
925 tb->AddTool(m_searchTool);
926 win->Show();
927 }
928 }
929
930 tb->Realize();
931 }
932
933 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
934 {
935 event.Enable( m_textWindow->CanCopy() );
936 }
937
938 void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event)
939 {
940 wxToolBar *tbar = GetToolBar();
941 event.Enable( tbar &&
942 tbar->HasFlag(wxTB_TEXT) &&
943 !tbar->HasFlag(wxTB_NOICONS) );
944 }
945
946 void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event))
947 {
948 GetToolBar()->SetToolShortHelp(wxID_NEW, wxT("New toolbar button"));
949 }
950
951 void MyFrame::OnToolbarStyle(wxCommandEvent& event)
952 {
953 long style = GetToolBar()->GetWindowStyle();
954 style &= ~(wxTB_NOICONS | wxTB_TEXT);
955
956 switch ( event.GetId() )
957 {
958 case IDM_TOOLBAR_SHOW_TEXT:
959 style |= wxTB_NOICONS | wxTB_TEXT;
960 break;
961
962 case IDM_TOOLBAR_SHOW_ICONS:
963 // nothing to do
964 break;
965
966 case IDM_TOOLBAR_SHOW_BOTH:
967 style |= wxTB_TEXT;
968 }
969
970 GetToolBar()->SetWindowStyle(style);
971 }
972
973 void MyFrame::OnToolbarBgCol(wxCommandEvent& WXUNUSED(event))
974 {
975 wxColour col = wxGetColourFromUser
976 (
977 this,
978 GetToolBar()->GetBackgroundColour(),
979 "Toolbar background colour"
980 );
981 if ( col.IsOk() )
982 {
983 GetToolBar()->SetBackgroundColour(col);
984 GetToolBar()->Refresh();
985 }
986 }
987
988 void MyFrame::OnToolbarCustomBg(wxCommandEvent& event)
989 {
990 wxToolBarBase *tb = GetToolBar();
991
992 if ( event.IsChecked() )
993 {
994 tb->Connect
995 (
996 wxEVT_ERASE_BACKGROUND,
997 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground),
998 NULL,
999 this
1000 );
1001 }
1002 else
1003 {
1004 tb->Disconnect
1005 (
1006 wxEVT_ERASE_BACKGROUND,
1007 wxEraseEventHandler(MyFrame::OnEraseToolBarBackground),
1008 NULL,
1009 this
1010 );
1011 }
1012
1013 tb->Refresh();
1014 }
1015
1016 void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event))
1017 {
1018 m_pathBmp = wxLoadFileSelector("custom bitmap", "");
1019
1020 RecreateToolbar();
1021 }
1022
1023 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
1024 {
1025 m_nPrint++;
1026
1027 wxToolBarBase *tb = GetToolBar();
1028 tb->InsertTool(0, wxID_PRINT, wxT("New print"),
1029 wxBITMAP(print), wxNullBitmap,
1030 wxITEM_NORMAL,
1031 wxT("Delete this tool"),
1032 wxT("This button was inserted into the toolbar"));
1033
1034 // must call Realize() after adding a new button
1035 tb->Realize();
1036 }
1037
1038 void MyFrame::OnToggleRadioBtn(wxCommandEvent& event)
1039 {
1040 if ( m_tbar )
1041 {
1042 m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 +
1043 event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true);
1044 }
1045 }
1046
1047 void MyFrame::OnToolDropdown(wxCommandEvent& event)
1048 {
1049 wxString str;
1050 str.Printf( wxT("Dropdown on tool %d\n"), event.GetId());
1051 m_textWindow->WriteText( str );
1052
1053 event.Skip();
1054 }