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