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