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