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