]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/toolbar.cpp
Quick VA fix
[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".
ad9bb75f 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
VZ
37
38#if USE_GENERIC_TBAR
39 #if !wxUSE_TOOLBAR_SIMPLE
9fb35cf1
VZ
40 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
41 to 1 in setup.h and recompile the library.
ad4ae6ed
VZ
42 #else
43 #include <wx/tbarsmpl.h>
44 #endif
45#endif // USE_GENERIC_TBAR
46
ad9bb75f
VZ
47// ----------------------------------------------------------------------------
48// resources
49// ----------------------------------------------------------------------------
14d1ccd8 50
a4294b78 51#if defined(__WXGTK__) || defined(__WXMOTIF__)
ad9bb75f
VZ
52 #include "mondrian.xpm"
53 #include "bitmaps/new.xpm"
54 #include "bitmaps/open.xpm"
55 #include "bitmaps/save.xpm"
56 #include "bitmaps/copy.xpm"
57 #include "bitmaps/cut.xpm"
5ef2e633 58 #include "bitmaps/preview.xpm" // paste XPM
ad9bb75f 59 #include "bitmaps/print.xpm"
ad9bb75f
VZ
60 #include "bitmaps/help.xpm"
61#endif // GTK or Motif
62
63// ----------------------------------------------------------------------------
64// classes
65// ----------------------------------------------------------------------------
66
67// Define a new application
5ef2e633 68class MyApp : public wxApp
ad9bb75f
VZ
69{
70public:
71 bool OnInit();
ad9bb75f 72};
47908e25 73
ad9bb75f
VZ
74// Define a new frame
75class MyFrame: public wxFrame
76{
77public:
78 MyFrame(wxFrame *parent,
79 wxWindowID id = -1,
80 const wxString& title = "wxToolBar Sample",
81 const wxPoint& pos = wxDefaultPosition,
82 const wxSize& size = wxDefaultSize,
83 long style = wxDEFAULT_FRAME_STYLE);
14d1ccd8 84
5ef2e633
VZ
85 void RecreateToolbar();
86
ad9bb75f
VZ
87 void OnQuit(wxCommandEvent& event);
88 void OnAbout(wxCommandEvent& event);
89
5ef2e633
VZ
90 void OnToggleToolbarSize(wxCommandEvent& event);
91 void OnToggleToolbarOrient(wxCommandEvent& event);
98066234 92 void OnToggleToolbarRows(wxCommandEvent& event);
5ef2e633 93
ad4ae6ed
VZ
94 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
95 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
bdc72a22 96 void OnInsertPrint(wxCommandEvent& event);
ad4ae6ed 97 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
ad9bb75f 98
ad9bb75f
VZ
99 void OnToolLeftClick(wxCommandEvent& event);
100 void OnToolEnter(wxCommandEvent& event);
101
1c383dba
VZ
102 void OnCombo(wxCommandEvent& event);
103
5ef2e633
VZ
104 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
105
b487a08f
JS
106 void OnToggleFullScreen(wxCommandEvent& event);
107
ad4ae6ed
VZ
108#if USE_GENERIC_TBAR
109 virtual wxToolBar *OnCreateToolBar(long style,
110 wxWindowID id,
111 const wxString& name );
112#endif // USE_GENERIC_TBAR
113
ad9bb75f
VZ
114private:
115 void DoEnablePrint();
97d7bfb8 116 void DoDeletePrint();
ad9bb75f
VZ
117 void DoToggleHelp();
118
5ef2e633
VZ
119 bool m_smallToolbar,
120 m_horzToolbar;
98066234
VZ
121 size_t m_rows; // 1 or 2 only
122
ad9bb75f
VZ
123 wxTextCtrl* m_textWindow;
124
ad9bb75f
VZ
125 DECLARE_EVENT_TABLE()
126};
127
128// ----------------------------------------------------------------------------
129// constants
130// ----------------------------------------------------------------------------
131
132const int ID_TOOLBAR = 500;
133
134enum
14d1ccd8 135{
5ef2e633
VZ
136 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
137 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
98066234 138 IDM_TOOLBAR_TOGGLETOOLBARROWS,
ad9bb75f 139 IDM_TOOLBAR_ENABLEPRINT,
97d7bfb8 140 IDM_TOOLBAR_DELETEPRINT,
bdc72a22 141 IDM_TOOLBAR_INSERTPRINT,
1c383dba 142 IDM_TOOLBAR_TOGGLEHELP,
b487a08f 143 IDM_TOOLBAR_TOGGLEFULLSCREEN,
1c383dba
VZ
144
145 ID_COMBO = 1000
ad9bb75f 146};
14d1ccd8 147
ad9bb75f
VZ
148// ----------------------------------------------------------------------------
149// event tables
150// ----------------------------------------------------------------------------
14d1ccd8 151
ad9bb75f
VZ
152// Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
153// help button.
14d1ccd8 154
ad9bb75f
VZ
155BEGIN_EVENT_TABLE(MyFrame, wxFrame)
156 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
157 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
14d1ccd8 158
5ef2e633
VZ
159 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
160 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
98066234 161 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
5ef2e633 162
ad9bb75f 163 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
97d7bfb8 164 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
bdc72a22 165 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
ad9bb75f 166 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
b487a08f 167 EVT_MENU(IDM_TOOLBAR_TOGGLEFULLSCREEN, MyFrame::OnToggleFullScreen)
14d1ccd8 168
ad9bb75f 169 EVT_MENU(-1, MyFrame::OnToolLeftClick)
14d1ccd8 170
1c383dba
VZ
171 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
172
ad9bb75f 173 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
5ef2e633
VZ
174
175 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
176 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
ad9bb75f 177END_EVENT_TABLE()
14d1ccd8 178
ad9bb75f
VZ
179// ============================================================================
180// implementation
181// ============================================================================
14d1ccd8 182
ad9bb75f
VZ
183// ----------------------------------------------------------------------------
184// MyApp
185// ----------------------------------------------------------------------------
14d1ccd8 186
ad9bb75f 187IMPLEMENT_APP(MyApp)
14d1ccd8 188
ad9bb75f
VZ
189// The `main program' equivalent, creating the windows and returning the
190// main frame
191bool MyApp::OnInit()
192{
193 // Create the main frame window
194 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
195 "wxToolBar Sample",
196 wxPoint(100, 100), wxSize(450, 300));
14d1ccd8 197
7fb23305
VZ
198 frame->Show(TRUE);
199
200 frame->SetStatusText("Hello, wxWindows");
201
202 SetTopWindow(frame);
203
204 return TRUE;
14d1ccd8
JS
205}
206
5ef2e633 207void MyFrame::RecreateToolbar()
14d1ccd8 208{
5ef2e633 209 // delete and recreate the toolbar
ad4ae6ed 210 wxToolBarBase *toolBar = GetToolBar();
5ef2e633 211 delete toolBar;
14d1ccd8 212
5ef2e633
VZ
213 SetToolBar(NULL);
214
215 long style = wxNO_BORDER | wxTB_FLAT | wxTB_DOCKABLE;
216 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
217
218 toolBar = CreateToolBar(style, ID_TOOLBAR);
219 toolBar->SetMargins( 4, 4 );
220
221 // Set up toolbar
222 wxBitmap toolBarBitmaps[8];
223
224 toolBarBitmaps[0] = wxBITMAP(new);
225 toolBarBitmaps[1] = wxBITMAP(open);
40515a21
VZ
226 toolBarBitmaps[2] = wxBITMAP(save);
227 toolBarBitmaps[3] = wxBITMAP(copy);
228 toolBarBitmaps[4] = wxBITMAP(cut);
229 toolBarBitmaps[5] = wxBITMAP(paste);
230 toolBarBitmaps[6] = wxBITMAP(print);
231 toolBarBitmaps[7] = wxBITMAP(help);
232
5ef2e633
VZ
233 if ( !m_smallToolbar )
234 {
40515a21
VZ
235 int w = 2*toolBarBitmaps[0].GetWidth(),
236 h = 2*toolBarBitmaps[0].GetHeight();
237 for ( size_t n = 0; n < WXSIZEOF(toolBarBitmaps); n++ )
238 {
239 toolBarBitmaps[n] =
240 wxImage(toolBarBitmaps[n]).Scale(w, h).ConvertToBitmap();
241 }
242
243 toolBar->SetToolBitmapSize(wxSize(w, h));
5ef2e633 244 }
14d1ccd8
JS
245
246#ifdef __WXMSW__
5ef2e633 247 int width = 24;
14d1ccd8 248#else
5ef2e633 249 int width = 16;
14d1ccd8 250#endif
c8f1f088 251
5ef2e633 252 int currentX = 5;
7fb23305 253
5ef2e633
VZ
254 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
255 currentX += width + 5;
256 toolBar->AddTool(wxID_OPEN, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
14d1ccd8 257
ad4ae6ed
VZ
258 // neither the generic nor Motif native toolbars really support this
259#if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
5ef2e633
VZ
260 // adding a combo to a vertical toolbar is not very smart
261 if ( m_horzToolbar )
262 {
263 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO);
264 combo->Append("This");
265 combo->Append("is a");
266 combo->Append("combobox");
267 combo->Append("in a");
268 combo->Append("toolbar");
269 toolBar->AddControl(combo);
270 }
ad4ae6ed 271#endif // toolbars which don't support controls
14d1ccd8 272
40515a21
VZ
273 currentX += width + 5;
274 toolBar->AddTool(wxID_SAVE, toolBarBitmaps[2], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
275 currentX += width + 5;
276 toolBar->AddTool(wxID_COPY, toolBarBitmaps[3], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
277 currentX += width + 5;
278 toolBar->AddTool(wxID_CUT, toolBarBitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
279 currentX += width + 5;
280 toolBar->AddTool(wxID_PASTE, toolBarBitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
281 currentX += width + 5;
282 toolBar->AddTool(wxID_PRINT, toolBarBitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool");
283 currentX += width + 5;
284 toolBar->AddSeparator();
285 toolBar->AddTool(wxID_HELP, toolBarBitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
13437238 286
5ef2e633
VZ
287 // after adding the buttons to the toolbar, must call Realize() to reflect
288 // the changes
289 toolBar->Realize();
98066234
VZ
290
291 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
14d1ccd8
JS
292}
293
ad9bb75f
VZ
294// ----------------------------------------------------------------------------
295// MyFrame
296// ----------------------------------------------------------------------------
13437238
JS
297
298// Define my frame constructor
7fb23305
VZ
299MyFrame::MyFrame(wxFrame* parent,
300 wxWindowID id,
301 const wxString& title,
302 const wxPoint& pos,
303 const wxSize& size,
304 long style)
305 : wxFrame(parent, id, title, pos, size, style)
14d1ccd8 306{
e179bd65 307 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
98066234 308
40515a21 309 m_smallToolbar = TRUE;
98066234
VZ
310 m_horzToolbar = TRUE;
311 m_rows = 1;
ad9bb75f
VZ
312
313 // Give it a status line
314 CreateStatusBar();
315
316 // Give it an icon
317 SetIcon(wxICON(mondrian));
318
319 // Make a menubar
320 wxMenu *tbarMenu = new wxMenu;
5ef2e633
VZ
321 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
322 "&Toggle toolbar size\tCtrl-S",
323 "Toggle between big/small toolbar",
324 TRUE);
325 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
326 "Toggle toolbar &orientation\tCtrl-O",
327 "Toggle toolbar orientation",
328 TRUE);
98066234
VZ
329 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
330 "Toggle number of &rows\tCtrl-R",
331 "Toggle number of toolbar rows between 1 and 2",
332 TRUE);
5ef2e633
VZ
333
334 tbarMenu->AppendSeparator();
335
fc6bbc6d
VZ
336 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
337 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
338 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
339 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
ad9bb75f 340
b487a08f
JS
341#ifdef __WXMSW__
342 tbarMenu->AppendSeparator();
343 tbarMenu->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN, "Toggle &full screen mode\tCtrl-F", "");
344#endif
345
ad9bb75f
VZ
346 wxMenu *fileMenu = new wxMenu;
347 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
348
ad9bb75f
VZ
349 wxMenu *helpMenu = new wxMenu;
350 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
351
352 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
353
354 menuBar->Append(fileMenu, "&File");
355 menuBar->Append(tbarMenu, "&Toolbar");
ad9bb75f
VZ
356 menuBar->Append(helpMenu, "&Help");
357
358 // Associate the menu bar with the frame
359 SetMenuBar(menuBar);
360
361 // Create the toolbar
5ef2e633
VZ
362 RecreateToolbar();
363}
ad9bb75f 364
ad4ae6ed
VZ
365#if USE_GENERIC_TBAR
366
367wxToolBar* MyFrame::OnCreateToolBar(long style,
368 wxWindowID id,
369 const wxString& name)
370{
371 return (wxToolBar *)new wxToolBarSimple(this, id,
372 wxDefaultPosition, wxDefaultSize,
373 style, name);
374}
375
376#endif // USE_GENERIC_TBAR
377
5ef2e633
VZ
378void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
379{
380 m_smallToolbar = !m_smallToolbar;
ad9bb75f 381
5ef2e633 382 RecreateToolbar();
7fb23305
VZ
383}
384
98066234
VZ
385void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
386{
387 // m_rows may be only 1 or 2
388 m_rows = 3 - m_rows;
389
390 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
391
9fb35cf1 392 //RecreateToolbar(); -- this is unneeded
98066234
VZ
393}
394
5ef2e633 395void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
7fb23305 396{
5ef2e633 397 m_horzToolbar = !m_horzToolbar;
7fb23305 398
5ef2e633 399 RecreateToolbar();
14d1ccd8
JS
400}
401
47908e25 402void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 403{
13437238 404 Close(TRUE);
14d1ccd8
JS
405}
406
47908e25 407void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 408{
1d5b7a0b 409 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 410}
14d1ccd8 411
13437238
JS
412void MyFrame::OnToolLeftClick(wxCommandEvent& event)
413{
e179bd65
RR
414 wxString str;
415 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
416 m_textWindow->WriteText( str );
ad9bb75f 417
e179bd65
RR
418 if (event.GetId() == wxID_HELP)
419 {
ad9bb75f 420 if ( event.GetExtraLong() != 0 )
e179bd65
RR
421 m_textWindow->WriteText( _T("Help button down now.\n") );
422 else
423 m_textWindow->WriteText( _T("Help button up now.\n") );
424 }
ad9bb75f 425
e179bd65
RR
426 if (event.GetId() == wxID_COPY)
427 {
7fb23305 428 DoEnablePrint();
e179bd65 429 }
ad9bb75f 430
e179bd65
RR
431 if (event.GetId() == wxID_CUT)
432 {
7fb23305 433 DoToggleHelp();
e179bd65 434 }
1c4f8f8d 435
97d7bfb8
RR
436 if (event.GetId() == wxID_PRINT)
437 {
438 DoDeletePrint();
439 }
14d1ccd8
JS
440}
441
1c383dba
VZ
442void MyFrame::OnCombo(wxCommandEvent& event)
443{
444 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
445}
446
7fb23305
VZ
447void MyFrame::DoEnablePrint()
448{
ad4ae6ed 449 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
450 if (tb->GetToolEnabled(wxID_PRINT))
451 tb->EnableTool( wxID_PRINT, FALSE );
452 else
453 tb->EnableTool( wxID_PRINT, TRUE );
454}
455
97d7bfb8
RR
456void MyFrame::DoDeletePrint()
457{
ad4ae6ed 458 wxToolBarBase *tb = GetToolBar();
1c4f8f8d 459
97d7bfb8
RR
460 tb->DeleteTool( wxID_PRINT );
461}
462
7fb23305
VZ
463void MyFrame::DoToggleHelp()
464{
ad4ae6ed 465 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
466 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
467}
468
5ef2e633
VZ
469void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
470{
471 event.Enable( m_textWindow->CanCopy() );
472}
473
bdc72a22
VZ
474void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
475{
5ef2e633 476 wxBitmap bmp = wxBITMAP(print);
bdc72a22 477
fc6bbc6d 478 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
5ef2e633
VZ
479 FALSE, (wxObject *) NULL,
480 "Delete this tool",
481 "This button was inserted into the toolbar");
bdc72a22
VZ
482
483 GetToolBar()->Realize();
484}
485
13437238
JS
486void MyFrame::OnToolEnter(wxCommandEvent& event)
487{
ad4ae6ed
VZ
488 if (event.GetSelection() > -1)
489 {
490 wxString str;
491 str.Printf(_T("This is tool number %d"), event.GetSelection());
492 SetStatusText(str);
493 }
494 else
495 SetStatusText("");
13437238 496}
14d1ccd8 497
b487a08f
JS
498void MyFrame::OnToggleFullScreen(wxCommandEvent& event)
499{
500#ifdef __WXMSW__
501 ShowFullScreen(!IsFullScreen());
502#endif
503}
504