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