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