cosemtic fixes
[wxWidgets.git] / samples / statbar / statbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: statbar.cpp
3 // Purpose: wxStatusBar sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.02.00
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "statbar.cpp"
22 #pragma interface "statbar.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if !wxUSE_STATUSBAR
33 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
34 #endif // wxUSE_STATUSBAR
35
36 // for all others, include the necessary headers (this file is usually all you
37 // need because it includes almost all "standard" wxWindows headers
38 #ifndef WX_PRECOMP
39 #include "wx/app.h"
40 #include "wx/frame.h"
41 #include "wx/statusbr.h"
42 #include "wx/timer.h"
43 #include "wx/checkbox.h"
44 #include "wx/statbmp.h"
45 #include "wx/menu.h"
46 #include "wx/msgdlg.h"
47 #endif
48
49 #include "wx/datetime.h"
50
51 // ----------------------------------------------------------------------------
52 // resources
53 // ----------------------------------------------------------------------------
54
55 #include "green.xpm"
56 #include "red.xpm"
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp : public wxApp
64 {
65 public:
66 // override base class virtuals
67 // ----------------------------
68
69 // this one is called on application startup and is a good place for the app
70 // initialization (doing it here and not in the ctor allows to have an error
71 // return: if OnInit() returns false, the application terminates)
72 virtual bool OnInit();
73 };
74
75 // A custom status bar which contains controls, icons &c
76 class MyStatusBar : public wxStatusBar
77 {
78 public:
79 MyStatusBar(wxWindow *parent);
80 virtual ~MyStatusBar();
81
82 void UpdateClock();
83
84 // event handlers
85 void OnSize(wxSizeEvent& event);
86 void OnToggleClock(wxCommandEvent& event);
87
88 private:
89 enum
90 {
91 Field_Text,
92 Field_Checkbox,
93 Field_Bitmap,
94 Field_Clock,
95 Field_Max
96 };
97
98 class MyTimer : public wxTimer
99 {
100 public:
101 MyTimer(MyStatusBar *statbar) {m_statbar = statbar; }
102
103 virtual void Notify() { m_statbar->UpdateClock(); }
104
105 private:
106 MyStatusBar *m_statbar;
107 } m_timer;
108
109 wxCheckBox *m_checkbox;
110 wxStaticBitmap *m_statbmp;
111
112 DECLARE_EVENT_TABLE()
113 };
114
115 // Define a new frame type: this is going to be our main frame
116 class MyFrame : public wxFrame
117 {
118 public:
119 // ctor(s)
120 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
121 virtual ~MyFrame();
122
123 // event handlers (these functions should _not_ be virtual)
124 void OnQuit(wxCommandEvent& event);
125 void OnAbout(wxCommandEvent& event);
126 void OnRecreateStatusBar(wxCommandEvent& event);
127
128 private:
129 enum StatBarKind
130 {
131 StatBar_Default,
132 StatBar_Custom,
133 StatBar_Max
134 } m_statbarKind;
135
136 void DoCreateStatusBar(StatBarKind kind);
137
138 wxStatusBar *m_statbarDefault;
139 MyStatusBar *m_statbarCustom;
140
141 // any class wishing to process wxWindows events must use this macro
142 DECLARE_EVENT_TABLE()
143 };
144
145 // ----------------------------------------------------------------------------
146 // constants
147 // ----------------------------------------------------------------------------
148
149 // IDs for the controls and the menu commands
150 enum
151 {
152 // menu items
153 StatusBar_Quit = 1,
154 StatusBar_Recreate,
155 StatusBar_About,
156 StatusBar_Checkbox = 1000
157 };
158
159 static const int BITMAP_SIZE_X = 32;
160 static const int BITMAP_SIZE_Y = 15;
161
162 // ----------------------------------------------------------------------------
163 // event tables and other macros for wxWindows
164 // ----------------------------------------------------------------------------
165
166 // the event tables connect the wxWindows events with the functions (event
167 // handlers) which process them. It can be also done at run-time, but for the
168 // simple menu events like this the static method is much simpler.
169 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
170 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
171 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
172 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
173 END_EVENT_TABLE()
174
175 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
176 EVT_SIZE(MyStatusBar::OnSize)
177 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
178 END_EVENT_TABLE()
179
180 // Create a new application object: this macro will allow wxWindows to create
181 // the application object during program execution (it's better than using a
182 // static object for many reasons) and also declares the accessor function
183 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
184 // not wxApp)
185 IMPLEMENT_APP(MyApp)
186
187 // ============================================================================
188 // implementation
189 // ============================================================================
190
191 // ----------------------------------------------------------------------------
192 // the application class
193 // ----------------------------------------------------------------------------
194
195 // `Main program' equivalent: the program execution "starts" here
196 bool MyApp::OnInit()
197 {
198 // create the main application window
199 MyFrame *frame = new MyFrame("wxStatusBar sample",
200 wxPoint(50, 50), wxSize(450, 340));
201
202 // and show it (the frames, unlike simple controls, are not shown when
203 // created initially)
204 frame->Show(TRUE);
205
206 // success: wxApp::OnRun() will be called which will enter the main message
207 // loop and the application will run. If we returned FALSE here, the
208 // application would exit immediately.
209 return TRUE;
210 }
211
212 // ----------------------------------------------------------------------------
213 // main frame
214 // ----------------------------------------------------------------------------
215
216 // frame constructor
217 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
218 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
219 {
220 m_statbarDefault = NULL;
221 m_statbarCustom = NULL;
222
223 #ifdef __WXMAC__
224 // we need this in order to allow the about menu relocation, since ABOUT is
225 // not the default id of the about menu
226 wxApp::s_macAboutMenuItemId = StatusBar_About;
227 #endif
228
229 // create a menu bar
230 wxMenu *menuFile = new wxMenu;
231 menuFile->Append(StatusBar_Quit, "E&xit\tAlt-X", "Quit this program");
232
233 wxMenu *statbarMenu = new wxMenu;
234 statbarMenu->Append(StatusBar_Recreate, "&Recreate\tCtrl-R",
235 "Toggle status bar format");
236
237 wxMenu *helpMenu = new wxMenu;
238 helpMenu->Append(StatusBar_About, "&About...\tCtrl-A", "Show about dialog");
239
240 // now append the freshly created menu to the menu bar...
241 wxMenuBar *menuBar = new wxMenuBar();
242 menuBar->Append(menuFile, "&File");
243 menuBar->Append(statbarMenu, "&Status bar");
244 menuBar->Append(helpMenu, "&Help");
245
246 // ... and attach this menu bar to the frame
247 SetMenuBar(menuBar);
248
249 // create default status bar to start with
250 CreateStatusBar(2);
251 SetStatusText("Welcome to wxWindows!");
252
253 m_statbarDefault = GetStatusBar();
254 }
255
256 MyFrame::~MyFrame()
257 {
258 SetStatusBar(NULL);
259
260 delete m_statbarDefault;
261 delete m_statbarCustom;
262 }
263
264 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
265 {
266 wxStatusBar *statbarOld = GetStatusBar();
267 if ( statbarOld )
268 {
269 statbarOld->Hide();
270 }
271
272 switch ( kind )
273 {
274 case StatBar_Default:
275 SetStatusBar(m_statbarDefault);
276 break;
277
278 case StatBar_Custom:
279 if ( !m_statbarCustom )
280 {
281 m_statbarCustom = new MyStatusBar(this);
282 }
283 SetStatusBar(m_statbarCustom);
284 break;
285
286 default:
287 wxFAIL_MSG("unknown stat bar kind");
288 }
289
290 PositionStatusBar();
291 GetStatusBar()->Show();
292
293 m_statbarKind = kind;
294 }
295
296 // event handlers
297 void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
298 {
299 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
300 : StatBar_Custom);
301 }
302
303 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
304 {
305 // TRUE is to force the frame to close
306 Close(TRUE);
307 }
308
309 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
310 {
311 wxMessageBox("wxStatusBar sample\n(c) 2000 Vadim Zeitlin",
312 "About statbar", wxOK | wxICON_INFORMATION, this);
313 }
314
315 // ----------------------------------------------------------------------------
316 // MyStatusBar
317 // ----------------------------------------------------------------------------
318
319 #ifdef __VISUALC__
320 // 'this' : used in base member initializer list -- so what??
321 #pragma warning(disable: 4355)
322 #endif
323
324 MyStatusBar::MyStatusBar(wxWindow *parent)
325 : wxStatusBar(parent, -1), m_timer(this)
326 {
327 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
328
329 SetFieldsCount(Field_Max);
330 SetStatusWidths(Field_Max, widths);
331
332 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock"));
333 m_checkbox->SetValue(TRUE);
334
335 m_statbmp = new wxStaticBitmap(this, -1, wxIcon(green_xpm));
336
337 m_timer.Start(1000);
338
339 SetMinHeight(BITMAP_SIZE_Y);
340
341 UpdateClock();
342 }
343
344 #ifdef __VISUALC__
345 #pragma warning(default: 4355)
346 #endif
347
348 MyStatusBar::~MyStatusBar()
349 {
350 if ( m_timer.IsRunning() )
351 {
352 m_timer.Stop();
353 }
354 }
355
356 void MyStatusBar::OnSize(wxSizeEvent& event)
357 {
358 wxRect rect;
359 GetFieldRect(Field_Checkbox, rect);
360
361 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
362
363 GetFieldRect(Field_Bitmap, rect);
364 m_statbmp->Move(rect.x + (rect.width - BITMAP_SIZE_X) / 2,
365 rect.y + (rect.height - BITMAP_SIZE_Y) / 2);
366
367 event.Skip();
368 }
369
370 void MyStatusBar::OnToggleClock(wxCommandEvent& event)
371 {
372 if ( m_checkbox->GetValue() )
373 {
374 m_timer.Start(1000);
375
376 m_statbmp->SetIcon(wxIcon(green_xpm));
377
378 UpdateClock();
379 }
380 else // don't show clock
381 {
382 m_timer.Stop();
383
384 m_statbmp->SetIcon(wxIcon(red_xpm));
385
386 SetStatusText("", Field_Clock);
387 }
388 }
389
390 void MyStatusBar::UpdateClock()
391 {
392 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
393 }