]>
Commit | Line | Data |
---|---|---|
14d1ccd8 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: wxToolBar sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
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". |
ad9bb75f | 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 VZ |
32 | #include <wx/log.h> |
33 | ||
ad9bb75f VZ |
34 | // ---------------------------------------------------------------------------- |
35 | // resources | |
36 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 37 | |
a4294b78 | 38 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
ad9bb75f VZ |
39 | #include "mondrian.xpm" |
40 | #include "bitmaps/new.xpm" | |
41 | #include "bitmaps/open.xpm" | |
42 | #include "bitmaps/save.xpm" | |
43 | #include "bitmaps/copy.xpm" | |
44 | #include "bitmaps/cut.xpm" | |
45 | // #include "bitmaps/paste.xpm" | |
46 | #include "bitmaps/print.xpm" | |
47 | #include "bitmaps/preview.xpm" | |
48 | #include "bitmaps/help.xpm" | |
49 | #endif // GTK or Motif | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // classes | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // Define a new application | |
56 | class MyApp: public wxApp | |
57 | { | |
58 | public: | |
59 | bool OnInit(); | |
60 | bool InitToolbar(wxToolBar* toolBar, bool smallicons = FALSE); | |
61 | }; | |
47908e25 | 62 | |
ad9bb75f VZ |
63 | // Define a new frame |
64 | class MyFrame: public wxFrame | |
65 | { | |
66 | public: | |
67 | MyFrame(wxFrame *parent, | |
68 | wxWindowID id = -1, | |
69 | const wxString& title = "wxToolBar Sample", | |
70 | const wxPoint& pos = wxDefaultPosition, | |
71 | const wxSize& size = wxDefaultSize, | |
72 | long style = wxDEFAULT_FRAME_STYLE); | |
14d1ccd8 | 73 | |
ad9bb75f VZ |
74 | void OnQuit(wxCommandEvent& event); |
75 | void OnAbout(wxCommandEvent& event); | |
76 | ||
77 | void OnToggleToolbar(wxCommandEvent& event); | |
78 | void OnEnablePrint(wxCommandEvent& event) { DoEnablePrint(); } | |
97d7bfb8 | 79 | void OnDeletePrint(wxCommandEvent& event) { DoDeletePrint(); } |
ad9bb75f VZ |
80 | void OnToggleHelp(wxCommandEvent& event) { DoToggleHelp(); } |
81 | ||
ad9bb75f VZ |
82 | void OnToolLeftClick(wxCommandEvent& event); |
83 | void OnToolEnter(wxCommandEvent& event); | |
84 | ||
1c383dba VZ |
85 | void OnCombo(wxCommandEvent& event); |
86 | ||
ad9bb75f VZ |
87 | private: |
88 | void DoEnablePrint(); | |
97d7bfb8 | 89 | void DoDeletePrint(); |
ad9bb75f VZ |
90 | void DoToggleHelp(); |
91 | ||
92 | bool m_smallToolbar; | |
93 | wxTextCtrl* m_textWindow; | |
94 | ||
ad9bb75f VZ |
95 | DECLARE_EVENT_TABLE() |
96 | }; | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // constants | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | const int ID_TOOLBAR = 500; | |
103 | ||
104 | enum | |
14d1ccd8 | 105 | { |
ad9bb75f VZ |
106 | IDM_TOOLBAR_TOGGLETOOLBAR = 200, |
107 | IDM_TOOLBAR_ENABLEPRINT, | |
97d7bfb8 | 108 | IDM_TOOLBAR_DELETEPRINT, |
1c383dba VZ |
109 | IDM_TOOLBAR_TOGGLEHELP, |
110 | ||
111 | ID_COMBO = 1000 | |
ad9bb75f | 112 | }; |
14d1ccd8 | 113 | |
ad9bb75f VZ |
114 | // ---------------------------------------------------------------------------- |
115 | // event tables | |
116 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 117 | |
ad9bb75f VZ |
118 | // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar |
119 | // help button. | |
14d1ccd8 | 120 | |
ad9bb75f VZ |
121 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
122 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
123 | EVT_MENU(wxID_HELP, MyFrame::OnAbout) | |
14d1ccd8 | 124 | |
ad9bb75f VZ |
125 | EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR, MyFrame::OnToggleToolbar) |
126 | EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint) | |
97d7bfb8 | 127 | EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint) |
ad9bb75f | 128 | EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp) |
14d1ccd8 | 129 | |
ad9bb75f | 130 | EVT_MENU(-1, MyFrame::OnToolLeftClick) |
14d1ccd8 | 131 | |
1c383dba VZ |
132 | EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo) |
133 | ||
ad9bb75f VZ |
134 | EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter) |
135 | END_EVENT_TABLE() | |
14d1ccd8 | 136 | |
ad9bb75f VZ |
137 | // ============================================================================ |
138 | // implementation | |
139 | // ============================================================================ | |
14d1ccd8 | 140 | |
ad9bb75f VZ |
141 | // ---------------------------------------------------------------------------- |
142 | // MyApp | |
143 | // ---------------------------------------------------------------------------- | |
14d1ccd8 | 144 | |
ad9bb75f | 145 | IMPLEMENT_APP(MyApp) |
14d1ccd8 | 146 | |
ad9bb75f VZ |
147 | // The `main program' equivalent, creating the windows and returning the |
148 | // main frame | |
149 | bool MyApp::OnInit() | |
150 | { | |
151 | // Create the main frame window | |
152 | MyFrame* frame = new MyFrame((wxFrame *) NULL, -1, | |
153 | "wxToolBar Sample", | |
154 | wxPoint(100, 100), wxSize(450, 300)); | |
14d1ccd8 | 155 | |
ad9bb75f VZ |
156 | // VZ: what's this for?? |
157 | #if 0 | |
7fb23305 VZ |
158 | // Force a resize. This should probably be replaced by a call to a wxFrame |
159 | // function that lays out default decorations and the remaining content window. | |
160 | wxSizeEvent event(wxSize(-1, -1), frame->GetId()); | |
161 | frame->OnSize(event); | |
ad9bb75f VZ |
162 | #endif // 0 |
163 | ||
7fb23305 VZ |
164 | frame->Show(TRUE); |
165 | ||
166 | frame->SetStatusText("Hello, wxWindows"); | |
167 | ||
168 | SetTopWindow(frame); | |
169 | ||
170 | return TRUE; | |
14d1ccd8 JS |
171 | } |
172 | ||
d676ebcf | 173 | bool MyApp::InitToolbar(wxToolBar* toolBar, bool smallicons) |
14d1ccd8 JS |
174 | { |
175 | // Set up toolbar | |
176 | wxBitmap* toolBarBitmaps[8]; | |
177 | ||
178 | #ifdef __WXMSW__ | |
179 | toolBarBitmaps[0] = new wxBitmap("icon1"); | |
180 | toolBarBitmaps[1] = new wxBitmap("icon2"); | |
d676ebcf | 181 | if ( !smallicons ) |
7fb23305 VZ |
182 | { |
183 | toolBarBitmaps[2] = new wxBitmap("icon3"); | |
184 | toolBarBitmaps[3] = new wxBitmap("icon4"); | |
185 | toolBarBitmaps[4] = new wxBitmap("icon5"); | |
186 | toolBarBitmaps[5] = new wxBitmap("icon6"); | |
187 | toolBarBitmaps[6] = new wxBitmap("icon7"); | |
188 | toolBarBitmaps[7] = new wxBitmap("icon8"); | |
189 | } | |
47908e25 RR |
190 | #else |
191 | toolBarBitmaps[0] = new wxBitmap( new_xpm ); | |
192 | toolBarBitmaps[1] = new wxBitmap( open_xpm ); | |
d676ebcf | 193 | if ( !smallicons ) |
7fb23305 VZ |
194 | { |
195 | toolBarBitmaps[2] = new wxBitmap( save_xpm ); | |
196 | toolBarBitmaps[3] = new wxBitmap( copy_xpm ); | |
197 | toolBarBitmaps[4] = new wxBitmap( cut_xpm ); | |
198 | toolBarBitmaps[5] = new wxBitmap( preview_xpm ); | |
199 | toolBarBitmaps[6] = new wxBitmap( print_xpm ); | |
200 | toolBarBitmaps[7] = new wxBitmap( help_xpm ); | |
201 | } | |
14d1ccd8 JS |
202 | #endif |
203 | ||
204 | #ifdef __WXMSW__ | |
205 | int width = 24; | |
206 | #else | |
207 | int width = 16; | |
208 | #endif | |
14d1ccd8 JS |
209 | int currentX = 5; |
210 | ||
a4294b78 | 211 | toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file"); |
14d1ccd8 | 212 | currentX += width + 5; |
a4294b78 | 213 | toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file"); |
c8f1f088 VZ |
214 | currentX += width + 5; |
215 | toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1"); | |
216 | ||
1c383dba VZ |
217 | wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO); |
218 | combo->Append("This"); | |
219 | combo->Append("is a"); | |
220 | combo->Append("combobox"); | |
221 | combo->Append("in a"); | |
222 | combo->Append("toolbar"); | |
223 | toolBar->AddControl(combo); | |
7fb23305 | 224 | |
d676ebcf | 225 | if ( !smallicons ) |
7fb23305 VZ |
226 | { |
227 | currentX += width + 5; | |
f152cf5a | 228 | toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2"); |
7fb23305 VZ |
229 | currentX += width + 5; |
230 | toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button"); | |
231 | currentX += width + 5; | |
232 | toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste"); | |
233 | currentX += width + 5; | |
97d7bfb8 | 234 | toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool"); |
7fb23305 VZ |
235 | currentX += width + 5; |
236 | toolBar->AddSeparator(); | |
f152cf5a | 237 | toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button"); |
7fb23305 | 238 | } |
14d1ccd8 | 239 | |
81d66cf3 | 240 | toolBar->Realize(); |
14d1ccd8 JS |
241 | |
242 | // Can delete the bitmaps since they're reference counted | |
c8f1f088 | 243 | int i, max = smallicons ? 3 : WXSIZEOF(toolBarBitmaps); |
7fb23305 | 244 | for (i = 0; i < max; i++) |
14d1ccd8 | 245 | delete toolBarBitmaps[i]; |
13437238 JS |
246 | |
247 | return TRUE; | |
14d1ccd8 JS |
248 | } |
249 | ||
ad9bb75f VZ |
250 | // ---------------------------------------------------------------------------- |
251 | // MyFrame | |
252 | // ---------------------------------------------------------------------------- | |
13437238 JS |
253 | |
254 | // Define my frame constructor | |
7fb23305 VZ |
255 | MyFrame::MyFrame(wxFrame* parent, |
256 | wxWindowID id, | |
257 | const wxString& title, | |
258 | const wxPoint& pos, | |
259 | const wxSize& size, | |
260 | long style) | |
261 | : wxFrame(parent, id, title, pos, size, style) | |
14d1ccd8 | 262 | { |
e179bd65 | 263 | m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE); |
7fb23305 | 264 | m_smallToolbar = FALSE; |
ad9bb75f VZ |
265 | |
266 | // Give it a status line | |
267 | CreateStatusBar(); | |
268 | ||
269 | // Give it an icon | |
270 | SetIcon(wxICON(mondrian)); | |
271 | ||
272 | // Make a menubar | |
273 | wxMenu *tbarMenu = new wxMenu; | |
274 | tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBAR, "&Toggle toolbar", "Change the toolbar kind"); | |
275 | tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button", ""); | |
97d7bfb8 | 276 | tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button", ""); |
ad9bb75f VZ |
277 | tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button", ""); |
278 | ||
279 | wxMenu *fileMenu = new wxMenu; | |
280 | fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" ); | |
281 | ||
ad9bb75f VZ |
282 | wxMenu *helpMenu = new wxMenu; |
283 | helpMenu->Append(wxID_HELP, "&About", "About toolbar sample"); | |
284 | ||
285 | wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE ); | |
286 | ||
287 | menuBar->Append(fileMenu, "&File"); | |
288 | menuBar->Append(tbarMenu, "&Toolbar"); | |
ad9bb75f VZ |
289 | menuBar->Append(helpMenu, "&Help"); |
290 | ||
291 | // Associate the menu bar with the frame | |
292 | SetMenuBar(menuBar); | |
293 | ||
294 | // Create the toolbar | |
295 | wxToolBar *tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL | | |
296 | wxTB_FLAT | wxTB_DOCKABLE, | |
297 | ID_TOOLBAR); | |
298 | ||
bf9e3e73 | 299 | tbar->SetMargins( 4, 4 ); |
ad9bb75f VZ |
300 | |
301 | wxGetApp().InitToolbar(tbar); | |
7fb23305 VZ |
302 | } |
303 | ||
ad9bb75f | 304 | void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event)) |
7fb23305 | 305 | { |
ad9bb75f VZ |
306 | // delete and recreate the toolbar |
307 | wxToolBar *tbar = GetToolBar(); | |
308 | delete tbar; | |
309 | ||
7fb23305 | 310 | SetToolBar(NULL); |
ad9bb75f VZ |
311 | tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL | |
312 | wxTB_FLAT | wxTB_DOCKABLE, | |
313 | ID_TOOLBAR); | |
7fb23305 VZ |
314 | |
315 | m_smallToolbar = !m_smallToolbar; | |
ad9bb75f | 316 | wxGetApp().InitToolbar(tbar, m_smallToolbar); |
14d1ccd8 JS |
317 | } |
318 | ||
47908e25 | 319 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
14d1ccd8 | 320 | { |
13437238 | 321 | Close(TRUE); |
14d1ccd8 JS |
322 | } |
323 | ||
47908e25 | 324 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
14d1ccd8 | 325 | { |
1d5b7a0b | 326 | (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar"); |
13437238 | 327 | } |
14d1ccd8 | 328 | |
13437238 JS |
329 | void MyFrame::OnToolLeftClick(wxCommandEvent& event) |
330 | { | |
e179bd65 RR |
331 | wxString str; |
332 | str.Printf( _T("Clicked on tool %d\n"), event.GetId()); | |
333 | m_textWindow->WriteText( str ); | |
ad9bb75f | 334 | |
e179bd65 RR |
335 | if (event.GetId() == wxID_HELP) |
336 | { | |
ad9bb75f | 337 | if ( event.GetExtraLong() != 0 ) |
e179bd65 RR |
338 | m_textWindow->WriteText( _T("Help button down now.\n") ); |
339 | else | |
340 | m_textWindow->WriteText( _T("Help button up now.\n") ); | |
341 | } | |
ad9bb75f | 342 | |
e179bd65 RR |
343 | if (event.GetId() == wxID_COPY) |
344 | { | |
7fb23305 | 345 | DoEnablePrint(); |
e179bd65 | 346 | } |
ad9bb75f | 347 | |
e179bd65 RR |
348 | if (event.GetId() == wxID_CUT) |
349 | { | |
7fb23305 | 350 | DoToggleHelp(); |
e179bd65 | 351 | } |
1c4f8f8d | 352 | |
97d7bfb8 RR |
353 | if (event.GetId() == wxID_PRINT) |
354 | { | |
355 | DoDeletePrint(); | |
356 | } | |
14d1ccd8 JS |
357 | } |
358 | ||
1c383dba VZ |
359 | void MyFrame::OnCombo(wxCommandEvent& event) |
360 | { | |
361 | wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str()); | |
362 | } | |
363 | ||
7fb23305 VZ |
364 | void MyFrame::DoEnablePrint() |
365 | { | |
366 | wxToolBar *tb = GetToolBar(); | |
367 | if (tb->GetToolEnabled(wxID_PRINT)) | |
368 | tb->EnableTool( wxID_PRINT, FALSE ); | |
369 | else | |
370 | tb->EnableTool( wxID_PRINT, TRUE ); | |
371 | } | |
372 | ||
97d7bfb8 RR |
373 | void MyFrame::DoDeletePrint() |
374 | { | |
375 | wxToolBar *tb = GetToolBar(); | |
1c4f8f8d VZ |
376 | |
377 | // only implemented in wxGTK for now | |
378 | #ifndef __WXGTK__ | |
162999bf JS |
379 | wxMessageBox("Sorry, wxToolBar::DeleteTool is not implemented under Windows."); |
380 | #else | |
97d7bfb8 | 381 | tb->DeleteTool( wxID_PRINT ); |
162999bf | 382 | #endif |
97d7bfb8 RR |
383 | } |
384 | ||
7fb23305 VZ |
385 | void MyFrame::DoToggleHelp() |
386 | { | |
387 | wxToolBar *tb = GetToolBar(); | |
388 | tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) ); | |
389 | } | |
390 | ||
13437238 JS |
391 | void MyFrame::OnToolEnter(wxCommandEvent& event) |
392 | { | |
81d66cf3 | 393 | if (event.GetSelection() > -1) |
13437238 JS |
394 | { |
395 | wxString str; | |
58dea4b0 | 396 | str.Printf(_T("This is tool number %d"), event.GetSelection()); |
13437238 JS |
397 | SetStatusText(str); |
398 | } | |
399 | else | |
400 | SetStatusText(""); | |
401 | } | |
14d1ccd8 | 402 |