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