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