]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
A bit more testing.
[wxWidgets.git] / samples / toolbar / test.cpp
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
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
34 // ----------------------------------------------------------------------------
35 // resources
36 // ----------------------------------------------------------------------------
37
38 #if defined(__WXGTK__) || defined(__WXMOTIF__)
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
56 class MyApp: public wxApp
57 {
58 public:
59 bool OnInit();
60 bool InitToolbar(wxToolBar* toolBar, bool smallicons = FALSE);
61 };
62
63 // Define a new frame
64 class MyFrame: public wxFrame
65 {
66 public:
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);
73
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
88 private:
89 void DoEnablePrint();
90 void DoToggleHelp();
91
92 bool m_smallToolbar;
93 wxTextCtrl* m_textWindow;
94
95 DECLARE_EVENT_TABLE()
96 };
97
98 // ----------------------------------------------------------------------------
99 // constants
100 // ----------------------------------------------------------------------------
101
102 const int ID_TOOLBAR = 500;
103
104 enum
105 {
106 IDM_TOOLBAR_TOGGLETOOLBAR = 200,
107 IDM_TOOLBAR_ENABLEPRINT,
108 IDM_TOOLBAR_TOGGLEHELP
109 };
110
111 // ----------------------------------------------------------------------------
112 // event tables
113 // ----------------------------------------------------------------------------
114
115 // Notice that wxID_HELP will be processed for the 'About' menu and the toolbar
116 // help button.
117
118 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
119 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
120 EVT_MENU(wxID_HELP, MyFrame::OnAbout)
121
122 EVT_MENU(IDM_TOOLBAR_TOGGLETOOLBAR, MyFrame::OnToggleToolbar)
123 EVT_MENU(IDM_TOOLBAR_ENABLEPRINT, MyFrame::OnEnablePrint)
124 EVT_MENU(IDM_TOOLBAR_TOGGLEHELP, MyFrame::OnToggleHelp)
125
126 EVT_MENU(-1, MyFrame::OnToolLeftClick)
127
128 EVT_TOOL_ENTER(ID_TOOLBAR, MyFrame::OnToolEnter)
129 END_EVENT_TABLE()
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 // ----------------------------------------------------------------------------
136 // MyApp
137 // ----------------------------------------------------------------------------
138
139 IMPLEMENT_APP(MyApp)
140
141 // The `main program' equivalent, creating the windows and returning the
142 // main frame
143 bool 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));
149
150 // VZ: what's this for??
151 #if 0
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);
156 #endif // 0
157
158 frame->Show(TRUE);
159
160 frame->SetStatusText("Hello, wxWindows");
161
162 SetTopWindow(frame);
163
164 return TRUE;
165 }
166
167 bool MyApp::InitToolbar(wxToolBar* toolBar, bool smallicons)
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");
175 if ( !smallicons )
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 }
184 #else
185 toolBarBitmaps[0] = new wxBitmap( new_xpm );
186 toolBarBitmaps[1] = new wxBitmap( open_xpm );
187 if ( !smallicons )
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 }
196 #endif
197
198 #ifdef __WXMSW__
199 int width = 24;
200 #else
201 int width = 16;
202 #endif
203 int currentX = 5;
204
205 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
206 currentX += width + 5;
207 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
208
209 if ( !smallicons )
210 {
211 currentX += width + 5;
212 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 1");
213 currentX += width + 5;
214 toolBar->AddSeparator();
215 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Toggle button 2");
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();
225 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help button");
226
227 toolBar->ToggleTool( wxID_SAVE, TRUE );
228 toolBar->ToggleTool( wxID_COPY, TRUE );
229 toolBar->ToggleTool( wxID_COPY, FALSE );
230 toolBar->EnableTool( wxID_PRINT, FALSE );
231 }
232
233 toolBar->Realize();
234
235 // Can delete the bitmaps since they're reference counted
236 int i, max = smallicons ? 2 : WXSIZEOF(toolBarBitmaps);
237 for (i = 0; i < max; i++)
238 delete toolBarBitmaps[i];
239
240 return TRUE;
241 }
242
243 // ----------------------------------------------------------------------------
244 // MyFrame
245 // ----------------------------------------------------------------------------
246
247 // Define my frame constructor
248 MyFrame::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)
255 {
256 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
257 m_smallToolbar = FALSE;
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
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");
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);
294 }
295
296 void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
297 {
298 // delete and recreate the toolbar
299 wxToolBar *tbar = GetToolBar();
300 delete tbar;
301
302 SetToolBar(NULL);
303 tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL |
304 wxTB_FLAT | wxTB_DOCKABLE,
305 ID_TOOLBAR);
306
307 m_smallToolbar = !m_smallToolbar;
308 wxGetApp().InitToolbar(tbar, m_smallToolbar);
309 }
310
311 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
312 {
313 Close(TRUE);
314 }
315
316 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
317 {
318 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
319 }
320
321 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
322 {
323 wxString str;
324 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
325 m_textWindow->WriteText( str );
326
327 if (event.GetId() == wxID_HELP)
328 {
329 if ( event.GetExtraLong() != 0 )
330 m_textWindow->WriteText( _T("Help button down now.\n") );
331 else
332 m_textWindow->WriteText( _T("Help button up now.\n") );
333 }
334
335 if (event.GetId() == wxID_COPY)
336 {
337 DoEnablePrint();
338 }
339
340 if (event.GetId() == wxID_CUT)
341 {
342 DoToggleHelp();
343 }
344 }
345
346 void 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
355 void MyFrame::DoToggleHelp()
356 {
357 wxToolBar *tb = GetToolBar();
358 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
359 }
360
361 void MyFrame::OnToolEnter(wxCommandEvent& event)
362 {
363 if (event.GetSelection() > -1)
364 {
365 wxString str;
366 str.Printf(_T("This is tool number %d"), event.GetSelection());
367 SetStatusText(str);
368 }
369 else
370 SetStatusText("");
371 }
372