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