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