]>
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 | ||
ee1a622d | 108 | wxBitmap CreateBitmapForButton(bool on = false); |
f6bcfd97 | 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 | 148 | void OnRecreateStatusBar(wxCommandEvent& event); |
c2919ab3 VZ |
149 | void OnSetStyleNormal(wxCommandEvent& event); |
150 | void OnSetStyleFlat(wxCommandEvent& event); | |
151 | void OnSetStyleRaised(wxCommandEvent& event); | |
2286341c VZ |
152 | |
153 | private: | |
154 | enum StatBarKind | |
155 | { | |
156 | StatBar_Default, | |
157 | StatBar_Custom, | |
158 | StatBar_Max | |
159 | } m_statbarKind; | |
ffd0623c JS |
160 | void OnUpdateSetStatusFields(wxUpdateUIEvent& event); |
161 | void OnUpdateStatusBarToggle(wxUpdateUIEvent& event); | |
c2919ab3 VZ |
162 | void OnUpdateSetStyleNormal(wxUpdateUIEvent& event); |
163 | void OnUpdateSetStyleFlat(wxUpdateUIEvent& event); | |
164 | void OnUpdateSetStyleRaised(wxUpdateUIEvent& event); | |
ffd0623c | 165 | void OnStatusBarToggle(wxCommandEvent& event); |
2286341c | 166 | void DoCreateStatusBar(StatBarKind kind); |
c2919ab3 | 167 | void ApplyStyle(); |
2286341c VZ |
168 | |
169 | wxStatusBar *m_statbarDefault; | |
170 | MyStatusBar *m_statbarCustom; | |
171 | ||
c2919ab3 VZ |
172 | int m_statbarStyle; |
173 | ||
be5a51fb | 174 | // any class wishing to process wxWidgets events must use this macro |
2286341c VZ |
175 | DECLARE_EVENT_TABLE() |
176 | }; | |
177 | ||
cbc66a27 VZ |
178 | // Our about dialog ith its status bar |
179 | class MyAboutDialog : public wxDialog | |
180 | { | |
181 | public: | |
182 | MyAboutDialog(wxWindow *parent); | |
183 | }; | |
184 | ||
2286341c VZ |
185 | // ---------------------------------------------------------------------------- |
186 | // constants | |
187 | // ---------------------------------------------------------------------------- | |
188 | ||
189 | // IDs for the controls and the menu commands | |
190 | enum | |
191 | { | |
192 | // menu items | |
193 | StatusBar_Quit = 1, | |
633d67bb | 194 | StatusBar_SetFields, |
2286341c VZ |
195 | StatusBar_Recreate, |
196 | StatusBar_About, | |
ffd0623c | 197 | StatusBar_Toggle, |
c2919ab3 VZ |
198 | StatusBar_Checkbox = 1000, |
199 | StatusBar_SetStyle, | |
200 | StatusBar_SetStyleNormal, | |
201 | StatusBar_SetStyleFlat, | |
202 | StatusBar_SetStyleRaised | |
2286341c VZ |
203 | }; |
204 | ||
205 | static const int BITMAP_SIZE_X = 32; | |
206 | static const int BITMAP_SIZE_Y = 15; | |
207 | ||
208 | // ---------------------------------------------------------------------------- | |
be5a51fb | 209 | // event tables and other macros for wxWidgets |
2286341c VZ |
210 | // ---------------------------------------------------------------------------- |
211 | ||
be5a51fb | 212 | // the event tables connect the wxWidgets events with the functions (event |
2286341c VZ |
213 | // handlers) which process them. It can be also done at run-time, but for the |
214 | // simple menu events like this the static method is much simpler. | |
ffd0623c JS |
215 | #ifdef USE_MDI_PARENT_FRAME |
216 | BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame) | |
217 | #else | |
2286341c | 218 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
ffd0623c | 219 | #endif |
2286341c | 220 | EVT_MENU(StatusBar_Quit, MyFrame::OnQuit) |
633d67bb | 221 | EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields) |
2286341c VZ |
222 | EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar) |
223 | EVT_MENU(StatusBar_About, MyFrame::OnAbout) | |
ffd0623c | 224 | EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle) |
c2919ab3 VZ |
225 | EVT_MENU(StatusBar_SetStyleNormal, MyFrame::OnSetStyleNormal) |
226 | EVT_MENU(StatusBar_SetStyleFlat, MyFrame::OnSetStyleFlat) | |
227 | EVT_MENU(StatusBar_SetStyleRaised, MyFrame::OnSetStyleRaised) | |
ffd0623c JS |
228 | EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle) |
229 | EVT_UPDATE_UI(StatusBar_SetFields, MyFrame::OnUpdateSetStatusFields) | |
c2919ab3 VZ |
230 | EVT_UPDATE_UI(StatusBar_SetStyleNormal, MyFrame::OnUpdateSetStyleNormal) |
231 | EVT_UPDATE_UI(StatusBar_SetStyleFlat, MyFrame::OnUpdateSetStyleFlat) | |
232 | EVT_UPDATE_UI(StatusBar_SetStyleRaised, MyFrame::OnUpdateSetStyleRaised) | |
2286341c VZ |
233 | END_EVENT_TABLE() |
234 | ||
235 | BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar) | |
236 | EVT_SIZE(MyStatusBar::OnSize) | |
237 | EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock) | |
ee1a622d WS |
238 | EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton) |
239 | EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer) | |
2286341c VZ |
240 | END_EVENT_TABLE() |
241 | ||
be5a51fb | 242 | // Create a new application object: this macro will allow wxWidgets to create |
2286341c VZ |
243 | // the application object during program execution (it's better than using a |
244 | // static object for many reasons) and also declares the accessor function | |
245 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
246 | // not wxApp) | |
247 | IMPLEMENT_APP(MyApp) | |
248 | ||
249 | // ============================================================================ | |
250 | // implementation | |
251 | // ============================================================================ | |
252 | ||
253 | // ---------------------------------------------------------------------------- | |
254 | // the application class | |
255 | // ---------------------------------------------------------------------------- | |
256 | ||
257 | // `Main program' equivalent: the program execution "starts" here | |
258 | bool MyApp::OnInit() | |
259 | { | |
260 | // create the main application window | |
ab1ca7b3 | 261 | MyFrame *frame = new MyFrame(_T("wxStatusBar sample"), |
2286341c VZ |
262 | wxPoint(50, 50), wxSize(450, 340)); |
263 | ||
264 | // and show it (the frames, unlike simple controls, are not shown when | |
265 | // created initially) | |
ee1a622d | 266 | frame->Show(true); |
2286341c VZ |
267 | |
268 | // success: wxApp::OnRun() will be called which will enter the main message | |
ee1a622d | 269 | // loop and the application will run. If we returned 'false' here, the |
2286341c | 270 | // application would exit immediately. |
ee1a622d | 271 | return true; |
2286341c VZ |
272 | } |
273 | ||
274 | // ---------------------------------------------------------------------------- | |
275 | // main frame | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
278 | // frame constructor | |
279 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
ffd0623c | 280 | #ifdef USE_MDI_PARENT_FRAME |
ee1a622d | 281 | : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size) |
ffd0623c | 282 | #else |
ee1a622d | 283 | : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size) |
ffd0623c | 284 | #endif |
2286341c VZ |
285 | { |
286 | m_statbarDefault = NULL; | |
287 | m_statbarCustom = NULL; | |
288 | ||
c2919ab3 VZ |
289 | m_statbarStyle = wxSB_NORMAL; |
290 | ||
2286341c VZ |
291 | #ifdef __WXMAC__ |
292 | // we need this in order to allow the about menu relocation, since ABOUT is | |
293 | // not the default id of the about menu | |
294 | wxApp::s_macAboutMenuItemId = StatusBar_About; | |
295 | #endif | |
296 | ||
297 | // create a menu bar | |
298 | wxMenu *menuFile = new wxMenu; | |
ab1ca7b3 | 299 | menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
2286341c VZ |
300 | |
301 | wxMenu *statbarMenu = new wxMenu; | |
ab1ca7b3 MB |
302 | statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"), |
303 | _T("Set the number of status bar fields")); | |
304 | statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"), | |
305 | _T("Toggle the status bar display"), true); | |
306 | statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"), | |
307 | _T("Toggle status bar format")); | |
2286341c | 308 | |
c2919ab3 VZ |
309 | wxMenu *statbarStyleMenu = new wxMenu; |
310 | statbarStyleMenu->Append(StatusBar_SetStyleNormal, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true); | |
311 | statbarStyleMenu->Append(StatusBar_SetStyleFlat, _T("&Flat"), _T("Sets the style of the first field to flat look"), true); | |
312 | statbarStyleMenu->Append(StatusBar_SetStyleRaised, _T("&Raised"), _T("Sets the style of the first field to raised look"), true); | |
313 | statbarMenu->Append(StatusBar_SetStyle, _T("Field style"), statbarStyleMenu); | |
314 | ||
2286341c | 315 | wxMenu *helpMenu = new wxMenu; |
ab1ca7b3 | 316 | helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
2286341c VZ |
317 | |
318 | // now append the freshly created menu to the menu bar... | |
319 | wxMenuBar *menuBar = new wxMenuBar(); | |
ab1ca7b3 MB |
320 | menuBar->Append(menuFile, _T("&File")); |
321 | menuBar->Append(statbarMenu, _T("&Status bar")); | |
322 | menuBar->Append(helpMenu, _T("&Help")); | |
2286341c VZ |
323 | |
324 | // ... and attach this menu bar to the frame | |
325 | SetMenuBar(menuBar); | |
326 | ||
327 | // create default status bar to start with | |
328 | CreateStatusBar(2); | |
ffd0623c | 329 | m_statbarKind = StatBar_Default; |
be5a51fb | 330 | SetStatusText(_T("Welcome to wxWidgets!")); |
2286341c VZ |
331 | |
332 | m_statbarDefault = GetStatusBar(); | |
333 | } | |
334 | ||
335 | MyFrame::~MyFrame() | |
336 | { | |
337 | SetStatusBar(NULL); | |
338 | ||
339 | delete m_statbarDefault; | |
340 | delete m_statbarCustom; | |
341 | } | |
342 | ||
343 | void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind) | |
344 | { | |
345 | wxStatusBar *statbarOld = GetStatusBar(); | |
346 | if ( statbarOld ) | |
347 | { | |
348 | statbarOld->Hide(); | |
349 | } | |
350 | ||
351 | switch ( kind ) | |
352 | { | |
353 | case StatBar_Default: | |
354 | SetStatusBar(m_statbarDefault); | |
355 | break; | |
356 | ||
357 | case StatBar_Custom: | |
358 | if ( !m_statbarCustom ) | |
359 | { | |
360 | m_statbarCustom = new MyStatusBar(this); | |
361 | } | |
362 | SetStatusBar(m_statbarCustom); | |
363 | break; | |
364 | ||
365 | default: | |
a60b1f5d | 366 | wxFAIL_MSG(wxT("unknown stat bar kind")); |
2286341c VZ |
367 | } |
368 | ||
c2919ab3 | 369 | ApplyStyle(); |
2286341c | 370 | GetStatusBar()->Show(); |
f6bcfd97 | 371 | PositionStatusBar(); |
2286341c VZ |
372 | |
373 | m_statbarKind = kind; | |
374 | } | |
375 | ||
ffd0623c JS |
376 | void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent& event) |
377 | { | |
c2919ab3 | 378 | // only allow the settings of the number of status fields for the default |
ffd0623c JS |
379 | // status bar |
380 | wxStatusBar *sb = GetStatusBar(); | |
381 | event.Enable(sb == m_statbarDefault); | |
382 | } | |
383 | ||
2286341c | 384 | // event handlers |
633d67bb VZ |
385 | void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event)) |
386 | { | |
387 | wxStatusBar *sb = GetStatusBar(); | |
388 | ||
389 | long nFields = wxGetNumberFromUser | |
390 | ( | |
bf69498a VZ |
391 | _T("Select the number of fields in the status bar"), |
392 | _T("Fields:"), | |
be5a51fb | 393 | _T("wxWidgets statusbar sample"), |
633d67bb VZ |
394 | sb->GetFieldsCount(), |
395 | 1, 5, | |
396 | this | |
397 | ); | |
398 | ||
399 | // we don't check if the number changed at all on purpose: calling | |
400 | // SetFieldsCount() with the same number of fields should be ok | |
401 | if ( nFields != -1 ) | |
402 | { | |
71e03035 VZ |
403 | static const int widthsFor2Fields[] = { 200, -1 }; |
404 | static const int widthsFor3Fields[] = { -1, -2, -1 }; | |
405 | static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 }; | |
406 | ||
bf69498a | 407 | static const int *widthsAll[] = |
633d67bb | 408 | { |
71e03035 VZ |
409 | NULL, // 1 field: default |
410 | widthsFor2Fields, // 2 fields: 1 fixed, 1 var | |
411 | widthsFor3Fields, // 3 fields: 3 var | |
412 | widthsFor4Fields, // 4 fields: 3 fixed, 2 vars | |
413 | NULL // 5 fields: default (all have same width) | |
414 | }; | |
633d67bb | 415 | |
bf69498a VZ |
416 | const int * const widths = widthsAll[nFields - 1]; |
417 | sb->SetFieldsCount(nFields, widths); | |
633d67bb | 418 | |
bf69498a VZ |
419 | wxString s; |
420 | for ( long n = 0; n < nFields; n++ ) | |
421 | { | |
422 | if ( widths ) | |
423 | { | |
424 | if ( widths[n] > 0 ) | |
425 | s.Printf(_T("fixed (%d)"), widths[n]); | |
426 | else | |
427 | s.Printf(_T("variable (*%d)"), -widths[n]); | |
428 | } | |
429 | else | |
430 | { | |
431 | s = _T("default"); | |
432 | } | |
433 | ||
434 | SetStatusText(s, n); | |
435 | } | |
633d67bb VZ |
436 | } |
437 | else | |
438 | { | |
4693b20c | 439 | wxLogStatus(this, wxT("Cancelled")); |
633d67bb VZ |
440 | } |
441 | } | |
442 | ||
ffd0623c JS |
443 | void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event) |
444 | { | |
2f6c54eb | 445 | event.Check(GetStatusBar() != 0); |
ffd0623c JS |
446 | } |
447 | ||
448 | void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event)) | |
449 | { | |
450 | wxStatusBar *statbarOld = GetStatusBar(); | |
451 | if ( statbarOld ) | |
452 | { | |
453 | statbarOld->Hide(); | |
454 | SetStatusBar(0); | |
455 | } | |
456 | else | |
457 | { | |
458 | DoCreateStatusBar(m_statbarKind); | |
459 | } | |
460 | #ifdef __WXMSW__ | |
be5a51fb | 461 | // The following is a kludge suggested by Vadim Zeitlin (one of the wxWidgets |
ffd0623c JS |
462 | // authors) while we look for a proper fix.. |
463 | // SendSizeEvent(); | |
464 | #endif | |
465 | } | |
466 | ||
2286341c VZ |
467 | void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event)) |
468 | { | |
469 | DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default | |
470 | : StatBar_Custom); | |
471 | } | |
472 | ||
473 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
474 | { | |
ee1a622d WS |
475 | // true is to force the frame to close |
476 | Close(true); | |
2286341c VZ |
477 | } |
478 | ||
479 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
480 | { | |
cbc66a27 VZ |
481 | MyAboutDialog dlg(this); |
482 | dlg.ShowModal(); | |
483 | } | |
484 | ||
c2919ab3 VZ |
485 | void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent &event) |
486 | { | |
487 | event.Check(m_statbarStyle == wxSB_NORMAL); | |
488 | } | |
489 | ||
490 | void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent &event) | |
491 | { | |
492 | event.Check(m_statbarStyle == wxSB_FLAT); | |
493 | } | |
494 | ||
495 | void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent &event) | |
496 | { | |
497 | event.Check(m_statbarStyle == wxSB_RAISED); | |
498 | } | |
499 | ||
bdb1f15c | 500 | void MyFrame::OnSetStyleNormal(wxCommandEvent & WXUNUSED(event)) |
c2919ab3 VZ |
501 | { |
502 | m_statbarStyle = wxSB_NORMAL; | |
503 | ApplyStyle(); | |
504 | } | |
505 | ||
bdb1f15c | 506 | void MyFrame::OnSetStyleFlat(wxCommandEvent & WXUNUSED(event)) |
c2919ab3 VZ |
507 | { |
508 | m_statbarStyle = wxSB_FLAT; | |
509 | ApplyStyle(); | |
510 | } | |
511 | ||
bdb1f15c | 512 | void MyFrame::OnSetStyleRaised(wxCommandEvent & WXUNUSED(event)) |
c2919ab3 VZ |
513 | { |
514 | m_statbarStyle = wxSB_RAISED; | |
515 | ApplyStyle(); | |
516 | } | |
517 | ||
518 | void MyFrame::ApplyStyle() | |
519 | { | |
520 | wxStatusBar *sb = GetStatusBar(); | |
521 | int fields = sb->GetFieldsCount(); | |
522 | int *styles = new int[fields]; | |
523 | ||
524 | for (int i = 1; i < fields; i++) | |
525 | styles[i] = wxSB_NORMAL; | |
526 | ||
527 | styles[0] = m_statbarStyle; | |
528 | ||
529 | sb->SetStatusStyles(fields, styles); | |
530 | ||
531 | delete [] styles; | |
532 | } | |
533 | ||
cbc66a27 VZ |
534 | // ---------------------------------------------------------------------------- |
535 | // MyAboutDialog | |
536 | // ---------------------------------------------------------------------------- | |
537 | ||
538 | MyAboutDialog::MyAboutDialog(wxWindow *parent) | |
ee1a622d | 539 | : wxDialog(parent, wxID_ANY, wxString(_T("About statbar")), |
cbc66a27 VZ |
540 | wxDefaultPosition, wxDefaultSize, |
541 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) | |
542 | { | |
ee1a622d | 543 | wxStaticText *text = new wxStaticText(this, wxID_ANY, |
ab1ca7b3 MB |
544 | _T("wxStatusBar sample\n") |
545 | _T("(c) 2000 Vadim Zeitlin")); | |
cbc66a27 | 546 | |
ab1ca7b3 | 547 | wxButton *btn = new wxButton(this, wxID_OK, _T("&Close")); |
a1a43961 | 548 | |
8b8bff20 VZ |
549 | // create the top status bar without the size grip (default style), |
550 | // otherwise it looks weird | |
ee1a622d | 551 | wxStatusBar *statbarTop = new wxStatusBar(this, wxID_ANY, 0); |
8b8bff20 | 552 | statbarTop->SetFieldsCount(3); |
ab1ca7b3 MB |
553 | statbarTop->SetStatusText(_T("This is a top status bar"), 0); |
554 | statbarTop->SetStatusText(_T("in a dialog"), 1); | |
555 | statbarTop->SetStatusText(_T("Great, isn't it?"), 2); | |
8b8bff20 | 556 | |
ee1a622d | 557 | wxStatusBar *statbarBottom = new wxStatusBar(this, wxID_ANY); |
8b8bff20 | 558 | statbarBottom->SetFieldsCount(2); |
ab1ca7b3 MB |
559 | statbarBottom->SetStatusText(_T("This is a bottom status bar"), 0); |
560 | statbarBottom->SetStatusText(_T("in a dialog"), 1); | |
cbc66a27 VZ |
561 | |
562 | wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
8b8bff20 | 563 | sizerTop->Add(statbarTop, 0, wxGROW); |
cbc66a27 | 564 | sizerTop->Add(-1, 10, 1, wxGROW); |
a1a43961 VZ |
565 | sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20); |
566 | sizerTop->Add(-1, 10, 1, wxGROW); | |
567 | sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20); | |
cbc66a27 | 568 | sizerTop->Add(-1, 10, 1, wxGROW); |
8b8bff20 | 569 | sizerTop->Add(statbarBottom, 0, wxGROW); |
cbc66a27 | 570 | |
cbc66a27 VZ |
571 | SetSizer(sizerTop); |
572 | ||
573 | sizerTop->Fit(this); | |
574 | sizerTop->SetSizeHints(this); | |
2286341c VZ |
575 | } |
576 | ||
577 | // ---------------------------------------------------------------------------- | |
578 | // MyStatusBar | |
579 | // ---------------------------------------------------------------------------- | |
580 | ||
85401ffe VZ |
581 | #ifdef __VISUALC__ |
582 | // 'this' : used in base member initializer list -- so what?? | |
583 | #pragma warning(disable: 4355) | |
584 | #endif | |
585 | ||
2286341c | 586 | MyStatusBar::MyStatusBar(wxWindow *parent) |
ee1a622d | 587 | : wxStatusBar(parent, wxID_ANY), m_timer(this), m_checkbox(NULL) |
2286341c VZ |
588 | { |
589 | static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 }; | |
590 | ||
591 | SetFieldsCount(Field_Max); | |
592 | SetStatusWidths(Field_Max, widths); | |
593 | ||
594 | m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock")); | |
ee1a622d | 595 | m_checkbox->SetValue(true); |
2286341c | 596 | |
f6bcfd97 | 597 | #ifdef USE_STATIC_BITMAP |
ee1a622d | 598 | m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm)); |
f6bcfd97 | 599 | #else |
ee1a622d | 600 | m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(), |
71e03035 VZ |
601 | wxDefaultPosition, wxDefaultSize, |
602 | wxBU_EXACTFIT); | |
f6bcfd97 | 603 | #endif |
2286341c VZ |
604 | |
605 | m_timer.Start(1000); | |
606 | ||
85401ffe VZ |
607 | SetMinHeight(BITMAP_SIZE_Y); |
608 | ||
2286341c VZ |
609 | UpdateClock(); |
610 | } | |
611 | ||
85401ffe VZ |
612 | #ifdef __VISUALC__ |
613 | #pragma warning(default: 4355) | |
614 | #endif | |
615 | ||
2286341c VZ |
616 | MyStatusBar::~MyStatusBar() |
617 | { | |
618 | if ( m_timer.IsRunning() ) | |
619 | { | |
620 | m_timer.Stop(); | |
621 | } | |
622 | } | |
623 | ||
f6bcfd97 BP |
624 | wxBitmap MyStatusBar::CreateBitmapForButton(bool on) |
625 | { | |
626 | static const int BMP_BUTTON_SIZE_X = 10; | |
627 | static const int BMP_BUTTON_SIZE_Y = 9; | |
628 | ||
629 | wxBitmap bitmap(BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y); | |
630 | wxMemoryDC dc; | |
631 | dc.SelectObject(bitmap); | |
632 | dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH); | |
633 | dc.SetBackground(*wxLIGHT_GREY_BRUSH); | |
634 | dc.Clear(); | |
635 | dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y); | |
636 | dc.SelectObject(wxNullBitmap); | |
637 | ||
638 | return bitmap; | |
639 | } | |
640 | ||
2286341c VZ |
641 | void MyStatusBar::OnSize(wxSizeEvent& event) |
642 | { | |
f6bcfd97 BP |
643 | if ( !m_checkbox ) |
644 | return; | |
645 | ||
2286341c VZ |
646 | wxRect rect; |
647 | GetFieldRect(Field_Checkbox, rect); | |
648 | ||
649 | m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4); | |
650 | ||
651 | GetFieldRect(Field_Bitmap, rect); | |
f6bcfd97 | 652 | wxSize size = m_statbmp->GetSize(); |
f6bcfd97 BP |
653 | |
654 | m_statbmp->Move(rect.x + (rect.width - size.x) / 2, | |
655 | rect.y + (rect.height - size.y) / 2); | |
2286341c VZ |
656 | |
657 | event.Skip(); | |
658 | } | |
659 | ||
f6bcfd97 BP |
660 | void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event)) |
661 | { | |
662 | m_checkbox->SetValue(!m_checkbox->GetValue()); | |
663 | ||
664 | DoToggle(); | |
665 | } | |
666 | ||
667 | void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event)) | |
668 | { | |
669 | DoToggle(); | |
670 | } | |
671 | ||
672 | void MyStatusBar::DoToggle() | |
2286341c VZ |
673 | { |
674 | if ( m_checkbox->GetValue() ) | |
675 | { | |
676 | m_timer.Start(1000); | |
677 | ||
f6bcfd97 | 678 | #ifdef USE_STATIC_BITMAP |
85401ffe | 679 | m_statbmp->SetIcon(wxIcon(green_xpm)); |
f6bcfd97 | 680 | #else |
ee1a622d | 681 | m_statbmp->SetBitmapLabel(CreateBitmapForButton(false)); |
f6bcfd97 BP |
682 | m_statbmp->Refresh(); |
683 | #endif | |
2286341c VZ |
684 | |
685 | UpdateClock(); | |
686 | } | |
687 | else // don't show clock | |
688 | { | |
689 | m_timer.Stop(); | |
690 | ||
f6bcfd97 | 691 | #ifdef USE_STATIC_BITMAP |
85401ffe | 692 | m_statbmp->SetIcon(wxIcon(red_xpm)); |
f6bcfd97 | 693 | #else |
ee1a622d | 694 | m_statbmp->SetBitmapLabel(CreateBitmapForButton(true)); |
f6bcfd97 BP |
695 | m_statbmp->Refresh(); |
696 | #endif | |
2286341c | 697 | |
dabbc6a5 | 698 | SetStatusText(wxEmptyString, Field_Clock); |
2286341c VZ |
699 | } |
700 | } | |
701 | ||
702 | void MyStatusBar::UpdateClock() | |
703 | { | |
704 | SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock); | |
705 | } |