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