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