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