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