]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/toolbar.cpp
last line missing
[wxWidgets.git] / samples / toolbar / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include <wx/wxprec.h>
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include <wx/wx.h>
29 #endif
30
31 #include <wx/toolbar.h>
32 #include <wx/log.h>
33
34 // define this to 1 to use wxToolBarSimple instead of the native one
35 #define USE_GENERIC_TBAR 0
36
37 #if USE_GENERIC_TBAR
38 #if !wxUSE_TOOLBAR_SIMPLE
39 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
40 to 1 in setup.h and recompile the library.
41 #else
42 #include <wx/tbarsmpl.h>
43 #endif
44 #endif // USE_GENERIC_TBAR
45
46 // ----------------------------------------------------------------------------
47 // resources
48 // ----------------------------------------------------------------------------
49
50 #if defined(__WXGTK__) || defined(__WXMOTIF__)
51 #include "mondrian.xpm"
52 #include "bitmaps/new.xpm"
53 #include "bitmaps/open.xpm"
54 #include "bitmaps/save.xpm"
55 #include "bitmaps/copy.xpm"
56 #include "bitmaps/cut.xpm"
57 #include "bitmaps/preview.xpm" // paste XPM
58 #include "bitmaps/print.xpm"
59 #include "bitmaps/help.xpm"
60 #endif // GTK or Motif
61
62 // ----------------------------------------------------------------------------
63 // classes
64 // ----------------------------------------------------------------------------
65
66 // Define a new application
67 class MyApp : public wxApp
68 {
69 public:
70 bool OnInit();
71 };
72
73 // Define a new frame
74 class MyFrame: public wxFrame
75 {
76 public:
77 MyFrame(wxFrame *parent,
78 wxWindowID id = -1,
79 const wxString& title = "wxToolBar Sample",
80 const wxPoint& pos = wxDefaultPosition,
81 const wxSize& size = wxDefaultSize,
82 long style = wxDEFAULT_FRAME_STYLE);
83
84 void RecreateToolbar();
85
86 void OnQuit(wxCommandEvent& event);
87 void OnAbout(wxCommandEvent& event);
88
89 void OnToggleToolbarSize(wxCommandEvent& event);
90 void OnToggleToolbarOrient(wxCommandEvent& event);
91 void OnToggleToolbarRows(wxCommandEvent& event);
92
93 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
94 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
95 void OnInsertPrint(wxCommandEvent& event);
96 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
97
98 void OnToolLeftClick(wxCommandEvent& event);
99 void OnToolEnter(wxCommandEvent& event);
100
101 void OnCombo(wxCommandEvent& event);
102
103 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
104
105 #if USE_GENERIC_TBAR
106 virtual wxToolBar *OnCreateToolBar(long style,
107 wxWindowID id,
108 const wxString& name );
109 #endif // USE_GENERIC_TBAR
110
111 private:
112 void DoEnablePrint();
113 void DoDeletePrint();
114 void DoToggleHelp();
115
116 bool m_smallToolbar,
117 m_horzToolbar;
118 size_t m_rows; // 1 or 2 only
119
120 wxTextCtrl* m_textWindow;
121
122 DECLARE_EVENT_TABLE()
123 };
124
125 // ----------------------------------------------------------------------------
126 // constants
127 // ----------------------------------------------------------------------------
128
129 const int ID_TOOLBAR = 500;
130
131 enum
132 {
133 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
134 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
135 IDM_TOOLBAR_TOGGLETOOLBARROWS,
136 IDM_TOOLBAR_ENABLEPRINT,
137 IDM_TOOLBAR_DELETEPRINT,
138 IDM_TOOLBAR_INSERTPRINT,
139 IDM_TOOLBAR_TOGGLEHELP,
140
141 ID_COMBO = 1000
142 };
143
144 // ----------------------------------------------------------------------------
145 // event tables
146 // ----------------------------------------------------------------------------
147
148 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
149 // help button.
150
151 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
152 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
153 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
154
155 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
156 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
157 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
158
159 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
160 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
161 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
162 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
163
164 EVT_MENU(-1, MyFrame::OnToolLeftClick)
165
166 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
167
168 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
169
170 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
171 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
172 END_EVENT_TABLE()
173
174 // ============================================================================
175 // implementation
176 // ============================================================================
177
178 // ----------------------------------------------------------------------------
179 // MyApp
180 // ----------------------------------------------------------------------------
181
182 IMPLEMENT_APP(MyApp)
183
184 // The `main program' equivalent, creating the windows and returning the
185 // main frame
186 bool MyApp::OnInit()
187 {
188 // Create the main frame window
189 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
190 "wxToolBar Sample",
191 wxPoint(100, 100), wxSize(450, 300));
192
193 frame->Show(TRUE);
194
195 frame->SetStatusText("Hello, wxWindows");
196
197 SetTopWindow(frame);
198
199 return TRUE;
200 }
201
202 void MyFrame::RecreateToolbar()
203 {
204 // delete and recreate the toolbar
205 wxToolBarBase *toolBar = GetToolBar();
206 delete toolBar;
207
208 SetToolBar(NULL);
209
210 long style = wxNO_BORDER | wxTB_FLAT | wxTB_DOCKABLE;
211 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
212
213 toolBar = CreateToolBar(style, ID_TOOLBAR);
214 toolBar->SetMargins( 4, 4 );
215
216 // Set up toolbar
217 wxBitmap toolBarBitmaps[8];
218
219 toolBarBitmaps[0] = wxBITMAP(new);
220 toolBarBitmaps[1] = wxBITMAP(open);
221 if ( !m_smallToolbar )
222 {
223 toolBarBitmaps[2] = wxBITMAP(save);
224 toolBarBitmaps[3] = wxBITMAP(copy);
225 toolBarBitmaps[4] = wxBITMAP(cut);
226 toolBarBitmaps[5] = wxBITMAP(paste);
227 toolBarBitmaps[6] = wxBITMAP(print);
228 toolBarBitmaps[7] = wxBITMAP(help);
229 }
230
231 #ifdef __WXMSW__
232 int width = 24;
233 #else
234 int width = 16;
235 #endif
236
237 int currentX = 5;
238
239 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
240 currentX += width + 5;
241 toolBar->AddTool(wxID_OPEN, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
242
243 // neither the generic nor Motif native toolbars really support this
244 #if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
245 // adding a combo to a vertical toolbar is not very smart
246 if ( m_horzToolbar )
247 {
248 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO);
249 combo->Append("This");
250 combo->Append("is a");
251 combo->Append("combobox");
252 combo->Append("in a");
253 combo->Append("toolbar");
254 toolBar->AddControl(combo);
255 }
256 #endif // toolbars which don't support controls
257
258 if ( !m_smallToolbar )
259 {
260 currentX += width + 5;
261 toolBar->AddTool(wxID_SAVE, toolBarBitmaps[2], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
262 currentX += width + 5;
263 toolBar->AddTool(wxID_COPY, toolBarBitmaps[3], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
264 currentX += width + 5;
265 toolBar->AddTool(wxID_CUT, toolBarBitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
266 currentX += width + 5;
267 toolBar->AddTool(wxID_PASTE, toolBarBitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
268 currentX += width + 5;
269 toolBar->AddTool(wxID_PRINT, toolBarBitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool");
270 currentX += width + 5;
271 toolBar->AddSeparator();
272 toolBar->AddTool(wxID_HELP, toolBarBitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
273 }
274
275 // after adding the buttons to the toolbar, must call Realize() to reflect
276 // the changes
277 toolBar->Realize();
278
279 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
280 }
281
282 // ----------------------------------------------------------------------------
283 // MyFrame
284 // ----------------------------------------------------------------------------
285
286 // Define my frame constructor
287 MyFrame::MyFrame(wxFrame* parent,
288 wxWindowID id,
289 const wxString& title,
290 const wxPoint& pos,
291 const wxSize& size,
292 long style)
293 : wxFrame(parent, id, title, pos, size, style)
294 {
295 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
296
297 m_smallToolbar = FALSE;
298 m_horzToolbar = TRUE;
299 m_rows = 1;
300
301 // Give it a status line
302 CreateStatusBar();
303
304 // Give it an icon
305 SetIcon(wxICON(mondrian));
306
307 // Make a menubar
308 wxMenu *tbarMenu = new wxMenu;
309 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
310 "&Toggle toolbar size\tCtrl-S",
311 "Toggle between big/small toolbar",
312 TRUE);
313 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
314 "Toggle toolbar &orientation\tCtrl-O",
315 "Toggle toolbar orientation",
316 TRUE);
317 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
318 "Toggle number of &rows\tCtrl-R",
319 "Toggle number of toolbar rows between 1 and 2",
320 TRUE);
321
322 tbarMenu->AppendSeparator();
323
324 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
325 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
326 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
327 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
328
329 wxMenu *fileMenu = new wxMenu;
330 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
331
332 wxMenu *helpMenu = new wxMenu;
333 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
334
335 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
336
337 menuBar->Append(fileMenu, "&File");
338 menuBar->Append(tbarMenu, "&Toolbar");
339 menuBar->Append(helpMenu, "&Help");
340
341 // Associate the menu bar with the frame
342 SetMenuBar(menuBar);
343
344 // Create the toolbar
345 RecreateToolbar();
346 }
347
348 #if USE_GENERIC_TBAR
349
350 wxToolBar* MyFrame::OnCreateToolBar(long style,
351 wxWindowID id,
352 const wxString& name)
353 {
354 return (wxToolBar *)new wxToolBarSimple(this, id,
355 wxDefaultPosition, wxDefaultSize,
356 style, name);
357 }
358
359 #endif // USE_GENERIC_TBAR
360
361 void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
362 {
363 m_smallToolbar = !m_smallToolbar;
364
365 RecreateToolbar();
366 }
367
368 void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
369 {
370 // m_rows may be only 1 or 2
371 m_rows = 3 - m_rows;
372
373 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
374
375 //RecreateToolbar(); -- this is unneeded
376 }
377
378 void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
379 {
380 m_horzToolbar = !m_horzToolbar;
381
382 RecreateToolbar();
383 }
384
385 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
386 {
387 Close(TRUE);
388 }
389
390 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
391 {
392 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
393 }
394
395 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
396 {
397 wxString str;
398 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
399 m_textWindow->WriteText( str );
400
401 if (event.GetId() == wxID_HELP)
402 {
403 if ( event.GetExtraLong() != 0 )
404 m_textWindow->WriteText( _T("Help button down now.\n") );
405 else
406 m_textWindow->WriteText( _T("Help button up now.\n") );
407 }
408
409 if (event.GetId() == wxID_COPY)
410 {
411 DoEnablePrint();
412 }
413
414 if (event.GetId() == wxID_CUT)
415 {
416 DoToggleHelp();
417 }
418
419 if (event.GetId() == wxID_PRINT)
420 {
421 DoDeletePrint();
422 }
423 }
424
425 void MyFrame::OnCombo(wxCommandEvent& event)
426 {
427 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
428 }
429
430 void MyFrame::DoEnablePrint()
431 {
432 wxToolBarBase *tb = GetToolBar();
433 if (tb->GetToolEnabled(wxID_PRINT))
434 tb->EnableTool( wxID_PRINT, FALSE );
435 else
436 tb->EnableTool( wxID_PRINT, TRUE );
437 }
438
439 void MyFrame::DoDeletePrint()
440 {
441 wxToolBarBase *tb = GetToolBar();
442
443 tb->DeleteTool( wxID_PRINT );
444 }
445
446 void MyFrame::DoToggleHelp()
447 {
448 wxToolBarBase *tb = GetToolBar();
449 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
450 }
451
452 void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
453 {
454 event.Enable( m_textWindow->CanCopy() );
455 }
456
457 void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
458 {
459 wxBitmap bmp = wxBITMAP(print);
460
461 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
462 FALSE, (wxObject *) NULL,
463 "Delete this tool",
464 "This button was inserted into the toolbar");
465
466 GetToolBar()->Realize();
467 }
468
469 void MyFrame::OnToolEnter(wxCommandEvent& event)
470 {
471 if (event.GetSelection() > -1)
472 {
473 wxString str;
474 str.Printf(_T("This is tool number %d"), event.GetSelection());
475 SetStatusText(str);
476 }
477 else
478 SetStatusText("");
479 }
480