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