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