]>
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 | #include "wx/filedlg.h" | |
35 | #include "wx/spinctrl.h" | |
36 | #include "wx/srchctrl.h" | |
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_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW | |
47 | #error You need to enable XPM support to use XPM bitmaps with toolbar! | |
48 | #endif // USE_XPM_BITMAPS | |
49 | ||
50 | // If this is 1, the sample will test an extra toolbar identical to the | |
51 | // main one, but not managed by the frame. This can test subtle differences | |
52 | // in the way toolbars are handled, especially on Mac where there is one | |
53 | // native, 'installed' toolbar. | |
54 | #define USE_UNMANAGED_TOOLBAR 0 | |
55 | ||
56 | // ---------------------------------------------------------------------------- | |
57 | // resources | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
61 | #include "mondrian.xpm" | |
62 | #endif | |
63 | ||
64 | #if USE_XPM_BITMAPS | |
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 | enum Positions | |
76 | { | |
77 | TOOLBAR_LEFT, | |
78 | TOOLBAR_TOP, | |
79 | TOOLBAR_RIGHT, | |
80 | TOOLBAR_BOTTOM | |
81 | }; | |
82 | ||
83 | // ---------------------------------------------------------------------------- | |
84 | // classes | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | // Define a new application | |
88 | class MyApp : public wxApp | |
89 | { | |
90 | public: | |
91 | bool OnInit(); | |
92 | }; | |
93 | ||
94 | // Define a new frame | |
95 | class MyFrame: public wxFrame | |
96 | { | |
97 | public: | |
98 | MyFrame(wxFrame *parent, | |
99 | wxWindowID id = wxID_ANY, | |
100 | const wxString& title = _T("wxToolBar Sample"), | |
101 | const wxPoint& pos = wxDefaultPosition, | |
102 | const wxSize& size = wxDefaultSize, | |
103 | long style = wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE); | |
104 | ||
105 | void PopulateToolbar(wxToolBarBase* toolBar); | |
106 | void RecreateToolbar(); | |
107 | ||
108 | void OnQuit(wxCommandEvent& event); | |
109 | void OnAbout(wxCommandEvent& event); | |
110 | ||
111 | void OnSize(wxSizeEvent& event); | |
112 | ||
113 | void OnToggleToolbar(wxCommandEvent& event); | |
114 | void OnToggleAnotherToolbar(wxCommandEvent& event); | |
115 | void OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event)); | |
116 | ||
117 | void OnToggleToolbarSize(wxCommandEvent& event); | |
118 | void OnChangeOrientation(wxCommandEvent& event); | |
119 | void OnToggleToolbarRows(wxCommandEvent& event); | |
120 | void OnToggleTooltips(wxCommandEvent& event); | |
121 | void OnToggleCustomDisabled(wxCommandEvent& event); | |
122 | ||
123 | void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); } | |
124 | void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); } | |
125 | void OnInsertPrint(wxCommandEvent& event); | |
126 | void OnChangeToolTip(wxCommandEvent& event); | |
127 | void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); } | |
128 | void OnToggleRadioBtn(wxCommandEvent& event); | |
129 | ||
130 | void OnToolbarStyle(wxCommandEvent& event); | |
131 | void OnToolbarCustomBitmap(wxCommandEvent& event); | |
132 | ||
133 | void OnToolLeftClick(wxCommandEvent& event); | |
134 | void OnToolRightClick(wxCommandEvent& event); | |
135 | void OnToolDropdown(wxCommandEvent& event); | |
136 | ||
137 | void OnCombo(wxCommandEvent& event); | |
138 | ||
139 | void OnUpdateCopyAndCut(wxUpdateUIEvent& event); | |
140 | void OnUpdateToggleHorzText(wxUpdateUIEvent& event); | |
141 | void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event) | |
142 | { event.Enable( m_tbar != NULL ); } | |
143 | ||
144 | private: | |
145 | void DoEnablePrint(); | |
146 | void DoDeletePrint(); | |
147 | void DoToggleHelp(); | |
148 | ||
149 | void LayoutChildren(); | |
150 | ||
151 | bool m_smallToolbar, | |
152 | m_horzText, | |
153 | m_useCustomDisabled, | |
154 | m_showTooltips; | |
155 | size_t m_rows; // 1 or 2 only | |
156 | ||
157 | // the number of print buttons we have (they're added/removed dynamically) | |
158 | size_t m_nPrint; | |
159 | ||
160 | // store toolbar position for future use | |
161 | Positions m_toolbarPosition; | |
162 | ||
163 | wxTextCtrl *m_textWindow; | |
164 | ||
165 | wxPanel *m_panel; | |
166 | wxToolBar *m_extraToolBar; | |
167 | ||
168 | wxToolBar *m_tbar; | |
169 | ||
170 | // the path to the custom bitmap for the test toolbar tool | |
171 | wxString m_pathBmp; | |
172 | ||
173 | DECLARE_EVENT_TABLE() | |
174 | }; | |
175 | ||
176 | // ---------------------------------------------------------------------------- | |
177 | // constants | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
180 | const int ID_TOOLBAR = 500; | |
181 | ||
182 | static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT; | |
183 | ||
184 | enum | |
185 | { | |
186 | IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200, | |
187 | IDM_TOOLBAR_TOGGLETOOLBARROWS, | |
188 | IDM_TOOLBAR_TOGGLETOOLTIPS, | |
189 | IDM_TOOLBAR_TOGGLECUSTOMDISABLED, | |
190 | IDM_TOOLBAR_ENABLEPRINT, | |
191 | IDM_TOOLBAR_DELETEPRINT, | |
192 | IDM_TOOLBAR_INSERTPRINT, | |
193 | IDM_TOOLBAR_TOGGLEHELP, | |
194 | IDM_TOOLBAR_TOGGLERADIOBTN1, | |
195 | IDM_TOOLBAR_TOGGLERADIOBTN2, | |
196 | IDM_TOOLBAR_TOGGLERADIOBTN3, | |
197 | IDM_TOOLBAR_TOGGLE_TOOLBAR, | |
198 | IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, | |
199 | IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, | |
200 | IDM_TOOLBAR_CHANGE_TOOLTIP, | |
201 | IDM_TOOLBAR_SHOW_TEXT, | |
202 | IDM_TOOLBAR_SHOW_ICONS, | |
203 | IDM_TOOLBAR_SHOW_BOTH, | |
204 | IDM_TOOLBAR_CUSTOM_PATH, | |
205 | IDM_TOOLBAR_TOP_ORIENTATION, | |
206 | IDM_TOOLBAR_LEFT_ORIENTATION, | |
207 | IDM_TOOLBAR_BOTTOM_ORIENTATION, | |
208 | IDM_TOOLBAR_RIGHT_ORIENTATION, | |
209 | IDM_TOOLBAR_OTHER_1, | |
210 | IDM_TOOLBAR_OTHER_2, | |
211 | IDM_TOOLBAR_OTHER_3, | |
212 | ||
213 | ID_COMBO = 1000, | |
214 | ID_SPIN = 1001 | |
215 | }; | |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // event tables | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar | |
222 | // help button. | |
223 | ||
224 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
225 | EVT_SIZE(MyFrame::OnSize) | |
226 | ||
227 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
228 | EVT_MENU(wxID_HELP, MyFrame::OnAbout) | |
229 | ||
230 | EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR, MyFrame::OnToggleToolbar) | |
231 | EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar) | |
232 | EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, MyFrame::OnToggleHorizontalText) | |
233 | ||
234 | EVT_MENU_RANGE(IDM_TOOLBAR_TOP_ORIENTATION, IDM_TOOLBAR_RIGHT_ORIENTATION, MyFrame::OnChangeOrientation) | |
235 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize) | |
236 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows) | |
237 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS, MyFrame::OnToggleTooltips) | |
238 | EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, MyFrame::OnToggleCustomDisabled) | |
239 | ||
240 | EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint) | |
241 | EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint) | |
242 | EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint) | |
243 | EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp) | |
244 | EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3, | |
245 | MyFrame::OnToggleRadioBtn) | |
246 | EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip) | |
247 | ||
248 | EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH, | |
249 | MyFrame::OnToolbarStyle) | |
250 | ||
251 | EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH, MyFrame::OnToolbarCustomBitmap) | |
252 | ||
253 | EVT_MENU(wxID_ANY, MyFrame::OnToolLeftClick) | |
254 | ||
255 | EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo) | |
256 | ||
257 | EVT_TOOL_RCLICKED(wxID_ANY, MyFrame::OnToolRightClick) | |
258 | ||
259 | EVT_TOOL_DROPDOWN(wxID_ANY, MyFrame::OnToolDropdown) | |
260 | ||
261 | EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut) | |
262 | EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut) | |
263 | ||
264 | EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, | |
265 | IDM_TOOLBAR_TOGGLERADIOBTN3, | |
266 | MyFrame::OnUpdateToggleRadioBtn) | |
267 | EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, | |
268 | MyFrame::OnUpdateToggleHorzText) | |
269 | END_EVENT_TABLE() | |
270 | ||
271 | // ============================================================================ | |
272 | // implementation | |
273 | // ============================================================================ | |
274 | ||
275 | // ---------------------------------------------------------------------------- | |
276 | // MyApp | |
277 | // ---------------------------------------------------------------------------- | |
278 | ||
279 | IMPLEMENT_APP(MyApp) | |
280 | ||
281 | // The `main program' equivalent, creating the windows and returning the | |
282 | // main frame | |
283 | bool MyApp::OnInit() | |
284 | { | |
285 | if ( !wxApp::OnInit() ) | |
286 | return false; | |
287 | ||
288 | // Create the main frame window | |
289 | MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY, | |
290 | _T("wxToolBar Sample"), | |
291 | wxPoint(100, 100), wxSize(550, 300)); | |
292 | ||
293 | frame->Show(true); | |
294 | ||
295 | #if wxUSE_STATUSBAR | |
296 | frame->SetStatusText(_T("Hello, wxWidgets")); | |
297 | #endif | |
298 | ||
299 | wxInitAllImageHandlers(); | |
300 | ||
301 | SetTopWindow(frame); | |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | void MyFrame::RecreateToolbar() | |
307 | { | |
308 | #ifdef __WXWINCE__ | |
309 | // On Windows CE, we should not delete the | |
310 | // previous toolbar in case it contains the menubar. | |
311 | // We'll try to accommodate this usage in due course. | |
312 | wxToolBar* toolBar = CreateToolBar(); | |
313 | #else | |
314 | // delete and recreate the toolbar | |
315 | wxToolBarBase *toolBar = GetToolBar(); | |
316 | long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE; | |
317 | ||
318 | delete toolBar; | |
319 | ||
320 | SetToolBar(NULL); | |
321 | ||
322 | style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL | wxTB_BOTTOM | wxTB_RIGHT | wxTB_HORZ_LAYOUT); | |
323 | switch( m_toolbarPosition ) | |
324 | { | |
325 | case TOOLBAR_LEFT: | |
326 | style |= wxTB_LEFT; | |
327 | break; | |
328 | case TOOLBAR_TOP: | |
329 | style |= wxTB_TOP; | |
330 | break; | |
331 | case TOOLBAR_RIGHT: | |
332 | style |= wxTB_RIGHT; | |
333 | break; | |
334 | case TOOLBAR_BOTTOM: | |
335 | style |= wxTB_BOTTOM; | |
336 | break; | |
337 | } | |
338 | ||
339 | if ( m_showTooltips ) | |
340 | style &= ~wxTB_NO_TOOLTIPS; | |
341 | else | |
342 | style |= wxTB_NO_TOOLTIPS; | |
343 | ||
344 | if ( style & wxTB_TEXT && !(style & wxTB_NOICONS) && m_horzText ) | |
345 | style |= wxTB_HORZ_LAYOUT; | |
346 | ||
347 | toolBar = CreateToolBar(style, ID_TOOLBAR); | |
348 | #endif | |
349 | ||
350 | PopulateToolbar(toolBar); | |
351 | } | |
352 | ||
353 | void MyFrame::PopulateToolbar(wxToolBarBase* toolBar) | |
354 | { | |
355 | // Set up toolbar | |
356 | enum | |
357 | { | |
358 | Tool_new, | |
359 | Tool_open, | |
360 | Tool_save, | |
361 | Tool_copy, | |
362 | Tool_cut, | |
363 | Tool_paste, | |
364 | Tool_print, | |
365 | Tool_help, | |
366 | Tool_Max | |
367 | }; | |
368 | ||
369 | wxBitmap toolBarBitmaps[Tool_Max]; | |
370 | ||
371 | #if USE_XPM_BITMAPS | |
372 | #define INIT_TOOL_BMP(bmp) \ | |
373 | toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm) | |
374 | #else // !USE_XPM_BITMAPS | |
375 | #define INIT_TOOL_BMP(bmp) \ | |
376 | toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp) | |
377 | #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS | |
378 | ||
379 | INIT_TOOL_BMP(new); | |
380 | INIT_TOOL_BMP(open); | |
381 | INIT_TOOL_BMP(save); | |
382 | INIT_TOOL_BMP(copy); | |
383 | INIT_TOOL_BMP(cut); | |
384 | INIT_TOOL_BMP(paste); | |
385 | INIT_TOOL_BMP(print); | |
386 | INIT_TOOL_BMP(help); | |
387 | ||
388 | int w = toolBarBitmaps[Tool_new].GetWidth(), | |
389 | h = toolBarBitmaps[Tool_new].GetHeight(); | |
390 | ||
391 | if ( !m_smallToolbar ) | |
392 | { | |
393 | w *= 2; | |
394 | h *= 2; | |
395 | ||
396 | for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ ) | |
397 | { | |
398 | toolBarBitmaps[n] = | |
399 | wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h)); | |
400 | } | |
401 | } | |
402 | ||
403 | toolBar->SetToolBitmapSize(wxSize(w, h)); | |
404 | ||
405 | toolBar->AddTool(wxID_NEW, _T("New"), | |
406 | toolBarBitmaps[Tool_new], wxNullBitmap, wxITEM_DROPDOWN, | |
407 | _T("New file"), _T("This is help for new file tool")); | |
408 | ||
409 | wxMenu* menu = new wxMenu; | |
410 | menu->Append(wxID_ANY, _T("&First dummy item")); | |
411 | menu->Append(wxID_ANY, _T("&Second dummy item")); | |
412 | menu->AppendSeparator(); | |
413 | menu->Append(wxID_EXIT, _T("Exit")); | |
414 | toolBar->SetDropdownMenu(wxID_NEW, menu); | |
415 | ||
416 | toolBar->AddTool(wxID_OPEN, _T("Open"), | |
417 | toolBarBitmaps[Tool_open], wxNullBitmap, wxITEM_NORMAL, | |
418 | _T("Open file"), _T("This is help for open file tool")); | |
419 | ||
420 | // the generic toolbar doesn't really support this | |
421 | #if wxUSE_TOOLBAR_NATIVE && !defined(__WXX11__) || defined(__WXUNIVERSAL__) | |
422 | // adding a combo to a vertical toolbar is not very smart | |
423 | if ( !( toolBar->IsVertical() ) ) | |
424 | { | |
425 | wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, wxEmptyString, wxDefaultPosition, wxSize(100,-1) ); | |
426 | combo->Append(_T("This")); | |
427 | combo->Append(_T("is a")); | |
428 | combo->Append(_T("combobox")); | |
429 | combo->Append(_T("in a")); | |
430 | combo->Append(_T("toolbar")); | |
431 | toolBar->AddControl(combo, _T("Combo Label")); | |
432 | ||
433 | wxSpinCtrl *spin = new wxSpinCtrl( toolBar, ID_SPIN, wxT("0"), wxDefaultPosition, wxSize(80,wxDefaultCoord), 0, 0, 100 ); | |
434 | toolBar->AddControl( spin ); | |
435 | ||
436 | wxTextCtrl *text = new wxTextCtrl( toolBar, -1, wxT("text"), wxDefaultPosition, wxSize(80,wxDefaultCoord) ); | |
437 | toolBar->AddControl( text ); | |
438 | ||
439 | wxSearchCtrl *srch = new wxSearchCtrl( toolBar, -1, wxT("xx"), wxDefaultPosition, wxSize(80,wxDefaultCoord), wxSUNKEN_BORDER ); | |
440 | toolBar->AddControl( srch ); | |
441 | } | |
442 | #endif // toolbars which don't support controls | |
443 | ||
444 | toolBar->AddTool(wxID_SAVE, _T("Save"), toolBarBitmaps[Tool_save], _T("Toggle button 1"), wxITEM_CHECK); | |
445 | toolBar->AddTool(wxID_COPY, _T("Copy"), toolBarBitmaps[Tool_copy], _T("Toggle button 2"), wxITEM_CHECK); | |
446 | toolBar->AddTool(wxID_CUT, _T("Cut"), toolBarBitmaps[Tool_cut], _T("Toggle/Untoggle help button")); | |
447 | toolBar->AddTool(wxID_PASTE, _T("Paste"), toolBarBitmaps[Tool_paste], _T("Paste")); | |
448 | ||
449 | if ( m_useCustomDisabled ) | |
450 | { | |
451 | wxBitmap bmpDisabled(w, h); | |
452 | { | |
453 | wxMemoryDC dc; | |
454 | dc.SelectObject(bmpDisabled); | |
455 | dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0); | |
456 | ||
457 | wxPen pen(*wxRED, 5); | |
458 | dc.SetPen(pen); | |
459 | dc.DrawLine(0, 0, w, h); | |
460 | } | |
461 | ||
462 | toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print], | |
463 | bmpDisabled); | |
464 | } | |
465 | else | |
466 | { | |
467 | toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print], | |
468 | _T("Delete this tool. This is a very long tooltip to test whether it does the right thing when the tooltip is more than Windows can cope with.")); | |
469 | } | |
470 | ||
471 | toolBar->AddSeparator(); | |
472 | toolBar->AddTool(wxID_HELP, _T("Help"), toolBarBitmaps[Tool_help], _T("Help button"), wxITEM_CHECK); | |
473 | ||
474 | if ( !m_pathBmp.empty() ) | |
475 | { | |
476 | // create a tool with a custom bitmap for testing | |
477 | wxImage img(m_pathBmp); | |
478 | if ( img.Ok() ) | |
479 | { | |
480 | if ( img.GetWidth() > w && img.GetHeight() > h ) | |
481 | img = img.GetSubImage(wxRect(0, 0, w, h)); | |
482 | ||
483 | toolBar->AddSeparator(); | |
484 | toolBar->AddTool(wxID_ANY, _T("Custom"), img); | |
485 | } | |
486 | } | |
487 | ||
488 | // after adding the buttons to the toolbar, must call Realize() to reflect | |
489 | // the changes | |
490 | toolBar->Realize(); | |
491 | ||
492 | toolBar->SetRows(!(toolBar->IsVertical()) ? m_rows : 10 / m_rows); | |
493 | } | |
494 | ||
495 | // ---------------------------------------------------------------------------- | |
496 | // MyFrame | |
497 | // ---------------------------------------------------------------------------- | |
498 | ||
499 | // Define my frame constructor | |
500 | MyFrame::MyFrame(wxFrame* parent, | |
501 | wxWindowID id, | |
502 | const wxString& title, | |
503 | const wxPoint& pos, | |
504 | const wxSize& size, | |
505 | long style) | |
506 | : wxFrame(parent, id, title, pos, size, style) | |
507 | { | |
508 | m_tbar = NULL; | |
509 | ||
510 | m_smallToolbar = true; | |
511 | m_horzText = false; | |
512 | m_useCustomDisabled = false; | |
513 | m_showTooltips = true; | |
514 | ||
515 | m_rows = 1; | |
516 | m_nPrint = 1; | |
517 | ||
518 | #if wxUSE_STATUSBAR | |
519 | // Give it a status line | |
520 | CreateStatusBar(); | |
521 | #endif | |
522 | ||
523 | // Give it an icon | |
524 | SetIcon(wxICON(mondrian)); | |
525 | ||
526 | // Make a menubar | |
527 | wxMenu *tbarMenu = new wxMenu; | |
528 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR, | |
529 | _T("Toggle &toolbar\tCtrl-Z"), | |
530 | _T("Show or hide the toolbar")); | |
531 | ||
532 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, | |
533 | _T("Toggle &another toolbar\tCtrl-A"), | |
534 | _T("Show/hide another test toolbar")); | |
535 | ||
536 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, | |
537 | _T("Toggle hori&zontal text\tCtrl-H"), | |
538 | _T("Show text under/alongside the icon")); | |
539 | ||
540 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE, | |
541 | _T("&Toggle toolbar size\tCtrl-S"), | |
542 | _T("Toggle between big/small toolbar")); | |
543 | ||
544 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS, | |
545 | _T("Toggle number of &rows\tCtrl-R"), | |
546 | _T("Toggle number of toolbar rows between 1 and 2")); | |
547 | ||
548 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS, | |
549 | _T("Show &tooltips\tCtrl-L"), | |
550 | _T("Show tooltips for the toolbar tools")); | |
551 | ||
552 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, | |
553 | _T("Use c&ustom disabled images\tCtrl-U"), | |
554 | _T("Switch between using system-generated and custom disabled images")); | |
555 | ||
556 | ||
557 | tbarMenu->AppendSeparator(); | |
558 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_TOP_ORIENTATION, | |
559 | _T("Set toolbar at the top of the window"), | |
560 | _T("Set toolbar at the top of the window")); | |
561 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_LEFT_ORIENTATION, | |
562 | _T("Set toolbar at the left of the window"), | |
563 | _T("Set toolbar at the left of the window")); | |
564 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_BOTTOM_ORIENTATION, | |
565 | _T("Set toolbar at the bottom of the window"), | |
566 | _T("Set toolbar at the bottom of the window")); | |
567 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_RIGHT_ORIENTATION, | |
568 | _T("Set toolbar at the right edge of the window"), | |
569 | _T("Set toolbar at the right edge of the window")); | |
570 | tbarMenu->AppendSeparator(); | |
571 | ||
572 | tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, _T("&Enable print button\tCtrl-E")); | |
573 | tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, _T("&Delete print button\tCtrl-D")); | |
574 | tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, _T("&Insert print button\tCtrl-I")); | |
575 | tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, _T("Toggle &help button\tCtrl-T")); | |
576 | tbarMenu->AppendSeparator(); | |
577 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, _T("Toggle &1st radio button\tCtrl-1")); | |
578 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, _T("Toggle &2nd radio button\tCtrl-2")); | |
579 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, _T("Toggle &3rd radio button\tCtrl-3")); | |
580 | tbarMenu->AppendSeparator(); | |
581 | tbarMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, _T("Change tool tip")); | |
582 | tbarMenu->AppendSeparator(); | |
583 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, _T("Show &text\tCtrl-Alt-T")); | |
584 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, _T("Show &icons\tCtrl-Alt-I")); | |
585 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, _T("Show &both\tCtrl-Alt-B")); | |
586 | tbarMenu->AppendSeparator(); | |
587 | tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, _T("Custom &bitmap...\tCtrl-B")); | |
588 | ||
589 | wxMenu *fileMenu = new wxMenu; | |
590 | fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") ); | |
591 | ||
592 | wxMenu *helpMenu = new wxMenu; | |
593 | helpMenu->Append(wxID_HELP, _T("&About"), _T("About toolbar sample")); | |
594 | ||
595 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
596 | ||
597 | menuBar->Append(fileMenu, _T("&File")); | |
598 | menuBar->Append(tbarMenu, _T("&Toolbar")); | |
599 | menuBar->Append(helpMenu, _T("&Help")); | |
600 | ||
601 | // Associate the menu bar with the frame | |
602 | SetMenuBar(menuBar); | |
603 | ||
604 | menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true); | |
605 | menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true); | |
606 | ||
607 | menuBar->Check(IDM_TOOLBAR_TOP_ORIENTATION, true ); | |
608 | m_toolbarPosition = TOOLBAR_TOP; | |
609 | ||
610 | // Create the toolbar | |
611 | RecreateToolbar(); | |
612 | ||
613 | m_panel = new wxPanel(this, wxID_ANY); | |
614 | #if USE_UNMANAGED_TOOLBAR | |
615 | m_extraToolBar = new wxToolBar(m_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_TEXT|wxTB_FLAT|wxTB_TOP); | |
616 | PopulateToolbar(m_extraToolBar); | |
617 | #else | |
618 | m_extraToolBar = NULL; | |
619 | #endif | |
620 | ||
621 | m_textWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); | |
622 | ||
623 | wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); | |
624 | m_panel->SetSizer(sizer); | |
625 | if (m_extraToolBar) | |
626 | sizer->Add(m_extraToolBar, 0, wxEXPAND, 0); | |
627 | sizer->Add(m_textWindow, 1, wxEXPAND, 0); | |
628 | } | |
629 | ||
630 | void MyFrame::LayoutChildren() | |
631 | { | |
632 | wxSize size = GetClientSize(); | |
633 | ||
634 | int offset; | |
635 | if ( m_tbar ) | |
636 | { | |
637 | m_tbar->SetSize(0, 0, wxDefaultCoord, size.y); | |
638 | ||
639 | offset = m_tbar->GetSize().x; | |
640 | } | |
641 | else | |
642 | { | |
643 | offset = 0; | |
644 | } | |
645 | ||
646 | m_panel->SetSize(offset, 0, size.x - offset, size.y); | |
647 | } | |
648 | ||
649 | void MyFrame::OnSize(wxSizeEvent& event) | |
650 | { | |
651 | if ( m_tbar ) | |
652 | { | |
653 | LayoutChildren(); | |
654 | } | |
655 | else | |
656 | { | |
657 | event.Skip(); | |
658 | } | |
659 | } | |
660 | ||
661 | void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event)) | |
662 | { | |
663 | wxToolBar *tbar = GetToolBar(); | |
664 | ||
665 | if ( !tbar ) | |
666 | { | |
667 | RecreateToolbar(); | |
668 | } | |
669 | else | |
670 | { | |
671 | delete tbar; | |
672 | ||
673 | SetToolBar(NULL); | |
674 | } | |
675 | } | |
676 | ||
677 | void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event)) | |
678 | { | |
679 | m_horzText = !m_horzText; | |
680 | ||
681 | RecreateToolbar(); | |
682 | } | |
683 | ||
684 | void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event)) | |
685 | { | |
686 | if ( m_tbar ) | |
687 | { | |
688 | delete m_tbar; | |
689 | m_tbar = NULL; | |
690 | } | |
691 | else | |
692 | { | |
693 | long style = GetToolBar() ? GetToolBar()->GetWindowStyle() | |
694 | : TOOLBAR_STYLE; | |
695 | style &= ~wxTB_HORIZONTAL; | |
696 | style |= wxTB_VERTICAL; | |
697 | ||
698 | m_tbar = new wxToolBar(this, wxID_ANY, | |
699 | wxDefaultPosition, wxDefaultSize, | |
700 | style); | |
701 | ||
702 | m_tbar->SetMargins(4, 4); | |
703 | ||
704 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, _T("First"), wxBITMAP(new)); | |
705 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, _T("Second"), wxBITMAP(open)); | |
706 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, _T("Third"), wxBITMAP(save)); | |
707 | m_tbar->AddSeparator(); | |
708 | m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help)); | |
709 | ||
710 | m_tbar->Realize(); | |
711 | } | |
712 | ||
713 | LayoutChildren(); | |
714 | } | |
715 | ||
716 | void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event)) | |
717 | { | |
718 | m_smallToolbar = !m_smallToolbar; | |
719 | ||
720 | RecreateToolbar(); | |
721 | } | |
722 | ||
723 | void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event)) | |
724 | { | |
725 | // m_rows may be only 1 or 2 | |
726 | m_rows = 3 - m_rows; | |
727 | ||
728 | GetToolBar()->SetRows(!(GetToolBar()->IsVertical()) ? m_rows : 10 / m_rows); | |
729 | ||
730 | //RecreateToolbar(); -- this is unneeded | |
731 | } | |
732 | ||
733 | void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event)) | |
734 | { | |
735 | m_showTooltips = !m_showTooltips; | |
736 | ||
737 | RecreateToolbar(); | |
738 | } | |
739 | ||
740 | void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event)) | |
741 | { | |
742 | m_useCustomDisabled = !m_useCustomDisabled; | |
743 | ||
744 | RecreateToolbar(); | |
745 | } | |
746 | ||
747 | void MyFrame::OnChangeOrientation(wxCommandEvent& event) | |
748 | { | |
749 | switch( event.GetId() ) | |
750 | { | |
751 | case IDM_TOOLBAR_LEFT_ORIENTATION: | |
752 | m_toolbarPosition = TOOLBAR_LEFT; | |
753 | break; | |
754 | case IDM_TOOLBAR_TOP_ORIENTATION: | |
755 | m_toolbarPosition = TOOLBAR_TOP; | |
756 | break; | |
757 | case IDM_TOOLBAR_RIGHT_ORIENTATION: | |
758 | m_toolbarPosition = TOOLBAR_RIGHT; | |
759 | break; | |
760 | case IDM_TOOLBAR_BOTTOM_ORIENTATION: | |
761 | m_toolbarPosition = TOOLBAR_BOTTOM; | |
762 | break; | |
763 | } | |
764 | RecreateToolbar(); | |
765 | } | |
766 | ||
767 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
768 | { | |
769 | Close(true); | |
770 | } | |
771 | ||
772 | void MyFrame::OnAbout(wxCommandEvent& event) | |
773 | { | |
774 | if ( event.IsChecked() ) | |
775 | m_textWindow->WriteText( _T("Help button down now.\n") ); | |
776 | else | |
777 | m_textWindow->WriteText( _T("Help button up now.\n") ); | |
778 | ||
779 | (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar")); | |
780 | } | |
781 | ||
782 | void MyFrame::OnToolLeftClick(wxCommandEvent& event) | |
783 | { | |
784 | wxString str; | |
785 | str.Printf( _T("Clicked on tool %d\n"), event.GetId()); | |
786 | m_textWindow->WriteText( str ); | |
787 | ||
788 | if (event.GetId() == wxID_COPY) | |
789 | { | |
790 | DoEnablePrint(); | |
791 | } | |
792 | ||
793 | if (event.GetId() == wxID_CUT) | |
794 | { | |
795 | DoToggleHelp(); | |
796 | } | |
797 | ||
798 | if (event.GetId() == wxID_PRINT) | |
799 | { | |
800 | DoDeletePrint(); | |
801 | } | |
802 | } | |
803 | ||
804 | void MyFrame::OnToolRightClick(wxCommandEvent& event) | |
805 | { | |
806 | m_textWindow->AppendText( | |
807 | wxString::Format(_T("Tool %d right clicked.\n"), | |
808 | (int) event.GetInt())); | |
809 | } | |
810 | ||
811 | void MyFrame::OnCombo(wxCommandEvent& event) | |
812 | { | |
813 | wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str()); | |
814 | } | |
815 | ||
816 | void MyFrame::DoEnablePrint() | |
817 | { | |
818 | if ( !m_nPrint ) | |
819 | return; | |
820 | ||
821 | wxToolBarBase *tb = GetToolBar(); | |
822 | tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT)); | |
823 | } | |
824 | ||
825 | void MyFrame::DoDeletePrint() | |
826 | { | |
827 | if ( !m_nPrint ) | |
828 | return; | |
829 | ||
830 | wxToolBarBase *tb = GetToolBar(); | |
831 | tb->DeleteTool( wxID_PRINT ); | |
832 | ||
833 | m_nPrint--; | |
834 | } | |
835 | ||
836 | void MyFrame::DoToggleHelp() | |
837 | { | |
838 | wxToolBarBase *tb = GetToolBar(); | |
839 | tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) ); | |
840 | } | |
841 | ||
842 | void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event) | |
843 | { | |
844 | event.Enable( m_textWindow->CanCopy() ); | |
845 | } | |
846 | ||
847 | void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event) | |
848 | { | |
849 | wxToolBar *tbar = GetToolBar(); | |
850 | event.Enable( tbar && | |
851 | tbar->HasFlag(wxTB_TEXT) && | |
852 | !tbar->HasFlag(wxTB_NOICONS) ); | |
853 | } | |
854 | ||
855 | void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event)) | |
856 | { | |
857 | GetToolBar()->SetToolShortHelp(wxID_NEW, _T("New toolbar button")); | |
858 | } | |
859 | ||
860 | void MyFrame::OnToolbarStyle(wxCommandEvent& event) | |
861 | { | |
862 | long style = GetToolBar()->GetWindowStyle(); | |
863 | style &= ~(wxTB_NOICONS | wxTB_TEXT); | |
864 | ||
865 | switch ( event.GetId() ) | |
866 | { | |
867 | case IDM_TOOLBAR_SHOW_TEXT: | |
868 | style |= wxTB_NOICONS | wxTB_TEXT; | |
869 | break; | |
870 | ||
871 | case IDM_TOOLBAR_SHOW_ICONS: | |
872 | // nothing to do | |
873 | break; | |
874 | ||
875 | case IDM_TOOLBAR_SHOW_BOTH: | |
876 | style |= wxTB_TEXT; | |
877 | } | |
878 | ||
879 | GetToolBar()->SetWindowStyle(style); | |
880 | } | |
881 | ||
882 | void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event)) | |
883 | { | |
884 | m_pathBmp = wxFileSelector(_T("Custom bitmap path")); | |
885 | ||
886 | RecreateToolbar(); | |
887 | } | |
888 | ||
889 | void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event)) | |
890 | { | |
891 | m_nPrint++; | |
892 | ||
893 | wxToolBarBase *tb = GetToolBar(); | |
894 | tb->InsertTool(0, wxID_PRINT, _T("New print"), | |
895 | wxBITMAP(print), wxNullBitmap, | |
896 | wxITEM_NORMAL, | |
897 | _T("Delete this tool"), | |
898 | _T("This button was inserted into the toolbar")); | |
899 | ||
900 | // must call Realize() after adding a new button | |
901 | tb->Realize(); | |
902 | } | |
903 | ||
904 | void MyFrame::OnToggleRadioBtn(wxCommandEvent& event) | |
905 | { | |
906 | if ( m_tbar ) | |
907 | { | |
908 | m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 + | |
909 | event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true); | |
910 | } | |
911 | } | |
912 | ||
913 | void MyFrame::OnToolDropdown(wxCommandEvent& event) | |
914 | { | |
915 | wxString str; | |
916 | str.Printf( _T("Dropdown on tool %d\n"), event.GetId()); | |
917 | m_textWindow->WriteText( str ); | |
918 | ||
919 | event.Skip(); | |
920 | } |