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 OnSetStatusTexts(wxCommandEvent
& event
);
159 void OnRecreateStatusBar(wxCommandEvent
& event
);
160 void OnSetStyleNormal(wxCommandEvent
& event
);
161 void OnSetStyleFlat(wxCommandEvent
& event
);
162 void OnSetStyleRaised(wxCommandEvent
& event
);
173 void OnUpdateSetStatusTexts(wxUpdateUIEvent
& event
);
174 void OnUpdateResetFieldsWidth(wxUpdateUIEvent
& event
);
175 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
176 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
177 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
178 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
179 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
180 void OnStatusBarToggle(wxCommandEvent
& event
);
181 void DoCreateStatusBar(StatBarKind kind
);
184 wxStatusBar
*m_statbarDefault
;
185 MyStatusBar
*m_statbarCustom
;
189 // any class wishing to process wxWidgets events must use this macro
190 DECLARE_EVENT_TABLE()
193 // Our about dialog ith its status bar
194 class MyAboutDialog
: public wxDialog
197 MyAboutDialog(wxWindow
*parent
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 // IDs for the controls and the menu commands
211 StatusBar_ResetFieldsWidth
,
215 StatusBar_Checkbox
= 1000,
217 StatusBar_SetStyleNormal
,
218 StatusBar_SetStyleFlat
,
219 StatusBar_SetStyleRaised
222 static const int BITMAP_SIZE_X
= 32;
223 static const int BITMAP_SIZE_Y
= 15;
225 // ----------------------------------------------------------------------------
226 // event tables and other macros for wxWidgets
227 // ----------------------------------------------------------------------------
229 // the event tables connect the wxWidgets events with the functions (event
230 // handlers) which process them. It can be also done at run-time, but for the
231 // simple menu events like this the static method is much simpler.
232 #ifdef USE_MDI_PARENT_FRAME
233 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
235 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
237 EVT_MENU(StatusBar_Quit
, MyFrame
::OnQuit
)
238 EVT_MENU(StatusBar_SetFields
, MyFrame
::OnSetStatusFields
)
239 EVT_MENU(StatusBar_SetTexts
, MyFrame
::OnSetStatusTexts
)
240 EVT_MENU(StatusBar_ResetFieldsWidth
, MyFrame
::OnResetFieldsWidth
)
241 EVT_MENU(StatusBar_Recreate
, MyFrame
::OnRecreateStatusBar
)
242 EVT_MENU(StatusBar_About
, MyFrame
::OnAbout
)
243 EVT_MENU(StatusBar_Toggle
, MyFrame
::OnStatusBarToggle
)
244 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame
::OnSetStyleNormal
)
245 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame
::OnSetStyleFlat
)
246 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame
::OnSetStyleRaised
)
248 EVT_UPDATE_UI(StatusBar_ResetFieldsWidth
, MyFrame
::OnUpdateResetFieldsWidth
)
249 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame
::OnUpdateStatusBarToggle
)
250 EVT_UPDATE_UI(StatusBar_SetTexts
, MyFrame
::OnUpdateSetStatusTexts
)
251 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame
::OnUpdateSetStatusFields
)
252 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame
::OnUpdateSetStyleNormal
)
253 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame
::OnUpdateSetStyleFlat
)
254 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame
::OnUpdateSetStyleRaised
)
257 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
258 EVT_SIZE(MyStatusBar
::OnSize
)
260 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar
::OnToggleClock
)
262 EVT_BUTTON(wxID_ANY
, MyStatusBar
::OnButton
)
264 EVT_TIMER(wxID_ANY
, MyStatusBar
::OnTimer
)
268 // Create a new application object: this macro will allow wxWidgets to create
269 // the application object during program execution (it's better than using a
270 // static object for many reasons) and also declares the accessor function
271 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
275 // ============================================================================
277 // ============================================================================
279 // ----------------------------------------------------------------------------
280 // the application class
281 // ----------------------------------------------------------------------------
283 // `Main program' equivalent: the program execution "starts" here
286 if ( !wxApp
::OnInit() )
289 // create the main application window
290 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
291 wxPoint(50, 50), wxSize(450, 340));
293 // and show it (the frames, unlike simple controls, are not shown when
294 // created initially)
297 // success: wxApp::OnRun() will be called which will enter the main message
298 // loop and the application will run. If we returned 'false' here, the
299 // application would exit immediately.
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
308 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
309 #ifdef USE_MDI_PARENT_FRAME
310 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
312 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
315 m_statbarDefault
= NULL
;
316 m_statbarCustom
= NULL
;
318 m_statbarStyle
= wxSB_NORMAL
;
321 // we need this in order to allow the about menu relocation, since ABOUT is
322 // not the default id of the about menu
323 wxApp
::s_macAboutMenuItemId
= StatusBar_About
;
327 wxMenu
*menuFile
= new wxMenu
;
328 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
330 wxMenu
*statbarMenu
= new wxMenu
;
331 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
332 _T("Set the number of status bar fields"));
333 statbarMenu
->Append(StatusBar_SetTexts
, _T("&Set field text\tCtrl-T"),
334 _T("Set the text to display for each status bar field"));
336 wxMenu
*statbarStyleMenu
= new wxMenu
;
337 statbarStyleMenu
->Append(StatusBar_SetStyleNormal
, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
338 statbarStyleMenu
->Append(StatusBar_SetStyleFlat
, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
339 statbarStyleMenu
->Append(StatusBar_SetStyleRaised
, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
341 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
343 statbarMenu
->Append(StatusBar_ResetFieldsWidth
, _T("Reset field widths"),
344 _T("Sets all fields to the same width"));
345 statbarMenu
->AppendSeparator();
347 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
348 _T("Toggle the status bar display"), true);
349 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
350 _T("Toggle status bar format"));
352 wxMenu
*helpMenu
= new wxMenu
;
353 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
355 // now append the freshly created menu to the menu bar...
356 wxMenuBar
*menuBar
= new wxMenuBar();
357 menuBar
->Append(menuFile
, _T("&File"));
358 menuBar
->Append(statbarMenu
, _T("&Status bar"));
359 menuBar
->Append(helpMenu
, _T("&Help"));
361 // ... and attach this menu bar to the frame
364 // create default status bar to start with
366 m_statbarKind
= StatBar_Default
;
367 SetStatusText(_T("Welcome to wxWidgets!"));
369 m_statbarDefault
= GetStatusBar();
376 delete m_statbarDefault
;
377 delete m_statbarCustom
;
380 void MyFrame
::DoCreateStatusBar(MyFrame
::StatBarKind kind
)
382 wxStatusBar
*statbarOld
= GetStatusBar();
390 case StatBar_Default
:
391 SetStatusBar(m_statbarDefault
);
395 if ( !m_statbarCustom
)
397 m_statbarCustom
= new MyStatusBar(this);
399 SetStatusBar(m_statbarCustom
);
403 wxFAIL_MSG(wxT("unknown stat bar kind"));
407 GetStatusBar()->Show();
410 m_statbarKind
= kind
;
414 // ----------------------------------------------------------------------------
415 // main frame - event handlers
416 // ----------------------------------------------------------------------------
418 void MyFrame
::OnUpdateSetStatusTexts(wxUpdateUIEvent
& event
)
420 // only allow the settings of the text of status fields for the default
422 wxStatusBar
*sb
= GetStatusBar();
423 event
.Enable(sb
== m_statbarDefault
);
426 void MyFrame
::OnSetStatusTexts(wxCommandEvent
& WXUNUSED(event
))
428 wxStatusBar
*sb
= GetStatusBar();
431 for (int i
=0; i
<sb
->GetFieldsCount(); i
++)
434 wxGetTextFromUser(wxString
::Format("Enter the text for the %d-th field:", i
+1),
435 "Input field text", "A dummy test string", this);
437 sb
->SetStatusText(txt
, i
);
441 void MyFrame
::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
443 // only allow the settings of the number of status fields for the default
445 wxStatusBar
*sb
= GetStatusBar();
446 event
.Enable(sb
== m_statbarDefault
);
449 void MyFrame
::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
451 wxStatusBar
*sb
= GetStatusBar();
453 long nFields
= wxGetNumberFromUser
455 _T("Select the number of fields in the status bar"),
457 _T("wxWidgets statusbar sample"),
458 sb
->GetFieldsCount(),
463 // we don't check if the number changed at all on purpose: calling
464 // SetFieldsCount() with the same number of fields should be ok
467 static const int widthsFor2Fields
[] = { 200, -1 };
468 static const int widthsFor3Fields
[] = { -1, -2, -1 };
469 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
471 static const int *widthsAll
[] =
473 NULL
, // 1 field: default
474 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
475 widthsFor3Fields
, // 3 fields: 3 var
476 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
477 NULL
// 5 fields: default (all have same width)
480 const int * const widths
= widthsAll
[nFields
- 1];
481 sb
->SetFieldsCount(nFields
, widths
);
484 for ( long n
= 0; n
< nFields
; n
++ )
489 s
.Printf(_T("fixed (%d)"), widths
[n
]);
491 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
503 wxLogStatus(this, wxT("Cancelled"));
507 void MyFrame
::OnUpdateResetFieldsWidth(wxUpdateUIEvent
& event
)
509 // only allow the settings of the number of status fields for the default
511 wxStatusBar
*sb
= GetStatusBar();
512 event
.Enable(sb
== m_statbarDefault
);
515 void MyFrame
::OnResetFieldsWidth(wxCommandEvent
& WXUNUSED(event
))
517 wxStatusBar
*pStat
= GetStatusBar();
520 int n
= pStat
->GetFieldsCount();
521 pStat
->SetStatusWidths(n
, NULL
);
522 for (int i
=0; i
<n
; i
++)
523 pStat
->SetStatusText("same size", i
);
527 void MyFrame
::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
529 event
.Check(GetStatusBar() != NULL
);
532 void MyFrame
::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
534 wxStatusBar
*statbarOld
= GetStatusBar();
542 DoCreateStatusBar(m_statbarKind
);
546 void MyFrame
::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
548 DoCreateStatusBar(m_statbarKind
== StatBar_Custom ? StatBar_Default
552 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
554 // true is to force the frame to close
558 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
560 MyAboutDialog
dlg(this);
564 void MyFrame
::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
566 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
569 void MyFrame
::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
571 event
.Check(m_statbarStyle
== wxSB_FLAT
);
574 void MyFrame
::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
576 event
.Check(m_statbarStyle
== wxSB_RAISED
);
579 void MyFrame
::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
581 m_statbarStyle
= wxSB_NORMAL
;
585 void MyFrame
::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
587 m_statbarStyle
= wxSB_FLAT
;
591 void MyFrame
::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
593 m_statbarStyle
= wxSB_RAISED
;
597 void MyFrame
::ApplyStyle()
599 wxStatusBar
*sb
= GetStatusBar();
600 int fields
= sb
->GetFieldsCount();
601 int *styles
= new int[fields
];
603 for (int i
= 1; i
< fields
; i
++)
604 styles
[i
] = wxSB_NORMAL
;
606 styles
[0] = m_statbarStyle
;
608 sb
->SetStatusStyles(fields
, styles
);
613 // ----------------------------------------------------------------------------
615 // ----------------------------------------------------------------------------
617 MyAboutDialog
::MyAboutDialog(wxWindow
*parent
)
618 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
619 wxDefaultPosition
, wxDefaultSize
,
620 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
622 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
623 _T("wxStatusBar sample\n")
624 _T("(c) 2000 Vadim Zeitlin"));
626 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
628 // create the top status bar without the size grip (default style),
629 // otherwise it looks weird
630 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
631 statbarTop
->SetFieldsCount(3);
632 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
633 statbarTop
->SetStatusText(_T("in a dialog"), 1);
634 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
636 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
637 statbarBottom
->SetFieldsCount(2);
638 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
639 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
641 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
642 sizerTop
->Add(statbarTop
, 0, wxGROW
);
643 sizerTop
->Add(-1, 10, 1, wxGROW
);
644 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
645 sizerTop
->Add(-1, 10, 1, wxGROW
);
646 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
647 sizerTop
->Add(-1, 10, 1, wxGROW
);
648 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
650 SetSizerAndFit(sizerTop
);
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
658 // 'this' : used in base member initializer list -- so what??
659 #pragma warning(disable: 4355)
662 MyStatusBar
::MyStatusBar(wxWindow
*parent
)
663 : wxStatusBar(parent
, wxID_ANY
)
671 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
673 SetFieldsCount(Field_Max
);
674 SetStatusWidths(Field_Max
, widths
);
677 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
678 m_checkbox
->SetValue(true);
681 #ifdef USE_STATIC_BITMAP
682 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
684 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
685 wxDefaultPosition
, wxDefaultSize
,
693 SetMinHeight(wxMax(m_statbmp
->GetBestSize().GetHeight(),
694 m_checkbox
->GetBestSize().GetHeight()));
700 #pragma warning(default: 4355)
703 MyStatusBar
::~MyStatusBar()
706 if ( m_timer
.IsRunning() )
713 #define BMP_BUTTON_SIZE_X 10
714 #define BMP_BUTTON_SIZE_Y 10
716 wxBitmap MyStatusBar
::CreateBitmapForButton(bool on
)
718 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
+1, BMP_BUTTON_SIZE_Y
+1);
720 dc
.SelectObject(bitmap
);
721 dc
.SetBrush(on ?
*wxGREEN_BRUSH
: *wxRED_BRUSH
);
722 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
724 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
725 dc
.SelectObject(wxNullBitmap
);
730 void MyStatusBar
::OnSize(wxSizeEvent
& event
)
738 GetFieldRect(Field_Checkbox
, rect
);
741 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
744 GetFieldRect(Field_Bitmap
, rect
);
745 wxSize size
= m_statbmp
->GetSize();
747 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
748 rect
.y
+ (rect
.height
- size
.y
) / 2);
753 void MyStatusBar
::OnButton(wxCommandEvent
& WXUNUSED(event
))
756 m_checkbox
->SetValue(!m_checkbox
->GetValue());
762 void MyStatusBar
::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
767 void MyStatusBar
::DoToggle()
770 if ( m_checkbox
->GetValue() )
776 #ifdef USE_STATIC_BITMAP
777 m_statbmp
->SetIcon(wxIcon(green_xpm
));
779 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
780 m_statbmp
->Refresh();
785 else // don't show clock
791 #ifdef USE_STATIC_BITMAP
792 m_statbmp
->SetIcon(wxIcon(red_xpm
));
794 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
795 m_statbmp
->Refresh();
798 SetStatusText(wxEmptyString
, Field_Clock
);
803 void MyStatusBar
::UpdateClock()
805 SetStatusText(wxDateTime
::Now().FormatTime(), Field_Clock
);