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