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