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