]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/toolbar.cpp
added wxEncodingConverter - scripts for creating convertion tables
[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
ad4ae6ed 34// define this to 1 to use wxToolBarSimple instead of the native one
9fb35cf1 35#define USE_GENERIC_TBAR 0
ad4ae6ed
VZ
36
37#if USE_GENERIC_TBAR
38 #if !wxUSE_TOOLBAR_SIMPLE
9fb35cf1
VZ
39 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
40 to 1 in setup.h and recompile the library.
ad4ae6ed
VZ
41 #else
42 #include <wx/tbarsmpl.h>
43 #endif
44#endif // USE_GENERIC_TBAR
45
ad9bb75f
VZ
46// ----------------------------------------------------------------------------
47// resources
48// ----------------------------------------------------------------------------
14d1ccd8 49
a4294b78 50#if defined(__WXGTK__) || defined(__WXMOTIF__)
ad9bb75f
VZ
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"
5ef2e633 57 #include "bitmaps/preview.xpm" // paste XPM
ad9bb75f 58 #include "bitmaps/print.xpm"
ad9bb75f
VZ
59 #include "bitmaps/help.xpm"
60#endif // GTK or Motif
61
62// ----------------------------------------------------------------------------
63// classes
64// ----------------------------------------------------------------------------
65
66// Define a new application
5ef2e633 67class MyApp : public wxApp
ad9bb75f
VZ
68{
69public:
70 bool OnInit();
ad9bb75f 71};
47908e25 72
ad9bb75f
VZ
73// Define a new frame
74class MyFrame: public wxFrame
75{
76public:
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);
14d1ccd8 83
5ef2e633
VZ
84 void RecreateToolbar();
85
ad9bb75f
VZ
86 void OnQuit(wxCommandEvent& event);
87 void OnAbout(wxCommandEvent& event);
88
5ef2e633
VZ
89 void OnToggleToolbarSize(wxCommandEvent& event);
90 void OnToggleToolbarOrient(wxCommandEvent& event);
98066234 91 void OnToggleToolbarRows(wxCommandEvent& event);
5ef2e633 92
ad4ae6ed
VZ
93 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
94 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
bdc72a22 95 void OnInsertPrint(wxCommandEvent& event);
ad4ae6ed 96 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
ad9bb75f 97
ad9bb75f
VZ
98 void OnToolLeftClick(wxCommandEvent& event);
99 void OnToolEnter(wxCommandEvent& event);
100
1c383dba
VZ
101 void OnCombo(wxCommandEvent& event);
102
5ef2e633
VZ
103 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
104
ad4ae6ed
VZ
105#if USE_GENERIC_TBAR
106 virtual wxToolBar *OnCreateToolBar(long style,
107 wxWindowID id,
108 const wxString& name );
109#endif // USE_GENERIC_TBAR
110
ad9bb75f
VZ
111private:
112 void DoEnablePrint();
97d7bfb8 113 void DoDeletePrint();
ad9bb75f
VZ
114 void DoToggleHelp();
115
5ef2e633
VZ
116 bool m_smallToolbar,
117 m_horzToolbar;
98066234
VZ
118 size_t m_rows; // 1 or 2 only
119
ad9bb75f
VZ
120 wxTextCtrl* m_textWindow;
121
ad9bb75f
VZ
122 DECLARE_EVENT_TABLE()
123};
124
125// ----------------------------------------------------------------------------
126// constants
127// ----------------------------------------------------------------------------
128
129const int ID_TOOLBAR = 500;
130
131enum
14d1ccd8 132{
5ef2e633
VZ
133 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
134 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
98066234 135 IDM_TOOLBAR_TOGGLETOOLBARROWS,
ad9bb75f 136 IDM_TOOLBAR_ENABLEPRINT,
97d7bfb8 137 IDM_TOOLBAR_DELETEPRINT,
bdc72a22 138 IDM_TOOLBAR_INSERTPRINT,
1c383dba
VZ
139 IDM_TOOLBAR_TOGGLEHELP,
140
141 ID_COMBO = 1000
ad9bb75f 142};
14d1ccd8 143
ad9bb75f
VZ
144// ----------------------------------------------------------------------------
145// event tables
146// ----------------------------------------------------------------------------
14d1ccd8 147
ad9bb75f
VZ
148// Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
149// help button.
14d1ccd8 150
ad9bb75f
VZ
151BEGIN_EVENT_TABLE(MyFrame, wxFrame)
152 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
153 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
14d1ccd8 154
5ef2e633
VZ
155 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
156 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
98066234 157 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
5ef2e633 158
ad9bb75f 159 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
97d7bfb8 160 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
bdc72a22 161 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
ad9bb75f 162 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
14d1ccd8 163
ad9bb75f 164 EVT_MENU(-1, MyFrame::OnToolLeftClick)
14d1ccd8 165
1c383dba
VZ
166 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
167
ad9bb75f 168 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
5ef2e633
VZ
169
170 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
171 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
ad9bb75f 172END_EVENT_TABLE()
14d1ccd8 173
ad9bb75f
VZ
174// ============================================================================
175// implementation
176// ============================================================================
14d1ccd8 177
ad9bb75f
VZ
178// ----------------------------------------------------------------------------
179// MyApp
180// ----------------------------------------------------------------------------
14d1ccd8 181
ad9bb75f 182IMPLEMENT_APP(MyApp)
14d1ccd8 183
ad9bb75f
VZ
184// The `main program' equivalent, creating the windows and returning the
185// main frame
186bool 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));
14d1ccd8 192
7fb23305
VZ
193 frame->Show(TRUE);
194
195 frame->SetStatusText("Hello, wxWindows");
196
197 SetTopWindow(frame);
198
199 return TRUE;
14d1ccd8
JS
200}
201
5ef2e633 202void MyFrame::RecreateToolbar()
14d1ccd8 203{
5ef2e633 204 // delete and recreate the toolbar
ad4ae6ed 205 wxToolBarBase *toolBar = GetToolBar();
5ef2e633 206 delete toolBar;
14d1ccd8 207
5ef2e633
VZ
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 }
14d1ccd8
JS
230
231#ifdef __WXMSW__
5ef2e633 232 int width = 24;
14d1ccd8 233#else
5ef2e633 234 int width = 16;
14d1ccd8 235#endif
c8f1f088 236
5ef2e633 237 int currentX = 5;
7fb23305 238
5ef2e633
VZ
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");
14d1ccd8 242
ad4ae6ed
VZ
243 // neither the generic nor Motif native toolbars really support this
244#if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
5ef2e633
VZ
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 }
ad4ae6ed 256#endif // toolbars which don't support controls
14d1ccd8 257
5ef2e633
VZ
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 }
13437238 274
5ef2e633
VZ
275 // after adding the buttons to the toolbar, must call Realize() to reflect
276 // the changes
277 toolBar->Realize();
98066234
VZ
278
279 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
14d1ccd8
JS
280}
281
ad9bb75f
VZ
282// ----------------------------------------------------------------------------
283// MyFrame
284// ----------------------------------------------------------------------------
13437238
JS
285
286// Define my frame constructor
7fb23305
VZ
287MyFrame::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)
14d1ccd8 294{
e179bd65 295 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
98066234 296
7fb23305 297 m_smallToolbar = FALSE;
98066234
VZ
298 m_horzToolbar = TRUE;
299 m_rows = 1;
ad9bb75f
VZ
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;
5ef2e633
VZ
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);
98066234
VZ
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);
5ef2e633
VZ
321
322 tbarMenu->AppendSeparator();
323
fc6bbc6d
VZ
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", "");
ad9bb75f
VZ
328
329 wxMenu *fileMenu = new wxMenu;
330 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
331
ad9bb75f
VZ
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");
ad9bb75f
VZ
339 menuBar->Append(helpMenu, "&Help");
340
341 // Associate the menu bar with the frame
342 SetMenuBar(menuBar);
343
344 // Create the toolbar
5ef2e633
VZ
345 RecreateToolbar();
346}
ad9bb75f 347
ad4ae6ed
VZ
348#if USE_GENERIC_TBAR
349
350wxToolBar* 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
5ef2e633
VZ
361void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
362{
363 m_smallToolbar = !m_smallToolbar;
ad9bb75f 364
5ef2e633 365 RecreateToolbar();
7fb23305
VZ
366}
367
98066234
VZ
368void 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
9fb35cf1 375 //RecreateToolbar(); -- this is unneeded
98066234
VZ
376}
377
5ef2e633 378void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
7fb23305 379{
5ef2e633 380 m_horzToolbar = !m_horzToolbar;
7fb23305 381
5ef2e633 382 RecreateToolbar();
14d1ccd8
JS
383}
384
47908e25 385void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 386{
13437238 387 Close(TRUE);
14d1ccd8
JS
388}
389
47908e25 390void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 391{
1d5b7a0b 392 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 393}
14d1ccd8 394
13437238
JS
395void MyFrame::OnToolLeftClick(wxCommandEvent& event)
396{
e179bd65
RR
397 wxString str;
398 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
399 m_textWindow->WriteText( str );
ad9bb75f 400
e179bd65
RR
401 if (event.GetId() == wxID_HELP)
402 {
ad9bb75f 403 if ( event.GetExtraLong() != 0 )
e179bd65
RR
404 m_textWindow->WriteText( _T("Help button down now.\n") );
405 else
406 m_textWindow->WriteText( _T("Help button up now.\n") );
407 }
ad9bb75f 408
e179bd65
RR
409 if (event.GetId() == wxID_COPY)
410 {
7fb23305 411 DoEnablePrint();
e179bd65 412 }
ad9bb75f 413
e179bd65
RR
414 if (event.GetId() == wxID_CUT)
415 {
7fb23305 416 DoToggleHelp();
e179bd65 417 }
1c4f8f8d 418
97d7bfb8
RR
419 if (event.GetId() == wxID_PRINT)
420 {
421 DoDeletePrint();
422 }
14d1ccd8
JS
423}
424
1c383dba
VZ
425void MyFrame::OnCombo(wxCommandEvent& event)
426{
427 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
428}
429
7fb23305
VZ
430void MyFrame::DoEnablePrint()
431{
ad4ae6ed 432 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
433 if (tb->GetToolEnabled(wxID_PRINT))
434 tb->EnableTool( wxID_PRINT, FALSE );
435 else
436 tb->EnableTool( wxID_PRINT, TRUE );
437}
438
97d7bfb8
RR
439void MyFrame::DoDeletePrint()
440{
ad4ae6ed 441 wxToolBarBase *tb = GetToolBar();
1c4f8f8d 442
97d7bfb8
RR
443 tb->DeleteTool( wxID_PRINT );
444}
445
7fb23305
VZ
446void MyFrame::DoToggleHelp()
447{
ad4ae6ed 448 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
449 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
450}
451
5ef2e633
VZ
452void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
453{
454 event.Enable( m_textWindow->CanCopy() );
455}
456
bdc72a22
VZ
457void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
458{
5ef2e633 459 wxBitmap bmp = wxBITMAP(print);
bdc72a22 460
fc6bbc6d 461 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
5ef2e633
VZ
462 FALSE, (wxObject *) NULL,
463 "Delete this tool",
464 "This button was inserted into the toolbar");
bdc72a22
VZ
465
466 GetToolBar()->Realize();
467}
468
13437238
JS
469void MyFrame::OnToolEnter(wxCommandEvent& event)
470{
ad4ae6ed
VZ
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("");
13437238 479}
14d1ccd8 480