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