]>
Commit | Line | Data |
---|---|---|
14d1ccd8 | 1 | ///////////////////////////////////////////////////////////////////////////// |
fc6bbc6d | 2 | // Name: toolbar.cpp |
14d1ccd8 JS |
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 | |
ad9bb75f | 9 | // Licence: wxWindows licence |
14d1ccd8 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
ad9bb75f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14d1ccd8 | 20 | // For compilers that support precompilation, includes "wx/wx.h". |
92a19c2e | 21 | #include "wx/wxprec.h" |
14d1ccd8 JS |
22 | |
23 | #ifdef __BORLANDC__ | |
ad9bb75f | 24 | #pragma hdrstop |
14d1ccd8 JS |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
5f4d35b8 | 28 | #include "wx/wx.h" |
14d1ccd8 JS |
29 | #endif |
30 | ||
5f4d35b8 WS |
31 | #include "wx/toolbar.h" |
32 | #include "wx/log.h" | |
33 | #include "wx/image.h" | |
5e3b8b93 | 34 | #include "wx/filedlg.h" |
8bbe427f | 35 | |
f6bcfd97 | 36 | // define this to use XPMs everywhere (by default, BMPs are used under Win) |
0ed1d09c | 37 | // BMPs use less space, but aren't compiled into the executable on other platforms |
f6bcfd97 BP |
38 | #ifdef __WXMSW__ |
39 | #define USE_XPM_BITMAPS 0 | |
40 | #else | |
41 | #define USE_XPM_BITMAPS 1 | |
42 | #endif | |
43 | ||
f6bcfd97 BP |
44 | #if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW |
45 | #error You need to enable XPM support to use XPM bitmaps with toolbar! | |
46 | #endif // USE_XPM_BITMAPS | |
47 | ||
ad9bb75f VZ |
48 | // ---------------------------------------------------------------------------- |
49 | // resources | |
50 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 51 | |
5f4d35b8 | 52 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
ad9bb75f | 53 | #include "mondrian.xpm" |
5f4d35b8 WS |
54 | #endif |
55 | ||
56 | #if USE_XPM_BITMAPS | |
ad9bb75f VZ |
57 | #include "bitmaps/new.xpm" |
58 | #include "bitmaps/open.xpm" | |
59 | #include "bitmaps/save.xpm" | |
60 | #include "bitmaps/copy.xpm" | |
61 | #include "bitmaps/cut.xpm" | |
5ef2e633 | 62 | #include "bitmaps/preview.xpm" // paste XPM |
ad9bb75f | 63 | #include "bitmaps/print.xpm" |
ad9bb75f | 64 | #include "bitmaps/help.xpm" |
f6bcfd97 | 65 | #endif // USE_XPM_BITMAPS |
ad9bb75f VZ |
66 | |
67 | // ---------------------------------------------------------------------------- | |
68 | // classes | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | // Define a new application | |
5ef2e633 | 72 | class MyApp : public wxApp |
ad9bb75f VZ |
73 | { |
74 | public: | |
75 | bool OnInit(); | |
ad9bb75f | 76 | }; |
47908e25 | 77 | |
ad9bb75f VZ |
78 | // Define a new frame |
79 | class MyFrame: public wxFrame | |
80 | { | |
81 | public: | |
82 | MyFrame(wxFrame *parent, | |
bc2ec626 | 83 | wxWindowID id = wxID_ANY, |
ab1ca7b3 | 84 | const wxString& title = _T("wxToolBar Sample"), |
ad9bb75f VZ |
85 | const wxPoint& pos = wxDefaultPosition, |
86 | const wxSize& size = wxDefaultSize, | |
229de929 | 87 | long style = wxDEFAULT_FRAME_STYLE|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE); |
14d1ccd8 | 88 | |
5ef2e633 VZ |
89 | void RecreateToolbar(); |
90 | ||
ad9bb75f VZ |
91 | void OnQuit(wxCommandEvent& event); |
92 | void OnAbout(wxCommandEvent& event); | |
93 | ||
f6bcfd97 BP |
94 | void OnSize(wxSizeEvent& event); |
95 | ||
ba0abe3c | 96 | void OnToggleToolbar(wxCommandEvent& event); |
f6bcfd97 | 97 | void OnToggleAnotherToolbar(wxCommandEvent& event); |
96dc4236 | 98 | void OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event)); |
f6bcfd97 | 99 | |
5ef2e633 VZ |
100 | void OnToggleToolbarSize(wxCommandEvent& event); |
101 | void OnToggleToolbarOrient(wxCommandEvent& event); | |
98066234 | 102 | void OnToggleToolbarRows(wxCommandEvent& event); |
6a1b3ead | 103 | void OnToggleTooltips(wxCommandEvent& event); |
8d0a7b56 | 104 | void OnToggleCustomDisabled(wxCommandEvent& event); |
5ef2e633 | 105 | |
ad4ae6ed VZ |
106 | void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); } |
107 | void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); } | |
bdc72a22 | 108 | void OnInsertPrint(wxCommandEvent& event); |
a1f79c1e | 109 | void OnChangeToolTip(wxCommandEvent& event); |
ad4ae6ed | 110 | void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); } |
3bf4189d | 111 | void OnToggleRadioBtn(wxCommandEvent& event); |
ad9bb75f | 112 | |
ec483b11 | 113 | void OnToolbarStyle(wxCommandEvent& event); |
5e3b8b93 | 114 | void OnToolbarCustomBitmap(wxCommandEvent& event); |
ec483b11 | 115 | |
ad9bb75f | 116 | void OnToolLeftClick(wxCommandEvent& event); |
e052a316 | 117 | void OnToolRightClick(wxCommandEvent& event); |
ad9bb75f VZ |
118 | void OnToolEnter(wxCommandEvent& event); |
119 | ||
1c383dba VZ |
120 | void OnCombo(wxCommandEvent& event); |
121 | ||
5ef2e633 | 122 | void OnUpdateCopyAndCut(wxUpdateUIEvent& event); |
96dc4236 | 123 | void OnUpdateToggleHorzText(wxUpdateUIEvent& event); |
3bf4189d VZ |
124 | void OnUpdateToggleRadioBtn(wxUpdateUIEvent& event) |
125 | { event.Enable( m_tbar != NULL ); } | |
5ef2e633 | 126 | |
ad9bb75f VZ |
127 | private: |
128 | void DoEnablePrint(); | |
97d7bfb8 | 129 | void DoDeletePrint(); |
ad9bb75f VZ |
130 | void DoToggleHelp(); |
131 | ||
f6bcfd97 BP |
132 | void LayoutChildren(); |
133 | ||
5ef2e633 | 134 | bool m_smallToolbar, |
96dc4236 | 135 | m_horzToolbar, |
8d0a7b56 | 136 | m_horzText, |
6a1b3ead VZ |
137 | m_useCustomDisabled, |
138 | m_showTooltips; | |
98066234 VZ |
139 | size_t m_rows; // 1 or 2 only |
140 | ||
2b5f62a0 VZ |
141 | // the number of print buttons we have (they're added/removed dynamically) |
142 | size_t m_nPrint; | |
143 | ||
f6bcfd97 BP |
144 | wxTextCtrl *m_textWindow; |
145 | ||
146 | wxToolBar *m_tbar; | |
ad9bb75f | 147 | |
5e3b8b93 VZ |
148 | // the path to the custom bitmap for the test toolbar tool |
149 | wxString m_pathBmp; | |
150 | ||
ad9bb75f VZ |
151 | DECLARE_EVENT_TABLE() |
152 | }; | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // constants | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | const int ID_TOOLBAR = 500; | |
159 | ||
dd46ee66 | 160 | static const long TOOLBAR_STYLE = wxTB_FLAT | wxTB_DOCKABLE | wxTB_TEXT; |
86f65864 | 161 | |
ad9bb75f | 162 | enum |
14d1ccd8 | 163 | { |
5ef2e633 VZ |
164 | IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200, |
165 | IDM_TOOLBAR_TOGGLETOOLBARORIENT, | |
98066234 | 166 | IDM_TOOLBAR_TOGGLETOOLBARROWS, |
6a1b3ead | 167 | IDM_TOOLBAR_TOGGLETOOLTIPS, |
8d0a7b56 | 168 | IDM_TOOLBAR_TOGGLECUSTOMDISABLED, |
ad9bb75f | 169 | IDM_TOOLBAR_ENABLEPRINT, |
97d7bfb8 | 170 | IDM_TOOLBAR_DELETEPRINT, |
bdc72a22 | 171 | IDM_TOOLBAR_INSERTPRINT, |
1c383dba | 172 | IDM_TOOLBAR_TOGGLEHELP, |
3bf4189d VZ |
173 | IDM_TOOLBAR_TOGGLERADIOBTN1, |
174 | IDM_TOOLBAR_TOGGLERADIOBTN2, | |
175 | IDM_TOOLBAR_TOGGLERADIOBTN3, | |
ba0abe3c | 176 | IDM_TOOLBAR_TOGGLE_TOOLBAR, |
96dc4236 | 177 | IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, |
f6bcfd97 | 178 | IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, |
a1f79c1e | 179 | IDM_TOOLBAR_CHANGE_TOOLTIP, |
ec483b11 VZ |
180 | IDM_TOOLBAR_SHOW_TEXT, |
181 | IDM_TOOLBAR_SHOW_ICONS, | |
182 | IDM_TOOLBAR_SHOW_BOTH, | |
5e3b8b93 | 183 | IDM_TOOLBAR_CUSTOM_PATH, |
1c383dba | 184 | |
3bf4189d VZ |
185 | IDM_TOOLBAR_OTHER_1, |
186 | IDM_TOOLBAR_OTHER_2, | |
187 | IDM_TOOLBAR_OTHER_3, | |
188 | ||
1c383dba | 189 | ID_COMBO = 1000 |
ad9bb75f | 190 | }; |
14d1ccd8 | 191 | |
ad9bb75f VZ |
192 | // ---------------------------------------------------------------------------- |
193 | // event tables | |
194 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 195 | |
ad9bb75f VZ |
196 | // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar |
197 | // help button. | |
14d1ccd8 | 198 | |
ad9bb75f | 199 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
f6bcfd97 BP |
200 | EVT_SIZE(MyFrame::OnSize) |
201 | ||
ad9bb75f VZ |
202 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) |
203 | EVT_MENU(wxID_HELP, MyFrame::OnAbout) | |
14d1ccd8 | 204 | |
ba0abe3c | 205 | EVT_MENU(IDM_TOOLBAR_TOGGLE_TOOLBAR, MyFrame::OnToggleToolbar) |
f6bcfd97 | 206 | EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar) |
96dc4236 | 207 | EVT_MENU(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, MyFrame::OnToggleHorizontalText) |
f6bcfd97 | 208 | |
5ef2e633 VZ |
209 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize) |
210 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient) | |
98066234 | 211 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows) |
6a1b3ead | 212 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLTIPS, MyFrame::OnToggleTooltips) |
8d0a7b56 | 213 | EVT_MENU(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, MyFrame::OnToggleCustomDisabled) |
5ef2e633 | 214 | |
ad9bb75f | 215 | EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint) |
97d7bfb8 | 216 | EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint) |
bdc72a22 | 217 | EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint) |
ad9bb75f | 218 | EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp) |
3bf4189d VZ |
219 | EVT_MENU_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, IDM_TOOLBAR_TOGGLERADIOBTN3, |
220 | MyFrame::OnToggleRadioBtn) | |
a1f79c1e | 221 | EVT_MENU(IDM_TOOLBAR_CHANGE_TOOLTIP, MyFrame::OnChangeToolTip) |
14d1ccd8 | 222 | |
ec483b11 VZ |
223 | EVT_MENU_RANGE(IDM_TOOLBAR_SHOW_TEXT, IDM_TOOLBAR_SHOW_BOTH, |
224 | MyFrame::OnToolbarStyle) | |
225 | ||
5e3b8b93 VZ |
226 | EVT_MENU(IDM_TOOLBAR_CUSTOM_PATH, MyFrame::OnToolbarCustomBitmap) |
227 | ||
bc2ec626 | 228 | EVT_MENU(wxID_ANY, MyFrame::OnToolLeftClick) |
14d1ccd8 | 229 | |
1c383dba VZ |
230 | EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo) |
231 | ||
ad9bb75f | 232 | EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter) |
e052a316 | 233 | EVT_TOOL_RCLICKED(wxID_ANY, MyFrame::OnToolRightClick) |
5ef2e633 VZ |
234 | |
235 | EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut) | |
236 | EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut) | |
96dc4236 | 237 | |
3bf4189d VZ |
238 | EVT_UPDATE_UI_RANGE(IDM_TOOLBAR_TOGGLERADIOBTN1, |
239 | IDM_TOOLBAR_TOGGLERADIOBTN3, | |
240 | MyFrame::OnUpdateToggleRadioBtn) | |
96dc4236 VZ |
241 | EVT_UPDATE_UI(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, |
242 | MyFrame::OnUpdateToggleHorzText) | |
ad9bb75f | 243 | END_EVENT_TABLE() |
14d1ccd8 | 244 | |
ad9bb75f VZ |
245 | // ============================================================================ |
246 | // implementation | |
247 | // ============================================================================ | |
14d1ccd8 | 248 | |
ad9bb75f VZ |
249 | // ---------------------------------------------------------------------------- |
250 | // MyApp | |
251 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 252 | |
ad9bb75f | 253 | IMPLEMENT_APP(MyApp) |
14d1ccd8 | 254 | |
ad9bb75f VZ |
255 | // The `main program' equivalent, creating the windows and returning the |
256 | // main frame | |
257 | bool MyApp::OnInit() | |
258 | { | |
259 | // Create the main frame window | |
bc2ec626 | 260 | MyFrame* frame = new MyFrame((wxFrame *) NULL, wxID_ANY, |
ab1ca7b3 | 261 | _T("wxToolBar Sample"), |
5e3b8b93 | 262 | wxPoint(100, 100), wxSize(550, 300)); |
14d1ccd8 | 263 | |
bc2ec626 | 264 | frame->Show(true); |
7fb23305 | 265 | |
8520f137 | 266 | #if wxUSE_STATUSBAR |
be5a51fb | 267 | frame->SetStatusText(_T("Hello, wxWidgets")); |
0cfc2a92 | 268 | #endif |
7fb23305 | 269 | |
5e3b8b93 VZ |
270 | wxInitAllImageHandlers(); |
271 | ||
7fb23305 VZ |
272 | SetTopWindow(frame); |
273 | ||
bc2ec626 | 274 | return true; |
14d1ccd8 JS |
275 | } |
276 | ||
5ef2e633 | 277 | void MyFrame::RecreateToolbar() |
14d1ccd8 | 278 | { |
bf95a04f JS |
279 | #ifdef __WXWINCE__ |
280 | // On Windows CE, we should not delete the | |
281 | // previous toolbar in case it contains the menubar. | |
34621cc5 | 282 | // We'll try to accommodate this usage in due course. |
bf95a04f JS |
283 | wxToolBar* toolBar = CreateToolBar(); |
284 | #else | |
5ef2e633 | 285 | // delete and recreate the toolbar |
ad4ae6ed | 286 | wxToolBarBase *toolBar = GetToolBar(); |
ec483b11 VZ |
287 | long style = toolBar ? toolBar->GetWindowStyle() : TOOLBAR_STYLE; |
288 | ||
5ef2e633 | 289 | delete toolBar; |
14d1ccd8 | 290 | |
5ef2e633 VZ |
291 | SetToolBar(NULL); |
292 | ||
96dc4236 | 293 | style &= ~(wxTB_HORIZONTAL | wxTB_VERTICAL | wxTB_HORZ_LAYOUT); |
5ef2e633 | 294 | style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL; |
96dc4236 | 295 | |
6a1b3ead VZ |
296 | if ( m_showTooltips ) |
297 | style &= ~wxTB_NO_TOOLTIPS; | |
298 | else | |
299 | style |= wxTB_NO_TOOLTIPS; | |
300 | ||
96dc4236 VZ |
301 | if ( style & wxTB_TEXT && !(style & wxTB_NOICONS) && m_horzText ) |
302 | style |= wxTB_HORZ_LAYOUT; | |
5ef2e633 VZ |
303 | |
304 | toolBar = CreateToolBar(style, ID_TOOLBAR); | |
bf95a04f | 305 | #endif |
5ef2e633 VZ |
306 | |
307 | // Set up toolbar | |
8d0a7b56 VZ |
308 | enum |
309 | { | |
310 | Tool_new, | |
311 | Tool_open, | |
312 | Tool_save, | |
313 | Tool_copy, | |
314 | Tool_cut, | |
315 | Tool_paste, | |
316 | Tool_print, | |
317 | Tool_help, | |
318 | Tool_Max | |
319 | }; | |
320 | ||
321 | wxBitmap toolBarBitmaps[Tool_Max]; | |
5ef2e633 | 322 | |
f6bcfd97 | 323 | #if USE_XPM_BITMAPS |
8d0a7b56 VZ |
324 | #define INIT_TOOL_BMP(bmp) \ |
325 | toolBarBitmaps[Tool_##bmp] = wxBitmap(bmp##_xpm) | |
f6bcfd97 | 326 | #else // !USE_XPM_BITMAPS |
8d0a7b56 VZ |
327 | #define INIT_TOOL_BMP(bmp) \ |
328 | toolBarBitmaps[Tool_##bmp] = wxBITMAP(bmp) | |
f6bcfd97 | 329 | #endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS |
40515a21 | 330 | |
8d0a7b56 VZ |
331 | INIT_TOOL_BMP(new); |
332 | INIT_TOOL_BMP(open); | |
333 | INIT_TOOL_BMP(save); | |
334 | INIT_TOOL_BMP(copy); | |
335 | INIT_TOOL_BMP(cut); | |
336 | INIT_TOOL_BMP(paste); | |
337 | INIT_TOOL_BMP(print); | |
338 | INIT_TOOL_BMP(help); | |
339 | ||
340 | int w = toolBarBitmaps[Tool_new].GetWidth(), | |
341 | h = toolBarBitmaps[Tool_new].GetHeight(); | |
342 | ||
5ef2e633 VZ |
343 | if ( !m_smallToolbar ) |
344 | { | |
8d0a7b56 VZ |
345 | w *= 2; |
346 | h *= 2; | |
347 | ||
348 | for ( size_t n = Tool_new; n < WXSIZEOF(toolBarBitmaps); n++ ) | |
40515a21 VZ |
349 | { |
350 | toolBarBitmaps[n] = | |
368d59f0 | 351 | wxBitmap(toolBarBitmaps[n].ConvertToImage().Scale(w, h)); |
40515a21 | 352 | } |
5ef2e633 | 353 | } |
14d1ccd8 | 354 | |
6a1b3ead VZ |
355 | toolBar->SetToolBitmapSize(wxSize(w, h)); |
356 | ||
8d0a7b56 VZ |
357 | toolBar->AddTool(wxID_NEW, _T("New"), toolBarBitmaps[Tool_new], _T("New file")); |
358 | toolBar->AddTool(wxID_OPEN, _T("Open"), toolBarBitmaps[Tool_open], _T("Open file")); | |
14d1ccd8 | 359 | |
aae91497 | 360 | // the generic toolbar doesn't really support this |
27cc9142 | 361 | #if wxUSE_TOOLBAR_NATIVE && !defined(__WXX11__) || defined(__WXUNIVERSAL__) |
5ef2e633 VZ |
362 | // adding a combo to a vertical toolbar is not very smart |
363 | if ( m_horzToolbar ) | |
364 | { | |
5f4d35b8 | 365 | wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO, wxEmptyString, wxDefaultPosition, wxSize(200,wxDefaultCoord) ); |
2b5f62a0 VZ |
366 | combo->Append(_T("This")); |
367 | combo->Append(_T("is a")); | |
368 | combo->Append(_T("combobox")); | |
369 | combo->Append(_T("in a")); | |
370 | combo->Append(_T("toolbar")); | |
5ef2e633 VZ |
371 | toolBar->AddControl(combo); |
372 | } | |
ad4ae6ed | 373 | #endif // toolbars which don't support controls |
14d1ccd8 | 374 | |
8d0a7b56 VZ |
375 | toolBar->AddTool(wxID_SAVE, _T("Save"), toolBarBitmaps[Tool_save], _T("Toggle button 1"), wxITEM_CHECK); |
376 | toolBar->AddTool(wxID_COPY, _T("Copy"), toolBarBitmaps[Tool_copy], _T("Toggle button 2"), wxITEM_CHECK); | |
377 | toolBar->AddTool(wxID_CUT, _T("Cut"), toolBarBitmaps[Tool_cut], _T("Toggle/Untoggle help button")); | |
378 | toolBar->AddTool(wxID_PASTE, _T("Paste"), toolBarBitmaps[Tool_paste], _T("Paste")); | |
379 | ||
380 | if ( m_useCustomDisabled ) | |
381 | { | |
382 | wxBitmap bmpDisabled(w, h); | |
383 | { | |
384 | wxMemoryDC dc; | |
385 | dc.SelectObject(bmpDisabled); | |
386 | dc.DrawBitmap(toolBarBitmaps[Tool_print], 0, 0); | |
387 | ||
388 | wxPen pen(*wxRED, 5); | |
389 | dc.SetPen(pen); | |
390 | dc.DrawLine(0, 0, w, h); | |
391 | } | |
392 | ||
393 | toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print], | |
394 | bmpDisabled); | |
395 | } | |
396 | else | |
397 | { | |
398 | toolBar->AddTool(wxID_PRINT, _T("Print"), toolBarBitmaps[Tool_print], | |
399 | _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.")); | |
400 | } | |
401 | ||
40515a21 | 402 | toolBar->AddSeparator(); |
8d0a7b56 | 403 | toolBar->AddTool(wxID_HELP, _T("Help"), toolBarBitmaps[Tool_help], _T("Help button"), wxITEM_CHECK); |
13437238 | 404 | |
5e3b8b93 VZ |
405 | if ( !m_pathBmp.empty() ) |
406 | { | |
407 | // create a tool with a custom bitmap for testing | |
408 | wxImage img(m_pathBmp); | |
409 | if ( img.Ok() ) | |
410 | { | |
411 | if ( img.GetWidth() > w && img.GetHeight() > h ) | |
412 | img = img.GetSubImage(wxRect(0, 0, w, h)); | |
413 | ||
414 | toolBar->AddSeparator(); | |
415 | toolBar->AddTool(wxID_ANY, _T("Custom"), img); | |
416 | } | |
417 | } | |
418 | ||
5ef2e633 VZ |
419 | // after adding the buttons to the toolbar, must call Realize() to reflect |
420 | // the changes | |
421 | toolBar->Realize(); | |
98066234 VZ |
422 | |
423 | toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows); | |
14d1ccd8 JS |
424 | } |
425 | ||
ad9bb75f VZ |
426 | // ---------------------------------------------------------------------------- |
427 | // MyFrame | |
428 | // ---------------------------------------------------------------------------- | |
13437238 JS |
429 | |
430 | // Define my frame constructor | |
7fb23305 VZ |
431 | MyFrame::MyFrame(wxFrame* parent, |
432 | wxWindowID id, | |
433 | const wxString& title, | |
434 | const wxPoint& pos, | |
435 | const wxSize& size, | |
436 | long style) | |
437 | : wxFrame(parent, id, title, pos, size, style) | |
14d1ccd8 | 438 | { |
f6bcfd97 | 439 | m_tbar = NULL; |
98066234 | 440 | |
bc2ec626 WS |
441 | m_smallToolbar = true; |
442 | m_horzToolbar = true; | |
443 | m_horzText = false; | |
8d0a7b56 | 444 | m_useCustomDisabled = false; |
6a1b3ead VZ |
445 | m_showTooltips = true; |
446 | ||
98066234 | 447 | m_rows = 1; |
2b5f62a0 | 448 | m_nPrint = 1; |
ad9bb75f | 449 | |
8520f137 | 450 | #if wxUSE_STATUSBAR |
ad9bb75f VZ |
451 | // Give it a status line |
452 | CreateStatusBar(); | |
0cfc2a92 | 453 | #endif |
ad9bb75f VZ |
454 | |
455 | // Give it an icon | |
456 | SetIcon(wxICON(mondrian)); | |
457 | ||
458 | // Make a menubar | |
459 | wxMenu *tbarMenu = new wxMenu; | |
ba0abe3c | 460 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_TOOLBAR, |
2b5f62a0 VZ |
461 | _T("Toggle &toolbar\tCtrl-Z"), |
462 | _T("Show or hide the toolbar")); | |
ba0abe3c VZ |
463 | |
464 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, | |
2b5f62a0 VZ |
465 | _T("Toggle &another toolbar\tCtrl-A"), |
466 | _T("Show/hide another test toolbar")); | |
ba0abe3c | 467 | |
96dc4236 VZ |
468 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLE_HORIZONTAL_TEXT, |
469 | _T("Toggle hori&zontal text\tCtrl-H"), | |
470 | _T("Show text under/alongside the icon")); | |
471 | ||
ba0abe3c | 472 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARSIZE, |
2b5f62a0 VZ |
473 | _T("&Toggle toolbar size\tCtrl-S"), |
474 | _T("Toggle between big/small toolbar")); | |
ba0abe3c VZ |
475 | |
476 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARORIENT, | |
2b5f62a0 VZ |
477 | _T("Toggle toolbar &orientation\tCtrl-O"), |
478 | _T("Toggle toolbar orientation")); | |
ba0abe3c VZ |
479 | |
480 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLBARROWS, | |
2b5f62a0 VZ |
481 | _T("Toggle number of &rows\tCtrl-R"), |
482 | _T("Toggle number of toolbar rows between 1 and 2")); | |
5ef2e633 | 483 | |
6a1b3ead VZ |
484 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLETOOLTIPS, |
485 | _T("Show &tooltips\tCtrl-L"), | |
486 | _T("Show tooltips for the toolbar tools")); | |
487 | ||
8d0a7b56 VZ |
488 | tbarMenu->AppendCheckItem(IDM_TOOLBAR_TOGGLECUSTOMDISABLED, |
489 | _T("Use c&ustom disabled images\tCtrl-U"), | |
490 | _T("Switch between using system-generated and custom disabled images")); | |
491 | ||
492 | ||
5ef2e633 VZ |
493 | tbarMenu->AppendSeparator(); |
494 | ||
5e3b8b93 VZ |
495 | tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, _T("&Enable print button\tCtrl-E")); |
496 | tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, _T("&Delete print button\tCtrl-D")); | |
497 | tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, _T("&Insert print button\tCtrl-I")); | |
498 | tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, _T("Toggle &help button\tCtrl-T")); | |
a1f79c1e | 499 | tbarMenu->AppendSeparator(); |
5e3b8b93 VZ |
500 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN1, _T("Toggle &1st radio button\tCtrl-1")); |
501 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN2, _T("Toggle &2nd radio button\tCtrl-2")); | |
502 | tbarMenu->Append(IDM_TOOLBAR_TOGGLERADIOBTN3, _T("Toggle &3rd radio button\tCtrl-3")); | |
3bf4189d | 503 | tbarMenu->AppendSeparator(); |
5e3b8b93 | 504 | tbarMenu->Append(IDM_TOOLBAR_CHANGE_TOOLTIP, _T("Change tool tip")); |
ec483b11 | 505 | tbarMenu->AppendSeparator(); |
f5e6e431 VZ |
506 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_TEXT, _T("Show &text\tCtrl-Alt-T")); |
507 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_ICONS, _T("Show &icons\tCtrl-Alt-I")); | |
508 | tbarMenu->AppendRadioItem(IDM_TOOLBAR_SHOW_BOTH, _T("Show &both\tCtrl-Alt-B")); | |
5e3b8b93 VZ |
509 | tbarMenu->AppendSeparator(); |
510 | tbarMenu->Append(IDM_TOOLBAR_CUSTOM_PATH, _T("Custom &bitmap...\tCtrl-B")); | |
b487a08f | 511 | |
ad9bb75f | 512 | wxMenu *fileMenu = new wxMenu; |
2b5f62a0 | 513 | fileMenu->Append(wxID_EXIT, _T("E&xit\tAlt-X"), _T("Quit toolbar sample") ); |
ad9bb75f | 514 | |
ad9bb75f | 515 | wxMenu *helpMenu = new wxMenu; |
2b5f62a0 | 516 | helpMenu->Append(wxID_HELP, _T("&About"), _T("About toolbar sample")); |
ad9bb75f VZ |
517 | |
518 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
519 | ||
2b5f62a0 VZ |
520 | menuBar->Append(fileMenu, _T("&File")); |
521 | menuBar->Append(tbarMenu, _T("&Toolbar")); | |
522 | menuBar->Append(helpMenu, _T("&Help")); | |
ad9bb75f VZ |
523 | |
524 | // Associate the menu bar with the frame | |
525 | SetMenuBar(menuBar); | |
526 | ||
bc2ec626 | 527 | menuBar->Check(IDM_TOOLBAR_SHOW_BOTH, true); |
6a1b3ead | 528 | menuBar->Check(IDM_TOOLBAR_TOGGLETOOLTIPS, true); |
c631abda | 529 | |
ad9bb75f | 530 | // Create the toolbar |
5ef2e633 | 531 | RecreateToolbar(); |
949b8e62 | 532 | |
5f4d35b8 | 533 | m_textWindow = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE); |
5ef2e633 | 534 | } |
ad9bb75f | 535 | |
f6bcfd97 BP |
536 | void MyFrame::LayoutChildren() |
537 | { | |
538 | wxSize size = GetClientSize(); | |
539 | ||
540 | int offset; | |
541 | if ( m_tbar ) | |
542 | { | |
7a7d2a14 | 543 | m_tbar->SetSize(0, 0, wxDefaultCoord, size.y); |
f6bcfd97 BP |
544 | |
545 | offset = m_tbar->GetSize().x; | |
546 | } | |
547 | else | |
548 | { | |
549 | offset = 0; | |
550 | } | |
551 | ||
552 | m_textWindow->SetSize(offset, 0, size.x - offset, size.y); | |
553 | } | |
554 | ||
555 | void MyFrame::OnSize(wxSizeEvent& event) | |
556 | { | |
557 | if ( m_tbar ) | |
558 | { | |
559 | LayoutChildren(); | |
560 | } | |
561 | else | |
562 | { | |
563 | event.Skip(); | |
564 | } | |
565 | } | |
566 | ||
ba0abe3c VZ |
567 | void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event)) |
568 | { | |
569 | wxToolBar *tbar = GetToolBar(); | |
570 | ||
571 | if ( !tbar ) | |
572 | { | |
573 | RecreateToolbar(); | |
574 | } | |
575 | else | |
576 | { | |
577 | delete tbar; | |
578 | ||
579 | SetToolBar(NULL); | |
580 | } | |
581 | } | |
582 | ||
96dc4236 VZ |
583 | void MyFrame::OnToggleHorizontalText(wxCommandEvent& WXUNUSED(event)) |
584 | { | |
585 | m_horzText = !m_horzText; | |
586 | ||
587 | RecreateToolbar(); | |
588 | } | |
589 | ||
f6bcfd97 BP |
590 | void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event)) |
591 | { | |
592 | if ( m_tbar ) | |
593 | { | |
594 | delete m_tbar; | |
595 | m_tbar = NULL; | |
596 | } | |
597 | else | |
598 | { | |
8927d6d1 VZ |
599 | long style = GetToolBar() ? GetToolBar()->GetWindowStyle() |
600 | : TOOLBAR_STYLE; | |
ec483b11 VZ |
601 | style &= ~wxTB_HORIZONTAL; |
602 | style |= wxTB_VERTICAL; | |
603 | ||
bc2ec626 | 604 | m_tbar = new wxToolBar(this, wxID_ANY, |
f6bcfd97 | 605 | wxDefaultPosition, wxDefaultSize, |
ec483b11 VZ |
606 | style); |
607 | ||
69c14ebe VZ |
608 | m_tbar->SetMargins(4, 4); |
609 | ||
3bf4189d VZ |
610 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_1, _T("First"), wxBITMAP(new)); |
611 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_2, _T("Second"), wxBITMAP(open)); | |
612 | m_tbar->AddRadioTool(IDM_TOOLBAR_OTHER_3, _T("Third"), wxBITMAP(save)); | |
ec483b11 VZ |
613 | m_tbar->AddSeparator(); |
614 | m_tbar->AddTool(wxID_HELP, _T("Help"), wxBITMAP(help)); | |
615 | ||
f6bcfd97 BP |
616 | m_tbar->Realize(); |
617 | } | |
618 | ||
619 | LayoutChildren(); | |
620 | } | |
621 | ||
5ef2e633 VZ |
622 | void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event)) |
623 | { | |
624 | m_smallToolbar = !m_smallToolbar; | |
ad9bb75f | 625 | |
5ef2e633 | 626 | RecreateToolbar(); |
7fb23305 VZ |
627 | } |
628 | ||
98066234 VZ |
629 | void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event)) |
630 | { | |
631 | // m_rows may be only 1 or 2 | |
632 | m_rows = 3 - m_rows; | |
633 | ||
634 | GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows); | |
635 | ||
9fb35cf1 | 636 | //RecreateToolbar(); -- this is unneeded |
98066234 VZ |
637 | } |
638 | ||
6a1b3ead VZ |
639 | void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event)) |
640 | { | |
641 | m_showTooltips = !m_showTooltips; | |
642 | ||
643 | RecreateToolbar(); | |
644 | } | |
645 | ||
8d0a7b56 VZ |
646 | void MyFrame::OnToggleCustomDisabled(wxCommandEvent& WXUNUSED(event)) |
647 | { | |
648 | m_useCustomDisabled = !m_useCustomDisabled; | |
649 | ||
650 | RecreateToolbar(); | |
651 | } | |
652 | ||
5ef2e633 | 653 | void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event)) |
7fb23305 | 654 | { |
5ef2e633 | 655 | m_horzToolbar = !m_horzToolbar; |
7fb23305 | 656 | |
5ef2e633 | 657 | RecreateToolbar(); |
14d1ccd8 JS |
658 | } |
659 | ||
47908e25 | 660 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
14d1ccd8 | 661 | { |
bc2ec626 | 662 | Close(true); |
14d1ccd8 JS |
663 | } |
664 | ||
e08bbf37 | 665 | void MyFrame::OnAbout(wxCommandEvent& event) |
14d1ccd8 | 666 | { |
e08bbf37 VZ |
667 | if ( event.IsChecked() ) |
668 | m_textWindow->WriteText( _T("Help button down now.\n") ); | |
669 | else | |
670 | m_textWindow->WriteText( _T("Help button up now.\n") ); | |
671 | ||
be5a51fb | 672 | (void)wxMessageBox(_T("wxWidgets toolbar sample"), _T("About wxToolBar")); |
13437238 | 673 | } |
14d1ccd8 | 674 | |
13437238 JS |
675 | void MyFrame::OnToolLeftClick(wxCommandEvent& event) |
676 | { | |
e179bd65 RR |
677 | wxString str; |
678 | str.Printf( _T("Clicked on tool %d\n"), event.GetId()); | |
679 | m_textWindow->WriteText( str ); | |
ad9bb75f | 680 | |
e179bd65 RR |
681 | if (event.GetId() == wxID_COPY) |
682 | { | |
7fb23305 | 683 | DoEnablePrint(); |
e179bd65 | 684 | } |
ad9bb75f | 685 | |
e179bd65 RR |
686 | if (event.GetId() == wxID_CUT) |
687 | { | |
7fb23305 | 688 | DoToggleHelp(); |
e179bd65 | 689 | } |
1c4f8f8d | 690 | |
97d7bfb8 RR |
691 | if (event.GetId() == wxID_PRINT) |
692 | { | |
693 | DoDeletePrint(); | |
694 | } | |
14d1ccd8 JS |
695 | } |
696 | ||
e052a316 VZ |
697 | void MyFrame::OnToolRightClick(wxCommandEvent& event) |
698 | { | |
699 | m_textWindow->AppendText( | |
60c474a0 MB |
700 | wxString::Format(_T("Tool %d right clicked.\n"), |
701 | (int) event.GetInt())); | |
e052a316 VZ |
702 | } |
703 | ||
1c383dba VZ |
704 | void MyFrame::OnCombo(wxCommandEvent& event) |
705 | { | |
706 | wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str()); | |
707 | } | |
708 | ||
7fb23305 VZ |
709 | void MyFrame::DoEnablePrint() |
710 | { | |
2b5f62a0 VZ |
711 | if ( !m_nPrint ) |
712 | return; | |
713 | ||
ad4ae6ed | 714 | wxToolBarBase *tb = GetToolBar(); |
2b5f62a0 | 715 | tb->EnableTool(wxID_PRINT, !tb->GetToolEnabled(wxID_PRINT)); |
7fb23305 VZ |
716 | } |
717 | ||
97d7bfb8 RR |
718 | void MyFrame::DoDeletePrint() |
719 | { | |
2b5f62a0 VZ |
720 | if ( !m_nPrint ) |
721 | return; | |
1c4f8f8d | 722 | |
2b5f62a0 | 723 | wxToolBarBase *tb = GetToolBar(); |
97d7bfb8 | 724 | tb->DeleteTool( wxID_PRINT ); |
2b5f62a0 VZ |
725 | |
726 | m_nPrint--; | |
97d7bfb8 RR |
727 | } |
728 | ||
7fb23305 VZ |
729 | void MyFrame::DoToggleHelp() |
730 | { | |
ad4ae6ed | 731 | wxToolBarBase *tb = GetToolBar(); |
7fb23305 VZ |
732 | tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) ); |
733 | } | |
734 | ||
5ef2e633 VZ |
735 | void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event) |
736 | { | |
96dc4236 VZ |
737 | event.Enable( m_textWindow->CanCopy() ); |
738 | } | |
739 | ||
740 | void MyFrame::OnUpdateToggleHorzText(wxUpdateUIEvent& event) | |
741 | { | |
742 | wxToolBar *tbar = GetToolBar(); | |
743 | event.Enable( tbar && | |
744 | tbar->HasFlag(wxTB_TEXT) && | |
745 | !tbar->HasFlag(wxTB_NOICONS) ); | |
5ef2e633 VZ |
746 | } |
747 | ||
a1f79c1e VZ |
748 | void MyFrame::OnChangeToolTip(wxCommandEvent& WXUNUSED(event)) |
749 | { | |
750 | GetToolBar()->SetToolShortHelp(wxID_NEW, _T("New toolbar button")); | |
751 | } | |
752 | ||
ec483b11 VZ |
753 | void MyFrame::OnToolbarStyle(wxCommandEvent& event) |
754 | { | |
755 | long style = GetToolBar()->GetWindowStyle(); | |
756 | style &= ~(wxTB_NOICONS | wxTB_TEXT); | |
757 | ||
758 | switch ( event.GetId() ) | |
759 | { | |
760 | case IDM_TOOLBAR_SHOW_TEXT: | |
761 | style |= wxTB_NOICONS | wxTB_TEXT; | |
762 | break; | |
763 | ||
764 | case IDM_TOOLBAR_SHOW_ICONS: | |
765 | // nothing to do | |
766 | break; | |
767 | ||
768 | case IDM_TOOLBAR_SHOW_BOTH: | |
769 | style |= wxTB_TEXT; | |
770 | } | |
771 | ||
772 | GetToolBar()->SetWindowStyle(style); | |
773 | } | |
774 | ||
5e3b8b93 VZ |
775 | void MyFrame::OnToolbarCustomBitmap(wxCommandEvent& WXUNUSED(event)) |
776 | { | |
777 | m_pathBmp = wxFileSelector(_T("Custom bitmap path")); | |
778 | ||
779 | RecreateToolbar(); | |
780 | } | |
781 | ||
bdc72a22 VZ |
782 | void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event)) |
783 | { | |
2b5f62a0 | 784 | m_nPrint++; |
bdc72a22 | 785 | |
2b5f62a0 VZ |
786 | wxToolBarBase *tb = GetToolBar(); |
787 | tb->InsertTool(0, wxID_PRINT, _T("New print"), | |
788 | wxBITMAP(print), wxNullBitmap, | |
789 | wxITEM_NORMAL, | |
790 | _T("Delete this tool"), | |
791 | _T("This button was inserted into the toolbar")); | |
792 | ||
793 | // must call Realize() after adding a new button | |
794 | tb->Realize(); | |
bdc72a22 VZ |
795 | } |
796 | ||
13437238 JS |
797 | void MyFrame::OnToolEnter(wxCommandEvent& event) |
798 | { | |
8520f137 | 799 | #if wxUSE_STATUSBAR |
ad4ae6ed VZ |
800 | if (event.GetSelection() > -1) |
801 | { | |
802 | wxString str; | |
803 | str.Printf(_T("This is tool number %d"), event.GetSelection()); | |
804 | SetStatusText(str); | |
805 | } | |
806 | else | |
5f4d35b8 | 807 | SetStatusText(wxEmptyString); |
8520f137 WS |
808 | #else |
809 | wxUnusedVar(event); | |
810 | #endif // wxUSE_STATUSBAR | |
13437238 | 811 | } |
14d1ccd8 | 812 | |
3bf4189d VZ |
813 | void MyFrame::OnToggleRadioBtn(wxCommandEvent& event) |
814 | { | |
815 | if ( m_tbar ) | |
816 | { | |
817 | m_tbar->ToggleTool(IDM_TOOLBAR_OTHER_1 + | |
818 | event.GetId() - IDM_TOOLBAR_TOGGLERADIOBTN1, true); | |
819 | } | |
820 | } |