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