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