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