tests for the new toolbar features (icons text and the radio buttons)
[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 = 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_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(450, 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 // Create the toolbar
401 RecreateToolbar();
402 }
403
404 #if USE_GENERIC_TBAR
405
406 wxToolBar* MyFrame::OnCreateToolBar(long style,
407 wxWindowID id,
408 const wxString& name)
409 {
410 return (wxToolBar *)new wxToolBarSimple(this, id,
411 wxDefaultPosition, wxDefaultSize,
412 style, name);
413 }
414
415 #endif // USE_GENERIC_TBAR
416
417 void MyFrame::LayoutChildren()
418 {
419 wxSize size = GetClientSize();
420
421 int offset;
422 if ( m_tbar )
423 {
424 m_tbar->SetSize(-1, size.y);
425 m_tbar->Move(0, 0);
426
427 offset = m_tbar->GetSize().x;
428 }
429 else
430 {
431 offset = 0;
432 }
433
434 m_textWindow->SetSize(offset, 0, size.x - offset, size.y);
435 }
436
437 void MyFrame::OnSize(wxSizeEvent& event)
438 {
439 if ( m_tbar )
440 {
441 LayoutChildren();
442 }
443 else
444 {
445 event.Skip();
446 }
447 }
448
449 void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
450 {
451 if ( m_tbar )
452 {
453 delete m_tbar;
454 m_tbar = NULL;
455 }
456 else
457 {
458 long style = GetToolBar()->GetWindowStyle();
459 style &= ~wxTB_HORIZONTAL;
460 style |= wxTB_VERTICAL;
461
462 m_tbar = new wxToolBar(this, -1,
463 wxDefaultPosition, wxDefaultSize,
464 style);
465
466 m_tbar->AddRadioTool(wxID_NEW, _T("First"), wxBITMAP(new));
467 m_tbar->AddRadioTool(wxID_NEW, _T("Second"), wxBITMAP(new));
468 m_tbar->AddRadioTool(wxID_NEW, _T("Third"), wxBITMAP(new));
469 m_tbar->AddSeparator();
470 m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help));
471
472 m_tbar->Realize();
473 }
474
475 LayoutChildren();
476 }
477
478 void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
479 {
480 m_smallToolbar = !m_smallToolbar;
481
482 RecreateToolbar();
483 }
484
485 void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
486 {
487 // m_rows may be only 1 or 2
488 m_rows = 3 - m_rows;
489
490 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
491
492 //RecreateToolbar(); -- this is unneeded
493 }
494
495 void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
496 {
497 m_horzToolbar = !m_horzToolbar;
498
499 RecreateToolbar();
500 }
501
502 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
503 {
504 Close(TRUE);
505 }
506
507 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
508 {
509 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
510 }
511
512 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
513 {
514 wxString str;
515 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
516 m_textWindow->WriteText( str );
517
518 if (event.GetId() == wxID_HELP)
519 {
520 if ( event.GetExtraLong() != 0 )
521 m_textWindow->WriteText( _T("Help button down now.\n") );
522 else
523 m_textWindow->WriteText( _T("Help button up now.\n") );
524 }
525
526 if (event.GetId() == wxID_COPY)
527 {
528 DoEnablePrint();
529 }
530
531 if (event.GetId() == wxID_CUT)
532 {
533 DoToggleHelp();
534 }
535
536 if (event.GetId() == wxID_PRINT)
537 {
538 DoDeletePrint();
539 }
540 }
541
542 void MyFrame::OnCombo(wxCommandEvent& event)
543 {
544 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
545 }
546
547 void MyFrame::DoEnablePrint()
548 {
549 wxToolBarBase *tb = GetToolBar();
550 if (tb->GetToolEnabled(wxID_PRINT))
551 tb->EnableTool( wxID_PRINT, FALSE );
552 else
553 tb->EnableTool( wxID_PRINT, TRUE );
554 }
555
556 void MyFrame::DoDeletePrint()
557 {
558 wxToolBarBase *tb = GetToolBar();
559
560 tb->DeleteTool( wxID_PRINT );
561 }
562
563 void MyFrame::DoToggleHelp()
564 {
565 wxToolBarBase *tb = GetToolBar();
566 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
567 }
568
569 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
570 {
571 event.Enable( m_textWindow->CanCopy() );
572 }
573
574 void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event))
575 {
576 GetToolBar()->SetToolShortHelp(wxID_NEW, _T("New toolbar button"));
577 }
578
579 void MyFrame::OnToolbarStyle(wxCommandEvent& event)
580 {
581 long style = GetToolBar()->GetWindowStyle();
582 style &= ~(wxTB_NOICONS | wxTB_TEXT);
583
584 switch ( event.GetId() )
585 {
586 case IDM_TOOLBAR_SHOW_TEXT:
587 style |= wxTB_NOICONS | wxTB_TEXT;
588 break;
589
590 case IDM_TOOLBAR_SHOW_ICONS:
591 // nothing to do
592 break;
593
594 case IDM_TOOLBAR_SHOW_BOTH:
595 style |= wxTB_TEXT;
596 }
597
598 GetToolBar()->SetWindowStyle(style);
599 }
600
601 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
602 {
603 wxBitmap bmp = wxBITMAP(print);
604
605 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
606 FALSE, (wxObject *) NULL,
607 "Delete this tool",
608 "This button was inserted into the toolbar");
609
610 GetToolBar()->Realize();
611 }
612
613 void MyFrame::OnToolEnter(wxCommandEvent& event)
614 {
615 if (event.GetSelection() > -1)
616 {
617 wxString str;
618 str.Printf(_T("This is tool number %d"), event.GetSelection());
619 SetStatusText(str);
620 }
621 else
622 SetStatusText("");
623 }
624