1. added wxGTK::wxToolBar::SetToolShortHelp() and test for it in sample
[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
35 // define this to 1 to use wxToolBarSimple instead of the native one
36 #define USE_GENERIC_TBAR 0
37
38 // define this to use XPMs everywhere (by default, BMPs are used under Win)
39 #ifdef __WXMSW__
40 #define USE_XPM_BITMAPS 0
41 #else
42 #define USE_XPM_BITMAPS 1
43 #endif
44
45 #if USE_GENERIC_TBAR
46 #if !wxUSE_TOOLBAR_SIMPLE
47 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
48 to 1 in setup.h and recompile the library.
49 #else
50 #include <wx/tbarsmpl.h>
51 #endif
52 #endif // USE_GENERIC_TBAR
53
54 #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
55 #error You need to enable XPM support to use XPM bitmaps with toolbar!
56 #endif // USE_XPM_BITMAPS
57
58 // ----------------------------------------------------------------------------
59 // resources
60 // ----------------------------------------------------------------------------
61
62 #if USE_XPM_BITMAPS
63 #include "mondrian.xpm"
64 #include "bitmaps/new.xpm"
65 #include "bitmaps/open.xpm"
66 #include "bitmaps/save.xpm"
67 #include "bitmaps/copy.xpm"
68 #include "bitmaps/cut.xpm"
69 #include "bitmaps/preview.xpm" // paste XPM
70 #include "bitmaps/print.xpm"
71 #include "bitmaps/help.xpm"
72 #endif // USE_XPM_BITMAPS
73
74 // ----------------------------------------------------------------------------
75 // classes
76 // ----------------------------------------------------------------------------
77
78 // Define a new application
79 class MyApp : public wxApp
80 {
81 public:
82 bool OnInit();
83 };
84
85 // Define a new frame
86 class MyFrame: public wxFrame
87 {
88 public:
89 MyFrame(wxFrame *parent,
90 wxWindowID id = -1,
91 const wxString& title = "wxToolBar Sample",
92 const wxPoint& pos = wxDefaultPosition,
93 const wxSize& size = wxDefaultSize,
94 long style = wxDEFAULT_FRAME_STYLE);
95
96 void RecreateToolbar();
97
98 void OnQuit(wxCommandEvent& event);
99 void OnAbout(wxCommandEvent& event);
100
101 void OnSize(wxSizeEvent& event);
102
103 void OnToggleAnotherToolbar(wxCommandEvent& event);
104
105 void OnToggleToolbarSize(wxCommandEvent& event);
106 void OnToggleToolbarOrient(wxCommandEvent& event);
107 void OnToggleToolbarRows(wxCommandEvent& event);
108
109 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
110 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
111 void OnInsertPrint(wxCommandEvent& event);
112 void OnChangeToolTip(wxCommandEvent& event);
113 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
114
115 void OnToolLeftClick(wxCommandEvent& event);
116 void OnToolEnter(wxCommandEvent& event);
117
118 void OnCombo(wxCommandEvent& event);
119
120 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
121
122 void OnToggleFullScreen(wxCommandEvent& event);
123
124 #if USE_GENERIC_TBAR
125 virtual wxToolBar *OnCreateToolBar(long style,
126 wxWindowID id,
127 const wxString& name );
128 #endif // USE_GENERIC_TBAR
129
130 private:
131 void DoEnablePrint();
132 void DoDeletePrint();
133 void DoToggleHelp();
134
135 void LayoutChildren();
136
137 bool m_smallToolbar,
138 m_horzToolbar;
139 size_t m_rows; // 1 or 2 only
140
141 wxTextCtrl *m_textWindow;
142
143 wxToolBar *m_tbar;
144
145 DECLARE_EVENT_TABLE()
146 };
147
148 // ----------------------------------------------------------------------------
149 // constants
150 // ----------------------------------------------------------------------------
151
152 const int ID_TOOLBAR = 500;
153
154 enum
155 {
156 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
157 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
158 IDM_TOOLBAR_TOGGLETOOLBARROWS,
159 IDM_TOOLBAR_ENABLEPRINT,
160 IDM_TOOLBAR_DELETEPRINT,
161 IDM_TOOLBAR_INSERTPRINT,
162 IDM_TOOLBAR_TOGGLEHELP,
163 IDM_TOOLBAR_TOGGLEFULLSCREEN,
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_TOGGLEFULLSCREEN, MyFrame::OnToggleFullScreen)
194 EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip)
195
196 EVT_MENU(-1, MyFrame::OnToolLeftClick)
197
198 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
199
200 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
201
202 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
203 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
204 END_EVENT_TABLE()
205
206 // ============================================================================
207 // implementation
208 // ============================================================================
209
210 // ----------------------------------------------------------------------------
211 // MyApp
212 // ----------------------------------------------------------------------------
213
214 IMPLEMENT_APP(MyApp)
215
216 // The `main program' equivalent, creating the windows and returning the
217 // main frame
218 bool MyApp::OnInit()
219 {
220 // Create the main frame window
221 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
222 "wxToolBar Sample",
223 wxPoint(100, 100), wxSize(450, 300));
224
225 frame->Show(TRUE);
226
227 frame->SetStatusText("Hello, wxWindows");
228
229 SetTopWindow(frame);
230
231 return TRUE;
232 }
233
234 void MyFrame::RecreateToolbar()
235 {
236 // delete and recreate the toolbar
237 wxToolBarBase *toolBar = GetToolBar();
238 delete toolBar;
239
240 SetToolBar(NULL);
241
242 long style = wxNO_BORDER | wxTB_FLAT | wxTB_DOCKABLE;
243 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
244
245 toolBar = CreateToolBar(style, ID_TOOLBAR);
246 toolBar->SetMargins( 4, 4 );
247
248 // Set up toolbar
249 wxBitmap toolBarBitmaps[8];
250
251 #if USE_XPM_BITMAPS
252 toolBarBitmaps[0] = wxBitmap(new_xpm);
253 toolBarBitmaps[1] = wxBitmap(open_xpm);
254 toolBarBitmaps[2] = wxBitmap(save_xpm);
255 toolBarBitmaps[3] = wxBitmap(copy_xpm);
256 toolBarBitmaps[4] = wxBitmap(cut_xpm);
257 toolBarBitmaps[5] = wxBitmap(paste_xpm);
258 toolBarBitmaps[6] = wxBitmap(print_xpm);
259 toolBarBitmaps[7] = wxBitmap(help_xpm);
260 #else // !USE_XPM_BITMAPS
261 toolBarBitmaps[0] = wxBITMAP(new);
262 toolBarBitmaps[1] = wxBITMAP(open);
263 toolBarBitmaps[2] = wxBITMAP(save);
264 toolBarBitmaps[3] = wxBITMAP(copy);
265 toolBarBitmaps[4] = wxBITMAP(cut);
266 toolBarBitmaps[5] = wxBITMAP(paste);
267 toolBarBitmaps[6] = wxBITMAP(print);
268 toolBarBitmaps[7] = wxBITMAP(help);
269 #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
270
271 if ( !m_smallToolbar )
272 {
273 int w = 2*toolBarBitmaps[0].GetWidth(),
274 h = 2*toolBarBitmaps[0].GetHeight();
275 for ( size_t n = 0; n < WXSIZEOF(toolBarBitmaps); n++ )
276 {
277 toolBarBitmaps[n] =
278 wxImage(toolBarBitmaps[n]).Scale(w, h).ConvertToBitmap();
279 }
280
281 toolBar->SetToolBitmapSize(wxSize(w, h));
282 }
283
284 #ifdef __WXMSW__
285 int width = 24;
286 #else
287 int width = 16;
288 #endif
289
290 int currentX = 5;
291
292 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
293 currentX += width + 5;
294 toolBar->AddTool(wxID_OPEN, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
295
296 // neither the generic nor Motif native toolbars really support this
297 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
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);
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 currentX += width + 5;
312 toolBar->AddTool(wxID_SAVE, toolBarBitmaps[2], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
313 currentX += width + 5;
314 toolBar->AddTool(wxID_COPY, toolBarBitmaps[3], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
315 currentX += width + 5;
316 toolBar->AddTool(wxID_CUT, toolBarBitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
317 currentX += width + 5;
318 toolBar->AddTool(wxID_PASTE, toolBarBitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
319 currentX += width + 5;
320 toolBar->AddTool(wxID_PRINT, toolBarBitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool");
321 currentX += width + 5;
322 toolBar->AddSeparator();
323 toolBar->AddTool(wxID_HELP, toolBarBitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
324
325 // after adding the buttons to the toolbar, must call Realize() to reflect
326 // the changes
327 toolBar->Realize();
328
329 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
330 }
331
332 // ----------------------------------------------------------------------------
333 // MyFrame
334 // ----------------------------------------------------------------------------
335
336 // Define my frame constructor
337 MyFrame::MyFrame(wxFrame* parent,
338 wxWindowID id,
339 const wxString& title,
340 const wxPoint& pos,
341 const wxSize& size,
342 long style)
343 : wxFrame(parent, id, title, pos, size, style)
344 {
345 m_tbar = NULL;
346 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
347
348 m_smallToolbar = TRUE;
349 m_horzToolbar = TRUE;
350 m_rows = 1;
351
352 // Give it a status line
353 CreateStatusBar();
354
355 // Give it an icon
356 SetIcon(wxICON(mondrian));
357
358 // Make a menubar
359 wxMenu *tbarMenu = new wxMenu;
360 tbarMenu->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
361 "Toggle &another toolbar\tCtrl-A",
362 "Show/hide another test toolbar",
363 TRUE);
364
365 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
366 "&Toggle toolbar size\tCtrl-S",
367 "Toggle between big/small toolbar",
368 TRUE);
369 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
370 "Toggle toolbar &orientation\tCtrl-O",
371 "Toggle toolbar orientation",
372 TRUE);
373 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
374 "Toggle number of &rows\tCtrl-R",
375 "Toggle number of toolbar rows between 1 and 2",
376 TRUE);
377
378 tbarMenu->AppendSeparator();
379
380 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
381 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
382 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
383 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
384 tbarMenu->AppendSeparator();
385 tbarMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, "Change tool tip", "");
386 tbarMenu->AppendSeparator();
387 tbarMenu->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN, "Toggle &full screen mode\tCtrl-F", "");
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 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
601 void MyFrame::OnToggleFullScreen(wxCommandEvent& event)
602 {
603 #ifdef __WXMSW__
604 ShowFullScreen(!IsFullScreen());
605 #endif
606 }
607