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