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