]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/toolbar/toolbar.cpp
Added missing VC++ project files
[wxWidgets.git] / samples / toolbar / toolbar.cpp
... / ...
CommitLineData
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#include <wx/image.h>
34
35// define this to 1 to use wxToolBarSimple instead of the native one
36#define USE_GENERIC_TBAR 0
37
38// define this to use XPMs everywhere (by default, BMPs are used under Win)
39#ifdef __WXMSW__
40 #define USE_XPM_BITMAPS 0
41#else
42 #define USE_XPM_BITMAPS 1
43#endif
44
45#if USE_GENERIC_TBAR
46 #if !wxUSE_TOOLBAR_SIMPLE
47 #error wxToolBarSimple is not compiled in, set wxUSE_TOOLBAR_SIMPLE \
48 to 1 in setup.h and recompile the library.
49 #else
50 #include <wx/tbarsmpl.h>
51 #endif
52#endif // USE_GENERIC_TBAR
53
54#if USE_XPM_BITMAPS && defined(__WXMSW__) && !wxUSE_XPM_IN_MSW
55 #error You need to enable XPM support to use XPM bitmaps with toolbar!
56#endif // USE_XPM_BITMAPS
57
58// ----------------------------------------------------------------------------
59// resources
60// ----------------------------------------------------------------------------
61
62#if USE_XPM_BITMAPS
63 #include "mondrian.xpm"
64 #include "bitmaps/new.xpm"
65 #include "bitmaps/open.xpm"
66 #include "bitmaps/save.xpm"
67 #include "bitmaps/copy.xpm"
68 #include "bitmaps/cut.xpm"
69 #include "bitmaps/preview.xpm" // paste XPM
70 #include "bitmaps/print.xpm"
71 #include "bitmaps/help.xpm"
72#endif // USE_XPM_BITMAPS
73
74// ----------------------------------------------------------------------------
75// classes
76// ----------------------------------------------------------------------------
77
78// Define a new application
79class MyApp : public wxApp
80{
81public:
82 bool OnInit();
83};
84
85// Define a new frame
86class MyFrame: public wxFrame
87{
88public:
89 MyFrame(wxFrame *parent,
90 wxWindowID id = -1,
91 const wxString& title = "wxToolBar Sample",
92 const wxPoint& pos = wxDefaultPosition,
93 const wxSize& size = wxDefaultSize,
94 long style = wxDEFAULT_FRAME_STYLE);
95
96 void RecreateToolbar();
97
98 void OnQuit(wxCommandEvent& event);
99 void OnAbout(wxCommandEvent& event);
100
101 void OnSize(wxSizeEvent& event);
102
103 void OnToggleAnotherToolbar(wxCommandEvent& event);
104
105 void OnToggleToolbarSize(wxCommandEvent& event);
106 void OnToggleToolbarOrient(wxCommandEvent& event);
107 void OnToggleToolbarRows(wxCommandEvent& event);
108
109 void OnEnablePrint(wxCommandEvent& WXUNUSED(event)) { DoEnablePrint(); }
110 void OnDeletePrint(wxCommandEvent& WXUNUSED(event)) { DoDeletePrint(); }
111 void OnInsertPrint(wxCommandEvent& event);
112 void OnToggleHelp(wxCommandEvent& WXUNUSED(event)) { DoToggleHelp(); }
113
114 void OnToolLeftClick(wxCommandEvent& event);
115 void OnToolEnter(wxCommandEvent& event);
116
117 void OnCombo(wxCommandEvent& event);
118
119 void OnUpdateCopyAndCut(wxUpdateUIEvent& event);
120
121 void OnToggleFullScreen(wxCommandEvent& event);
122
123#if USE_GENERIC_TBAR
124 virtual wxToolBar *OnCreateToolBar(long style,
125 wxWindowID id,
126 const wxString& name );
127#endif // USE_GENERIC_TBAR
128
129private:
130 void DoEnablePrint();
131 void DoDeletePrint();
132 void DoToggleHelp();
133
134 void LayoutChildren();
135
136 bool m_smallToolbar,
137 m_horzToolbar;
138 size_t m_rows; // 1 or 2 only
139
140 wxTextCtrl *m_textWindow;
141
142 wxToolBar *m_tbar;
143
144 DECLARE_EVENT_TABLE()
145};
146
147// ----------------------------------------------------------------------------
148// constants
149// ----------------------------------------------------------------------------
150
151const int ID_TOOLBAR = 500;
152
153enum
154{
155 IDM_TOOLBAR_TOGGLETOOLBARSIZE = 200,
156 IDM_TOOLBAR_TOGGLETOOLBARORIENT,
157 IDM_TOOLBAR_TOGGLETOOLBARROWS,
158 IDM_TOOLBAR_ENABLEPRINT,
159 IDM_TOOLBAR_DELETEPRINT,
160 IDM_TOOLBAR_INSERTPRINT,
161 IDM_TOOLBAR_TOGGLEHELP,
162 IDM_TOOLBAR_TOGGLEFULLSCREEN,
163 IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
164
165 ID_COMBO = 1000
166};
167
168// ----------------------------------------------------------------------------
169// event tables
170// ----------------------------------------------------------------------------
171
172// Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
173// help button.
174
175BEGIN_EVENT_TABLE(MyFrame, wxFrame)
176 EVT_SIZE(MyFrame::OnSize)
177
178 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
179 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
180
181 EVT_MENU(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR, MyFrame::OnToggleAnotherToolbar)
182
183 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARSIZE, MyFrame::OnToggleToolbarSize)
184 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARORIENT, MyFrame::OnToggleToolbarOrient)
185 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBARROWS, MyFrame::OnToggleToolbarRows)
186
187 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
188 EVT_MENU(IDM_TOOLBAR_DELETEPRINT, MyFrame::OnDeletePrint)
189 EVT_MENU(IDM_TOOLBAR_INSERTPRINT, MyFrame::OnInsertPrint)
190 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
191 EVT_MENU(IDM_TOOLBAR_TOGGLEFULLSCREEN, MyFrame::OnToggleFullScreen)
192
193 EVT_MENU(-1, MyFrame::OnToolLeftClick)
194
195 EVT_COMBOBOX(ID_COMBO, MyFrame::OnCombo)
196
197 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
198
199 EVT_UPDATE_UI(wxID_COPY, MyFrame::OnUpdateCopyAndCut)
200 EVT_UPDATE_UI(wxID_CUT, MyFrame::OnUpdateCopyAndCut)
201END_EVENT_TABLE()
202
203// ============================================================================
204// implementation
205// ============================================================================
206
207// ----------------------------------------------------------------------------
208// MyApp
209// ----------------------------------------------------------------------------
210
211IMPLEMENT_APP(MyApp)
212
213// The `main program' equivalent, creating the windows and returning the
214// main frame
215bool MyApp::OnInit()
216{
217 // Create the main frame window
218 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
219 "wxToolBar Sample",
220 wxPoint(100, 100), wxSize(450, 300));
221
222 frame->Show(TRUE);
223
224 frame->SetStatusText("Hello, wxWindows");
225
226 SetTopWindow(frame);
227
228 return TRUE;
229}
230
231void MyFrame::RecreateToolbar()
232{
233 // delete and recreate the toolbar
234 wxToolBarBase *toolBar = GetToolBar();
235 delete toolBar;
236
237 SetToolBar(NULL);
238
239 long style = wxNO_BORDER | wxTB_FLAT | wxTB_DOCKABLE;
240 style |= m_horzToolbar ? wxTB_HORIZONTAL : wxTB_VERTICAL;
241
242 toolBar = CreateToolBar(style, ID_TOOLBAR);
243 toolBar->SetMargins( 4, 4 );
244
245 // Set up toolbar
246 wxBitmap toolBarBitmaps[8];
247
248#if USE_XPM_BITMAPS
249 toolBarBitmaps[0] = wxBitmap(new_xpm);
250 toolBarBitmaps[1] = wxBitmap(open_xpm);
251 toolBarBitmaps[2] = wxBitmap(save_xpm);
252 toolBarBitmaps[3] = wxBitmap(copy_xpm);
253 toolBarBitmaps[4] = wxBitmap(cut_xpm);
254 toolBarBitmaps[5] = wxBitmap(paste_xpm);
255 toolBarBitmaps[6] = wxBitmap(print_xpm);
256 toolBarBitmaps[7] = wxBitmap(help_xpm);
257#else // !USE_XPM_BITMAPS
258 toolBarBitmaps[0] = wxBITMAP(new);
259 toolBarBitmaps[1] = wxBITMAP(open);
260 toolBarBitmaps[2] = wxBITMAP(save);
261 toolBarBitmaps[3] = wxBITMAP(copy);
262 toolBarBitmaps[4] = wxBITMAP(cut);
263 toolBarBitmaps[5] = wxBITMAP(paste);
264 toolBarBitmaps[6] = wxBITMAP(print);
265 toolBarBitmaps[7] = wxBITMAP(help);
266#endif // USE_XPM_BITMAPS/!USE_XPM_BITMAPS
267
268 if ( !m_smallToolbar )
269 {
270 int w = 2*toolBarBitmaps[0].GetWidth(),
271 h = 2*toolBarBitmaps[0].GetHeight();
272 for ( size_t n = 0; n < WXSIZEOF(toolBarBitmaps); n++ )
273 {
274 toolBarBitmaps[n] =
275 wxImage(toolBarBitmaps[n]).Scale(w, h).ConvertToBitmap();
276 }
277
278 toolBar->SetToolBitmapSize(wxSize(w, h));
279 }
280
281#ifdef __WXMSW__
282 int width = 24;
283#else
284 int width = 16;
285#endif
286
287 int currentX = 5;
288
289 toolBar->AddTool(wxID_NEW, toolBarBitmaps[0], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
290 currentX += width + 5;
291 toolBar->AddTool(wxID_OPEN, toolBarBitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
292
293 // neither the generic nor Motif native toolbars really support this
294#if (wxUSE_TOOLBAR_NATIVE && !USE_GENERIC_TBAR) && !defined(__WXMOTIF__)
295 // adding a combo to a vertical toolbar is not very smart
296 if ( m_horzToolbar )
297 {
298 wxComboBox *combo = new wxComboBox(toolBar, ID_COMBO);
299 combo->Append("This");
300 combo->Append("is a");
301 combo->Append("combobox");
302 combo->Append("in a");
303 combo->Append("toolbar");
304 toolBar->AddControl(combo);
305 }
306#endif // toolbars which don't support controls
307
308 currentX += width + 5;
309 toolBar->AddTool(wxID_SAVE, toolBarBitmaps[2], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
310 currentX += width + 5;
311 toolBar->AddTool(wxID_COPY, toolBarBitmaps[3], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
312 currentX += width + 5;
313 toolBar->AddTool(wxID_CUT, toolBarBitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
314 currentX += width + 5;
315 toolBar->AddTool(wxID_PASTE, toolBarBitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
316 currentX += width + 5;
317 toolBar->AddTool(wxID_PRINT, toolBarBitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Delete this tool");
318 currentX += width + 5;
319 toolBar->AddSeparator();
320 toolBar->AddTool(wxID_HELP, toolBarBitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
321
322 // after adding the buttons to the toolbar, must call Realize() to reflect
323 // the changes
324 toolBar->Realize();
325
326 toolBar->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
327}
328
329// ----------------------------------------------------------------------------
330// MyFrame
331// ----------------------------------------------------------------------------
332
333// Define my frame constructor
334MyFrame::MyFrame(wxFrame* parent,
335 wxWindowID id,
336 const wxString& title,
337 const wxPoint& pos,
338 const wxSize& size,
339 long style)
340 : wxFrame(parent, id, title, pos, size, style)
341{
342 m_tbar = NULL;
343 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
344
345 m_smallToolbar = TRUE;
346 m_horzToolbar = TRUE;
347 m_rows = 1;
348
349 // Give it a status line
350 CreateStatusBar();
351
352 // Give it an icon
353 SetIcon(wxICON(mondrian));
354
355 // Make a menubar
356 wxMenu *tbarMenu = new wxMenu;
357 tbarMenu->Append(IDM_TOOLBAR_TOGGLE_ANOTHER_TOOLBAR,
358 "Toggle &another toolbar\tCtrl-A",
359 "Show/hide another test toolbar",
360 TRUE);
361
362 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARSIZE,
363 "&Toggle toolbar size\tCtrl-S",
364 "Toggle between big/small toolbar",
365 TRUE);
366 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARORIENT,
367 "Toggle toolbar &orientation\tCtrl-O",
368 "Toggle toolbar orientation",
369 TRUE);
370 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBARROWS,
371 "Toggle number of &rows\tCtrl-R",
372 "Toggle number of toolbar rows between 1 and 2",
373 TRUE);
374
375 tbarMenu->AppendSeparator();
376
377 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button\tCtrl-E", "");
378 tbarMenu->Append(IDM_TOOLBAR_DELETEPRINT, "&Delete print button\tCtrl-D", "");
379 tbarMenu->Append(IDM_TOOLBAR_INSERTPRINT, "&Insert print button\tCtrl-I", "");
380 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button\tCtrl-T", "");
381
382#ifdef __WXMSW__
383 tbarMenu->AppendSeparator();
384 tbarMenu->Append(IDM_TOOLBAR_TOGGLEFULLSCREEN, "Toggle &full screen mode\tCtrl-F", "");
385#endif
386
387 wxMenu *fileMenu = new wxMenu;
388 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
389
390 wxMenu *helpMenu = new wxMenu;
391 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
392
393 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
394
395 menuBar->Append(fileMenu, "&File");
396 menuBar->Append(tbarMenu, "&Toolbar");
397 menuBar->Append(helpMenu, "&Help");
398
399 // Associate the menu bar with the frame
400 SetMenuBar(menuBar);
401
402 // Create the toolbar
403 RecreateToolbar();
404}
405
406#if USE_GENERIC_TBAR
407
408wxToolBar* MyFrame::OnCreateToolBar(long style,
409 wxWindowID id,
410 const wxString& name)
411{
412 return (wxToolBar *)new wxToolBarSimple(this, id,
413 wxDefaultPosition, wxDefaultSize,
414 style, name);
415}
416
417#endif // USE_GENERIC_TBAR
418
419void MyFrame::LayoutChildren()
420{
421 wxSize size = GetClientSize();
422
423 int offset;
424 if ( m_tbar )
425 {
426 m_tbar->SetSize(-1, size.y);
427 m_tbar->Move(0, 0);
428
429 offset = m_tbar->GetSize().x;
430 }
431 else
432 {
433 offset = 0;
434 }
435
436 m_textWindow->SetSize(offset, 0, size.x - offset, size.y);
437}
438
439void MyFrame::OnSize(wxSizeEvent& event)
440{
441 if ( m_tbar )
442 {
443 LayoutChildren();
444 }
445 else
446 {
447 event.Skip();
448 }
449}
450
451void MyFrame::OnToggleAnotherToolbar(wxCommandEvent& WXUNUSED(event))
452{
453 if ( m_tbar )
454 {
455 delete m_tbar;
456 m_tbar = NULL;
457 }
458 else
459 {
460 m_tbar = new wxToolBar(this, -1,
461 wxDefaultPosition, wxDefaultSize,
462 wxTB_VERTICAL);
463 m_tbar->AddTool(wxID_HELP, wxBITMAP(help),
464 wxNullBitmap, FALSE,
465 NULL,
466 "This is the help button",
467 "This is the long help for the help button");
468 m_tbar->Realize();
469 }
470
471 LayoutChildren();
472}
473
474void MyFrame::OnToggleToolbarSize(wxCommandEvent& WXUNUSED(event))
475{
476 m_smallToolbar = !m_smallToolbar;
477
478 RecreateToolbar();
479}
480
481void MyFrame::OnToggleToolbarRows(wxCommandEvent& WXUNUSED(event))
482{
483 // m_rows may be only 1 or 2
484 m_rows = 3 - m_rows;
485
486 GetToolBar()->SetRows(m_horzToolbar ? m_rows : 10 / m_rows);
487
488 //RecreateToolbar(); -- this is unneeded
489}
490
491void MyFrame::OnToggleToolbarOrient(wxCommandEvent& WXUNUSED(event))
492{
493 m_horzToolbar = !m_horzToolbar;
494
495 RecreateToolbar();
496}
497
498void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
499{
500 Close(TRUE);
501}
502
503void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
504{
505 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
506}
507
508void MyFrame::OnToolLeftClick(wxCommandEvent& event)
509{
510 wxString str;
511 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
512 m_textWindow->WriteText( str );
513
514 if (event.GetId() == wxID_HELP)
515 {
516 if ( event.GetExtraLong() != 0 )
517 m_textWindow->WriteText( _T("Help button down now.\n") );
518 else
519 m_textWindow->WriteText( _T("Help button up now.\n") );
520 }
521
522 if (event.GetId() == wxID_COPY)
523 {
524 DoEnablePrint();
525 }
526
527 if (event.GetId() == wxID_CUT)
528 {
529 DoToggleHelp();
530 }
531
532 if (event.GetId() == wxID_PRINT)
533 {
534 DoDeletePrint();
535 }
536}
537
538void MyFrame::OnCombo(wxCommandEvent& event)
539{
540 wxLogStatus(_T("Combobox string '%s' selected"), event.GetString().c_str());
541}
542
543void MyFrame::DoEnablePrint()
544{
545 wxToolBarBase *tb = GetToolBar();
546 if (tb->GetToolEnabled(wxID_PRINT))
547 tb->EnableTool( wxID_PRINT, FALSE );
548 else
549 tb->EnableTool( wxID_PRINT, TRUE );
550}
551
552void MyFrame::DoDeletePrint()
553{
554 wxToolBarBase *tb = GetToolBar();
555
556 tb->DeleteTool( wxID_PRINT );
557}
558
559void MyFrame::DoToggleHelp()
560{
561 wxToolBarBase *tb = GetToolBar();
562 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
563}
564
565void MyFrame::OnUpdateCopyAndCut(wxUpdateUIEvent& event)
566{
567 event.Enable( m_textWindow->CanCopy() );
568}
569
570void MyFrame::OnInsertPrint(wxCommandEvent& WXUNUSED(event))
571{
572 wxBitmap bmp = wxBITMAP(print);
573
574 GetToolBar()->InsertTool(0, wxID_PRINT, bmp, wxNullBitmap,
575 FALSE, (wxObject *) NULL,
576 "Delete this tool",
577 "This button was inserted into the toolbar");
578
579 GetToolBar()->Realize();
580}
581
582void MyFrame::OnToolEnter(wxCommandEvent& event)
583{
584 if (event.GetSelection() > -1)
585 {
586 wxString str;
587 str.Printf(_T("This is tool number %d"), event.GetSelection());
588 SetStatusText(str);
589 }
590 else
591 SetStatusText("");
592}
593
594void MyFrame::OnToggleFullScreen(wxCommandEvent& event)
595{
596#ifdef __WXMSW__
597 ShowFullScreen(!IsFullScreen());
598#endif
599}
600