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