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