]> git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
fixed wxXmlResource::Load's detection of filenames to be done as early as possible
[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 #include "wx/fontdlg.h"
52
53 #ifndef __WXMSW__
54 #include "../sample.xpm"
55 #endif
56
57
58 // define this for the platforms which don't support wxBitmapButton (such as
59 // Motif), else a wxBitmapButton will be used
60 #ifdef __WXMOTIF__
61 #define USE_STATIC_BITMAP
62 #endif
63
64 //#define USE_MDI_PARENT_FRAME 1
65
66 #ifdef USE_MDI_PARENT_FRAME
67 #include "wx/mdi.h"
68 #endif // USE_MDI_PARENT_FRAME
69
70
71 // ----------------------------------------------------------------------------
72 // resources
73 // ----------------------------------------------------------------------------
74
75 #ifdef USE_STATIC_BITMAP
76 #include "green.xpm"
77 #include "red.xpm"
78 #endif // USE_STATIC_BITMAP
79
80 // ----------------------------------------------------------------------------
81 // private classes
82 // ----------------------------------------------------------------------------
83
84 // Define a new application type, each program should derive a class from wxApp
85 class MyApp : public wxApp
86 {
87 public:
88 // override base class virtuals
89 // ----------------------------
90
91 // this one is called on application startup and is a good place for the app
92 // initialization (doing it here and not in the ctor allows to have an error
93 // return: if OnInit() returns false, the application terminates)
94 virtual bool OnInit();
95 };
96
97 // A custom status bar which contains controls, icons &c
98 class MyStatusBar : public wxStatusBar
99 {
100 public:
101 MyStatusBar(wxWindow *parent);
102 virtual ~MyStatusBar();
103
104 void UpdateClock();
105
106 // event handlers
107 #if wxUSE_TIMER
108 void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); }
109 #endif
110 void OnSize(wxSizeEvent& event);
111 void OnToggleClock(wxCommandEvent& event);
112 void OnButton(wxCommandEvent& event);
113
114 private:
115 // toggle the state of the status bar controls
116 void DoToggle();
117
118 wxBitmap CreateBitmapForButton(bool on = false);
119
120 enum
121 {
122 Field_Text,
123 Field_Checkbox,
124 Field_Bitmap,
125 Field_Clock,
126 Field_Max
127 };
128
129 #if wxUSE_TIMER
130 wxTimer m_timer;
131 #endif
132
133 #if wxUSE_CHECKBOX
134 wxCheckBox *m_checkbox;
135 #endif
136 #ifdef USE_STATIC_BITMAP
137 wxStaticBitmap *m_statbmp;
138 #else
139 wxBitmapButton *m_statbmp;
140 #endif
141
142 DECLARE_EVENT_TABLE()
143 };
144
145 // Define a new frame type: this is going to be our main frame
146 class MyFrame : public wxFrame
147 {
148 public:
149 // ctor(s)
150 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
151 #ifdef USE_MDI_PARENT_FRAME
152 class MyFrame : public wxMDIParentFrame
153 #else
154 virtual ~MyFrame();
155 #endif
156
157 // event handlers (these functions should _not_ be virtual)
158 void OnQuit(wxCommandEvent& event);
159 void OnAbout(wxCommandEvent& event);
160
161 void OnResetFieldsWidth(wxCommandEvent& event);
162 void OnSetStatusFields(wxCommandEvent& event);
163 void OnSetStatusTexts(wxCommandEvent& event);
164 void OnSetStatusFont(wxCommandEvent& event);
165 void OnRecreateStatusBar(wxCommandEvent& event);
166 void OnSetStyleNormal(wxCommandEvent& event);
167 void OnSetStyleFlat(wxCommandEvent& event);
168 void OnSetStyleRaised(wxCommandEvent& event);
169
170 private:
171 enum StatBarKind
172 {
173 StatBar_Default,
174 StatBar_Custom,
175 StatBar_Max
176 } m_statbarKind;
177
178
179 void OnUpdateForDefaultStatusbar(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
214 StatusBar_SetFields,
215 StatusBar_SetTexts,
216 StatusBar_SetFont,
217 StatusBar_ResetFieldsWidth,
218
219 StatusBar_Recreate,
220 StatusBar_About,
221 StatusBar_Toggle,
222 StatusBar_Checkbox = 1000,
223 StatusBar_SetStyle,
224 StatusBar_SetStyleNormal,
225 StatusBar_SetStyleFlat,
226 StatusBar_SetStyleRaised
227 };
228
229 static const int BITMAP_SIZE_X = 32;
230 static const int BITMAP_SIZE_Y = 15;
231
232 // ----------------------------------------------------------------------------
233 // event tables and other macros for wxWidgets
234 // ----------------------------------------------------------------------------
235
236 // the event tables connect the wxWidgets events with the functions (event
237 // handlers) which process them. It can be also done at run-time, but for the
238 // simple menu events like this the static method is much simpler.
239 #ifdef USE_MDI_PARENT_FRAME
240 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
241 #else
242 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
243 #endif
244 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
245 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
246 EVT_MENU(StatusBar_SetTexts, MyFrame::OnSetStatusTexts)
247 EVT_MENU(StatusBar_SetFont, MyFrame::OnSetStatusFont)
248 EVT_MENU(StatusBar_ResetFieldsWidth, MyFrame::OnResetFieldsWidth)
249 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
250 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
251 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
252 EVT_MENU(StatusBar_SetStyleNormal, MyFrame::OnSetStyleNormal)
253 EVT_MENU(StatusBar_SetStyleFlat, MyFrame::OnSetStyleFlat)
254 EVT_MENU(StatusBar_SetStyleRaised, MyFrame::OnSetStyleRaised)
255
256 EVT_UPDATE_UI_RANGE(StatusBar_SetFields, StatusBar_ResetFieldsWidth,
257 MyFrame::OnUpdateForDefaultStatusbar)
258 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
259 EVT_UPDATE_UI(StatusBar_SetStyleNormal, MyFrame::OnUpdateSetStyleNormal)
260 EVT_UPDATE_UI(StatusBar_SetStyleFlat, MyFrame::OnUpdateSetStyleFlat)
261 EVT_UPDATE_UI(StatusBar_SetStyleRaised, MyFrame::OnUpdateSetStyleRaised)
262 END_EVENT_TABLE()
263
264 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
265 EVT_SIZE(MyStatusBar::OnSize)
266 #if wxUSE_CHECKBOX
267 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
268 #endif
269 EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton)
270 #if wxUSE_TIMER
271 EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer)
272 #endif
273 END_EVENT_TABLE()
274
275 // Create a new application object: this macro will allow wxWidgets to create
276 // the application object during program execution (it's better than using a
277 // static object for many reasons) and also declares the accessor function
278 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
279 // not wxApp)
280 IMPLEMENT_APP(MyApp)
281
282 // ============================================================================
283 // implementation
284 // ============================================================================
285
286 // ----------------------------------------------------------------------------
287 // the application class
288 // ----------------------------------------------------------------------------
289
290 // `Main program' equivalent: the program execution "starts" here
291 bool MyApp::OnInit()
292 {
293 if ( !wxApp::OnInit() )
294 return false;
295
296 // create the main application window
297 MyFrame *frame = new MyFrame(_T("wxStatusBar sample"),
298 wxPoint(50, 50), wxSize(450, 340));
299
300 // and show it (the frames, unlike simple controls, are not shown when
301 // created initially)
302 frame->Show(true);
303
304 // success: wxApp::OnRun() will be called which will enter the main message
305 // loop and the application will run. If we returned 'false' here, the
306 // application would exit immediately.
307 return true;
308 }
309
310 // ----------------------------------------------------------------------------
311 // main frame
312 // ----------------------------------------------------------------------------
313
314 // frame constructor
315 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
316 #ifdef USE_MDI_PARENT_FRAME
317 : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
318 #else
319 : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
320 #endif
321 {
322 SetIcon(wxICON(sample));
323
324 m_statbarDefault = NULL;
325 m_statbarCustom = NULL;
326
327 m_statbarStyle = wxSB_NORMAL;
328
329 #ifdef __WXMAC__
330 // we need this in order to allow the about menu relocation, since ABOUT is
331 // not the default id of the about menu
332 wxApp::s_macAboutMenuItemId = StatusBar_About;
333 #endif
334
335 // create a menu bar
336 wxMenu *menuFile = new wxMenu;
337 menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
338
339 wxMenu *statbarMenu = new wxMenu;
340 statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"),
341 _T("Set the number of status bar fields"));
342 statbarMenu->Append(StatusBar_SetTexts, _T("&Set field text\tCtrl-T"),
343 _T("Set the text to display for each status bar field"));
344 statbarMenu->Append(StatusBar_SetFont, _T("&Set field font\tCtrl-F"),
345 _T("Set the font to use for rendering status bar fields"));
346
347 wxMenu *statbarStyleMenu = new wxMenu;
348 statbarStyleMenu->Append(StatusBar_SetStyleNormal, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
349 statbarStyleMenu->Append(StatusBar_SetStyleFlat, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
350 statbarStyleMenu->Append(StatusBar_SetStyleRaised, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
351
352 statbarMenu->Append(StatusBar_SetStyle, _T("Field style"), statbarStyleMenu);
353
354 statbarMenu->Append(StatusBar_ResetFieldsWidth, _T("Reset field widths"),
355 _T("Sets all fields to the same width"));
356 statbarMenu->AppendSeparator();
357
358 statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"),
359 _T("Toggle the status bar display"), true);
360 statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"),
361 _T("Toggle status bar format"));
362
363 wxMenu *helpMenu = new wxMenu;
364 helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
365
366 // now append the freshly created menu to the menu bar...
367 wxMenuBar *menuBar = new wxMenuBar();
368 menuBar->Append(menuFile, _T("&File"));
369 menuBar->Append(statbarMenu, _T("&Status bar"));
370 menuBar->Append(helpMenu, _T("&Help"));
371
372 // ... and attach this menu bar to the frame
373 SetMenuBar(menuBar);
374
375 // create default status bar to start with
376 CreateStatusBar(2);
377 m_statbarKind = StatBar_Default;
378 SetStatusText(_T("Welcome to wxWidgets!"));
379
380 m_statbarDefault = GetStatusBar();
381 }
382
383 MyFrame::~MyFrame()
384 {
385 SetStatusBar(NULL);
386
387 delete m_statbarDefault;
388 delete m_statbarCustom;
389 }
390
391 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
392 {
393 wxStatusBar *statbarOld = GetStatusBar();
394 if ( statbarOld )
395 {
396 statbarOld->Hide();
397 }
398
399 switch ( kind )
400 {
401 case StatBar_Default:
402 SetStatusBar(m_statbarDefault);
403 break;
404
405 case StatBar_Custom:
406 if ( !m_statbarCustom )
407 {
408 m_statbarCustom = new MyStatusBar(this);
409 }
410 SetStatusBar(m_statbarCustom);
411 break;
412
413 default:
414 wxFAIL_MSG(wxT("unknown stat bar kind"));
415 }
416
417 ApplyStyle();
418 GetStatusBar()->Show();
419 PositionStatusBar();
420
421 m_statbarKind = kind;
422 }
423
424
425 // ----------------------------------------------------------------------------
426 // main frame - event handlers
427 // ----------------------------------------------------------------------------
428
429 void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event)
430 {
431 // only allow this feature for the default status bar
432 wxStatusBar *sb = GetStatusBar();
433 event.Enable(sb == m_statbarDefault);
434 }
435
436 void MyFrame::OnSetStatusTexts(wxCommandEvent& WXUNUSED(event))
437 {
438 wxStatusBar *sb = GetStatusBar();
439
440 wxString txt;
441 for (int i=0; i<sb->GetFieldsCount(); i++)
442 {
443 txt =
444 wxGetTextFromUser(wxString::Format("Enter the text for the %d-th field:", i+1),
445 "Input field text", "A dummy test string", this);
446
447 sb->SetStatusText(txt, i);
448 }
449 }
450
451 void MyFrame::OnSetStatusFont(wxCommandEvent& WXUNUSED(event))
452 {
453 wxStatusBar *sb = GetStatusBar();
454
455 wxFont fnt = wxGetFontFromUser(this, sb->GetFont(), "Choose statusbar font");
456 if (fnt.IsOk())
457 {
458 sb->SetFont(fnt);
459 sb->SetSize(sb->GetBestSize());
460 }
461 }
462
463 void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
464 {
465 wxStatusBar *sb = GetStatusBar();
466
467 long nFields = wxGetNumberFromUser
468 (
469 _T("Select the number of fields in the status bar"),
470 _T("Fields:"),
471 _T("wxWidgets statusbar sample"),
472 sb->GetFieldsCount(),
473 1, 5,
474 this
475 );
476
477 // we don't check if the number changed at all on purpose: calling
478 // SetFieldsCount() with the same number of fields should be ok
479 if ( nFields != -1 )
480 {
481 static const int widthsFor2Fields[] = { 200, -1 };
482 static const int widthsFor3Fields[] = { -1, -2, -1 };
483 static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 };
484
485 static const int *widthsAll[] =
486 {
487 NULL, // 1 field: default
488 widthsFor2Fields, // 2 fields: 1 fixed, 1 var
489 widthsFor3Fields, // 3 fields: 3 var
490 widthsFor4Fields, // 4 fields: 3 fixed, 2 vars
491 NULL // 5 fields: default (all have same width)
492 };
493
494 const int * const widths = widthsAll[nFields - 1];
495 sb->SetFieldsCount(nFields, widths);
496
497 wxString s;
498 for ( long n = 0; n < nFields; n++ )
499 {
500 if ( widths )
501 {
502 if ( widths[n] > 0 )
503 s.Printf(_T("fixed (%d)"), widths[n]);
504 else
505 s.Printf(_T("variable (*%d)"), -widths[n]);
506 }
507 else
508 {
509 s = _T("default");
510 }
511
512 SetStatusText(s, n);
513 }
514 }
515 else
516 {
517 wxLogStatus(this, wxT("Cancelled"));
518 }
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 if (!GetFieldRect(Field_Checkbox, rect))
745 {
746 event.Skip();
747 return;
748 }
749
750 #if wxUSE_CHECKBOX
751 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
752 #endif
753
754 GetFieldRect(Field_Bitmap, rect);
755 wxSize size = m_statbmp->GetSize();
756
757 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
758 rect.y + (rect.height - size.y) / 2);
759
760 event.Skip();
761 }
762
763 void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
764 {
765 #if wxUSE_CHECKBOX
766 m_checkbox->SetValue(!m_checkbox->GetValue());
767 #endif
768
769 DoToggle();
770 }
771
772 void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
773 {
774 DoToggle();
775 }
776
777 void MyStatusBar::DoToggle()
778 {
779 #if wxUSE_CHECKBOX
780 if ( m_checkbox->GetValue() )
781 {
782 #if wxUSE_TIMER
783 m_timer.Start(1000);
784 #endif
785
786 #ifdef USE_STATIC_BITMAP
787 m_statbmp->SetIcon(wxIcon(green_xpm));
788 #else
789 m_statbmp->SetBitmapLabel(CreateBitmapForButton(false));
790 m_statbmp->Refresh();
791 #endif
792
793 UpdateClock();
794 }
795 else // don't show clock
796 {
797 #if wxUSE_TIMER
798 m_timer.Stop();
799 #endif
800
801 #ifdef USE_STATIC_BITMAP
802 m_statbmp->SetIcon(wxIcon(red_xpm));
803 #else
804 m_statbmp->SetBitmapLabel(CreateBitmapForButton(true));
805 m_statbmp->Refresh();
806 #endif
807
808 SetStatusText(wxEmptyString, Field_Clock);
809 }
810 #endif
811 }
812
813 void MyStatusBar::UpdateClock()
814 {
815 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
816 }