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