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"
53 // define this for the platforms which don't support wxBitmapButton (such as
54 // Motif), else a wxBitmapButton will be used
56 #define USE_STATIC_BITMAP
59 //#define USE_MDI_PARENT_FRAME 1
61 #ifdef USE_MDI_PARENT_FRAME
63 #endif // USE_MDI_PARENT_FRAME
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 #ifdef USE_STATIC_BITMAP
73 #endif // USE_STATIC_BITMAP
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // Define a new application type, each program should derive a class from wxApp
80 class MyApp
: public wxApp
83 // override base class virtuals
84 // ----------------------------
86 // this one is called on application startup and is a good place for the app
87 // initialization (doing it here and not in the ctor allows to have an error
88 // return: if OnInit() returns false, the application terminates)
89 virtual bool OnInit();
92 // A custom status bar which contains controls, icons &c
93 class MyStatusBar
: public wxStatusBar
96 MyStatusBar(wxWindow
*parent
);
97 virtual ~MyStatusBar();
103 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
105 void OnSize(wxSizeEvent
& event
);
106 void OnToggleClock(wxCommandEvent
& event
);
107 void OnButton(wxCommandEvent
& event
);
110 // toggle the state of the status bar controls
113 wxBitmap
CreateBitmapForButton(bool on
= false);
129 wxCheckBox
*m_checkbox
;
131 #ifdef USE_STATIC_BITMAP
132 wxStaticBitmap
*m_statbmp
;
134 wxBitmapButton
*m_statbmp
;
137 DECLARE_EVENT_TABLE()
140 // Define a new frame type: this is going to be our main frame
141 class MyFrame
: public wxFrame
145 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
146 #ifdef USE_MDI_PARENT_FRAME
147 class MyFrame
: public wxMDIParentFrame
152 // event handlers (these functions should _not_ be virtual)
153 void OnQuit(wxCommandEvent
& event
);
154 void OnAbout(wxCommandEvent
& event
);
156 void OnResetFieldsWidth(wxCommandEvent
& event
);
157 void OnSetStatusFields(wxCommandEvent
& event
);
158 void OnRecreateStatusBar(wxCommandEvent
& event
);
159 void OnSetStyleNormal(wxCommandEvent
& event
);
160 void OnSetStyleFlat(wxCommandEvent
& event
);
161 void OnSetStyleRaised(wxCommandEvent
& event
);
171 void OnUpdateResetFieldsWidth(wxUpdateUIEvent
& event
);
172 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
173 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
174 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
175 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
176 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
177 void OnStatusBarToggle(wxCommandEvent
& event
);
178 void DoCreateStatusBar(StatBarKind kind
);
181 wxStatusBar
*m_statbarDefault
;
182 MyStatusBar
*m_statbarCustom
;
186 // any class wishing to process wxWidgets events must use this macro
187 DECLARE_EVENT_TABLE()
190 // Our about dialog ith its status bar
191 class MyAboutDialog
: public wxDialog
194 MyAboutDialog(wxWindow
*parent
);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 // IDs for the controls and the menu commands
207 StatusBar_ResetFieldsWidth
,
211 StatusBar_Checkbox
= 1000,
213 StatusBar_SetStyleNormal
,
214 StatusBar_SetStyleFlat
,
215 StatusBar_SetStyleRaised
218 static const int BITMAP_SIZE_X
= 32;
219 static const int BITMAP_SIZE_Y
= 15;
221 // ----------------------------------------------------------------------------
222 // event tables and other macros for wxWidgets
223 // ----------------------------------------------------------------------------
225 // the event tables connect the wxWidgets events with the functions (event
226 // handlers) which process them. It can be also done at run-time, but for the
227 // simple menu events like this the static method is much simpler.
228 #ifdef USE_MDI_PARENT_FRAME
229 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
231 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
233 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
234 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
235 EVT_MENU(StatusBar_ResetFieldsWidth
, MyFrame::OnResetFieldsWidth
)
236 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
237 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
238 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
239 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame::OnSetStyleNormal
)
240 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame::OnSetStyleFlat
)
241 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame::OnSetStyleRaised
)
243 EVT_UPDATE_UI(StatusBar_ResetFieldsWidth
, MyFrame::OnUpdateResetFieldsWidth
)
244 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
245 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
246 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame::OnUpdateSetStyleNormal
)
247 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame::OnUpdateSetStyleFlat
)
248 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame::OnUpdateSetStyleRaised
)
251 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
252 EVT_SIZE(MyStatusBar::OnSize
)
254 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
256 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
258 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
262 // Create a new application object: this macro will allow wxWidgets to create
263 // the application object during program execution (it's better than using a
264 // static object for many reasons) and also declares the accessor function
265 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
269 // ============================================================================
271 // ============================================================================
273 // ----------------------------------------------------------------------------
274 // the application class
275 // ----------------------------------------------------------------------------
277 // `Main program' equivalent: the program execution "starts" here
280 if ( !wxApp::OnInit() )
283 // create the main application window
284 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
285 wxPoint(50, 50), wxSize(450, 340));
287 // and show it (the frames, unlike simple controls, are not shown when
288 // created initially)
291 // success: wxApp::OnRun() will be called which will enter the main message
292 // loop and the application will run. If we returned 'false' here, the
293 // application would exit immediately.
297 // ----------------------------------------------------------------------------
299 // ----------------------------------------------------------------------------
302 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
303 #ifdef USE_MDI_PARENT_FRAME
304 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
306 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
309 m_statbarDefault
= NULL
;
310 m_statbarCustom
= NULL
;
312 m_statbarStyle
= wxSB_NORMAL
;
315 // we need this in order to allow the about menu relocation, since ABOUT is
316 // not the default id of the about menu
317 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
321 wxMenu
*menuFile
= new wxMenu
;
322 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
324 wxMenu
*statbarMenu
= new wxMenu
;
325 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
326 _T("Set the number of status bar fields"));
328 wxMenu
*statbarStyleMenu
= new wxMenu
;
329 statbarStyleMenu
->Append(StatusBar_SetStyleNormal
, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
330 statbarStyleMenu
->Append(StatusBar_SetStyleFlat
, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
331 statbarStyleMenu
->Append(StatusBar_SetStyleRaised
, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
332 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
334 statbarMenu
->Append(StatusBar_ResetFieldsWidth
, _T("Reset field widths"),
335 _T("Sets all fields to the same width"));
336 statbarMenu
->AppendSeparator();
338 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
339 _T("Toggle the status bar display"), true);
340 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
341 _T("Toggle status bar format"));
343 wxMenu
*helpMenu
= new wxMenu
;
344 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
346 // now append the freshly created menu to the menu bar...
347 wxMenuBar
*menuBar
= new wxMenuBar();
348 menuBar
->Append(menuFile
, _T("&File"));
349 menuBar
->Append(statbarMenu
, _T("&Status bar"));
350 menuBar
->Append(helpMenu
, _T("&Help"));
352 // ... and attach this menu bar to the frame
355 // create default status bar to start with
357 m_statbarKind
= StatBar_Default
;
358 SetStatusText(_T("Welcome to wxWidgets!"));
360 m_statbarDefault
= GetStatusBar();
367 delete m_statbarDefault
;
368 delete m_statbarCustom
;
371 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
373 wxStatusBar
*statbarOld
= GetStatusBar();
381 case StatBar_Default
:
382 SetStatusBar(m_statbarDefault
);
386 if ( !m_statbarCustom
)
388 m_statbarCustom
= new MyStatusBar(this);
390 SetStatusBar(m_statbarCustom
);
394 wxFAIL_MSG(wxT("unknown stat bar kind"));
398 GetStatusBar()->Show();
401 m_statbarKind
= kind
;
404 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
406 // only allow the settings of the number of status fields for the default
408 wxStatusBar
*sb
= GetStatusBar();
409 event
.Enable(sb
== m_statbarDefault
);
413 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
415 wxStatusBar
*sb
= GetStatusBar();
417 long nFields
= wxGetNumberFromUser
419 _T("Select the number of fields in the status bar"),
421 _T("wxWidgets statusbar sample"),
422 sb
->GetFieldsCount(),
427 // we don't check if the number changed at all on purpose: calling
428 // SetFieldsCount() with the same number of fields should be ok
431 static const int widthsFor2Fields
[] = { 200, -1 };
432 static const int widthsFor3Fields
[] = { -1, -2, -1 };
433 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
435 static const int *widthsAll
[] =
437 NULL
, // 1 field: default
438 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
439 widthsFor3Fields
, // 3 fields: 3 var
440 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
441 NULL
// 5 fields: default (all have same width)
444 const int * const widths
= widthsAll
[nFields
- 1];
445 sb
->SetFieldsCount(nFields
, widths
);
448 for ( long n
= 0; n
< nFields
; n
++ )
453 s
.Printf(_T("fixed (%d)"), widths
[n
]);
455 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
467 wxLogStatus(this, wxT("Cancelled"));
471 void MyFrame::OnUpdateResetFieldsWidth(wxUpdateUIEvent
& event
)
473 // only allow the settings of the number of status fields for the default
475 wxStatusBar
*sb
= GetStatusBar();
476 event
.Enable(sb
== m_statbarDefault
);
479 void MyFrame::OnResetFieldsWidth(wxCommandEvent
& WXUNUSED(event
))
481 wxStatusBar
*pStat
= GetStatusBar();
484 int n
= pStat
->GetFieldsCount();
485 pStat
->SetStatusWidths(n
, NULL
);
486 for (int i
=0; i
<n
; i
++)
487 pStat
->SetStatusText("same size", i
);
491 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
493 event
.Check(GetStatusBar() != NULL
);
496 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
498 wxStatusBar
*statbarOld
= GetStatusBar();
506 DoCreateStatusBar(m_statbarKind
);
510 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
512 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
516 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
518 // true is to force the frame to close
522 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
524 MyAboutDialog
dlg(this);
528 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
530 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
533 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
535 event
.Check(m_statbarStyle
== wxSB_FLAT
);
538 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
540 event
.Check(m_statbarStyle
== wxSB_RAISED
);
543 void MyFrame::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
545 m_statbarStyle
= wxSB_NORMAL
;
549 void MyFrame::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
551 m_statbarStyle
= wxSB_FLAT
;
555 void MyFrame::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
557 m_statbarStyle
= wxSB_RAISED
;
561 void MyFrame::ApplyStyle()
563 wxStatusBar
*sb
= GetStatusBar();
564 int fields
= sb
->GetFieldsCount();
565 int *styles
= new int[fields
];
567 for (int i
= 1; i
< fields
; i
++)
568 styles
[i
] = wxSB_NORMAL
;
570 styles
[0] = m_statbarStyle
;
572 sb
->SetStatusStyles(fields
, styles
);
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
582 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
583 wxDefaultPosition
, wxDefaultSize
,
584 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
586 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
587 _T("wxStatusBar sample\n")
588 _T("(c) 2000 Vadim Zeitlin"));
590 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
592 // create the top status bar without the size grip (default style),
593 // otherwise it looks weird
594 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
595 statbarTop
->SetFieldsCount(3);
596 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
597 statbarTop
->SetStatusText(_T("in a dialog"), 1);
598 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
600 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
601 statbarBottom
->SetFieldsCount(2);
602 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
603 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
605 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
606 sizerTop
->Add(statbarTop
, 0, wxGROW
);
607 sizerTop
->Add(-1, 10, 1, wxGROW
);
608 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
609 sizerTop
->Add(-1, 10, 1, wxGROW
);
610 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
611 sizerTop
->Add(-1, 10, 1, wxGROW
);
612 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
614 SetSizerAndFit(sizerTop
);
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
622 // 'this' : used in base member initializer list -- so what??
623 #pragma warning(disable: 4355)
626 MyStatusBar::MyStatusBar(wxWindow
*parent
)
627 : wxStatusBar(parent
, wxID_ANY
)
635 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
637 SetFieldsCount(Field_Max
);
638 SetStatusWidths(Field_Max
, widths
);
641 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
642 m_checkbox
->SetValue(true);
645 #ifdef USE_STATIC_BITMAP
646 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
648 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
649 wxDefaultPosition
, wxDefaultSize
,
657 SetMinHeight(wxMax(m_statbmp
->GetBestSize().GetHeight(),
658 m_checkbox
->GetBestSize().GetHeight()));
664 #pragma warning(default: 4355)
667 MyStatusBar::~MyStatusBar()
670 if ( m_timer
.IsRunning() )
677 #define BMP_BUTTON_SIZE_X 10
678 #define BMP_BUTTON_SIZE_Y 10
680 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
682 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
+1, BMP_BUTTON_SIZE_Y
+1);
684 dc
.SelectObject(bitmap
);
685 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
686 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
688 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
689 dc
.SelectObject(wxNullBitmap
);
694 void MyStatusBar::OnSize(wxSizeEvent
& event
)
702 GetFieldRect(Field_Checkbox
, rect
);
705 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
708 GetFieldRect(Field_Bitmap
, rect
);
709 wxSize size
= m_statbmp
->GetSize();
711 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
712 rect
.y
+ (rect
.height
- size
.y
) / 2);
717 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
720 m_checkbox
->SetValue(!m_checkbox
->GetValue());
726 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
731 void MyStatusBar::DoToggle()
734 if ( m_checkbox
->GetValue() )
740 #ifdef USE_STATIC_BITMAP
741 m_statbmp
->SetIcon(wxIcon(green_xpm
));
743 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
744 m_statbmp
->Refresh();
749 else // don't show clock
755 #ifdef USE_STATIC_BITMAP
756 m_statbmp
->SetIcon(wxIcon(red_xpm
));
758 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
759 m_statbmp
->Refresh();
762 SetStatusText(wxEmptyString
, Field_Clock
);
767 void MyStatusBar::UpdateClock()
769 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);