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