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 #include "../sample.xpm"
57 // define this for the platforms which don't support wxBitmapButton (such as
58 // Motif), else a wxBitmapButton will be used
60 #define USE_STATIC_BITMAP
63 //#define USE_MDI_PARENT_FRAME 1
65 #ifdef USE_MDI_PARENT_FRAME
67 #endif // USE_MDI_PARENT_FRAME
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 #ifdef USE_STATIC_BITMAP
77 #endif // USE_STATIC_BITMAP
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // Define a new application type, each program should derive a class from wxApp
84 class MyApp
: public wxApp
87 // override base class virtuals
88 // ----------------------------
90 // this one is called on application startup and is a good place for the app
91 // initialization (doing it here and not in the ctor allows to have an error
92 // return: if OnInit() returns false, the application terminates)
93 virtual bool OnInit();
96 // A custom status bar which contains controls, icons &c
97 class MyStatusBar
: public wxStatusBar
100 MyStatusBar(wxWindow
*parent
);
101 virtual ~MyStatusBar();
107 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
109 void OnSize(wxSizeEvent
& event
);
110 void OnToggleClock(wxCommandEvent
& event
);
111 void OnButton(wxCommandEvent
& event
);
114 // toggle the state of the status bar controls
117 wxBitmap
CreateBitmapForButton(bool on
= false);
133 wxCheckBox
*m_checkbox
;
135 #ifdef USE_STATIC_BITMAP
136 wxStaticBitmap
*m_statbmp
;
138 wxBitmapButton
*m_statbmp
;
141 DECLARE_EVENT_TABLE()
144 // Define a new frame type: this is going to be our main frame
145 class MyFrame
: public wxFrame
149 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
150 #ifdef USE_MDI_PARENT_FRAME
151 class MyFrame
: public wxMDIParentFrame
156 // event handlers (these functions should _not_ be virtual)
157 void OnQuit(wxCommandEvent
& event
);
158 void OnAbout(wxCommandEvent
& event
);
160 void OnResetFieldsWidth(wxCommandEvent
& event
);
161 void OnSetStatusFields(wxCommandEvent
& event
);
162 void OnSetStatusTexts(wxCommandEvent
& event
);
163 void OnRecreateStatusBar(wxCommandEvent
& event
);
164 void OnSetStyleNormal(wxCommandEvent
& event
);
165 void OnSetStyleFlat(wxCommandEvent
& event
);
166 void OnSetStyleRaised(wxCommandEvent
& event
);
177 void OnUpdateForDefaultStatusbar(wxUpdateUIEvent
& event
);
178 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
179 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
180 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
181 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
182 void OnStatusBarToggle(wxCommandEvent
& event
);
183 void DoCreateStatusBar(StatBarKind kind
);
186 wxStatusBar
*m_statbarDefault
;
187 MyStatusBar
*m_statbarCustom
;
191 // any class wishing to process wxWidgets events must use this macro
192 DECLARE_EVENT_TABLE()
195 // Our about dialog ith its status bar
196 class MyAboutDialog
: public wxDialog
199 MyAboutDialog(wxWindow
*parent
);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 // IDs for the controls and the menu commands
214 StatusBar_ResetFieldsWidth
,
219 StatusBar_Checkbox
= 1000,
221 StatusBar_SetStyleNormal
,
222 StatusBar_SetStyleFlat
,
223 StatusBar_SetStyleRaised
226 static const int BITMAP_SIZE_X
= 32;
227 static const int BITMAP_SIZE_Y
= 15;
229 // ----------------------------------------------------------------------------
230 // event tables and other macros for wxWidgets
231 // ----------------------------------------------------------------------------
233 // the event tables connect the wxWidgets events with the functions (event
234 // handlers) which process them. It can be also done at run-time, but for the
235 // simple menu events like this the static method is much simpler.
236 #ifdef USE_MDI_PARENT_FRAME
237 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
239 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
241 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
242 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
243 EVT_MENU(StatusBar_SetTexts
, MyFrame::OnSetStatusTexts
)
244 EVT_MENU(StatusBar_ResetFieldsWidth
, MyFrame::OnResetFieldsWidth
)
245 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
246 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
247 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
248 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame::OnSetStyleNormal
)
249 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame::OnSetStyleFlat
)
250 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame::OnSetStyleRaised
)
252 EVT_UPDATE_UI_RANGE(StatusBar_SetFields
, StatusBar_ResetFieldsWidth
,
253 MyFrame::OnUpdateForDefaultStatusbar
)
254 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
255 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame::OnUpdateSetStyleNormal
)
256 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame::OnUpdateSetStyleFlat
)
257 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame::OnUpdateSetStyleRaised
)
260 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
261 EVT_SIZE(MyStatusBar::OnSize
)
263 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
265 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
267 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
271 // Create a new application object: this macro will allow wxWidgets to create
272 // the application object during program execution (it's better than using a
273 // static object for many reasons) and also declares the accessor function
274 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
278 // ============================================================================
280 // ============================================================================
282 // ----------------------------------------------------------------------------
283 // the application class
284 // ----------------------------------------------------------------------------
286 // `Main program' equivalent: the program execution "starts" here
289 if ( !wxApp::OnInit() )
292 // create the main application window
293 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
294 wxPoint(50, 50), wxSize(450, 340));
296 // and show it (the frames, unlike simple controls, are not shown when
297 // created initially)
300 // success: wxApp::OnRun() will be called which will enter the main message
301 // loop and the application will run. If we returned 'false' here, the
302 // application would exit immediately.
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
311 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
312 #ifdef USE_MDI_PARENT_FRAME
313 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
315 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
318 SetIcon(wxICON(sample
));
320 m_statbarDefault
= NULL
;
321 m_statbarCustom
= NULL
;
323 m_statbarStyle
= wxSB_NORMAL
;
326 // we need this in order to allow the about menu relocation, since ABOUT is
327 // not the default id of the about menu
328 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
332 wxMenu
*menuFile
= new wxMenu
;
333 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
335 wxMenu
*statbarMenu
= new wxMenu
;
336 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
337 _T("Set the number of status bar fields"));
338 statbarMenu
->Append(StatusBar_SetTexts
, _T("&Set field text\tCtrl-T"),
339 _T("Set the text to display for each status bar field"));
341 wxMenu
*statbarStyleMenu
= new wxMenu
;
342 statbarStyleMenu
->Append(StatusBar_SetStyleNormal
, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
343 statbarStyleMenu
->Append(StatusBar_SetStyleFlat
, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
344 statbarStyleMenu
->Append(StatusBar_SetStyleRaised
, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
346 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
348 statbarMenu
->Append(StatusBar_ResetFieldsWidth
, _T("Reset field widths"),
349 _T("Sets all fields to the same width"));
350 statbarMenu
->AppendSeparator();
352 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
353 _T("Toggle the status bar display"), true);
354 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
355 _T("Toggle status bar format"));
357 wxMenu
*helpMenu
= new wxMenu
;
358 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
360 // now append the freshly created menu to the menu bar...
361 wxMenuBar
*menuBar
= new wxMenuBar();
362 menuBar
->Append(menuFile
, _T("&File"));
363 menuBar
->Append(statbarMenu
, _T("&Status bar"));
364 menuBar
->Append(helpMenu
, _T("&Help"));
366 // ... and attach this menu bar to the frame
369 // create default status bar to start with
371 m_statbarKind
= StatBar_Default
;
372 SetStatusText(_T("Welcome to wxWidgets!"));
374 m_statbarDefault
= GetStatusBar();
381 delete m_statbarDefault
;
382 delete m_statbarCustom
;
385 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
387 wxStatusBar
*statbarOld
= GetStatusBar();
395 case StatBar_Default
:
396 SetStatusBar(m_statbarDefault
);
400 if ( !m_statbarCustom
)
402 m_statbarCustom
= new MyStatusBar(this);
404 SetStatusBar(m_statbarCustom
);
408 wxFAIL_MSG(wxT("unknown stat bar kind"));
412 GetStatusBar()->Show();
415 m_statbarKind
= kind
;
419 // ----------------------------------------------------------------------------
420 // main frame - event handlers
421 // ----------------------------------------------------------------------------
423 void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent
& event
)
425 // only allow this feature for the default status bar
426 wxStatusBar
*sb
= GetStatusBar();
427 event
.Enable(sb
== m_statbarDefault
);
430 void MyFrame::OnSetStatusTexts(wxCommandEvent
& WXUNUSED(event
))
432 wxStatusBar
*sb
= GetStatusBar();
435 for (int i
=0; i
<sb
->GetFieldsCount(); i
++)
438 wxGetTextFromUser(wxString::Format("Enter the text for the %d-th field:", i
+1),
439 "Input field text", "A dummy test string", this);
441 sb
->SetStatusText(txt
, i
);
445 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
447 wxStatusBar
*sb
= GetStatusBar();
449 long nFields
= wxGetNumberFromUser
451 _T("Select the number of fields in the status bar"),
453 _T("wxWidgets statusbar sample"),
454 sb
->GetFieldsCount(),
459 // we don't check if the number changed at all on purpose: calling
460 // SetFieldsCount() with the same number of fields should be ok
463 static const int widthsFor2Fields
[] = { 200, -1 };
464 static const int widthsFor3Fields
[] = { -1, -2, -1 };
465 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
467 static const int *widthsAll
[] =
469 NULL
, // 1 field: default
470 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
471 widthsFor3Fields
, // 3 fields: 3 var
472 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
473 NULL
// 5 fields: default (all have same width)
476 const int * const widths
= widthsAll
[nFields
- 1];
477 sb
->SetFieldsCount(nFields
, widths
);
480 for ( long n
= 0; n
< nFields
; n
++ )
485 s
.Printf(_T("fixed (%d)"), widths
[n
]);
487 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
499 wxLogStatus(this, wxT("Cancelled"));
503 void MyFrame::OnResetFieldsWidth(wxCommandEvent
& WXUNUSED(event
))
505 wxStatusBar
*pStat
= GetStatusBar();
508 int n
= pStat
->GetFieldsCount();
509 pStat
->SetStatusWidths(n
, NULL
);
510 for (int i
=0; i
<n
; i
++)
511 pStat
->SetStatusText("same size", i
);
515 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
517 event
.Check(GetStatusBar() != NULL
);
520 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
522 wxStatusBar
*statbarOld
= GetStatusBar();
530 DoCreateStatusBar(m_statbarKind
);
534 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
536 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
540 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
542 // true is to force the frame to close
546 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
548 MyAboutDialog
dlg(this);
552 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
554 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
557 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
559 event
.Check(m_statbarStyle
== wxSB_FLAT
);
562 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
564 event
.Check(m_statbarStyle
== wxSB_RAISED
);
567 void MyFrame::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
569 m_statbarStyle
= wxSB_NORMAL
;
573 void MyFrame::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
575 m_statbarStyle
= wxSB_FLAT
;
579 void MyFrame::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
581 m_statbarStyle
= wxSB_RAISED
;
585 void MyFrame::ApplyStyle()
587 wxStatusBar
*sb
= GetStatusBar();
588 int fields
= sb
->GetFieldsCount();
589 int *styles
= new int[fields
];
591 for (int i
= 1; i
< fields
; i
++)
592 styles
[i
] = wxSB_NORMAL
;
594 styles
[0] = m_statbarStyle
;
596 sb
->SetStatusStyles(fields
, styles
);
601 // ----------------------------------------------------------------------------
603 // ----------------------------------------------------------------------------
605 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
606 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
607 wxDefaultPosition
, wxDefaultSize
,
608 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
610 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
611 _T("wxStatusBar sample\n")
612 _T("(c) 2000 Vadim Zeitlin"));
614 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
616 // create the top status bar without the size grip (default style),
617 // otherwise it looks weird
618 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
619 statbarTop
->SetFieldsCount(3);
620 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
621 statbarTop
->SetStatusText(_T("in a dialog"), 1);
622 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
624 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
625 statbarBottom
->SetFieldsCount(2);
626 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
627 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
629 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
630 sizerTop
->Add(statbarTop
, 0, wxGROW
);
631 sizerTop
->Add(-1, 10, 1, wxGROW
);
632 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
633 sizerTop
->Add(-1, 10, 1, wxGROW
);
634 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
635 sizerTop
->Add(-1, 10, 1, wxGROW
);
636 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
638 SetSizerAndFit(sizerTop
);
641 // ----------------------------------------------------------------------------
643 // ----------------------------------------------------------------------------
646 // 'this' : used in base member initializer list -- so what??
647 #pragma warning(disable: 4355)
650 MyStatusBar::MyStatusBar(wxWindow
*parent
)
651 : wxStatusBar(parent
, wxID_ANY
)
659 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
661 SetFieldsCount(Field_Max
);
662 SetStatusWidths(Field_Max
, widths
);
665 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
666 m_checkbox
->SetValue(true);
669 #ifdef USE_STATIC_BITMAP
670 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
672 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
673 wxDefaultPosition
, wxDefaultSize
,
681 SetMinHeight(wxMax(m_statbmp
->GetBestSize().GetHeight(),
682 m_checkbox
->GetBestSize().GetHeight()));
688 #pragma warning(default: 4355)
691 MyStatusBar::~MyStatusBar()
694 if ( m_timer
.IsRunning() )
701 #define BMP_BUTTON_SIZE_X 10
702 #define BMP_BUTTON_SIZE_Y 10
704 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
706 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
+1, BMP_BUTTON_SIZE_Y
+1);
708 dc
.SelectObject(bitmap
);
709 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
710 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
712 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
713 dc
.SelectObject(wxNullBitmap
);
718 void MyStatusBar::OnSize(wxSizeEvent
& event
)
726 if (!GetFieldRect(Field_Checkbox
, rect
))
733 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
736 GetFieldRect(Field_Bitmap
, rect
);
737 wxSize size
= m_statbmp
->GetSize();
739 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
740 rect
.y
+ (rect
.height
- size
.y
) / 2);
745 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
748 m_checkbox
->SetValue(!m_checkbox
->GetValue());
754 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
759 void MyStatusBar::DoToggle()
762 if ( m_checkbox
->GetValue() )
768 #ifdef USE_STATIC_BITMAP
769 m_statbmp
->SetIcon(wxIcon(green_xpm
));
771 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
772 m_statbmp
->Refresh();
777 else // don't show clock
783 #ifdef USE_STATIC_BITMAP
784 m_statbmp
->SetIcon(wxIcon(red_xpm
));
786 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
787 m_statbmp
->Refresh();
790 SetStatusText(wxEmptyString
, Field_Clock
);
795 void MyStatusBar::UpdateClock()
797 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);