]> git.saurik.com Git - wxWidgets.git/blame - samples/statbar/statbar.cpp
symbols.txt is no longer.
[wxWidgets.git] / samples / statbar / statbar.cpp
CommitLineData
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"
34 #include "wx/frame.h"
35 #include "wx/statusbr.h"
2286341c
VZ
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"
633d67bb 41 #include "wx/textdlg.h"
cbc66a27 42 #include "wx/sizer.h"
f6c881b4 43 #include "wx/stattext.h"
f6bcfd97
BP
44 #include "wx/bmpbuttn.h"
45 #include "wx/dcmemory.h"
2286341c
VZ
46#endif
47
85401ffe
VZ
48#include "wx/datetime.h"
49
f6bcfd97
BP
50// define this for the platforms which don't support wxBitmapButton (such as
51// Motif), else a wxBitmapButton will be used
52#ifdef __WXMOTIF__
ffd0623c
JS
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
f6bcfd97
BP
58 #define USE_STATIC_BITMAP
59#endif
60
2286341c
VZ
61// ----------------------------------------------------------------------------
62// resources
63// ----------------------------------------------------------------------------
64
f6bcfd97
BP
65#ifdef USE_STATIC_BITMAP
66 #include "green.xpm"
67 #include "red.xpm"
68#endif // USE_STATIC_BITMAP
2286341c
VZ
69
70// ----------------------------------------------------------------------------
71// private classes
72// ----------------------------------------------------------------------------
73
74// Define a new application type, each program should derive a class from wxApp
75class MyApp : public wxApp
76{
77public:
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
88class MyStatusBar : public wxStatusBar
89{
90public:
91 MyStatusBar(wxWindow *parent);
92 virtual ~MyStatusBar();
93
94 void UpdateClock();
95
96 // event handlers
633d67bb 97 void OnTimer(wxTimerEvent& event) { UpdateClock(); }
2286341c
VZ
98 void OnSize(wxSizeEvent& event);
99 void OnToggleClock(wxCommandEvent& event);
f6bcfd97 100 void OnButton(wxCommandEvent& event);
2286341c
VZ
101
102private:
f6bcfd97
BP
103 // toggle the state of the status bar controls
104 void DoToggle();
105
106 wxBitmap CreateBitmapForButton(bool on = FALSE);
107
2286341c
VZ
108 enum
109 {
110 Field_Text,
111 Field_Checkbox,
112 Field_Bitmap,
113 Field_Clock,
114 Field_Max
115 };
116
633d67bb 117 wxTimer m_timer;
2286341c
VZ
118
119 wxCheckBox *m_checkbox;
f6bcfd97 120#ifdef USE_STATIC_BITMAP
2286341c 121 wxStaticBitmap *m_statbmp;
f6bcfd97
BP
122#else
123 wxBitmapButton *m_statbmp;
124#endif
2286341c
VZ
125
126 DECLARE_EVENT_TABLE()
127};
128
129// Define a new frame type: this is going to be our main frame
130class MyFrame : public wxFrame
131{
132public:
133 // ctor(s)
134 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
ffd0623c
JS
135#ifdef USE_MDI_PARENT_FRAME
136class MyFrame : public wxMDIParentFrame
137#else
2286341c 138 virtual ~MyFrame();
ffd0623c 139#endif
2286341c
VZ
140
141 // event handlers (these functions should _not_ be virtual)
142 void OnQuit(wxCommandEvent& event);
143 void OnAbout(wxCommandEvent& event);
633d67bb
VZ
144
145 void OnSetStatusFields(wxCommandEvent& event);
2286341c
VZ
146 void OnRecreateStatusBar(wxCommandEvent& event);
147
148private:
149 enum StatBarKind
150 {
151 StatBar_Default,
152 StatBar_Custom,
153 StatBar_Max
154 } m_statbarKind;
ffd0623c
JS
155 void OnUpdateSetStatusFields(wxUpdateUIEvent& event);
156 void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
157 void OnStatusBarToggle(wxCommandEvent& event);
2286341c
VZ
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
cbc66a27
VZ
167// Our about dialog ith its status bar
168class MyAboutDialog : public wxDialog
169{
170public:
171 MyAboutDialog(wxWindow *parent);
172};
173
2286341c
VZ
174// ----------------------------------------------------------------------------
175// constants
176// ----------------------------------------------------------------------------
177
178// IDs for the controls and the menu commands
179enum
180{
181 // menu items
182 StatusBar_Quit = 1,
633d67bb 183 StatusBar_SetFields,
2286341c
VZ
184 StatusBar_Recreate,
185 StatusBar_About,
ffd0623c 186 StatusBar_Toggle,
2286341c
VZ
187 StatusBar_Checkbox = 1000
188};
189
190static const int BITMAP_SIZE_X = 32;
191static 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.
ffd0623c
JS
200#ifdef USE_MDI_PARENT_FRAME
201BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
202#else
2286341c 203BEGIN_EVENT_TABLE(MyFrame, wxFrame)
ffd0623c 204#endif
2286341c 205 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
633d67bb 206 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
2286341c
VZ
207 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
208 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
ffd0623c
JS
209 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
210 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
211 EVT_UPDATE_UI(StatusBar_SetFields, MyFrame::OnUpdateSetStatusFields)
2286341c
VZ
212END_EVENT_TABLE()
213
214BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
215 EVT_SIZE(MyStatusBar::OnSize)
216 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
f6bcfd97 217 EVT_BUTTON(-1, MyStatusBar::OnButton)
633d67bb 218 EVT_TIMER(-1, MyStatusBar::OnTimer)
2286341c
VZ
219END_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)
226IMPLEMENT_APP(MyApp)
227
228// ============================================================================
229// implementation
230// ============================================================================
231
232// ----------------------------------------------------------------------------
233// the application class
234// ----------------------------------------------------------------------------
235
236// `Main program' equivalent: the program execution "starts" here
237bool 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
258MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
ffd0623c
JS
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
2286341c
VZ
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;
633d67bb
VZ
279 statbarMenu->Append(StatusBar_SetFields, "&Set field count\tCtrl-C",
280 "Set the number of status bar fields");
ffd0623c
JS
281 statbarMenu->Append(StatusBar_Toggle, "&Toggle Status Bar",
282 "Toggle the status bar display", true);
2286341c
VZ
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);
ffd0623c 300 m_statbarKind = StatBar_Default;
2286341c
VZ
301 SetStatusText("Welcome to wxWindows!");
302
303 m_statbarDefault = GetStatusBar();
304}
305
306MyFrame::~MyFrame()
307{
308 SetStatusBar(NULL);
309
310 delete m_statbarDefault;
311 delete m_statbarCustom;
312}
313
314void 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:
a60b1f5d 337 wxFAIL_MSG(wxT("unknown stat bar kind"));
2286341c
VZ
338 }
339
2286341c 340 GetStatusBar()->Show();
f6bcfd97 341 PositionStatusBar();
2286341c
VZ
342
343 m_statbarKind = kind;
344}
345
ffd0623c
JS
346void 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
2286341c 355// event handlers
633d67bb
VZ
356void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
357{
358 wxStatusBar *sb = GetStatusBar();
359
360 long nFields = wxGetNumberFromUser
361 (
bf69498a
VZ
362 _T("Select the number of fields in the status bar"),
363 _T("Fields:"),
364 _T("wxWindows statusbar sample"),
633d67bb
VZ
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 {
71e03035
VZ
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
bf69498a 378 static const int *widthsAll[] =
633d67bb 379 {
71e03035
VZ
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 };
633d67bb 386
bf69498a
VZ
387 const int * const widths = widthsAll[nFields - 1];
388 sb->SetFieldsCount(nFields, widths);
633d67bb 389
bf69498a
VZ
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 }
633d67bb
VZ
407 }
408 else
409 {
4693b20c 410 wxLogStatus(this, wxT("Cancelled"));
633d67bb
VZ
411 }
412}
413
ffd0623c
JS
414void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event)
415{
2f6c54eb 416 event.Check(GetStatusBar() != 0);
ffd0623c
JS
417}
418
419void 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
2286341c
VZ
438void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
439{
440 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
441 : StatBar_Custom);
442}
443
444void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
445{
446 // TRUE is to force the frame to close
447 Close(TRUE);
448}
449
450void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
451{
cbc66a27
VZ
452 MyAboutDialog dlg(this);
453 dlg.ShowModal();
454}
455
456// ----------------------------------------------------------------------------
457// MyAboutDialog
458// ----------------------------------------------------------------------------
459
460MyAboutDialog::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
a1a43961
VZ
469 wxButton *btn = new wxButton(this, wxID_OK, "&Close");
470
8b8bff20
VZ
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);
cbc66a27
VZ
483
484 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
8b8bff20 485 sizerTop->Add(statbarTop, 0, wxGROW);
cbc66a27 486 sizerTop->Add(-1, 10, 1, wxGROW);
a1a43961
VZ
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);
cbc66a27 490 sizerTop->Add(-1, 10, 1, wxGROW);
8b8bff20 491 sizerTop->Add(statbarBottom, 0, wxGROW);
cbc66a27
VZ
492
493 SetAutoLayout(TRUE);
494 SetSizer(sizerTop);
495
496 sizerTop->Fit(this);
497 sizerTop->SetSizeHints(this);
2286341c
VZ
498}
499
500// ----------------------------------------------------------------------------
501// MyStatusBar
502// ----------------------------------------------------------------------------
503
85401ffe
VZ
504#ifdef __VISUALC__
505 // 'this' : used in base member initializer list -- so what??
506 #pragma warning(disable: 4355)
507#endif
508
2286341c 509MyStatusBar::MyStatusBar(wxWindow *parent)
f6bcfd97 510 : wxStatusBar(parent, -1), m_timer(this), m_checkbox(NULL)
2286341c
VZ
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
f6bcfd97 520#ifdef USE_STATIC_BITMAP
85401ffe 521 m_statbmp = new wxStaticBitmap(this, -1, wxIcon(green_xpm));
f6bcfd97 522#else
71e03035
VZ
523 m_statbmp = new wxBitmapButton(this, -1, CreateBitmapForButton(),
524 wxDefaultPosition, wxDefaultSize,
525 wxBU_EXACTFIT);
f6bcfd97 526#endif
2286341c
VZ
527
528 m_timer.Start(1000);
529
85401ffe
VZ
530 SetMinHeight(BITMAP_SIZE_Y);
531
2286341c
VZ
532 UpdateClock();
533}
534
85401ffe
VZ
535#ifdef __VISUALC__
536 #pragma warning(default: 4355)
537#endif
538
2286341c
VZ
539MyStatusBar::~MyStatusBar()
540{
541 if ( m_timer.IsRunning() )
542 {
543 m_timer.Stop();
544 }
545}
546
f6bcfd97
BP
547wxBitmap 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
2286341c
VZ
564void MyStatusBar::OnSize(wxSizeEvent& event)
565{
f6bcfd97
BP
566 if ( !m_checkbox )
567 return;
568
2286341c
VZ
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);
f6bcfd97 575 wxSize size = m_statbmp->GetSize();
f6bcfd97
BP
576
577 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
578 rect.y + (rect.height - size.y) / 2);
2286341c
VZ
579
580 event.Skip();
581}
582
f6bcfd97
BP
583void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
584{
585 m_checkbox->SetValue(!m_checkbox->GetValue());
586
587 DoToggle();
588}
589
590void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
591{
592 DoToggle();
593}
594
595void MyStatusBar::DoToggle()
2286341c
VZ
596{
597 if ( m_checkbox->GetValue() )
598 {
599 m_timer.Start(1000);
600
f6bcfd97 601#ifdef USE_STATIC_BITMAP
85401ffe 602 m_statbmp->SetIcon(wxIcon(green_xpm));
f6bcfd97
BP
603#else
604 m_statbmp->SetBitmapLabel(CreateBitmapForButton(FALSE));
605 m_statbmp->Refresh();
606#endif
2286341c
VZ
607
608 UpdateClock();
609 }
610 else // don't show clock
611 {
612 m_timer.Stop();
613
f6bcfd97 614#ifdef USE_STATIC_BITMAP
85401ffe 615 m_statbmp->SetIcon(wxIcon(red_xpm));
f6bcfd97
BP
616#else
617 m_statbmp->SetBitmapLabel(CreateBitmapForButton(TRUE));
618 m_statbmp->Refresh();
619#endif
2286341c
VZ
620
621 SetStatusText("", Field_Clock);
622 }
623}
624
625void MyStatusBar::UpdateClock()
626{
627 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
628}