]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/toolbar.cpp
a small script to regenerate makefiles without running configure
[wxWidgets.git] / samples / toolbar / toolbar.cpp
CommitLineData
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".
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"
5ef2e633 45 #include "bitmaps/preview.xpm" // paste XPM
ad9bb75f 46 #include "bitmaps/print.xpm"
ad9bb75f
VZ
47 #include "bitmaps/help.xpm"
48#endif // GTK or Motif
49
50// ----------------------------------------------------------------------------
51// classes
52// ----------------------------------------------------------------------------
53
54// Define a new application
5ef2e633 55class MyApp : public wxApp
ad9bb75f
VZ
56{
57public:
58 bool OnInit();
ad9bb75f 59};
47908e25 60
ad9bb75f
VZ
61// Define a new frame
62class MyFrame: public wxFrame
63{
64public:
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);
14d1ccd8 71
5ef2e633
VZ
72 void RecreateToolbar();
73
ad9bb75f
VZ
74 void OnQuit(wxCommandEvent& event);
75 void OnAbout(wxCommandEvent& event);
76
5ef2e633
VZ
77 void OnToggleToolbarSize(wxCommandEvent& event);
78 void OnToggleToolbarOrient(wxCommandEvent& event);
98066234 79 void OnToggleToolbarRows(wxCommandEvent& event);
5ef2e633 80
ad9bb75f 81 void OnEnablePrint(wxCommandEvent& event) { DoEnablePrint(); }
97d7bfb8 82 void OnDeletePrint(wxCommandEvent& event) { DoDeletePrint(); }
bdc72a22 83 void OnInsertPrint(wxCommandEvent& event);
ad9bb75f
VZ
84 void OnToggleHelp(wxCommandEvent& event) { DoToggleHelp(); }
85
ad9bb75f
VZ
86 void OnToolLeftClick(wxCommandEvent& event);
87 void OnToolEnter(wxCommandEvent& event);
88
1c383dba
VZ
89 void OnCombo(wxCommandEvent& event);
90
5ef2e633
VZ
91 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
92
ad9bb75f
VZ
93private:
94 void DoEnablePrint();
97d7bfb8 95 void DoDeletePrint();
ad9bb75f
VZ
96 void DoToggleHelp();
97
5ef2e633
VZ
98 bool m_smallToolbar,
99 m_horzToolbar;
98066234
VZ
100 size_t m_rows; // 1 or 2 only
101
ad9bb75f
VZ
102 wxTextCtrl* m_textWindow;
103
ad9bb75f
VZ
104 DECLARE_EVENT_TABLE()
105};
106
107// ----------------------------------------------------------------------------
108// constants
109// ----------------------------------------------------------------------------
110
111const int ID_TOOLBAR = 500;
112
113enum
14d1ccd8 114{
5ef2e633
VZ
115 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
116 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
98066234 117 IDM_TOOLBAR_TOGGLETOOLBARROWS,
ad9bb75f 118 IDM_TOOLBAR_ENABLEPRINT,
97d7bfb8 119 IDM_TOOLBAR_DELETEPRINT,
bdc72a22 120 IDM_TOOLBAR_INSERTPRINT,
1c383dba
VZ
121 IDM_TOOLBAR_TOGGLEHELP,
122
123 ID_COMBO = 1000
ad9bb75f 124};
14d1ccd8 125
ad9bb75f
VZ
126// ----------------------------------------------------------------------------
127// event tables
128// ----------------------------------------------------------------------------
14d1ccd8 129
ad9bb75f
VZ
130// Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
131// help button.
14d1ccd8 132
ad9bb75f
VZ
133BEGIN_EVENT_TABLE(MyFrame, wxFrame)
134 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
135 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
14d1ccd8 136
5ef2e633
VZ
137 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
138 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
98066234 139 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
5ef2e633 140
ad9bb75f 141 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
97d7bfb8 142 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
bdc72a22 143 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
ad9bb75f 144 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
14d1ccd8 145
ad9bb75f 146 EVT_MENU(-1, MyFrame::OnToolLeftClick)
14d1ccd8 147
1c383dba
VZ
148 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
149
ad9bb75f 150 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
5ef2e633
VZ
151
152 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
153 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
ad9bb75f 154END_EVENT_TABLE()
14d1ccd8 155
ad9bb75f
VZ
156// ============================================================================
157// implementation
158// ============================================================================
14d1ccd8 159
ad9bb75f
VZ
160// ----------------------------------------------------------------------------
161// MyApp
162// ----------------------------------------------------------------------------
14d1ccd8 163
ad9bb75f 164IMPLEMENT_APP(MyApp)
14d1ccd8 165
ad9bb75f
VZ
166// The `main program' equivalent, creating the windows and returning the
167// main frame
168bool 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));
14d1ccd8 174
7fb23305
VZ
175 frame->Show(TRUE);
176
177 frame->SetStatusText("Hello, wxWindows");
178
179 SetTopWindow(frame);
180
181 return TRUE;
14d1ccd8
JS
182}
183
5ef2e633 184void MyFrame::RecreateToolbar()
14d1ccd8 185{
5ef2e633
VZ
186 // delete and recreate the toolbar
187 wxToolBar *toolBar = GetToolBar();
188 delete toolBar;
14d1ccd8 189
5ef2e633
VZ
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 }
14d1ccd8
JS
212
213#ifdef __WXMSW__
5ef2e633 214 int width = 24;
14d1ccd8 215#else
5ef2e633 216 int width = 16;
14d1ccd8 217#endif
c8f1f088 218
5ef2e633 219 int currentX = 5;
7fb23305 220
5ef2e633
VZ
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");
14d1ccd8 224
5ef2e633
VZ
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 }
14d1ccd8 236
5ef2e633
VZ
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 }
13437238 253
5ef2e633
VZ
254 // after adding the buttons to the toolbar, must call Realize() to reflect
255 // the changes
256 toolBar->Realize();
98066234
VZ
257
258 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
14d1ccd8
JS
259}
260
ad9bb75f
VZ
261// ----------------------------------------------------------------------------
262// MyFrame
263// ----------------------------------------------------------------------------
13437238
JS
264
265// Define my frame constructor
7fb23305
VZ
266MyFrame::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)
14d1ccd8 273{
e179bd65 274 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
98066234 275
7fb23305 276 m_smallToolbar = FALSE;
98066234
VZ
277 m_horzToolbar = TRUE;
278 m_rows = 1;
ad9bb75f
VZ
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;
5ef2e633
VZ
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);
98066234
VZ
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);
5ef2e633
VZ
300
301 tbarMenu->AppendSeparator();
302
fc6bbc6d
VZ
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", "");
ad9bb75f
VZ
307
308 wxMenu *fileMenu = new wxMenu;
309 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
310
ad9bb75f
VZ
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");
ad9bb75f
VZ
318 menuBar->Append(helpMenu, "&Help");
319
320 // Associate the menu bar with the frame
321 SetMenuBar(menuBar);
322
323 // Create the toolbar
5ef2e633
VZ
324 RecreateToolbar();
325}
ad9bb75f 326
5ef2e633
VZ
327void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
328{
329 m_smallToolbar = !m_smallToolbar;
ad9bb75f 330
5ef2e633 331 RecreateToolbar();
7fb23305
VZ
332}
333
98066234
VZ
334void 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
5ef2e633 344void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
7fb23305 345{
5ef2e633 346 m_horzToolbar = !m_horzToolbar;
7fb23305 347
5ef2e633 348 RecreateToolbar();
14d1ccd8
JS
349}
350
47908e25 351void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 352{
13437238 353 Close(TRUE);
14d1ccd8
JS
354}
355
47908e25 356void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 357{
1d5b7a0b 358 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 359}
14d1ccd8 360
13437238
JS
361void MyFrame::OnToolLeftClick(wxCommandEvent& event)
362{
e179bd65
RR
363 wxString str;
364 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
365 m_textWindow->WriteText( str );
ad9bb75f 366
e179bd65
RR
367 if (event.GetId() == wxID_HELP)
368 {
ad9bb75f 369 if ( event.GetExtraLong() != 0 )
e179bd65
RR
370 m_textWindow->WriteText( _T("Help button down now.\n") );
371 else
372 m_textWindow->WriteText( _T("Help button up now.\n") );
373 }
ad9bb75f 374
e179bd65
RR
375 if (event.GetId() == wxID_COPY)
376 {
7fb23305 377 DoEnablePrint();
e179bd65 378 }
ad9bb75f 379
e179bd65
RR
380 if (event.GetId() == wxID_CUT)
381 {
7fb23305 382 DoToggleHelp();
e179bd65 383 }
1c4f8f8d 384
97d7bfb8
RR
385 if (event.GetId() == wxID_PRINT)
386 {
387 DoDeletePrint();
388 }
14d1ccd8
JS
389}
390
1c383dba
VZ
391void MyFrame::OnCombo(wxCommandEvent& event)
392{
393 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
394}
395
7fb23305
VZ
396void 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
97d7bfb8
RR
405void MyFrame::DoDeletePrint()
406{
407 wxToolBar *tb = GetToolBar();
1c4f8f8d 408
97d7bfb8
RR
409 tb->DeleteTool( wxID_PRINT );
410}
411
7fb23305
VZ
412void MyFrame::DoToggleHelp()
413{
414 wxToolBar *tb = GetToolBar();
415 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
416}
417
5ef2e633
VZ
418void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
419{
420 event.Enable( m_textWindow->CanCopy() );
421}
422
bdc72a22
VZ
423void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
424{
5ef2e633 425 wxBitmap bmp = wxBITMAP(print);
bdc72a22 426
fc6bbc6d 427 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
5ef2e633
VZ
428 FALSE, (wxObject *) NULL,
429 "Delete this tool",
430 "This button was inserted into the toolbar");
bdc72a22
VZ
431
432 GetToolBar()->Realize();
433}
434
13437238
JS
435void MyFrame::OnToolEnter(wxCommandEvent& event)
436{
81d66cf3 437 if (event.GetSelection() > -1)
13437238
JS
438 {
439 wxString str;
58dea4b0 440 str.Printf(_T("This is tool number %d"), event.GetSelection());
13437238
JS
441 SetStatusText(str);
442 }
443 else
444 SetStatusText("");
445}
14d1ccd8 446