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