]>
git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
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"
52 // define this for the platforms which don't support wxBitmapButton (such as
53 // Motif), else a wxBitmapButton will be used
55 //#define USE_MDI_PARENT_FRAME 1
57 #ifdef USE_MDI_PARENT_FRAME
59 #endif // USE_MDI_PARENT_FRAME
60 #define USE_STATIC_BITMAP
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #ifdef USE_STATIC_BITMAP
70 #endif // USE_STATIC_BITMAP
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // Define a new application type, each program should derive a class from wxApp
77 class MyApp
: public wxApp
80 // override base class virtuals
81 // ----------------------------
83 // this one is called on application startup and is a good place for the app
84 // initialization (doing it here and not in the ctor allows to have an error
85 // return: if OnInit() returns false, the application terminates)
86 virtual bool OnInit();
89 // A custom status bar which contains controls, icons &c
90 class MyStatusBar
: public wxStatusBar
93 MyStatusBar(wxWindow
*parent
);
94 virtual ~MyStatusBar();
100 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
102 void OnSize(wxSizeEvent
& event
);
103 void OnToggleClock(wxCommandEvent
& event
);
104 void OnButton(wxCommandEvent
& event
);
107 // toggle the state of the status bar controls
110 wxBitmap
CreateBitmapForButton(bool on
= false);
126 wxCheckBox
*m_checkbox
;
128 #ifdef USE_STATIC_BITMAP
129 wxStaticBitmap
*m_statbmp
;
131 wxBitmapButton
*m_statbmp
;
134 DECLARE_EVENT_TABLE()
137 // Define a new frame type: this is going to be our main frame
138 class MyFrame
: public wxFrame
142 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
143 #ifdef USE_MDI_PARENT_FRAME
144 class MyFrame
: public wxMDIParentFrame
149 // event handlers (these functions should _not_ be virtual)
150 void OnQuit(wxCommandEvent
& event
);
151 void OnAbout(wxCommandEvent
& event
);
153 void OnSetStatusFields(wxCommandEvent
& event
);
154 void OnRecreateStatusBar(wxCommandEvent
& event
);
155 void OnSetStyleNormal(wxCommandEvent
& event
);
156 void OnSetStyleFlat(wxCommandEvent
& event
);
157 void OnSetStyleRaised(wxCommandEvent
& event
);
166 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
167 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
168 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
169 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
170 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
171 void OnStatusBarToggle(wxCommandEvent
& event
);
172 void DoCreateStatusBar(StatBarKind kind
);
175 wxStatusBar
*m_statbarDefault
;
176 MyStatusBar
*m_statbarCustom
;
180 // any class wishing to process wxWidgets events must use this macro
181 DECLARE_EVENT_TABLE()
184 // Our about dialog ith its status bar
185 class MyAboutDialog
: public wxDialog
188 MyAboutDialog(wxWindow
*parent
);
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 // IDs for the controls and the menu commands
204 StatusBar_Checkbox
= 1000,
206 StatusBar_SetStyleNormal
,
207 StatusBar_SetStyleFlat
,
208 StatusBar_SetStyleRaised
211 static const int BITMAP_SIZE_X
= 32;
212 static const int BITMAP_SIZE_Y
= 15;
214 // ----------------------------------------------------------------------------
215 // event tables and other macros for wxWidgets
216 // ----------------------------------------------------------------------------
218 // the event tables connect the wxWidgets events with the functions (event
219 // handlers) which process them. It can be also done at run-time, but for the
220 // simple menu events like this the static method is much simpler.
221 #ifdef USE_MDI_PARENT_FRAME
222 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
224 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
226 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
227 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
228 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
229 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
230 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
231 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame::OnSetStyleNormal
)
232 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame::OnSetStyleFlat
)
233 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame::OnSetStyleRaised
)
234 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
235 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
236 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame::OnUpdateSetStyleNormal
)
237 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame::OnUpdateSetStyleFlat
)
238 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame::OnUpdateSetStyleRaised
)
241 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
242 EVT_SIZE(MyStatusBar::OnSize
)
244 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
246 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
248 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
252 // Create a new application object: this macro will allow wxWidgets to create
253 // the application object during program execution (it's better than using a
254 // static object for many reasons) and also declares the accessor function
255 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
259 // ============================================================================
261 // ============================================================================
263 // ----------------------------------------------------------------------------
264 // the application class
265 // ----------------------------------------------------------------------------
267 // `Main program' equivalent: the program execution "starts" here
270 // create the main application window
271 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
272 wxPoint(50, 50), wxSize(450, 340));
274 // and show it (the frames, unlike simple controls, are not shown when
275 // created initially)
278 // success: wxApp::OnRun() will be called which will enter the main message
279 // loop and the application will run. If we returned 'false' here, the
280 // application would exit immediately.
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
289 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
290 #ifdef USE_MDI_PARENT_FRAME
291 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
293 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
296 m_statbarDefault
= NULL
;
297 m_statbarCustom
= NULL
;
299 m_statbarStyle
= wxSB_NORMAL
;
302 // we need this in order to allow the about menu relocation, since ABOUT is
303 // not the default id of the about menu
304 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
308 wxMenu
*menuFile
= new wxMenu
;
309 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
311 wxMenu
*statbarMenu
= new wxMenu
;
312 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
313 _T("Set the number of status bar fields"));
314 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
315 _T("Toggle the status bar display"), true);
316 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
317 _T("Toggle status bar format"));
319 wxMenu
*statbarStyleMenu
= new wxMenu
;
320 statbarStyleMenu
->Append(StatusBar_SetStyleNormal
, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
321 statbarStyleMenu
->Append(StatusBar_SetStyleFlat
, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
322 statbarStyleMenu
->Append(StatusBar_SetStyleRaised
, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
323 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
325 wxMenu
*helpMenu
= new wxMenu
;
326 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
328 // now append the freshly created menu to the menu bar...
329 wxMenuBar
*menuBar
= new wxMenuBar();
330 menuBar
->Append(menuFile
, _T("&File"));
331 menuBar
->Append(statbarMenu
, _T("&Status bar"));
332 menuBar
->Append(helpMenu
, _T("&Help"));
334 // ... and attach this menu bar to the frame
337 // create default status bar to start with
339 m_statbarKind
= StatBar_Default
;
340 SetStatusText(_T("Welcome to wxWidgets!"));
342 m_statbarDefault
= GetStatusBar();
349 delete m_statbarDefault
;
350 delete m_statbarCustom
;
353 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
355 wxStatusBar
*statbarOld
= GetStatusBar();
363 case StatBar_Default
:
364 SetStatusBar(m_statbarDefault
);
368 if ( !m_statbarCustom
)
370 m_statbarCustom
= new MyStatusBar(this);
372 SetStatusBar(m_statbarCustom
);
376 wxFAIL_MSG(wxT("unknown stat bar kind"));
380 GetStatusBar()->Show();
383 m_statbarKind
= kind
;
386 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
388 // only allow the settings of the number of status fields for the default
390 wxStatusBar
*sb
= GetStatusBar();
391 event
.Enable(sb
== m_statbarDefault
);
395 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
397 wxStatusBar
*sb
= GetStatusBar();
399 long nFields
= wxGetNumberFromUser
401 _T("Select the number of fields in the status bar"),
403 _T("wxWidgets statusbar sample"),
404 sb
->GetFieldsCount(),
409 // we don't check if the number changed at all on purpose: calling
410 // SetFieldsCount() with the same number of fields should be ok
413 static const int widthsFor2Fields
[] = { 200, -1 };
414 static const int widthsFor3Fields
[] = { -1, -2, -1 };
415 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
417 static const int *widthsAll
[] =
419 NULL
, // 1 field: default
420 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
421 widthsFor3Fields
, // 3 fields: 3 var
422 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
423 NULL
// 5 fields: default (all have same width)
426 const int * const widths
= widthsAll
[nFields
- 1];
427 sb
->SetFieldsCount(nFields
, widths
);
430 for ( long n
= 0; n
< nFields
; n
++ )
435 s
.Printf(_T("fixed (%d)"), widths
[n
]);
437 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
449 wxLogStatus(this, wxT("Cancelled"));
453 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
455 event
.Check(GetStatusBar() != NULL
);
458 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
460 wxStatusBar
*statbarOld
= GetStatusBar();
468 DoCreateStatusBar(m_statbarKind
);
472 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
474 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
478 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
480 // true is to force the frame to close
484 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
486 MyAboutDialog
dlg(this);
490 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
492 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
495 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
497 event
.Check(m_statbarStyle
== wxSB_FLAT
);
500 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
502 event
.Check(m_statbarStyle
== wxSB_RAISED
);
505 void MyFrame::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
507 m_statbarStyle
= wxSB_NORMAL
;
511 void MyFrame::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
513 m_statbarStyle
= wxSB_FLAT
;
517 void MyFrame::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
519 m_statbarStyle
= wxSB_RAISED
;
523 void MyFrame::ApplyStyle()
525 wxStatusBar
*sb
= GetStatusBar();
526 int fields
= sb
->GetFieldsCount();
527 int *styles
= new int[fields
];
529 for (int i
= 1; i
< fields
; i
++)
530 styles
[i
] = wxSB_NORMAL
;
532 styles
[0] = m_statbarStyle
;
534 sb
->SetStatusStyles(fields
, styles
);
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
544 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
545 wxDefaultPosition
, wxDefaultSize
,
546 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
548 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
549 _T("wxStatusBar sample\n")
550 _T("(c) 2000 Vadim Zeitlin"));
552 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
554 // create the top status bar without the size grip (default style),
555 // otherwise it looks weird
556 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
557 statbarTop
->SetFieldsCount(3);
558 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
559 statbarTop
->SetStatusText(_T("in a dialog"), 1);
560 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
562 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
563 statbarBottom
->SetFieldsCount(2);
564 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
565 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
567 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
568 sizerTop
->Add(statbarTop
, 0, wxGROW
);
569 sizerTop
->Add(-1, 10, 1, wxGROW
);
570 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
571 sizerTop
->Add(-1, 10, 1, wxGROW
);
572 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
573 sizerTop
->Add(-1, 10, 1, wxGROW
);
574 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
579 sizerTop
->SetSizeHints(this);
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
587 // 'this' : used in base member initializer list -- so what??
588 #pragma warning(disable: 4355)
591 MyStatusBar::MyStatusBar(wxWindow
*parent
)
592 : wxStatusBar(parent
, wxID_ANY
)
600 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
602 SetFieldsCount(Field_Max
);
603 SetStatusWidths(Field_Max
, widths
);
606 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
607 m_checkbox
->SetValue(true);
610 #ifdef USE_STATIC_BITMAP
611 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
613 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
614 wxDefaultPosition
, wxDefaultSize
,
622 SetMinHeight(BITMAP_SIZE_Y
);
628 #pragma warning(default: 4355)
631 MyStatusBar::~MyStatusBar()
634 if ( m_timer
.IsRunning() )
641 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
643 static const int BMP_BUTTON_SIZE_X
= 10;
644 static const int BMP_BUTTON_SIZE_Y
= 9;
646 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
648 dc
.SelectObject(bitmap
);
649 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
650 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
652 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
653 dc
.SelectObject(wxNullBitmap
);
658 void MyStatusBar::OnSize(wxSizeEvent
& event
)
666 GetFieldRect(Field_Checkbox
, rect
);
669 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
672 GetFieldRect(Field_Bitmap
, rect
);
673 wxSize size
= m_statbmp
->GetSize();
675 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
676 rect
.y
+ (rect
.height
- size
.y
) / 2);
681 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
684 m_checkbox
->SetValue(!m_checkbox
->GetValue());
690 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
695 void MyStatusBar::DoToggle()
698 if ( m_checkbox
->GetValue() )
704 #ifdef USE_STATIC_BITMAP
705 m_statbmp
->SetIcon(wxIcon(green_xpm
));
707 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
708 m_statbmp
->Refresh();
713 else // don't show clock
719 #ifdef USE_STATIC_BITMAP
720 m_statbmp
->SetIcon(wxIcon(red_xpm
));
722 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
723 m_statbmp
->Refresh();
726 SetStatusText(wxEmptyString
, Field_Clock
);
731 void MyStatusBar::UpdateClock()
733 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);