]> git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
renamed wxEventFixModule to wxWakeUpIdleModule; added error checking; slight code...
[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 // 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 #if !wxUSE_STATUSBAR
28 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
29 #endif // wxUSE_STATUSBAR
30
31 // for all others, include the necessary headers
32 #ifndef WX_PRECOMP
33 #include "wx/app.h"
34 #include "wx/log.h"
35 #include "wx/frame.h"
36 #include "wx/statusbr.h"
37 #include "wx/timer.h"
38 #include "wx/checkbox.h"
39 #include "wx/statbmp.h"
40 #include "wx/menu.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textdlg.h"
43 #include "wx/sizer.h"
44 #include "wx/stattext.h"
45 #include "wx/bmpbuttn.h"
46 #include "wx/dcmemory.h"
47 #endif
48
49 #include "wx/datetime.h"
50
51 // define this for the platforms which don't support wxBitmapButton (such as
52 // Motif), else a wxBitmapButton will be used
53 #ifdef __WXMOTIF__
54 //#define USE_MDI_PARENT_FRAME 1
55
56 #ifdef USE_MDI_PARENT_FRAME
57 #include "wx/mdi.h"
58 #endif // USE_MDI_PARENT_FRAME
59 #define USE_STATIC_BITMAP
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // resources
64 // ----------------------------------------------------------------------------
65
66 #ifdef USE_STATIC_BITMAP
67 #include "green.xpm"
68 #include "red.xpm"
69 #endif // USE_STATIC_BITMAP
70
71 // ----------------------------------------------------------------------------
72 // private classes
73 // ----------------------------------------------------------------------------
74
75 // Define a new application type, each program should derive a class from wxApp
76 class MyApp : public wxApp
77 {
78 public:
79 // override base class virtuals
80 // ----------------------------
81
82 // this one is called on application startup and is a good place for the app
83 // initialization (doing it here and not in the ctor allows to have an error
84 // return: if OnInit() returns false, the application terminates)
85 virtual bool OnInit();
86 };
87
88 // A custom status bar which contains controls, icons &c
89 class MyStatusBar : public wxStatusBar
90 {
91 public:
92 MyStatusBar(wxWindow *parent);
93 virtual ~MyStatusBar();
94
95 void UpdateClock();
96
97 // event handlers
98 void OnTimer(wxTimerEvent& event) { UpdateClock(); }
99 void OnSize(wxSizeEvent& event);
100 void OnToggleClock(wxCommandEvent& event);
101 void OnButton(wxCommandEvent& event);
102
103 private:
104 // toggle the state of the status bar controls
105 void DoToggle();
106
107 wxBitmap CreateBitmapForButton(bool on = FALSE);
108
109 enum
110 {
111 Field_Text,
112 Field_Checkbox,
113 Field_Bitmap,
114 Field_Clock,
115 Field_Max
116 };
117
118 wxTimer m_timer;
119
120 wxCheckBox *m_checkbox;
121 #ifdef USE_STATIC_BITMAP
122 wxStaticBitmap *m_statbmp;
123 #else
124 wxBitmapButton *m_statbmp;
125 #endif
126
127 DECLARE_EVENT_TABLE()
128 };
129
130 // Define a new frame type: this is going to be our main frame
131 class MyFrame : public wxFrame
132 {
133 public:
134 // ctor(s)
135 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
136 #ifdef USE_MDI_PARENT_FRAME
137 class MyFrame : public wxMDIParentFrame
138 #else
139 virtual ~MyFrame();
140 #endif
141
142 // event handlers (these functions should _not_ be virtual)
143 void OnQuit(wxCommandEvent& event);
144 void OnAbout(wxCommandEvent& event);
145
146 void OnSetStatusFields(wxCommandEvent& event);
147 void OnRecreateStatusBar(wxCommandEvent& event);
148
149 private:
150 enum StatBarKind
151 {
152 StatBar_Default,
153 StatBar_Custom,
154 StatBar_Max
155 } m_statbarKind;
156 void OnUpdateSetStatusFields(wxUpdateUIEvent& event);
157 void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
158 void OnStatusBarToggle(wxCommandEvent& event);
159 void DoCreateStatusBar(StatBarKind kind);
160
161 wxStatusBar *m_statbarDefault;
162 MyStatusBar *m_statbarCustom;
163
164 // any class wishing to process wxWindows events must use this macro
165 DECLARE_EVENT_TABLE()
166 };
167
168 // Our about dialog ith its status bar
169 class MyAboutDialog : public wxDialog
170 {
171 public:
172 MyAboutDialog(wxWindow *parent);
173 };
174
175 // ----------------------------------------------------------------------------
176 // constants
177 // ----------------------------------------------------------------------------
178
179 // IDs for the controls and the menu commands
180 enum
181 {
182 // menu items
183 StatusBar_Quit = 1,
184 StatusBar_SetFields,
185 StatusBar_Recreate,
186 StatusBar_About,
187 StatusBar_Toggle,
188 StatusBar_Checkbox = 1000
189 };
190
191 static const int BITMAP_SIZE_X = 32;
192 static const int BITMAP_SIZE_Y = 15;
193
194 // ----------------------------------------------------------------------------
195 // event tables and other macros for wxWindows
196 // ----------------------------------------------------------------------------
197
198 // the event tables connect the wxWindows events with the functions (event
199 // handlers) which process them. It can be also done at run-time, but for the
200 // simple menu events like this the static method is much simpler.
201 #ifdef USE_MDI_PARENT_FRAME
202 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
203 #else
204 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
205 #endif
206 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
207 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
208 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
209 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
210 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
211 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
212 EVT_UPDATE_UI(StatusBar_SetFields, MyFrame::OnUpdateSetStatusFields)
213 END_EVENT_TABLE()
214
215 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
216 EVT_SIZE(MyStatusBar::OnSize)
217 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
218 EVT_BUTTON(-1, MyStatusBar::OnButton)
219 EVT_TIMER(-1, MyStatusBar::OnTimer)
220 END_EVENT_TABLE()
221
222 // Create a new application object: this macro will allow wxWindows to create
223 // the application object during program execution (it's better than using a
224 // static object for many reasons) and also declares the accessor function
225 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
226 // not wxApp)
227 IMPLEMENT_APP(MyApp)
228
229 // ============================================================================
230 // implementation
231 // ============================================================================
232
233 // ----------------------------------------------------------------------------
234 // the application class
235 // ----------------------------------------------------------------------------
236
237 // `Main program' equivalent: the program execution "starts" here
238 bool MyApp::OnInit()
239 {
240 // create the main application window
241 MyFrame *frame = new MyFrame(_T("wxStatusBar sample"),
242 wxPoint(50, 50), wxSize(450, 340));
243
244 // and show it (the frames, unlike simple controls, are not shown when
245 // created initially)
246 frame->Show(TRUE);
247
248 // success: wxApp::OnRun() will be called which will enter the main message
249 // loop and the application will run. If we returned FALSE here, the
250 // application would exit immediately.
251 return TRUE;
252 }
253
254 // ----------------------------------------------------------------------------
255 // main frame
256 // ----------------------------------------------------------------------------
257
258 // frame constructor
259 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
260 #ifdef USE_MDI_PARENT_FRAME
261 : wxMDIParentFrame((wxWindow *)NULL, -1, title, pos, size)
262 #else
263 : wxFrame((wxWindow *)NULL, -1, title, pos, size)
264 #endif
265 {
266 m_statbarDefault = NULL;
267 m_statbarCustom = NULL;
268
269 #ifdef __WXMAC__
270 // we need this in order to allow the about menu relocation, since ABOUT is
271 // not the default id of the about menu
272 wxApp::s_macAboutMenuItemId = StatusBar_About;
273 #endif
274
275 // create a menu bar
276 wxMenu *menuFile = new wxMenu;
277 menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
278
279 wxMenu *statbarMenu = new wxMenu;
280 statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"),
281 _T("Set the number of status bar fields"));
282 statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"),
283 _T("Toggle the status bar display"), true);
284 statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"),
285 _T("Toggle status bar format"));
286
287 wxMenu *helpMenu = new wxMenu;
288 helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
289
290 // now append the freshly created menu to the menu bar...
291 wxMenuBar *menuBar = new wxMenuBar();
292 menuBar->Append(menuFile, _T("&File"));
293 menuBar->Append(statbarMenu, _T("&Status bar"));
294 menuBar->Append(helpMenu, _T("&Help"));
295
296 // ... and attach this menu bar to the frame
297 SetMenuBar(menuBar);
298
299 // create default status bar to start with
300 CreateStatusBar(2);
301 m_statbarKind = StatBar_Default;
302 SetStatusText(_T("Welcome to wxWindows!"));
303
304 m_statbarDefault = GetStatusBar();
305 }
306
307 MyFrame::~MyFrame()
308 {
309 SetStatusBar(NULL);
310
311 delete m_statbarDefault;
312 delete m_statbarCustom;
313 }
314
315 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
316 {
317 wxStatusBar *statbarOld = GetStatusBar();
318 if ( statbarOld )
319 {
320 statbarOld->Hide();
321 }
322
323 switch ( kind )
324 {
325 case StatBar_Default:
326 SetStatusBar(m_statbarDefault);
327 break;
328
329 case StatBar_Custom:
330 if ( !m_statbarCustom )
331 {
332 m_statbarCustom = new MyStatusBar(this);
333 }
334 SetStatusBar(m_statbarCustom);
335 break;
336
337 default:
338 wxFAIL_MSG(wxT("unknown stat bar kind"));
339 }
340
341 GetStatusBar()->Show();
342 PositionStatusBar();
343
344 m_statbarKind = kind;
345 }
346
347 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent& event)
348 {
349 // only allow the setting of the number of status fields for the default
350 // status bar
351 wxStatusBar *sb = GetStatusBar();
352 event.Enable(sb == m_statbarDefault);
353 }
354
355
356 // event handlers
357 void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
358 {
359 wxStatusBar *sb = GetStatusBar();
360
361 long nFields = wxGetNumberFromUser
362 (
363 _T("Select the number of fields in the status bar"),
364 _T("Fields:"),
365 _T("wxWindows statusbar sample"),
366 sb->GetFieldsCount(),
367 1, 5,
368 this
369 );
370
371 // we don't check if the number changed at all on purpose: calling
372 // SetFieldsCount() with the same number of fields should be ok
373 if ( nFields != -1 )
374 {
375 static const int widthsFor2Fields[] = { 200, -1 };
376 static const int widthsFor3Fields[] = { -1, -2, -1 };
377 static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 };
378
379 static const int *widthsAll[] =
380 {
381 NULL, // 1 field: default
382 widthsFor2Fields, // 2 fields: 1 fixed, 1 var
383 widthsFor3Fields, // 3 fields: 3 var
384 widthsFor4Fields, // 4 fields: 3 fixed, 2 vars
385 NULL // 5 fields: default (all have same width)
386 };
387
388 const int * const widths = widthsAll[nFields - 1];
389 sb->SetFieldsCount(nFields, widths);
390
391 wxString s;
392 for ( long n = 0; n < nFields; n++ )
393 {
394 if ( widths )
395 {
396 if ( widths[n] > 0 )
397 s.Printf(_T("fixed (%d)"), widths[n]);
398 else
399 s.Printf(_T("variable (*%d)"), -widths[n]);
400 }
401 else
402 {
403 s = _T("default");
404 }
405
406 SetStatusText(s, n);
407 }
408 }
409 else
410 {
411 wxLogStatus(this, wxT("Cancelled"));
412 }
413 }
414
415 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event)
416 {
417 event.Check(GetStatusBar() != 0);
418 }
419
420 void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event))
421 {
422 wxStatusBar *statbarOld = GetStatusBar();
423 if ( statbarOld )
424 {
425 statbarOld->Hide();
426 SetStatusBar(0);
427 }
428 else
429 {
430 DoCreateStatusBar(m_statbarKind);
431 }
432 #ifdef __WXMSW__
433 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWindows
434 // authors) while we look for a proper fix..
435 // SendSizeEvent();
436 #endif
437 }
438
439 void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
440 {
441 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
442 : StatBar_Custom);
443 }
444
445 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
446 {
447 // TRUE is to force the frame to close
448 Close(TRUE);
449 }
450
451 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
452 {
453 MyAboutDialog dlg(this);
454 dlg.ShowModal();
455 }
456
457 // ----------------------------------------------------------------------------
458 // MyAboutDialog
459 // ----------------------------------------------------------------------------
460
461 MyAboutDialog::MyAboutDialog(wxWindow *parent)
462 : wxDialog(parent, -1, wxString(_T("About statbar")),
463 wxDefaultPosition, wxDefaultSize,
464 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
465 {
466 wxStaticText *text = new wxStaticText(this, -1,
467 _T("wxStatusBar sample\n")
468 _T("(c) 2000 Vadim Zeitlin"));
469
470 wxButton *btn = new wxButton(this, wxID_OK, _T("&Close"));
471
472 // create the top status bar without the size grip (default style),
473 // otherwise it looks weird
474 wxStatusBar *statbarTop = new wxStatusBar(this, -1, 0);
475 statbarTop->SetFieldsCount(3);
476 statbarTop->SetStatusText(_T("This is a top status bar"), 0);
477 statbarTop->SetStatusText(_T("in a dialog"), 1);
478 statbarTop->SetStatusText(_T("Great, isn't it?"), 2);
479
480 wxStatusBar *statbarBottom = new wxStatusBar(this, -1);
481 statbarBottom->SetFieldsCount(2);
482 statbarBottom->SetStatusText(_T("This is a bottom status bar"), 0);
483 statbarBottom->SetStatusText(_T("in a dialog"), 1);
484
485 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
486 sizerTop->Add(statbarTop, 0, wxGROW);
487 sizerTop->Add(-1, 10, 1, wxGROW);
488 sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
489 sizerTop->Add(-1, 10, 1, wxGROW);
490 sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
491 sizerTop->Add(-1, 10, 1, wxGROW);
492 sizerTop->Add(statbarBottom, 0, wxGROW);
493
494 SetAutoLayout(TRUE);
495 SetSizer(sizerTop);
496
497 sizerTop->Fit(this);
498 sizerTop->SetSizeHints(this);
499 }
500
501 // ----------------------------------------------------------------------------
502 // MyStatusBar
503 // ----------------------------------------------------------------------------
504
505 #ifdef __VISUALC__
506 // 'this' : used in base member initializer list -- so what??
507 #pragma warning(disable: 4355)
508 #endif
509
510 MyStatusBar::MyStatusBar(wxWindow *parent)
511 : wxStatusBar(parent, -1), m_timer(this), m_checkbox(NULL)
512 {
513 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
514
515 SetFieldsCount(Field_Max);
516 SetStatusWidths(Field_Max, widths);
517
518 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock"));
519 m_checkbox->SetValue(TRUE);
520
521 #ifdef USE_STATIC_BITMAP
522 m_statbmp = new wxStaticBitmap(this, -1, wxIcon(green_xpm));
523 #else
524 m_statbmp = new wxBitmapButton(this, -1, CreateBitmapForButton(),
525 wxDefaultPosition, wxDefaultSize,
526 wxBU_EXACTFIT);
527 #endif
528
529 m_timer.Start(1000);
530
531 SetMinHeight(BITMAP_SIZE_Y);
532
533 UpdateClock();
534 }
535
536 #ifdef __VISUALC__
537 #pragma warning(default: 4355)
538 #endif
539
540 MyStatusBar::~MyStatusBar()
541 {
542 if ( m_timer.IsRunning() )
543 {
544 m_timer.Stop();
545 }
546 }
547
548 wxBitmap MyStatusBar::CreateBitmapForButton(bool on)
549 {
550 static const int BMP_BUTTON_SIZE_X = 10;
551 static const int BMP_BUTTON_SIZE_Y = 9;
552
553 wxBitmap bitmap(BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
554 wxMemoryDC dc;
555 dc.SelectObject(bitmap);
556 dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH);
557 dc.SetBackground(*wxLIGHT_GREY_BRUSH);
558 dc.Clear();
559 dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
560 dc.SelectObject(wxNullBitmap);
561
562 return bitmap;
563 }
564
565 void MyStatusBar::OnSize(wxSizeEvent& event)
566 {
567 if ( !m_checkbox )
568 return;
569
570 wxRect rect;
571 GetFieldRect(Field_Checkbox, rect);
572
573 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
574
575 GetFieldRect(Field_Bitmap, rect);
576 wxSize size = m_statbmp->GetSize();
577
578 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
579 rect.y + (rect.height - size.y) / 2);
580
581 event.Skip();
582 }
583
584 void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
585 {
586 m_checkbox->SetValue(!m_checkbox->GetValue());
587
588 DoToggle();
589 }
590
591 void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
592 {
593 DoToggle();
594 }
595
596 void MyStatusBar::DoToggle()
597 {
598 if ( m_checkbox->GetValue() )
599 {
600 m_timer.Start(1000);
601
602 #ifdef USE_STATIC_BITMAP
603 m_statbmp->SetIcon(wxIcon(green_xpm));
604 #else
605 m_statbmp->SetBitmapLabel(CreateBitmapForButton(FALSE));
606 m_statbmp->Refresh();
607 #endif
608
609 UpdateClock();
610 }
611 else // don't show clock
612 {
613 m_timer.Stop();
614
615 #ifdef USE_STATIC_BITMAP
616 m_statbmp->SetIcon(wxIcon(red_xpm));
617 #else
618 m_statbmp->SetBitmapLabel(CreateBitmapForButton(TRUE));
619 m_statbmp->Refresh();
620 #endif
621
622 SetStatusText(_T(""), Field_Clock);
623 }
624 }
625
626 void MyStatusBar::UpdateClock()
627 {
628 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
629 }