1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStatusBar sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
28 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
29 #endif // wxUSE_STATUSBAR
31 // for all others, include the necessary headers
36 #include "wx/statusbr.h"
38 #include "wx/checkbox.h"
39 #include "wx/statbmp.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textdlg.h"
44 #include "wx/stattext.h"
45 #include "wx/bmpbuttn.h"
46 #include "wx/dcmemory.h"
49 #include "wx/datetime.h"
50 #include "wx/numdlg.h"
51 #include "wx/fontdlg.h"
54 #include "../sample.xpm"
58 // define this for the platforms which don't support wxBitmapButton (such as
59 // Motif), else a wxBitmapButton will be used
61 #define USE_STATIC_BITMAP
64 //#define USE_MDI_PARENT_FRAME 1
66 #ifdef USE_MDI_PARENT_FRAME
68 #endif // USE_MDI_PARENT_FRAME
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 #ifdef USE_STATIC_BITMAP
78 #endif // USE_STATIC_BITMAP
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // Define a new application type, each program should derive a class from wxApp
85 class MyApp
: public wxApp
88 // override base class virtuals
89 // ----------------------------
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();
97 // A custom status bar which contains controls, icons &c
98 class MyStatusBar
: public wxStatusBar
101 MyStatusBar(wxWindow
*parent
);
102 virtual ~MyStatusBar();
108 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
110 void OnSize(wxSizeEvent
& event
);
111 void OnToggleClock(wxCommandEvent
& event
);
112 void OnButton(wxCommandEvent
& event
);
115 // toggle the state of the status bar controls
118 wxBitmap
CreateBitmapForButton(bool on
= false);
134 wxCheckBox
*m_checkbox
;
136 #ifdef USE_STATIC_BITMAP
137 wxStaticBitmap
*m_statbmp
;
139 wxBitmapButton
*m_statbmp
;
142 DECLARE_EVENT_TABLE()
145 // Define a new frame type: this is going to be our main frame
146 class MyFrame
: public wxFrame
150 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
151 #ifdef USE_MDI_PARENT_FRAME
152 class MyFrame
: public wxMDIParentFrame
157 // event handlers (these functions should _not_ be virtual)
158 void OnQuit(wxCommandEvent
& event
);
159 void OnAbout(wxCommandEvent
& event
);
161 void OnResetFieldsWidth(wxCommandEvent
& event
);
162 void OnSetStatusFields(wxCommandEvent
& event
);
163 void OnSetStatusTexts(wxCommandEvent
& event
);
164 void OnSetStatusFont(wxCommandEvent
& event
);
165 void OnRecreateStatusBar(wxCommandEvent
& event
);
166 void OnSetStyleNormal(wxCommandEvent
& event
);
167 void OnSetStyleFlat(wxCommandEvent
& event
);
168 void OnSetStyleRaised(wxCommandEvent
& event
);
179 void OnUpdateForDefaultStatusbar(wxUpdateUIEvent
& event
);
180 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
181 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
182 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
183 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
184 void OnStatusBarToggle(wxCommandEvent
& event
);
185 void DoCreateStatusBar(StatBarKind kind
);
188 wxStatusBar
*m_statbarDefault
;
189 MyStatusBar
*m_statbarCustom
;
193 // any class wishing to process wxWidgets events must use this macro
194 DECLARE_EVENT_TABLE()
197 // Our about dialog ith its status bar
198 class MyAboutDialog
: public wxDialog
201 MyAboutDialog(wxWindow
*parent
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 // IDs for the controls and the menu commands
217 StatusBar_ResetFieldsWidth
,
222 StatusBar_Checkbox
= 1000,
224 StatusBar_SetStyleNormal
,
225 StatusBar_SetStyleFlat
,
226 StatusBar_SetStyleRaised
229 static const int BITMAP_SIZE_X
= 32;
230 static const int BITMAP_SIZE_Y
= 15;
232 // ----------------------------------------------------------------------------
233 // event tables and other macros for wxWidgets
234 // ----------------------------------------------------------------------------
236 // the event tables connect the wxWidgets events with the functions (event
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.
239 #ifdef USE_MDI_PARENT_FRAME
240 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
242 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
244 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
245 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
246 EVT_MENU(StatusBar_SetTexts
, MyFrame::OnSetStatusTexts
)
247 EVT_MENU(StatusBar_SetFont
, MyFrame::OnSetStatusFont
)
248 EVT_MENU(StatusBar_ResetFieldsWidth
, MyFrame::OnResetFieldsWidth
)
249 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
250 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
251 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
252 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame::OnSetStyleNormal
)
253 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame::OnSetStyleFlat
)
254 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame::OnSetStyleRaised
)
256 EVT_UPDATE_UI_RANGE(StatusBar_SetFields
, StatusBar_ResetFieldsWidth
,
257 MyFrame::OnUpdateForDefaultStatusbar
)
258 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
259 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame::OnUpdateSetStyleNormal
)
260 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame::OnUpdateSetStyleFlat
)
261 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame::OnUpdateSetStyleRaised
)
264 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
265 EVT_SIZE(MyStatusBar::OnSize
)
267 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
269 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
271 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
275 // Create a new application object: this macro will allow wxWidgets to create
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
282 // ============================================================================
284 // ============================================================================
286 // ----------------------------------------------------------------------------
287 // the application class
288 // ----------------------------------------------------------------------------
290 // `Main program' equivalent: the program execution "starts" here
293 if ( !wxApp::OnInit() )
296 // create the main application window
297 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
298 wxPoint(50, 50), wxSize(450, 340));
300 // and show it (the frames, unlike simple controls, are not shown when
301 // created initially)
304 // success: wxApp::OnRun() will be called which will enter the main message
305 // loop and the application will run. If we returned 'false' here, the
306 // application would exit immediately.
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
315 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
316 #ifdef USE_MDI_PARENT_FRAME
317 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
319 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
322 SetIcon(wxICON(sample
));
324 m_statbarDefault
= NULL
;
325 m_statbarCustom
= NULL
;
327 m_statbarStyle
= wxSB_NORMAL
;
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
;
336 wxMenu
*menuFile
= new wxMenu
;
337 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
339 wxMenu
*statbarMenu
= new wxMenu
;
340 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
341 _T("Set the number of status bar fields"));
342 statbarMenu
->Append(StatusBar_SetTexts
, _T("&Set field text\tCtrl-T"),
343 _T("Set the text to display for each status bar field"));
344 statbarMenu
->Append(StatusBar_SetFont
, _T("&Set field font\tCtrl-F"),
345 _T("Set the font to use for rendering status bar fields"));
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);
352 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
354 statbarMenu
->Append(StatusBar_ResetFieldsWidth
, _T("Reset field widths"),
355 _T("Sets all fields to the same width"));
356 statbarMenu
->AppendSeparator();
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"));
363 wxMenu
*helpMenu
= new wxMenu
;
364 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
366 // now append the freshly created menu to the menu bar...
367 wxMenuBar
*menuBar
= new wxMenuBar();
368 menuBar
->Append(menuFile
, _T("&File"));
369 menuBar
->Append(statbarMenu
, _T("&Status bar"));
370 menuBar
->Append(helpMenu
, _T("&Help"));
372 // ... and attach this menu bar to the frame
375 // create default status bar to start with
377 m_statbarKind
= StatBar_Default
;
378 SetStatusText(_T("Welcome to wxWidgets!"));
380 m_statbarDefault
= GetStatusBar();
387 delete m_statbarDefault
;
388 delete m_statbarCustom
;
391 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
393 wxStatusBar
*statbarOld
= GetStatusBar();
401 case StatBar_Default
:
402 SetStatusBar(m_statbarDefault
);
406 if ( !m_statbarCustom
)
408 m_statbarCustom
= new MyStatusBar(this);
410 SetStatusBar(m_statbarCustom
);
414 wxFAIL_MSG(wxT("unknown stat bar kind"));
418 GetStatusBar()->Show();
421 m_statbarKind
= kind
;
425 // ----------------------------------------------------------------------------
426 // main frame - event handlers
427 // ----------------------------------------------------------------------------
429 void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent
& event
)
431 // only allow this feature for the default status bar
432 wxStatusBar
*sb
= GetStatusBar();
433 event
.Enable(sb
== m_statbarDefault
);
436 void MyFrame::OnSetStatusTexts(wxCommandEvent
& WXUNUSED(event
))
438 wxStatusBar
*sb
= GetStatusBar();
441 for (int i
=0; i
<sb
->GetFieldsCount(); i
++)
444 wxGetTextFromUser(wxString::Format("Enter the text for the %d-th field:", i
+1),
445 "Input field text", "A dummy test string", this);
447 sb
->SetStatusText(txt
, i
);
451 void MyFrame::OnSetStatusFont(wxCommandEvent
& WXUNUSED(event
))
453 wxStatusBar
*sb
= GetStatusBar();
455 wxFont fnt
= wxGetFontFromUser(this, sb
->GetFont(), "Choose statusbar font");
459 sb
->SetSize(sb
->GetBestSize());
463 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
465 wxStatusBar
*sb
= GetStatusBar();
467 long nFields
= wxGetNumberFromUser
469 _T("Select the number of fields in the status bar"),
471 _T("wxWidgets statusbar sample"),
472 sb
->GetFieldsCount(),
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
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 };
485 static const int *widthsAll
[] =
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)
494 const int * const widths
= widthsAll
[nFields
- 1];
495 sb
->SetFieldsCount(nFields
, widths
);
498 for ( long n
= 0; n
< nFields
; n
++ )
503 s
.Printf(_T("fixed (%d)"), widths
[n
]);
505 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
517 wxLogStatus(this, wxT("Cancelled"));
521 void MyFrame::OnResetFieldsWidth(wxCommandEvent
& WXUNUSED(event
))
523 wxStatusBar
*pStat
= GetStatusBar();
526 int n
= pStat
->GetFieldsCount();
527 pStat
->SetStatusWidths(n
, NULL
);
528 for (int i
=0; i
<n
; i
++)
529 pStat
->SetStatusText("same size", i
);
533 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
535 event
.Check(GetStatusBar() != NULL
);
538 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
540 wxStatusBar
*statbarOld
= GetStatusBar();
548 DoCreateStatusBar(m_statbarKind
);
552 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
554 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
558 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
560 // true is to force the frame to close
564 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
566 MyAboutDialog
dlg(this);
570 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
572 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
575 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
577 event
.Check(m_statbarStyle
== wxSB_FLAT
);
580 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
582 event
.Check(m_statbarStyle
== wxSB_RAISED
);
585 void MyFrame::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
587 m_statbarStyle
= wxSB_NORMAL
;
591 void MyFrame::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
593 m_statbarStyle
= wxSB_FLAT
;
597 void MyFrame::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
599 m_statbarStyle
= wxSB_RAISED
;
603 void MyFrame::ApplyStyle()
605 wxStatusBar
*sb
= GetStatusBar();
606 int fields
= sb
->GetFieldsCount();
607 int *styles
= new int[fields
];
609 for (int i
= 1; i
< fields
; i
++)
610 styles
[i
] = wxSB_NORMAL
;
612 styles
[0] = m_statbarStyle
;
614 sb
->SetStatusStyles(fields
, styles
);
619 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
623 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
624 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
625 wxDefaultPosition
, wxDefaultSize
,
626 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
628 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
629 _T("wxStatusBar sample\n")
630 _T("(c) 2000 Vadim Zeitlin"));
632 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
634 // create the top status bar without the size grip (default style),
635 // otherwise it looks weird
636 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
637 statbarTop
->SetFieldsCount(3);
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);
642 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
643 statbarBottom
->SetFieldsCount(2);
644 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
645 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
647 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
648 sizerTop
->Add(statbarTop
, 0, wxGROW
);
649 sizerTop
->Add(-1, 10, 1, wxGROW
);
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);
653 sizerTop
->Add(-1, 10, 1, wxGROW
);
654 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
656 SetSizerAndFit(sizerTop
);
659 // ----------------------------------------------------------------------------
661 // ----------------------------------------------------------------------------
664 // 'this' : used in base member initializer list -- so what??
665 #pragma warning(disable: 4355)
668 MyStatusBar::MyStatusBar(wxWindow
*parent
)
669 : wxStatusBar(parent
, wxID_ANY
)
677 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
679 SetFieldsCount(Field_Max
);
680 SetStatusWidths(Field_Max
, widths
);
683 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
684 m_checkbox
->SetValue(true);
687 #ifdef USE_STATIC_BITMAP
688 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
690 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
691 wxDefaultPosition
, wxDefaultSize
,
699 SetMinHeight(wxMax(m_statbmp
->GetBestSize().GetHeight(),
700 m_checkbox
->GetBestSize().GetHeight()));
706 #pragma warning(default: 4355)
709 MyStatusBar::~MyStatusBar()
712 if ( m_timer
.IsRunning() )
719 #define BMP_BUTTON_SIZE_X 10
720 #define BMP_BUTTON_SIZE_Y 10
722 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
724 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
+1, BMP_BUTTON_SIZE_Y
+1);
726 dc
.SelectObject(bitmap
);
727 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
728 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
730 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
731 dc
.SelectObject(wxNullBitmap
);
736 void MyStatusBar::OnSize(wxSizeEvent
& event
)
744 if (!GetFieldRect(Field_Checkbox
, rect
))
751 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
754 GetFieldRect(Field_Bitmap
, rect
);
755 wxSize size
= m_statbmp
->GetSize();
757 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
758 rect
.y
+ (rect
.height
- size
.y
) / 2);
763 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
766 m_checkbox
->SetValue(!m_checkbox
->GetValue());
772 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
777 void MyStatusBar::DoToggle()
780 if ( m_checkbox
->GetValue() )
786 #ifdef USE_STATIC_BITMAP
787 m_statbmp
->SetIcon(wxIcon(green_xpm
));
789 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
790 m_statbmp
->Refresh();
795 else // don't show clock
801 #ifdef USE_STATIC_BITMAP
802 m_statbmp
->SetIcon(wxIcon(red_xpm
));
804 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
805 m_statbmp
->Refresh();
808 SetStatusText(wxEmptyString
, Field_Clock
);
813 void MyStatusBar::UpdateClock()
815 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);