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