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