]> git.saurik.com Git - wxWidgets.git/blame - samples/toolbar/test.cpp
Some work on tabbing and menu accels,
[wxWidgets.git] / samples / toolbar / test.cpp
CommitLineData
14d1ccd8
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: test.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
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"
45 // #include "bitmaps/paste.xpm"
46 #include "bitmaps/print.xpm"
47 #include "bitmaps/preview.xpm"
48 #include "bitmaps/help.xpm"
49#endif // GTK or Motif
50
51// ----------------------------------------------------------------------------
52// classes
53// ----------------------------------------------------------------------------
54
55// Define a new application
56class MyApp: public wxApp
57{
58public:
59 bool OnInit();
60 bool InitToolbar(wxToolBar* toolBar, bool smallicons = FALSE);
61};
47908e25 62
ad9bb75f
VZ
63// Define a new frame
64class MyFrame: public wxFrame
65{
66public:
67 MyFrame(wxFrame *parent,
68 wxWindowID id = -1,
69 const wxString& title = "wxToolBar Sample",
70 const wxPoint& pos = wxDefaultPosition,
71 const wxSize& size = wxDefaultSize,
72 long style = wxDEFAULT_FRAME_STYLE);
14d1ccd8 73
ad9bb75f
VZ
74 virtual ~MyFrame() { delete m_menu; }
75
76 void OnQuit(wxCommandEvent& event);
77 void OnAbout(wxCommandEvent& event);
78
79 void OnToggleToolbar(wxCommandEvent& event);
80 void OnEnablePrint(wxCommandEvent& event) { DoEnablePrint(); }
81 void OnToggleHelp(wxCommandEvent& event) { DoToggleHelp(); }
82
83 void OnAppendMenu(wxCommandEvent& event);
84 void OnDeleteMenu(wxCommandEvent& event);
85 void OnToggleMenu(wxCommandEvent& event);
86
87 void OnToolLeftClick(wxCommandEvent& event);
88 void OnToolEnter(wxCommandEvent& event);
89
90private:
91 void DoEnablePrint();
92 void DoToggleHelp();
93
94 bool m_smallToolbar;
95 wxTextCtrl* m_textWindow;
96
97 wxMenu *m_menu;
98
99 DECLARE_EVENT_TABLE()
100};
101
102// ----------------------------------------------------------------------------
103// constants
104// ----------------------------------------------------------------------------
105
106const int ID_TOOLBAR = 500;
107
108enum
14d1ccd8 109{
ad9bb75f
VZ
110 IDM_TOOLBAR_TOGGLETOOLBAR = 200,
111 IDM_TOOLBAR_ENABLEPRINT,
112 IDM_TOOLBAR_TOGGLEHELP,
113 IDM_MENU_TOGGLE,
114 IDM_MENU_APPEND,
115 IDM_MENU_DELETE
116};
14d1ccd8 117
ad9bb75f
VZ
118// ----------------------------------------------------------------------------
119// event tables
120// ----------------------------------------------------------------------------
14d1ccd8 121
ad9bb75f
VZ
122// Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
123// help button.
14d1ccd8 124
ad9bb75f
VZ
125BEGIN_EVENT_TABLE(MyFrame, wxFrame)
126 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
127 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
14d1ccd8 128
ad9bb75f
VZ
129 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR, MyFrame::OnToggleToolbar)
130 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
131 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
14d1ccd8 132
ad9bb75f
VZ
133 EVT_MENU(IDM_MENU_TOGGLE, MyFrame::OnToggleMenu)
134 EVT_MENU(IDM_MENU_APPEND, MyFrame::OnAppendMenu)
135 EVT_MENU(IDM_MENU_DELETE, MyFrame::OnDeleteMenu)
14d1ccd8 136
ad9bb75f 137 EVT_MENU(-1, MyFrame::OnToolLeftClick)
14d1ccd8 138
ad9bb75f
VZ
139 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
140END_EVENT_TABLE()
14d1ccd8 141
ad9bb75f
VZ
142// ============================================================================
143// implementation
144// ============================================================================
14d1ccd8 145
ad9bb75f
VZ
146// ----------------------------------------------------------------------------
147// MyApp
148// ----------------------------------------------------------------------------
14d1ccd8 149
ad9bb75f 150IMPLEMENT_APP(MyApp)
14d1ccd8 151
ad9bb75f
VZ
152// The `main program' equivalent, creating the windows and returning the
153// main frame
154bool MyApp::OnInit()
155{
156 // Create the main frame window
157 MyFrame* frame = new MyFrame((wxFrame *) NULL, -1,
158 "wxToolBar Sample",
159 wxPoint(100, 100), wxSize(450, 300));
14d1ccd8 160
ad9bb75f
VZ
161 // VZ: what's this for??
162#if 0
7fb23305
VZ
163 // Force a resize. This should probably be replaced by a call to a wxFrame
164 // function that lays out default decorations and the remaining content window.
165 wxSizeEvent event(wxSize(-1, -1), frame->GetId());
166 frame->OnSize(event);
ad9bb75f
VZ
167#endif // 0
168
7fb23305
VZ
169 frame->Show(TRUE);
170
171 frame->SetStatusText("Hello, wxWindows");
172
173 SetTopWindow(frame);
174
175 return TRUE;
14d1ccd8
JS
176}
177
d676ebcf 178bool MyApp::InitToolbar(wxToolBar* toolBar, bool smallicons)
14d1ccd8
JS
179{
180 // Set up toolbar
181 wxBitmap* toolBarBitmaps[8];
182
183#ifdef __WXMSW__
184 toolBarBitmaps[0] = new wxBitmap("icon1");
185 toolBarBitmaps[1] = new wxBitmap("icon2");
d676ebcf 186 if ( !smallicons )
7fb23305
VZ
187 {
188 toolBarBitmaps[2] = new wxBitmap("icon3");
189 toolBarBitmaps[3] = new wxBitmap("icon4");
190 toolBarBitmaps[4] = new wxBitmap("icon5");
191 toolBarBitmaps[5] = new wxBitmap("icon6");
192 toolBarBitmaps[6] = new wxBitmap("icon7");
193 toolBarBitmaps[7] = new wxBitmap("icon8");
194 }
47908e25
RR
195#else
196 toolBarBitmaps[0] = new wxBitmap( new_xpm );
197 toolBarBitmaps[1] = new wxBitmap( open_xpm );
d676ebcf 198 if ( !smallicons )
7fb23305
VZ
199 {
200 toolBarBitmaps[2] = new wxBitmap( save_xpm );
201 toolBarBitmaps[3] = new wxBitmap( copy_xpm );
202 toolBarBitmaps[4] = new wxBitmap( cut_xpm );
203 toolBarBitmaps[5] = new wxBitmap( preview_xpm );
204 toolBarBitmaps[6] = new wxBitmap( print_xpm );
205 toolBarBitmaps[7] = new wxBitmap( help_xpm );
206 }
14d1ccd8
JS
207#endif
208
209#ifdef __WXMSW__
210 int width = 24;
211#else
212 int width = 16;
213#endif
14d1ccd8
JS
214 int currentX = 5;
215
a4294b78 216 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
14d1ccd8 217 currentX += width + 5;
a4294b78 218 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
7fb23305 219
d676ebcf 220 if ( !smallicons )
7fb23305
VZ
221 {
222 currentX += width + 5;
223 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
224 currentX += width + 5;
225 toolBar->AddSeparator();
226 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Disable/Enable print button");
227 currentX += width + 5;
228 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Toggle/Untoggle help button");
229 currentX += width + 5;
230 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
231 currentX += width + 5;
232 toolBar->AddSeparator();
233 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
234 currentX += width + 5;
235 toolBar->AddSeparator();
236 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), *(toolBarBitmaps[6]), TRUE, currentX, -1, (wxObject *) NULL, "Help");
ad9bb75f 237
7fb23305
VZ
238 toolBar->EnableTool( wxID_PRINT, FALSE );
239 }
14d1ccd8 240
81d66cf3 241 toolBar->Realize();
14d1ccd8
JS
242
243 // Can delete the bitmaps since they're reference counted
d676ebcf 244 int i, max = smallicons ? 2 : WXSIZEOF(toolBarBitmaps);
7fb23305 245 for (i = 0; i < max; i++)
14d1ccd8 246 delete toolBarBitmaps[i];
13437238
JS
247
248 return TRUE;
14d1ccd8
JS
249}
250
ad9bb75f
VZ
251// ----------------------------------------------------------------------------
252// MyFrame
253// ----------------------------------------------------------------------------
13437238
JS
254
255// Define my frame constructor
7fb23305
VZ
256MyFrame::MyFrame(wxFrame* parent,
257 wxWindowID id,
258 const wxString& title,
259 const wxPoint& pos,
260 const wxSize& size,
261 long style)
262 : wxFrame(parent, id, title, pos, size, style)
14d1ccd8 263{
ad9bb75f 264 m_menu = NULL;
e179bd65 265 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
7fb23305 266 m_smallToolbar = FALSE;
ad9bb75f
VZ
267
268 // Give it a status line
269 CreateStatusBar();
270
271 // Give it an icon
272 SetIcon(wxICON(mondrian));
273
274 // Make a menubar
275 wxMenu *tbarMenu = new wxMenu;
276 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBAR, "&Toggle toolbar", "Change the toolbar kind");
277 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button", "");
278 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button", "");
279
280 wxMenu *fileMenu = new wxMenu;
281 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
282
283 wxMenu *menuMenu = new wxMenu;
284 menuMenu->Append(IDM_MENU_APPEND, "&Append menu");
285 menuMenu->Append(IDM_MENU_DELETE, "&Delete menu");
286 menuMenu->Append(IDM_MENU_TOGGLE, "&Toggle menu", "", TRUE);
287
288 wxMenu *helpMenu = new wxMenu;
289 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
290
291 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
292
293 menuBar->Append(fileMenu, "&File");
294 menuBar->Append(tbarMenu, "&Toolbar");
295 menuBar->Append(menuMenu, "&Menubar");
296 menuBar->Append(helpMenu, "&Help");
297
298 // Associate the menu bar with the frame
299 SetMenuBar(menuBar);
300
301 // Create the toolbar
302 wxToolBar *tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL |
303 wxTB_FLAT | wxTB_DOCKABLE,
304 ID_TOOLBAR);
305
306 tbar->SetMargins( 2, 2 );
307
308 wxGetApp().InitToolbar(tbar);
7fb23305
VZ
309}
310
ad9bb75f 311void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
7fb23305 312{
ad9bb75f
VZ
313 // delete and recreate the toolbar
314 wxToolBar *tbar = GetToolBar();
315 delete tbar;
316
7fb23305 317 SetToolBar(NULL);
ad9bb75f
VZ
318 tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL |
319 wxTB_FLAT | wxTB_DOCKABLE,
320 ID_TOOLBAR);
7fb23305
VZ
321
322 m_smallToolbar = !m_smallToolbar;
ad9bb75f 323 wxGetApp().InitToolbar(tbar, m_smallToolbar);
14d1ccd8
JS
324}
325
47908e25 326void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
14d1ccd8 327{
13437238 328 Close(TRUE);
14d1ccd8
JS
329}
330
47908e25 331void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
14d1ccd8 332{
1d5b7a0b 333 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
13437238 334}
14d1ccd8 335
ad9bb75f
VZ
336void MyFrame::OnDeleteMenu(wxCommandEvent& WXUNUSED(event))
337{
338 wxMenuBar *mbar = GetMenuBar();
339
340 size_t count = mbar->GetMenuCount();
341 if ( count == 3 )
342 {
343 // don't let delete the first 3 menus
344 wxLogError("Can't delete any more menus");
345 }
346 else
347 {
348 delete mbar->Remove(count - 1);
349 }
350}
351
352void MyFrame::OnAppendMenu(wxCommandEvent& WXUNUSED(event))
353{
354 static s_count = 0;
355
356 wxMenu *menu = new wxMenu;
357 menu->Append(0, "First item");
358 menu->AppendSeparator();
359 menu->Append(0, "Second item");
360
361 wxString title;
362 title.Printf("Dummy menu &%d", ++s_count);
363
364 GetMenuBar()->Append(menu, title);
365}
366
367void MyFrame::OnToggleMenu(wxCommandEvent& WXUNUSED(event))
368{
369 wxMenuBar *mbar = GetMenuBar();
370 if ( !m_menu )
371 {
372 // hide the menu
373 m_menu = mbar->Remove(1);
374 }
375 else
376 {
377 // restore it
378 mbar->Insert(1, m_menu, "&Toolbar");
379 m_menu = NULL;
380 }
381}
382
13437238
JS
383void MyFrame::OnToolLeftClick(wxCommandEvent& event)
384{
e179bd65
RR
385 wxString str;
386 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
387 m_textWindow->WriteText( str );
ad9bb75f 388
e179bd65
RR
389 if (event.GetId() == wxID_HELP)
390 {
ad9bb75f 391 if ( event.GetExtraLong() != 0 )
e179bd65
RR
392 m_textWindow->WriteText( _T("Help button down now.\n") );
393 else
394 m_textWindow->WriteText( _T("Help button up now.\n") );
395 }
ad9bb75f 396
e179bd65
RR
397 if (event.GetId() == wxID_COPY)
398 {
7fb23305 399 DoEnablePrint();
e179bd65 400 }
ad9bb75f 401
e179bd65
RR
402 if (event.GetId() == wxID_CUT)
403 {
7fb23305 404 DoToggleHelp();
e179bd65 405 }
14d1ccd8
JS
406}
407
7fb23305
VZ
408void MyFrame::DoEnablePrint()
409{
410 wxToolBar *tb = GetToolBar();
411 if (tb->GetToolEnabled(wxID_PRINT))
412 tb->EnableTool( wxID_PRINT, FALSE );
413 else
414 tb->EnableTool( wxID_PRINT, TRUE );
415}
416
417void MyFrame::DoToggleHelp()
418{
419 wxToolBar *tb = GetToolBar();
420 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
421}
422
13437238
JS
423void MyFrame::OnToolEnter(wxCommandEvent& event)
424{
81d66cf3 425 if (event.GetSelection() > -1)
13437238
JS
426 {
427 wxString str;
58dea4b0 428 str.Printf(_T("This is tool number %d"), event.GetSelection());
13437238
JS
429 SetStatusText(str);
430 }
431 else
432 SetStatusText("");
433}
14d1ccd8 434