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