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