]> git.saurik.com Git - wxWidgets.git/blob - samples/toolbar/test.cpp
wxGTK compiles (and seems to work) again after wxMenu changes
[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, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
213 currentX += width + 5;
214 toolBar->AddSeparator();
215 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Disable/Enable print button");
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]), *(toolBarBitmaps[6]), TRUE, currentX, -1, (wxObject *) NULL, "Help");
226
227 toolBar->EnableTool( wxID_PRINT, FALSE );
228 }
229
230 toolBar->Realize();
231
232 // Can delete the bitmaps since they're reference counted
233 int i, max = smallicons ? 2 : WXSIZEOF(toolBarBitmaps);
234 for (i = 0; i < max; i++)
235 delete toolBarBitmaps[i];
236
237 return TRUE;
238 }
239
240 // ----------------------------------------------------------------------------
241 // MyFrame
242 // ----------------------------------------------------------------------------
243
244 // Define my frame constructor
245 MyFrame::MyFrame(wxFrame* parent,
246 wxWindowID id,
247 const wxString& title,
248 const wxPoint& pos,
249 const wxSize& size,
250 long style)
251 : wxFrame(parent, id, title, pos, size, style)
252 {
253 m_textWindow = new wxTextCtrl(this, -1, "", wxPoint(0, 0), wxSize(-1, -1), wxTE_MULTILINE);
254 m_smallToolbar = FALSE;
255
256 // Give it a status line
257 CreateStatusBar();
258
259 // Give it an icon
260 SetIcon(wxICON(mondrian));
261
262 // Make a menubar
263 wxMenu *tbarMenu = new wxMenu;
264 tbarMenu->Append(IDM_TOOLBAR_TOGGLETOOLBAR, "&Toggle toolbar", "Change the toolbar kind");
265 tbarMenu->Append(IDM_TOOLBAR_ENABLEPRINT, "&Enable print button", "");
266 tbarMenu->Append(IDM_TOOLBAR_TOGGLEHELP, "Toggle &help button", "");
267
268 wxMenu *fileMenu = new wxMenu;
269 fileMenu->Append(wxID_EXIT, "E&xit", "Quit toolbar sample" );
270
271 wxMenu *helpMenu = new wxMenu;
272 helpMenu->Append(wxID_HELP, "&About", "About toolbar sample");
273
274 wxMenuBar* menuBar = new wxMenuBar( wxMB_DOCKABLE );
275
276 menuBar->Append(fileMenu, "&File");
277 menuBar->Append(tbarMenu, "&Toolbar");
278 menuBar->Append(helpMenu, "&Help");
279
280 // Associate the menu bar with the frame
281 SetMenuBar(menuBar);
282
283 // Create the toolbar
284 wxToolBar *tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL |
285 wxTB_FLAT | wxTB_DOCKABLE,
286 ID_TOOLBAR);
287
288 tbar->SetMargins( 2, 2 );
289
290 wxGetApp().InitToolbar(tbar);
291 }
292
293 void MyFrame::OnToggleToolbar(wxCommandEvent& WXUNUSED(event))
294 {
295 // delete and recreate the toolbar
296 wxToolBar *tbar = GetToolBar();
297 delete tbar;
298
299 SetToolBar(NULL);
300 tbar = CreateToolBar(wxNO_BORDER | wxTB_HORIZONTAL |
301 wxTB_FLAT | wxTB_DOCKABLE,
302 ID_TOOLBAR);
303
304 m_smallToolbar = !m_smallToolbar;
305 wxGetApp().InitToolbar(tbar, m_smallToolbar);
306 }
307
308 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
309 {
310 Close(TRUE);
311 }
312
313 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
314 {
315 (void)wxMessageBox("wxWindows toolbar sample", "About wxToolBar");
316 }
317
318 void MyFrame::OnToolLeftClick(wxCommandEvent& event)
319 {
320 wxString str;
321 str.Printf( _T("Clicked on tool %d\n"), event.GetId());
322 m_textWindow->WriteText( str );
323
324 if (event.GetId() == wxID_HELP)
325 {
326 if ( event.GetExtraLong() != 0 )
327 m_textWindow->WriteText( _T("Help button down now.\n") );
328 else
329 m_textWindow->WriteText( _T("Help button up now.\n") );
330 }
331
332 if (event.GetId() == wxID_COPY)
333 {
334 DoEnablePrint();
335 }
336
337 if (event.GetId() == wxID_CUT)
338 {
339 DoToggleHelp();
340 }
341 }
342
343 void MyFrame::DoEnablePrint()
344 {
345 wxToolBar *tb = GetToolBar();
346 if (tb->GetToolEnabled(wxID_PRINT))
347 tb->EnableTool( wxID_PRINT, FALSE );
348 else
349 tb->EnableTool( wxID_PRINT, TRUE );
350 }
351
352 void MyFrame::DoToggleHelp()
353 {
354 wxToolBar *tb = GetToolBar();
355 tb->ToggleTool( wxID_HELP, !tb->GetToolState( wxID_HELP ) );
356 }
357
358 void MyFrame::OnToolEnter(wxCommandEvent& event)
359 {
360 if (event.GetSelection() > -1)
361 {
362 wxString str;
363 str.Printf(_T("This is tool number %d"), event.GetSelection());
364 SetStatusText(str);
365 }
366 else
367 SetStatusText("");
368 }
369