comment added to toolbar to say using BMPs uses less pace than XPMs
[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 // BMPs use less space, but aren't compiled into the executable on other platforms
41 #ifdef __WXMSW__
42 #define USE_XPM_BITMAPS 0
43 #else
44 #define USE_XPM_BITMAPS 1
45 #endif
46
47 #if USE_GENERIC_TBAR
48 #if !wxUSE_TOOLBAR_SIMPLE
49 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
50 to 1 in setup.h and recompile the library.
51 #else
52 #include <wx/tbarsmpl.h>
53 #endif
54 #endif // USE_GENERIC_TBAR
55
56 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
57 #error You need to enable XPM support to use XPM bitmaps with toolbar!
58 #endif // USE_XPM_BITMAPS
59
60 // ----------------------------------------------------------------------------
61 // resources
62 // ----------------------------------------------------------------------------
63
64 #if USE_XPM_BITMAPS
65 #include "mondrian.xpm"
66 #include "bitmaps/new.xpm"
67 #include "bitmaps/open.xpm"
68 #include "bitmaps/save.xpm"
69 #include "bitmaps/copy.xpm"
70 #include "bitmaps/cut.xpm"
71 #include "bitmaps/preview.xpm" // paste XPM
72 #include "bitmaps/print.xpm"
73 #include "bitmaps/help.xpm"
74 #endif // USE_XPM_BITMAPS
75
76 // ----------------------------------------------------------------------------
77 // classes
78 // ----------------------------------------------------------------------------
79
80 // Define a new application
81 class MyApp : public wxApp
82 {
83 public:
84 bool OnInit();
85 };
86
87 // Define a new frame
88 class MyFrame: public wxFrame
89 {
90 public:
91 MyFrame(wxFrame *parent,
92 wxWindowID id = -1,
93 const wxString& title = "wxToolBar Sample",
94 const wxPoint& pos = wxDefaultPosition,
95 const wxSize& size = wxDefaultSize,
96 long style = wxDEFAULT_FRAME_STYLE);
97
98 void RecreateToolbar();
99
100 void OnQuit(wxCommandEvent& event);
101 void OnAbout(wxCommandEvent& event);
102
103 void OnSize(wxSizeEvent& event);
104
105 void OnToggleAnotherToolbar(wxCommandEvent& event);
106
107 void OnToggleToolbarSize(wxCommandEvent& event);
108 void OnToggleToolbarOrient(wxCommandEvent& event);
109 void OnToggleToolbarRows(wxCommandEvent& event);
110
111 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
112 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
113 void OnInsertPrint(wxCommandEvent& event);
114 void OnChangeToolTip(wxCommandEvent& event);
115 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
116
117 void OnToolbarStyle(wxCommandEvent& event);
118
119 void OnToolLeftClick(wxCommandEvent& event);
120 void OnToolEnter(wxCommandEvent& event);
121
122 void OnCombo(wxCommandEvent& event);
123
124 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
125
126 #if USE_GENERIC_TBAR
127 virtual wxToolBar *OnCreateToolBar(long style,
128 wxWindowID id,
129 const wxString& name );
130 #endif // USE_GENERIC_TBAR
131
132 private:
133 void DoEnablePrint();
134 void DoDeletePrint();
135 void DoToggleHelp();
136
137 void LayoutChildren();
138
139 bool m_smallToolbar,
140 m_horzToolbar;
141 size_t m_rows; // 1 or 2 only
142
143 wxTextCtrl *m_textWindow;
144
145 wxToolBar *m_tbar;
146
147 DECLARE_EVENT_TABLE()
148 };
149
150 // ----------------------------------------------------------------------------
151 // constants
152 // ----------------------------------------------------------------------------
153
154 const int ID_TOOLBAR = 500;
155
156 static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT ;
157 // static const long TOOLBAR_STYLE = 0;
158
159 enum
160 {
161 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
162 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
163 IDM_TOOLBAR_TOGGLETOOLBARROWS,
164 IDM_TOOLBAR_ENABLEPRINT,
165 IDM_TOOLBAR_DELETEPRINT,
166 IDM_TOOLBAR_INSERTPRINT,
167 IDM_TOOLBAR_TOGGLEHELP,
168 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
169 IDM_TOOLBAR_CHANGE_TOOLTIP,
170 IDM_TOOLBAR_SHOW_TEXT,
171 IDM_TOOLBAR_SHOW_ICONS,
172 IDM_TOOLBAR_SHOW_BOTH,
173
174 ID_COMBO = 1000
175 };
176
177 // ----------------------------------------------------------------------------
178 // event tables
179 // ----------------------------------------------------------------------------
180
181 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
182 // help button.
183
184 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
185 EVT_SIZE(MyFrame::OnSize)
186
187 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
188 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
189
190 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar)
191
192 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
193 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
194 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
195
196 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
197 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
198 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
199 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
200 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip)
201
202 EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH,
203 MyFrame::OnToolbarStyle)
204
205 EVT_MENU(-1, MyFrame::OnToolLeftClick)
206
207 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
208
209 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
210
211 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
212 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
213 END_EVENT_TABLE()
214
215 // ============================================================================
216 // implementation
217 // ============================================================================
218
219 // ----------------------------------------------------------------------------
220 // MyApp
221 // ----------------------------------------------------------------------------
222
223 IMPLEMENT_APP(MyApp)
224
225 // The `main program' equivalent, creating the windows and returning the
226 // main frame
227 bool MyApp::OnInit()
228 {
229 // Create the main frame window
230 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
231 "wxToolBar Sample",
232 wxPoint(100, 100), wxSize(550, 300));
233
234 frame->Show(TRUE);
235
236 frame->SetStatusText("Hello, wxWindows");
237
238 SetTopWindow(frame);
239
240 return TRUE;
241 }
242
243 void MyFrame::RecreateToolbar()
244 {
245 // delete and recreate the toolbar
246 wxToolBarBase *toolBar = GetToolBar();
247 long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE;
248
249 delete toolBar;
250
251 SetToolBar(NULL);
252
253 style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL);
254 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
255
256 toolBar = CreateToolBar(style, ID_TOOLBAR);
257 //toolBar->SetMargins( 4, 4 );
258
259 // Set up toolbar
260 wxBitmap toolBarBitmaps[8];
261
262 #if USE_XPM_BITMAPS
263 toolBarBitmaps[0] = wxBitmap(new_xpm);
264 toolBarBitmaps[1] = wxBitmap(open_xpm);
265 toolBarBitmaps[2] = wxBitmap(save_xpm);
266 toolBarBitmaps[3] = wxBitmap(copy_xpm);
267 toolBarBitmaps[4] = wxBitmap(cut_xpm);
268 toolBarBitmaps[5] = wxBitmap(paste_xpm);
269 toolBarBitmaps[6] = wxBitmap(print_xpm);
270 toolBarBitmaps[7] = wxBitmap(help_xpm);
271 #else // !USE_XPM_BITMAPS
272 toolBarBitmaps[0] = wxBITMAP(new);
273 toolBarBitmaps[1] = wxBITMAP(open);
274 toolBarBitmaps[2] = wxBITMAP(save);
275 toolBarBitmaps[3] = wxBITMAP(copy);
276 toolBarBitmaps[4] = wxBITMAP(cut);
277 toolBarBitmaps[5] = wxBITMAP(paste);
278 toolBarBitmaps[6] = wxBITMAP(print);
279 toolBarBitmaps[7] = wxBITMAP(help);
280 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
281
282 if ( !m_smallToolbar )
283 {
284 int w = 2*toolBarBitmaps[0].GetWidth(),
285 h = 2*toolBarBitmaps[0].GetHeight();
286 for ( size_t n = 0; n < WXSIZEOF(toolBarBitmaps); n++ )
287 {
288 toolBarBitmaps[n] =
289 wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h));
290 }
291
292 toolBar->SetToolBitmapSize(wxSize(w, h));
293 }
294
295 toolBar->AddTool(wxID_NEW, _T("New"), toolBarBitmaps[0], _T("New file"));
296 toolBar->AddTool(wxID_OPEN, _T("Open"), toolBarBitmaps[1], _T("Open file"));
297
298 // neither the generic nor Motif native toolbars really support this
299 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__) && !defined(__WXX11__)
300 // adding a combo to a vertical toolbar is not very smart
301 if ( m_horzToolbar )
302 {
303 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, "", wxDefaultPosition, wxSize(200,-1) );
304 combo->Append("This");
305 combo->Append("is a");
306 combo->Append("combobox");
307 combo->Append("in a");
308 combo->Append("toolbar");
309 toolBar->AddControl(combo);
310 }
311 #endif // toolbars which don't support controls
312
313 toolBar->AddTool(wxID_SAVE, _T("Save"), toolBarBitmaps[2], _T("Toggle button 1"), wxITEM_CHECK);
314 toolBar->AddTool(wxID_COPY, _T("Copy"), toolBarBitmaps[3], _T("Toggle button 2"), wxITEM_CHECK);
315 toolBar->AddTool(wxID_CUT, _T("Cut"), toolBarBitmaps[4], _T("Toggle/Untoggle help button"));
316 toolBar->AddTool(wxID_PASTE, _T("Paste"), toolBarBitmaps[5], _T("Paste"));
317 toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[6], _T("Delete this tool"));
318 toolBar->AddSeparator();
319 toolBar->AddTool(wxID_HELP, _T("Help"), toolBarBitmaps[7], _T("Help button"), wxITEM_CHECK);
320
321 // after adding the buttons to the toolbar, must call Realize() to reflect
322 // the changes
323 toolBar->Realize();
324
325 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
326 }
327
328 // ----------------------------------------------------------------------------
329 // MyFrame
330 // ----------------------------------------------------------------------------
331
332 // Define my frame constructor
333 MyFrame::MyFrame(wxFrame* parent,
334 wxWindowID id,
335 const wxString& title,
336 const wxPoint& pos,
337 const wxSize& size,
338 long style)
339 : wxFrame(parent, id, title, pos, size, style)
340 {
341 m_tbar = NULL;
342 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
343
344 m_smallToolbar = TRUE;
345 m_horzToolbar = TRUE;
346 m_rows = 1;
347
348 // Give it a status line
349 CreateStatusBar();
350
351 // Give it an icon
352 SetIcon(wxICON(mondrian));
353
354 // Make a menubar
355 wxMenu *tbarMenu = new wxMenu;
356 tbarMenu->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
357 "Toggle &another toolbar\tCtrl-A",
358 "Show/hide another test toolbar",
359 TRUE);
360
361 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
362 "&Toggle toolbar size\tCtrl-S",
363 "Toggle between big/small toolbar",
364 TRUE);
365 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
366 "Toggle toolbar &orientation\tCtrl-O",
367 "Toggle toolbar orientation",
368 TRUE);
369 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
370 "Toggle number of &rows\tCtrl-R",
371 "Toggle number of toolbar rows between 1 and 2",
372 TRUE);
373
374 tbarMenu->AppendSeparator();
375
376 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
377 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
378 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
379 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
380 tbarMenu->AppendSeparator();
381 tbarMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, "Change tool tip", "");
382 tbarMenu->AppendSeparator();
383 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, "Show &text\tAlt-T");
384 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, "Show &icons\tAlt-I");
385 tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, "Show &both\tAlt-B");
386
387 wxMenu *fileMenu = new wxMenu;
388 fileMenu->Append(wxID_EXIT, "E&xit\tAlt-X", "Quit toolbar sample" );
389
390 wxMenu *helpMenu = new wxMenu;
391 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
392
393 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
394
395 menuBar->Append(fileMenu, "&File");
396 menuBar->Append(tbarMenu, "&Toolbar");
397 menuBar->Append(helpMenu, "&Help");
398
399 // Associate the menu bar with the frame
400 SetMenuBar(menuBar);
401
402 menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, TRUE);
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 long style = GetToolBar()->GetWindowStyle();
463 style &= ~wxTB_HORIZONTAL;
464 style |= wxTB_VERTICAL;
465
466 m_tbar = new wxToolBar(this, -1,
467 wxDefaultPosition, wxDefaultSize,
468 style);
469
470 m_tbar->AddRadioTool(wxID_NEW, _T("First"), wxBITMAP(new));
471 m_tbar->AddRadioTool(wxID_OPEN, _T("Second"), wxBITMAP(open));
472 m_tbar->AddRadioTool(wxID_SAVE, _T("Third"), wxBITMAP(save));
473 m_tbar->AddSeparator();
474 m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help));
475
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::OnToolbarStyle(wxCommandEvent& event)
584 {
585 long style = GetToolBar()->GetWindowStyle();
586 style &= ~(wxTB_NOICONS | wxTB_TEXT);
587
588 switch ( event.GetId() )
589 {
590 case IDM_TOOLBAR_SHOW_TEXT:
591 style |= wxTB_NOICONS | wxTB_TEXT;
592 break;
593
594 case IDM_TOOLBAR_SHOW_ICONS:
595 // nothing to do
596 break;
597
598 case IDM_TOOLBAR_SHOW_BOTH:
599 style |= wxTB_TEXT;
600 }
601
602 GetToolBar()->SetWindowStyle(style);
603 }
604
605 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
606 {
607 wxBitmap bmp = wxBITMAP(print);
608
609 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
610 FALSE, (wxObject *) NULL,
611 "Delete this tool",
612 "This button was inserted into the toolbar");
613
614 GetToolBar()->Realize();
615 }
616
617 void MyFrame::OnToolEnter(wxCommandEvent& event)
618 {
619 if (event.GetSelection() > -1)
620 {
621 wxString str;
622 str.Printf(_T("This is tool number %d"), event.GetSelection());
623 SetStatusText(str);
624 }
625 else
626 SetStatusText("");
627 }
628