]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/toolbar.cpp
PCX images can now be embedded in streams
[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);
9fb35cf1
VZ
220#if 0
221 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, -1, -1,
222 (wxObject *) NULL, "New file");
223 toolBar->Realize();
224 return;
225#endif
226
5ef2e633
VZ
227 toolBarBitmaps[1] = wxBITMAP(open);
228 if ( !m_smallToolbar )
229 {
230 toolBarBitmaps[2] = wxBITMAP(save);
231 toolBarBitmaps[3] = wxBITMAP(copy);
232 toolBarBitmaps[4] = wxBITMAP(cut);
233 toolBarBitmaps[5] = wxBITMAP(paste);
234 toolBarBitmaps[6] = wxBITMAP(print);
235 toolBarBitmaps[7] = wxBITMAP(help);
236 }
14d1ccd8
JS
237
238#ifdef __WXMSW__
5ef2e633 239 int width = 24;
14d1ccd8 240#else
5ef2e633 241 int width = 16;
14d1ccd8 242#endif
c8f1f088 243
5ef2e633 244 int currentX = 5;
7fb23305 245
5ef2e633
VZ
246 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
247 currentX += width + 5;
248 toolBar->AddTool(wxID_OPEN, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
14d1ccd8 249
ad4ae6ed
VZ
250 // neither the generic nor Motif native toolbars really support this
251#if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
5ef2e633
VZ
252 // adding a combo to a vertical toolbar is not very smart
253 if ( m_horzToolbar )
254 {
255 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO);
256 combo->Append("This");
257 combo->Append("is a");
258 combo->Append("combobox");
259 combo->Append("in a");
260 combo->Append("toolbar");
261 toolBar->AddControl(combo);
262 }
ad4ae6ed 263#endif // toolbars which don't support controls
14d1ccd8 264
5ef2e633
VZ
265 if ( !m_smallToolbar )
266 {
267 currentX += width + 5;
268 toolBar->AddTool(wxID_SAVE, toolBarBitmaps[2], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
269 currentX += width + 5;
270 toolBar->AddTool(wxID_COPY, toolBarBitmaps[3], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
271 currentX += width + 5;
272 toolBar->AddTool(wxID_CUT, toolBarBitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
273 currentX += width + 5;
274 toolBar->AddTool(wxID_PASTE, toolBarBitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
275 currentX += width + 5;
276 toolBar->AddTool(wxID_PRINT, toolBarBitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool");
277 currentX += width + 5;
278 toolBar->AddSeparator();
279 toolBar->AddTool(wxID_HELP, toolBarBitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
280 }
13437238 281
5ef2e633
VZ
282 // after adding the buttons to the toolbar, must call Realize() to reflect
283 // the changes
284 toolBar->Realize();
98066234
VZ
285
286 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
14d1ccd8
JS
287}
288
ad9bb75f
VZ
289// ----------------------------------------------------------------------------
290// MyFrame
291// ----------------------------------------------------------------------------
13437238
JS
292
293// Define my frame constructor
7fb23305
VZ
294MyFrame::MyFrame(wxFrame* parent,
295 wxWindowID id,
296 const wxString& title,
297 const wxPoint& pos,
298 const wxSize& size,
299 long style)
300 : wxFrame(parent, id, title, pos, size, style)
14d1ccd8 301{
e179bd65 302 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
98066234 303
7fb23305 304 m_smallToolbar = FALSE;
98066234
VZ
305 m_horzToolbar = TRUE;
306 m_rows = 1;
ad9bb75f
VZ
307
308 // Give it a status line
309 CreateStatusBar();
310
311 // Give it an icon
312 SetIcon(wxICON(mondrian));
313
314 // Make a menubar
315 wxMenu *tbarMenu = new wxMenu;
5ef2e633
VZ
316 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
317 "&Toggle toolbar size\tCtrl-S",
318 "Toggle between big/small toolbar",
319 TRUE);
320 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
321 "Toggle toolbar &orientation\tCtrl-O",
322 "Toggle toolbar orientation",
323 TRUE);
98066234
VZ
324 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
325 "Toggle number of &rows\tCtrl-R",
326 "Toggle number of toolbar rows between 1 and 2",
327 TRUE);
5ef2e633
VZ
328
329 tbarMenu->AppendSeparator();
330
fc6bbc6d
VZ
331 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
332 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
333 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
334 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
ad9bb75f
VZ
335
336 wxMenu *fileMenu = new wxMenu;
337 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
338
ad9bb75f
VZ
339 wxMenu *helpMenu = new wxMenu;
340 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
341
342 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
343
344 menuBar->Append(fileMenu, "&File");
345 menuBar->Append(tbarMenu, "&Toolbar");
ad9bb75f
VZ
346 menuBar->Append(helpMenu, "&Help");
347
348 // Associate the menu bar with the frame
349 SetMenuBar(menuBar);
350
351 // Create the toolbar
5ef2e633
VZ
352 RecreateToolbar();
353}
ad9bb75f 354
ad4ae6ed
VZ
355#if USE_GENERIC_TBAR
356
357wxToolBar* MyFrame::OnCreateToolBar(long style,
358 wxWindowID id,
359 const wxString& name)
360{
361 return (wxToolBar *)new wxToolBarSimple(this, id,
362 wxDefaultPosition, wxDefaultSize,
363 style, name);
364}
365
366#endif // USE_GENERIC_TBAR
367
5ef2e633
VZ
368void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
369{
370 m_smallToolbar = !m_smallToolbar;
ad9bb75f 371
5ef2e633 372 RecreateToolbar();
7fb23305
VZ
373}
374
98066234
VZ
375void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
376{
377 // m_rows may be only 1 or 2
378 m_rows = 3 - m_rows;
379
380 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
381
9fb35cf1 382 //RecreateToolbar(); -- this is unneeded
98066234
VZ
383}
384
5ef2e633 385void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
7fb23305 386{
5ef2e633 387 m_horzToolbar = !m_horzToolbar;
7fb23305 388
5ef2e633 389 RecreateToolbar();
14d1ccd8
JS
390}
391
47908e25 392void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 393{
13437238 394 Close(TRUE);
14d1ccd8
JS
395}
396
47908e25 397void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 398{
1d5b7a0b 399 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 400}
14d1ccd8 401
13437238
JS
402void MyFrame::OnToolLeftClick(wxCommandEvent& event)
403{
e179bd65
RR
404 wxString str;
405 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
406 m_textWindow->WriteText( str );
ad9bb75f 407
e179bd65
RR
408 if (event.GetId() == wxID_HELP)
409 {
ad9bb75f 410 if ( event.GetExtraLong() != 0 )
e179bd65
RR
411 m_textWindow->WriteText( _T("Help button down now.\n") );
412 else
413 m_textWindow->WriteText( _T("Help button up now.\n") );
414 }
ad9bb75f 415
e179bd65
RR
416 if (event.GetId() == wxID_COPY)
417 {
7fb23305 418 DoEnablePrint();
e179bd65 419 }
ad9bb75f 420
e179bd65
RR
421 if (event.GetId() == wxID_CUT)
422 {
7fb23305 423 DoToggleHelp();
e179bd65 424 }
1c4f8f8d 425
97d7bfb8
RR
426 if (event.GetId() == wxID_PRINT)
427 {
428 DoDeletePrint();
429 }
14d1ccd8
JS
430}
431
1c383dba
VZ
432void MyFrame::OnCombo(wxCommandEvent& event)
433{
434 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
435}
436
7fb23305
VZ
437void MyFrame::DoEnablePrint()
438{
ad4ae6ed 439 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
440 if (tb->GetToolEnabled(wxID_PRINT))
441 tb->EnableTool( wxID_PRINT, FALSE );
442 else
443 tb->EnableTool( wxID_PRINT, TRUE );
444}
445
97d7bfb8
RR
446void MyFrame::DoDeletePrint()
447{
ad4ae6ed 448 wxToolBarBase *tb = GetToolBar();
1c4f8f8d 449
97d7bfb8
RR
450 tb->DeleteTool( wxID_PRINT );
451}
452
7fb23305
VZ
453void MyFrame::DoToggleHelp()
454{
ad4ae6ed 455 wxToolBarBase *tb = GetToolBar();
7fb23305
VZ
456 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
457}
458
5ef2e633
VZ
459void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
460{
461 event.Enable( m_textWindow->CanCopy() );
462}
463
bdc72a22
VZ
464void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
465{
5ef2e633 466 wxBitmap bmp = wxBITMAP(print);
bdc72a22 467
fc6bbc6d 468 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
5ef2e633
VZ
469 FALSE, (wxObject *) NULL,
470 "Delete this tool",
471 "This button was inserted into the toolbar");
bdc72a22
VZ
472
473 GetToolBar()->Realize();
474}
475
13437238
JS
476void MyFrame::OnToolEnter(wxCommandEvent& event)
477{
ad4ae6ed
VZ
478 if (event.GetSelection() > -1)
479 {
480 wxString str;
481 str.Printf(_T("This is tool number %d"), event.GetSelection());
482 SetStatusText(str);
483 }
484 else
485 SetStatusText("");
13437238 486}
14d1ccd8 487