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