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