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