use a single update UI handler for all menu commands which act only on the default...
[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 #include "wx/numdlg.h"
51
52 #ifndef __WXMSW__
53 #include "../sample.xpm"
54 #endif
55
56
57 // define this for the platforms which don't support wxBitmapButton (such as
58 // Motif), else a wxBitmapButton will be used
59 #ifdef __WXMOTIF__
60 #define USE_STATIC_BITMAP
61 #endif
62
63 //#define USE_MDI_PARENT_FRAME 1
64
65 #ifdef USE_MDI_PARENT_FRAME
66 #include "wx/mdi.h"
67 #endif // USE_MDI_PARENT_FRAME
68
69
70 // ----------------------------------------------------------------------------
71 // resources
72 // ----------------------------------------------------------------------------
73
74 #ifdef USE_STATIC_BITMAP
75 #include "green.xpm"
76 #include "red.xpm"
77 #endif // USE_STATIC_BITMAP
78
79 // ----------------------------------------------------------------------------
80 // private classes
81 // ----------------------------------------------------------------------------
82
83 // Define a new application type, each program should derive a class from wxApp
84 class MyApp : public wxApp
85 {
86 public:
87 // override base class virtuals
88 // ----------------------------
89
90 // this one is called on application startup and is a good place for the app
91 // initialization (doing it here and not in the ctor allows to have an error
92 // return: if OnInit() returns false, the application terminates)
93 virtual bool OnInit();
94 };
95
96 // A custom status bar which contains controls, icons &c
97 class MyStatusBar : public wxStatusBar
98 {
99 public:
100 MyStatusBar(wxWindow *parent);
101 virtual ~MyStatusBar();
102
103 void UpdateClock();
104
105 // event handlers
106 #if wxUSE_TIMER
107 void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); }
108 #endif
109 void OnSize(wxSizeEvent& event);
110 void OnToggleClock(wxCommandEvent& event);
111 void OnButton(wxCommandEvent& event);
112
113 private:
114 // toggle the state of the status bar controls
115 void DoToggle();
116
117 wxBitmap CreateBitmapForButton(bool on = false);
118
119 enum
120 {
121 Field_Text,
122 Field_Checkbox,
123 Field_Bitmap,
124 Field_Clock,
125 Field_Max
126 };
127
128 #if wxUSE_TIMER
129 wxTimer m_timer;
130 #endif
131
132 #if wxUSE_CHECKBOX
133 wxCheckBox *m_checkbox;
134 #endif
135 #ifdef USE_STATIC_BITMAP
136 wxStaticBitmap *m_statbmp;
137 #else
138 wxBitmapButton *m_statbmp;
139 #endif
140
141 DECLARE_EVENT_TABLE()
142 };
143
144 // Define a new frame type: this is going to be our main frame
145 class MyFrame : public wxFrame
146 {
147 public:
148 // ctor(s)
149 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
150 #ifdef USE_MDI_PARENT_FRAME
151 class MyFrame : public wxMDIParentFrame
152 #else
153 virtual ~MyFrame();
154 #endif
155
156 // event handlers (these functions should _not_ be virtual)
157 void OnQuit(wxCommandEvent& event);
158 void OnAbout(wxCommandEvent& event);
159
160 void OnResetFieldsWidth(wxCommandEvent& event);
161 void OnSetStatusFields(wxCommandEvent& event);
162 void OnSetStatusTexts(wxCommandEvent& event);
163 void OnRecreateStatusBar(wxCommandEvent& event);
164 void OnSetStyleNormal(wxCommandEvent& event);
165 void OnSetStyleFlat(wxCommandEvent& event);
166 void OnSetStyleRaised(wxCommandEvent& event);
167
168 private:
169 enum StatBarKind
170 {
171 StatBar_Default,
172 StatBar_Custom,
173 StatBar_Max
174 } m_statbarKind;
175
176
177 void OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event);
178 void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
179 void OnUpdateSetStyleNormal(wxUpdateUIEvent& event);
180 void OnUpdateSetStyleFlat(wxUpdateUIEvent& event);
181 void OnUpdateSetStyleRaised(wxUpdateUIEvent& event);
182 void OnStatusBarToggle(wxCommandEvent& event);
183 void DoCreateStatusBar(StatBarKind kind);
184 void ApplyStyle();
185
186 wxStatusBar *m_statbarDefault;
187 MyStatusBar *m_statbarCustom;
188
189 int m_statbarStyle;
190
191 // any class wishing to process wxWidgets events must use this macro
192 DECLARE_EVENT_TABLE()
193 };
194
195 // Our about dialog ith its status bar
196 class MyAboutDialog : public wxDialog
197 {
198 public:
199 MyAboutDialog(wxWindow *parent);
200 };
201
202 // ----------------------------------------------------------------------------
203 // constants
204 // ----------------------------------------------------------------------------
205
206 // IDs for the controls and the menu commands
207 enum
208 {
209 // menu items
210 StatusBar_Quit = 1,
211
212 StatusBar_SetFields,
213 StatusBar_SetTexts,
214 StatusBar_ResetFieldsWidth,
215
216 StatusBar_Recreate,
217 StatusBar_About,
218 StatusBar_Toggle,
219 StatusBar_Checkbox = 1000,
220 StatusBar_SetStyle,
221 StatusBar_SetStyleNormal,
222 StatusBar_SetStyleFlat,
223 StatusBar_SetStyleRaised
224 };
225
226 static const int BITMAP_SIZE_X = 32;
227 static const int BITMAP_SIZE_Y = 15;
228
229 // ----------------------------------------------------------------------------
230 // event tables and other macros for wxWidgets
231 // ----------------------------------------------------------------------------
232
233 // the event tables connect the wxWidgets events with the functions (event
234 // handlers) which process them. It can be also done at run-time, but for the
235 // simple menu events like this the static method is much simpler.
236 #ifdef USE_MDI_PARENT_FRAME
237 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
238 #else
239 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
240 #endif
241 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
242 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
243 EVT_MENU(StatusBar_SetTexts, MyFrame::OnSetStatusTexts)
244 EVT_MENU(StatusBar_ResetFieldsWidth, MyFrame::OnResetFieldsWidth)
245 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
246 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
247 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
248 EVT_MENU(StatusBar_SetStyleNormal, MyFrame::OnSetStyleNormal)
249 EVT_MENU(StatusBar_SetStyleFlat, MyFrame::OnSetStyleFlat)
250 EVT_MENU(StatusBar_SetStyleRaised, MyFrame::OnSetStyleRaised)
251
252 EVT_UPDATE_UI_RANGE(StatusBar_SetFields, StatusBar_ResetFieldsWidth,
253 MyFrame::OnUpdateForDefaultStatusbar)
254 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
255 EVT_UPDATE_UI(StatusBar_SetStyleNormal, MyFrame::OnUpdateSetStyleNormal)
256 EVT_UPDATE_UI(StatusBar_SetStyleFlat, MyFrame::OnUpdateSetStyleFlat)
257 EVT_UPDATE_UI(StatusBar_SetStyleRaised, MyFrame::OnUpdateSetStyleRaised)
258 END_EVENT_TABLE()
259
260 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
261 EVT_SIZE(MyStatusBar::OnSize)
262 #if wxUSE_CHECKBOX
263 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
264 #endif
265 EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton)
266 #if wxUSE_TIMER
267 EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer)
268 #endif
269 END_EVENT_TABLE()
270
271 // Create a new application object: this macro will allow wxWidgets to create
272 // the application object during program execution (it's better than using a
273 // static object for many reasons) and also declares the accessor function
274 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
275 // not wxApp)
276 IMPLEMENT_APP(MyApp)
277
278 // ============================================================================
279 // implementation
280 // ============================================================================
281
282 // ----------------------------------------------------------------------------
283 // the application class
284 // ----------------------------------------------------------------------------
285
286 // `Main program' equivalent: the program execution "starts" here
287 bool MyApp::OnInit()
288 {
289 if ( !wxApp::OnInit() )
290 return false;
291
292 // create the main application window
293 MyFrame *frame = new MyFrame(_T("wxStatusBar sample"),
294 wxPoint(50, 50), wxSize(450, 340));
295
296 // and show it (the frames, unlike simple controls, are not shown when
297 // created initially)
298 frame->Show(true);
299
300 // success: wxApp::OnRun() will be called which will enter the main message
301 // loop and the application will run. If we returned 'false' here, the
302 // application would exit immediately.
303 return true;
304 }
305
306 // ----------------------------------------------------------------------------
307 // main frame
308 // ----------------------------------------------------------------------------
309
310 // frame constructor
311 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
312 #ifdef USE_MDI_PARENT_FRAME
313 : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
314 #else
315 : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
316 #endif
317 {
318 SetIcon(wxICON(sample));
319
320 m_statbarDefault = NULL;
321 m_statbarCustom = NULL;
322
323 m_statbarStyle = wxSB_NORMAL;
324
325 #ifdef __WXMAC__
326 // we need this in order to allow the about menu relocation, since ABOUT is
327 // not the default id of the about menu
328 wxApp::s_macAboutMenuItemId = StatusBar_About;
329 #endif
330
331 // create a menu bar
332 wxMenu *menuFile = new wxMenu;
333 menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
334
335 wxMenu *statbarMenu = new wxMenu;
336 statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"),
337 _T("Set the number of status bar fields"));
338 statbarMenu->Append(StatusBar_SetTexts, _T("&Set field text\tCtrl-T"),
339 _T("Set the text to display for each status bar field"));
340
341 wxMenu *statbarStyleMenu = new wxMenu;
342 statbarStyleMenu->Append(StatusBar_SetStyleNormal, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
343 statbarStyleMenu->Append(StatusBar_SetStyleFlat, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
344 statbarStyleMenu->Append(StatusBar_SetStyleRaised, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
345
346 statbarMenu->Append(StatusBar_SetStyle, _T("Field style"), statbarStyleMenu);
347
348 statbarMenu->Append(StatusBar_ResetFieldsWidth, _T("Reset field widths"),
349 _T("Sets all fields to the same width"));
350 statbarMenu->AppendSeparator();
351
352 statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"),
353 _T("Toggle the status bar display"), true);
354 statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"),
355 _T("Toggle status bar format"));
356
357 wxMenu *helpMenu = new wxMenu;
358 helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
359
360 // now append the freshly created menu to the menu bar...
361 wxMenuBar *menuBar = new wxMenuBar();
362 menuBar->Append(menuFile, _T("&File"));
363 menuBar->Append(statbarMenu, _T("&Status bar"));
364 menuBar->Append(helpMenu, _T("&Help"));
365
366 // ... and attach this menu bar to the frame
367 SetMenuBar(menuBar);
368
369 // create default status bar to start with
370 CreateStatusBar(2);
371 m_statbarKind = StatBar_Default;
372 SetStatusText(_T("Welcome to wxWidgets!"));
373
374 m_statbarDefault = GetStatusBar();
375 }
376
377 MyFrame::~MyFrame()
378 {
379 SetStatusBar(NULL);
380
381 delete m_statbarDefault;
382 delete m_statbarCustom;
383 }
384
385 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
386 {
387 wxStatusBar *statbarOld = GetStatusBar();
388 if ( statbarOld )
389 {
390 statbarOld->Hide();
391 }
392
393 switch ( kind )
394 {
395 case StatBar_Default:
396 SetStatusBar(m_statbarDefault);
397 break;
398
399 case StatBar_Custom:
400 if ( !m_statbarCustom )
401 {
402 m_statbarCustom = new MyStatusBar(this);
403 }
404 SetStatusBar(m_statbarCustom);
405 break;
406
407 default:
408 wxFAIL_MSG(wxT("unknown stat bar kind"));
409 }
410
411 ApplyStyle();
412 GetStatusBar()->Show();
413 PositionStatusBar();
414
415 m_statbarKind = kind;
416 }
417
418
419 // ----------------------------------------------------------------------------
420 // main frame - event handlers
421 // ----------------------------------------------------------------------------
422
423 void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event)
424 {
425 // only allow this feature for the default status bar
426 wxStatusBar *sb = GetStatusBar();
427 event.Enable(sb == m_statbarDefault);
428 }
429
430 void MyFrame::OnSetStatusTexts(wxCommandEvent& WXUNUSED(event))
431 {
432 wxStatusBar *sb = GetStatusBar();
433
434 wxString txt;
435 for (int i=0; i<sb->GetFieldsCount(); i++)
436 {
437 txt =
438 wxGetTextFromUser(wxString::Format("Enter the text for the %d-th field:", i+1),
439 "Input field text", "A dummy test string", this);
440
441 sb->SetStatusText(txt, i);
442 }
443 }
444
445 void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
446 {
447 wxStatusBar *sb = GetStatusBar();
448
449 long nFields = wxGetNumberFromUser
450 (
451 _T("Select the number of fields in the status bar"),
452 _T("Fields:"),
453 _T("wxWidgets statusbar sample"),
454 sb->GetFieldsCount(),
455 1, 5,
456 this
457 );
458
459 // we don't check if the number changed at all on purpose: calling
460 // SetFieldsCount() with the same number of fields should be ok
461 if ( nFields != -1 )
462 {
463 static const int widthsFor2Fields[] = { 200, -1 };
464 static const int widthsFor3Fields[] = { -1, -2, -1 };
465 static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 };
466
467 static const int *widthsAll[] =
468 {
469 NULL, // 1 field: default
470 widthsFor2Fields, // 2 fields: 1 fixed, 1 var
471 widthsFor3Fields, // 3 fields: 3 var
472 widthsFor4Fields, // 4 fields: 3 fixed, 2 vars
473 NULL // 5 fields: default (all have same width)
474 };
475
476 const int * const widths = widthsAll[nFields - 1];
477 sb->SetFieldsCount(nFields, widths);
478
479 wxString s;
480 for ( long n = 0; n < nFields; n++ )
481 {
482 if ( widths )
483 {
484 if ( widths[n] > 0 )
485 s.Printf(_T("fixed (%d)"), widths[n]);
486 else
487 s.Printf(_T("variable (*%d)"), -widths[n]);
488 }
489 else
490 {
491 s = _T("default");
492 }
493
494 SetStatusText(s, n);
495 }
496 }
497 else
498 {
499 wxLogStatus(this, wxT("Cancelled"));
500 }
501 }
502
503 void MyFrame::OnResetFieldsWidth(wxCommandEvent& WXUNUSED(event))
504 {
505 wxStatusBar *pStat = GetStatusBar();
506 if (pStat)
507 {
508 int n = pStat->GetFieldsCount();
509 pStat->SetStatusWidths(n, NULL);
510 for (int i=0; i<n; i++)
511 pStat->SetStatusText("same size", i);
512 }
513 }
514
515 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event)
516 {
517 event.Check(GetStatusBar() != NULL);
518 }
519
520 void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event))
521 {
522 wxStatusBar *statbarOld = GetStatusBar();
523 if ( statbarOld )
524 {
525 statbarOld->Hide();
526 SetStatusBar(NULL);
527 }
528 else
529 {
530 DoCreateStatusBar(m_statbarKind);
531 }
532 }
533
534 void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
535 {
536 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
537 : StatBar_Custom);
538 }
539
540 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
541 {
542 // true is to force the frame to close
543 Close(true);
544 }
545
546 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
547 {
548 MyAboutDialog dlg(this);
549 dlg.ShowModal();
550 }
551
552 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent &event)
553 {
554 event.Check(m_statbarStyle == wxSB_NORMAL);
555 }
556
557 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent &event)
558 {
559 event.Check(m_statbarStyle == wxSB_FLAT);
560 }
561
562 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent &event)
563 {
564 event.Check(m_statbarStyle == wxSB_RAISED);
565 }
566
567 void MyFrame::OnSetStyleNormal(wxCommandEvent & WXUNUSED(event))
568 {
569 m_statbarStyle = wxSB_NORMAL;
570 ApplyStyle();
571 }
572
573 void MyFrame::OnSetStyleFlat(wxCommandEvent & WXUNUSED(event))
574 {
575 m_statbarStyle = wxSB_FLAT;
576 ApplyStyle();
577 }
578
579 void MyFrame::OnSetStyleRaised(wxCommandEvent & WXUNUSED(event))
580 {
581 m_statbarStyle = wxSB_RAISED;
582 ApplyStyle();
583 }
584
585 void MyFrame::ApplyStyle()
586 {
587 wxStatusBar *sb = GetStatusBar();
588 int fields = sb->GetFieldsCount();
589 int *styles = new int[fields];
590
591 for (int i = 1; i < fields; i++)
592 styles[i] = wxSB_NORMAL;
593
594 styles[0] = m_statbarStyle;
595
596 sb->SetStatusStyles(fields, styles);
597
598 delete [] styles;
599 }
600
601 // ----------------------------------------------------------------------------
602 // MyAboutDialog
603 // ----------------------------------------------------------------------------
604
605 MyAboutDialog::MyAboutDialog(wxWindow *parent)
606 : wxDialog(parent, wxID_ANY, wxString(_T("About statbar")),
607 wxDefaultPosition, wxDefaultSize,
608 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
609 {
610 wxStaticText *text = new wxStaticText(this, wxID_ANY,
611 _T("wxStatusBar sample\n")
612 _T("(c) 2000 Vadim Zeitlin"));
613
614 wxButton *btn = new wxButton(this, wxID_OK, _T("&Close"));
615
616 // create the top status bar without the size grip (default style),
617 // otherwise it looks weird
618 wxStatusBar *statbarTop = new wxStatusBar(this, wxID_ANY, 0);
619 statbarTop->SetFieldsCount(3);
620 statbarTop->SetStatusText(_T("This is a top status bar"), 0);
621 statbarTop->SetStatusText(_T("in a dialog"), 1);
622 statbarTop->SetStatusText(_T("Great, isn't it?"), 2);
623
624 wxStatusBar *statbarBottom = new wxStatusBar(this, wxID_ANY);
625 statbarBottom->SetFieldsCount(2);
626 statbarBottom->SetStatusText(_T("This is a bottom status bar"), 0);
627 statbarBottom->SetStatusText(_T("in a dialog"), 1);
628
629 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
630 sizerTop->Add(statbarTop, 0, wxGROW);
631 sizerTop->Add(-1, 10, 1, wxGROW);
632 sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
633 sizerTop->Add(-1, 10, 1, wxGROW);
634 sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
635 sizerTop->Add(-1, 10, 1, wxGROW);
636 sizerTop->Add(statbarBottom, 0, wxGROW);
637
638 SetSizerAndFit(sizerTop);
639 }
640
641 // ----------------------------------------------------------------------------
642 // MyStatusBar
643 // ----------------------------------------------------------------------------
644
645 #ifdef __VISUALC__
646 // 'this' : used in base member initializer list -- so what??
647 #pragma warning(disable: 4355)
648 #endif
649
650 MyStatusBar::MyStatusBar(wxWindow *parent)
651 : wxStatusBar(parent, wxID_ANY)
652 #if wxUSE_TIMER
653 , m_timer(this)
654 #endif
655 #if wxUSE_CHECKBOX
656 , m_checkbox(NULL)
657 #endif
658 {
659 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
660
661 SetFieldsCount(Field_Max);
662 SetStatusWidths(Field_Max, widths);
663
664 #if wxUSE_CHECKBOX
665 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock"));
666 m_checkbox->SetValue(true);
667 #endif
668
669 #ifdef USE_STATIC_BITMAP
670 m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm));
671 #else
672 m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(),
673 wxDefaultPosition, wxDefaultSize,
674 wxBU_EXACTFIT);
675 #endif
676
677 #if wxUSE_TIMER
678 m_timer.Start(1000);
679 #endif
680
681 SetMinHeight(wxMax(m_statbmp->GetBestSize().GetHeight(),
682 m_checkbox->GetBestSize().GetHeight()));
683
684 UpdateClock();
685 }
686
687 #ifdef __VISUALC__
688 #pragma warning(default: 4355)
689 #endif
690
691 MyStatusBar::~MyStatusBar()
692 {
693 #if wxUSE_TIMER
694 if ( m_timer.IsRunning() )
695 {
696 m_timer.Stop();
697 }
698 #endif
699 }
700
701 #define BMP_BUTTON_SIZE_X 10
702 #define BMP_BUTTON_SIZE_Y 10
703
704 wxBitmap MyStatusBar::CreateBitmapForButton(bool on)
705 {
706 wxBitmap bitmap(BMP_BUTTON_SIZE_X+1, BMP_BUTTON_SIZE_Y+1);
707 wxMemoryDC dc;
708 dc.SelectObject(bitmap);
709 dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH);
710 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
711 dc.Clear();
712 dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
713 dc.SelectObject(wxNullBitmap);
714
715 return bitmap;
716 }
717
718 void MyStatusBar::OnSize(wxSizeEvent& event)
719 {
720 #if wxUSE_CHECKBOX
721 if ( !m_checkbox )
722 return;
723 #endif
724
725 wxRect rect;
726 if (!GetFieldRect(Field_Checkbox, rect))
727 {
728 event.Skip();
729 return;
730 }
731
732 #if wxUSE_CHECKBOX
733 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
734 #endif
735
736 GetFieldRect(Field_Bitmap, rect);
737 wxSize size = m_statbmp->GetSize();
738
739 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
740 rect.y + (rect.height - size.y) / 2);
741
742 event.Skip();
743 }
744
745 void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
746 {
747 #if wxUSE_CHECKBOX
748 m_checkbox->SetValue(!m_checkbox->GetValue());
749 #endif
750
751 DoToggle();
752 }
753
754 void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
755 {
756 DoToggle();
757 }
758
759 void MyStatusBar::DoToggle()
760 {
761 #if wxUSE_CHECKBOX
762 if ( m_checkbox->GetValue() )
763 {
764 #if wxUSE_TIMER
765 m_timer.Start(1000);
766 #endif
767
768 #ifdef USE_STATIC_BITMAP
769 m_statbmp->SetIcon(wxIcon(green_xpm));
770 #else
771 m_statbmp->SetBitmapLabel(CreateBitmapForButton(false));
772 m_statbmp->Refresh();
773 #endif
774
775 UpdateClock();
776 }
777 else // don't show clock
778 {
779 #if wxUSE_TIMER
780 m_timer.Stop();
781 #endif
782
783 #ifdef USE_STATIC_BITMAP
784 m_statbmp->SetIcon(wxIcon(red_xpm));
785 #else
786 m_statbmp->SetBitmapLabel(CreateBitmapForButton(true));
787 m_statbmp->Refresh();
788 #endif
789
790 SetStatusText(wxEmptyString, Field_Clock);
791 }
792 #endif
793 }
794
795 void MyStatusBar::UpdateClock()
796 {
797 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
798 }