]> git.saurik.com Git - wxWidgets.git/blame - samples/statbar/statbar.cpp
SWIGged updates for wxMac
[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__
53 #define USE_STATIC_BITMAP
54#endif
55
2286341c
VZ
56// ----------------------------------------------------------------------------
57// resources
58// ----------------------------------------------------------------------------
59
f6bcfd97
BP
60#ifdef USE_STATIC_BITMAP
61 #include "green.xpm"
62 #include "red.xpm"
63#endif // USE_STATIC_BITMAP
2286341c
VZ
64
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69// Define a new application type, each program should derive a class from wxApp
70class MyApp : public wxApp
71{
72public:
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
83class MyStatusBar : public wxStatusBar
84{
85public:
86 MyStatusBar(wxWindow *parent);
87 virtual ~MyStatusBar();
88
89 void UpdateClock();
90
91 // event handlers
633d67bb 92 void OnTimer(wxTimerEvent& event) { UpdateClock(); }
2286341c
VZ
93 void OnSize(wxSizeEvent& event);
94 void OnToggleClock(wxCommandEvent& event);
f6bcfd97 95 void OnButton(wxCommandEvent& event);
2286341c
VZ
96
97private:
f6bcfd97
BP
98 // toggle the state of the status bar controls
99 void DoToggle();
100
101 wxBitmap CreateBitmapForButton(bool on = FALSE);
102
2286341c
VZ
103 enum
104 {
105 Field_Text,
106 Field_Checkbox,
107 Field_Bitmap,
108 Field_Clock,
109 Field_Max
110 };
111
633d67bb 112 wxTimer m_timer;
2286341c
VZ
113
114 wxCheckBox *m_checkbox;
f6bcfd97 115#ifdef USE_STATIC_BITMAP
2286341c 116 wxStaticBitmap *m_statbmp;
f6bcfd97
BP
117#else
118 wxBitmapButton *m_statbmp;
119#endif
2286341c
VZ
120
121 DECLARE_EVENT_TABLE()
122};
123
124// Define a new frame type: this is going to be our main frame
125class MyFrame : public wxFrame
126{
127public:
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);
633d67bb
VZ
135
136 void OnSetStatusFields(wxCommandEvent& event);
2286341c
VZ
137 void OnRecreateStatusBar(wxCommandEvent& event);
138
139private:
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
cbc66a27
VZ
156// Our about dialog ith its status bar
157class MyAboutDialog : public wxDialog
158{
159public:
160 MyAboutDialog(wxWindow *parent);
161};
162
2286341c
VZ
163// ----------------------------------------------------------------------------
164// constants
165// ----------------------------------------------------------------------------
166
167// IDs for the controls and the menu commands
168enum
169{
170 // menu items
171 StatusBar_Quit = 1,
633d67bb 172 StatusBar_SetFields,
2286341c
VZ
173 StatusBar_Recreate,
174 StatusBar_About,
175 StatusBar_Checkbox = 1000
176};
177
178static const int BITMAP_SIZE_X = 32;
179static 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.
188BEGIN_EVENT_TABLE(MyFrame, wxFrame)
189 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
633d67bb 190 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
2286341c
VZ
191 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
192 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
193END_EVENT_TABLE()
194
195BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
196 EVT_SIZE(MyStatusBar::OnSize)
197 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
f6bcfd97 198 EVT_BUTTON(-1, MyStatusBar::OnButton)
633d67bb 199 EVT_TIMER(-1, MyStatusBar::OnTimer)
2286341c
VZ
200END_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)
207IMPLEMENT_APP(MyApp)
208
209// ============================================================================
210// implementation
211// ============================================================================
212
213// ----------------------------------------------------------------------------
214// the application class
215// ----------------------------------------------------------------------------
216
217// `Main program' equivalent: the program execution "starts" here
218bool 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
239MyFrame::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;
633d67bb
VZ
256 statbarMenu->Append(StatusBar_SetFields, "&Set field count\tCtrl-C",
257 "Set the number of status bar fields");
2286341c
VZ
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
280MyFrame::~MyFrame()
281{
282 SetStatusBar(NULL);
283
284 delete m_statbarDefault;
285 delete m_statbarCustom;
286}
287
288void 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:
a60b1f5d 311 wxFAIL_MSG(wxT("unknown stat bar kind"));
2286341c
VZ
312 }
313
2286341c 314 GetStatusBar()->Show();
f6bcfd97 315 PositionStatusBar();
2286341c
VZ
316
317 m_statbarKind = kind;
318}
319
320// event handlers
633d67bb
VZ
321void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
322{
323 wxStatusBar *sb = GetStatusBar();
324
325 long nFields = wxGetNumberFromUser
326 (
bf69498a
VZ
327 _T("Select the number of fields in the status bar"),
328 _T("Fields:"),
329 _T("wxWindows statusbar sample"),
633d67bb
VZ
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 {
71e03035
VZ
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
bf69498a 343 static const int *widthsAll[] =
633d67bb 344 {
71e03035
VZ
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 };
633d67bb 351
bf69498a
VZ
352 const int * const widths = widthsAll[nFields - 1];
353 sb->SetFieldsCount(nFields, widths);
633d67bb 354
bf69498a
VZ
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 }
633d67bb
VZ
372 }
373 else
374 {
4693b20c 375 wxLogStatus(this, wxT("Cancelled"));
633d67bb
VZ
376 }
377}
378
2286341c
VZ
379void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
380{
381 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
382 : StatBar_Custom);
383}
384
385void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
386{
387 // TRUE is to force the frame to close
388 Close(TRUE);
389}
390
391void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
392{
cbc66a27
VZ
393 MyAboutDialog dlg(this);
394 dlg.ShowModal();
395}
396
397// ----------------------------------------------------------------------------
398// MyAboutDialog
399// ----------------------------------------------------------------------------
400
401MyAboutDialog::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
a1a43961
VZ
410 wxButton *btn = new wxButton(this, wxID_OK, "&Close");
411
8b8bff20
VZ
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);
cbc66a27
VZ
424
425 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
8b8bff20 426 sizerTop->Add(statbarTop, 0, wxGROW);
cbc66a27 427 sizerTop->Add(-1, 10, 1, wxGROW);
a1a43961
VZ
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);
cbc66a27 431 sizerTop->Add(-1, 10, 1, wxGROW);
8b8bff20 432 sizerTop->Add(statbarBottom, 0, wxGROW);
cbc66a27
VZ
433
434 SetAutoLayout(TRUE);
435 SetSizer(sizerTop);
436
437 sizerTop->Fit(this);
438 sizerTop->SetSizeHints(this);
2286341c
VZ
439}
440
441// ----------------------------------------------------------------------------
442// MyStatusBar
443// ----------------------------------------------------------------------------
444
85401ffe
VZ
445#ifdef __VISUALC__
446 // 'this' : used in base member initializer list -- so what??
447 #pragma warning(disable: 4355)
448#endif
449
2286341c 450MyStatusBar::MyStatusBar(wxWindow *parent)
f6bcfd97 451 : wxStatusBar(parent, -1), m_timer(this), m_checkbox(NULL)
2286341c
VZ
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
f6bcfd97 461#ifdef USE_STATIC_BITMAP
85401ffe 462 m_statbmp = new wxStaticBitmap(this, -1, wxIcon(green_xpm));
f6bcfd97 463#else
71e03035
VZ
464 m_statbmp = new wxBitmapButton(this, -1, CreateBitmapForButton(),
465 wxDefaultPosition, wxDefaultSize,
466 wxBU_EXACTFIT);
f6bcfd97 467#endif
2286341c
VZ
468
469 m_timer.Start(1000);
470
85401ffe
VZ
471 SetMinHeight(BITMAP_SIZE_Y);
472
2286341c
VZ
473 UpdateClock();
474}
475
85401ffe
VZ
476#ifdef __VISUALC__
477 #pragma warning(default: 4355)
478#endif
479
2286341c
VZ
480MyStatusBar::~MyStatusBar()
481{
482 if ( m_timer.IsRunning() )
483 {
484 m_timer.Stop();
485 }
486}
487
f6bcfd97
BP
488wxBitmap 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
2286341c
VZ
505void MyStatusBar::OnSize(wxSizeEvent& event)
506{
f6bcfd97
BP
507 if ( !m_checkbox )
508 return;
509
2286341c
VZ
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);
f6bcfd97 516 wxSize size = m_statbmp->GetSize();
f6bcfd97
BP
517
518 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
519 rect.y + (rect.height - size.y) / 2);
2286341c
VZ
520
521 event.Skip();
522}
523
f6bcfd97
BP
524void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
525{
526 m_checkbox->SetValue(!m_checkbox->GetValue());
527
528 DoToggle();
529}
530
531void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
532{
533 DoToggle();
534}
535
536void MyStatusBar::DoToggle()
2286341c
VZ
537{
538 if ( m_checkbox->GetValue() )
539 {
540 m_timer.Start(1000);
541
f6bcfd97 542#ifdef USE_STATIC_BITMAP
85401ffe 543 m_statbmp->SetIcon(wxIcon(green_xpm));
f6bcfd97
BP
544#else
545 m_statbmp->SetBitmapLabel(CreateBitmapForButton(FALSE));
546 m_statbmp->Refresh();
547#endif
2286341c
VZ
548
549 UpdateClock();
550 }
551 else // don't show clock
552 {
553 m_timer.Stop();
554
f6bcfd97 555#ifdef USE_STATIC_BITMAP
85401ffe 556 m_statbmp->SetIcon(wxIcon(red_xpm));
f6bcfd97
BP
557#else
558 m_statbmp->SetBitmapLabel(CreateBitmapForButton(TRUE));
559 m_statbmp->Refresh();
560#endif
2286341c
VZ
561
562 SetStatusText("", Field_Clock);
563 }
564}
565
566void MyStatusBar::UpdateClock()
567{
568 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
569}