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