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