some appearance 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
37 #ifndef WX_PRECOMP
38 #include "wx/app.h"
39 #include "wx/frame.h"
40 #include "wx/statusbr.h"
41 #include "wx/timer.h"
42 #include "wx/checkbox.h"
43 #include "wx/statbmp.h"
44 #include "wx/menu.h"
45 #include "wx/msgdlg.h"
46 #include "wx/textdlg.h"
47 #include "wx/sizer.h"
48 #include "wx/stattext.h"
49 #endif
50
51 #include "wx/datetime.h"
52
53 // ----------------------------------------------------------------------------
54 // resources
55 // ----------------------------------------------------------------------------
56
57 #include "green.xpm"
58 #include "red.xpm"
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp : public wxApp
66 {
67 public:
68 // override base class virtuals
69 // ----------------------------
70
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
75 };
76
77 // A custom status bar which contains controls, icons &c
78 class MyStatusBar : public wxStatusBar
79 {
80 public:
81 MyStatusBar(wxWindow *parent);
82 virtual ~MyStatusBar();
83
84 void UpdateClock();
85
86 // event handlers
87 void OnTimer(wxTimerEvent& event) { UpdateClock(); }
88 void OnSize(wxSizeEvent& event);
89 void OnToggleClock(wxCommandEvent& event);
90
91 private:
92 enum
93 {
94 Field_Text,
95 Field_Checkbox,
96 Field_Bitmap,
97 Field_Clock,
98 Field_Max
99 };
100
101 wxTimer m_timer;
102
103 wxCheckBox *m_checkbox;
104 wxStaticBitmap *m_statbmp;
105
106 DECLARE_EVENT_TABLE()
107 };
108
109 // Define a new frame type: this is going to be our main frame
110 class MyFrame : public wxFrame
111 {
112 public:
113 // ctor(s)
114 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
115 virtual ~MyFrame();
116
117 // event handlers (these functions should _not_ be virtual)
118 void OnQuit(wxCommandEvent& event);
119 void OnAbout(wxCommandEvent& event);
120
121 void OnSetStatusFields(wxCommandEvent& event);
122 void OnRecreateStatusBar(wxCommandEvent& event);
123
124 private:
125 enum StatBarKind
126 {
127 StatBar_Default,
128 StatBar_Custom,
129 StatBar_Max
130 } m_statbarKind;
131
132 void DoCreateStatusBar(StatBarKind kind);
133
134 wxStatusBar *m_statbarDefault;
135 MyStatusBar *m_statbarCustom;
136
137 // any class wishing to process wxWindows events must use this macro
138 DECLARE_EVENT_TABLE()
139 };
140
141 // Our about dialog ith its status bar
142 class MyAboutDialog : public wxDialog
143 {
144 public:
145 MyAboutDialog(wxWindow *parent);
146 };
147
148 // ----------------------------------------------------------------------------
149 // constants
150 // ----------------------------------------------------------------------------
151
152 // IDs for the controls and the menu commands
153 enum
154 {
155 // menu items
156 StatusBar_Quit = 1,
157 StatusBar_SetFields,
158 StatusBar_Recreate,
159 StatusBar_About,
160 StatusBar_Checkbox = 1000
161 };
162
163 static const int BITMAP_SIZE_X = 32;
164 static const int BITMAP_SIZE_Y = 15;
165
166 // ----------------------------------------------------------------------------
167 // event tables and other macros for wxWindows
168 // ----------------------------------------------------------------------------
169
170 // the event tables connect the wxWindows events with the functions (event
171 // handlers) which process them. It can be also done at run-time, but for the
172 // simple menu events like this the static method is much simpler.
173 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
174 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
175 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
176 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
177 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
178 END_EVENT_TABLE()
179
180 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
181 EVT_SIZE(MyStatusBar::OnSize)
182 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
183 EVT_TIMER(-1, MyStatusBar::OnTimer)
184 END_EVENT_TABLE()
185
186 // Create a new application object: this macro will allow wxWindows to create
187 // the application object during program execution (it's better than using a
188 // static object for many reasons) and also declares the accessor function
189 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
190 // not wxApp)
191 IMPLEMENT_APP(MyApp)
192
193 // ============================================================================
194 // implementation
195 // ============================================================================
196
197 // ----------------------------------------------------------------------------
198 // the application class
199 // ----------------------------------------------------------------------------
200
201 // `Main program' equivalent: the program execution "starts" here
202 bool MyApp::OnInit()
203 {
204 // create the main application window
205 MyFrame *frame = new MyFrame("wxStatusBar sample",
206 wxPoint(50, 50), wxSize(450, 340));
207
208 // and show it (the frames, unlike simple controls, are not shown when
209 // created initially)
210 frame->Show(TRUE);
211
212 // success: wxApp::OnRun() will be called which will enter the main message
213 // loop and the application will run. If we returned FALSE here, the
214 // application would exit immediately.
215 return TRUE;
216 }
217
218 // ----------------------------------------------------------------------------
219 // main frame
220 // ----------------------------------------------------------------------------
221
222 // frame constructor
223 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
224 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
225 {
226 m_statbarDefault = NULL;
227 m_statbarCustom = NULL;
228
229 #ifdef __WXMAC__
230 // we need this in order to allow the about menu relocation, since ABOUT is
231 // not the default id of the about menu
232 wxApp::s_macAboutMenuItemId = StatusBar_About;
233 #endif
234
235 // create a menu bar
236 wxMenu *menuFile = new wxMenu;
237 menuFile->Append(StatusBar_Quit, "E&xit\tAlt-X", "Quit this program");
238
239 wxMenu *statbarMenu = new wxMenu;
240 statbarMenu->Append(StatusBar_SetFields, "&Set field count\tCtrl-C",
241 "Set the number of status bar fields");
242 statbarMenu->Append(StatusBar_Recreate, "&Recreate\tCtrl-R",
243 "Toggle status bar format");
244
245 wxMenu *helpMenu = new wxMenu;
246 helpMenu->Append(StatusBar_About, "&About...\tCtrl-A", "Show about dialog");
247
248 // now append the freshly created menu to the menu bar...
249 wxMenuBar *menuBar = new wxMenuBar();
250 menuBar->Append(menuFile, "&File");
251 menuBar->Append(statbarMenu, "&Status bar");
252 menuBar->Append(helpMenu, "&Help");
253
254 // ... and attach this menu bar to the frame
255 SetMenuBar(menuBar);
256
257 // create default status bar to start with
258 CreateStatusBar(2);
259 SetStatusText("Welcome to wxWindows!");
260
261 m_statbarDefault = GetStatusBar();
262 }
263
264 MyFrame::~MyFrame()
265 {
266 SetStatusBar(NULL);
267
268 delete m_statbarDefault;
269 delete m_statbarCustom;
270 }
271
272 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
273 {
274 wxStatusBar *statbarOld = GetStatusBar();
275 if ( statbarOld )
276 {
277 statbarOld->Hide();
278 }
279
280 switch ( kind )
281 {
282 case StatBar_Default:
283 SetStatusBar(m_statbarDefault);
284 break;
285
286 case StatBar_Custom:
287 if ( !m_statbarCustom )
288 {
289 m_statbarCustom = new MyStatusBar(this);
290 }
291 SetStatusBar(m_statbarCustom);
292 break;
293
294 default:
295 wxFAIL_MSG("unknown stat bar kind");
296 }
297
298 PositionStatusBar();
299 GetStatusBar()->Show();
300
301 m_statbarKind = kind;
302 }
303
304 // event handlers
305 void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
306 {
307 wxStatusBar *sb = GetStatusBar();
308
309 long nFields = wxGetNumberFromUser
310 (
311 "Select the number of fields in the status bar",
312 "Fields:",
313 "wxWindows statusbar sample",
314 sb->GetFieldsCount(),
315 1, 5,
316 this
317 );
318
319 // we don't check if the number changed at all on purpose: calling
320 // SetFieldsCount() with the same number of fields should be ok
321 if ( nFields != -1 )
322 {
323 // we set the widths only for 2 of them, otherwise let all the fields
324 // have equal width (the default behaviour)
325 const int *widths = NULL;
326 if ( nFields == 2 )
327 {
328 static const int widthsFor2Fields[2] = { 200, -1 };
329 widths = widthsFor2Fields;
330 }
331
332 sb->SetFieldsCount(nFields, widths);
333
334 wxLogStatus(this,
335 wxString::Format("Status bar now has %ld fields", nFields));
336 }
337 else
338 {
339 wxLogStatus(this, "Cancelled");
340 }
341 }
342
343 void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
344 {
345 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
346 : StatBar_Custom);
347 }
348
349 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
350 {
351 // TRUE is to force the frame to close
352 Close(TRUE);
353 }
354
355 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
356 {
357 MyAboutDialog dlg(this);
358 dlg.ShowModal();
359 }
360
361 // ----------------------------------------------------------------------------
362 // MyAboutDialog
363 // ----------------------------------------------------------------------------
364
365 MyAboutDialog::MyAboutDialog(wxWindow *parent)
366 : wxDialog(parent, -1, wxString("About statbar"),
367 wxDefaultPosition, wxDefaultSize,
368 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
369 {
370 wxStaticText *text = new wxStaticText(this, -1,
371 "wxStatusBar sample\n"
372 "(c) 2000 Vadim Zeitlin");
373
374 wxButton *btn = new wxButton(this, wxID_OK, "&Close");
375
376 // create the top status bar without the size grip (default style),
377 // otherwise it looks weird
378 wxStatusBar *statbarTop = new wxStatusBar(this, -1, 0);
379 statbarTop->SetFieldsCount(3);
380 statbarTop->SetStatusText("This is a top status bar", 0);
381 statbarTop->SetStatusText("in a dialog", 1);
382 statbarTop->SetStatusText("Great, isn't it?", 2);
383
384 wxStatusBar *statbarBottom = new wxStatusBar(this, -1);
385 statbarBottom->SetFieldsCount(2);
386 statbarBottom->SetStatusText("This is a bottom status bar", 0);
387 statbarBottom->SetStatusText("in a dialog", 1);
388
389 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
390 sizerTop->Add(statbarTop, 0, wxGROW);
391 sizerTop->Add(-1, 10, 1, wxGROW);
392 sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
393 sizerTop->Add(-1, 10, 1, wxGROW);
394 sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
395 sizerTop->Add(-1, 10, 1, wxGROW);
396 sizerTop->Add(statbarBottom, 0, wxGROW);
397
398 SetAutoLayout(TRUE);
399 SetSizer(sizerTop);
400
401 sizerTop->Fit(this);
402 sizerTop->SetSizeHints(this);
403 }
404
405 // ----------------------------------------------------------------------------
406 // MyStatusBar
407 // ----------------------------------------------------------------------------
408
409 #ifdef __VISUALC__
410 // 'this' : used in base member initializer list -- so what??
411 #pragma warning(disable: 4355)
412 #endif
413
414 MyStatusBar::MyStatusBar(wxWindow *parent)
415 : wxStatusBar(parent, -1), m_timer(this)
416 {
417 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
418
419 SetFieldsCount(Field_Max);
420 SetStatusWidths(Field_Max, widths);
421
422 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock"));
423 m_checkbox->SetValue(TRUE);
424
425 m_statbmp = new wxStaticBitmap(this, -1, wxIcon(green_xpm));
426
427 m_timer.Start(1000);
428
429 SetMinHeight(BITMAP_SIZE_Y);
430
431 UpdateClock();
432 }
433
434 #ifdef __VISUALC__
435 #pragma warning(default: 4355)
436 #endif
437
438 MyStatusBar::~MyStatusBar()
439 {
440 if ( m_timer.IsRunning() )
441 {
442 m_timer.Stop();
443 }
444 }
445
446 void MyStatusBar::OnSize(wxSizeEvent& event)
447 {
448 wxRect rect;
449 GetFieldRect(Field_Checkbox, rect);
450
451 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
452
453 GetFieldRect(Field_Bitmap, rect);
454 m_statbmp->Move(rect.x + (rect.width - BITMAP_SIZE_X) / 2,
455 rect.y + (rect.height - BITMAP_SIZE_Y) / 2);
456
457 event.Skip();
458 }
459
460 void MyStatusBar::OnToggleClock(wxCommandEvent& event)
461 {
462 if ( m_checkbox->GetValue() )
463 {
464 m_timer.Start(1000);
465
466 m_statbmp->SetIcon(wxIcon(green_xpm));
467
468 UpdateClock();
469 }
470 else // don't show clock
471 {
472 m_timer.Stop();
473
474 m_statbmp->SetIcon(wxIcon(red_xpm));
475
476 SetStatusText("", Field_Clock);
477 }
478 }
479
480 void MyStatusBar::UpdateClock()
481 {
482 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
483 }