]>
git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
c6c1ddb2be0b65e3c661a173df7b7cc4c72782ea
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();
99 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
100 void OnSize(wxSizeEvent
& event
);
101 void OnToggleClock(wxCommandEvent
& event
);
102 void OnButton(wxCommandEvent
& event
);
105 // toggle the state of the status bar controls
108 wxBitmap
CreateBitmapForButton(bool on
= false);
121 wxCheckBox
*m_checkbox
;
122 #ifdef USE_STATIC_BITMAP
123 wxStaticBitmap
*m_statbmp
;
125 wxBitmapButton
*m_statbmp
;
128 DECLARE_EVENT_TABLE()
131 // Define a new frame type: this is going to be our main frame
132 class MyFrame
: public wxFrame
136 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
137 #ifdef USE_MDI_PARENT_FRAME
138 class MyFrame
: public wxMDIParentFrame
143 // event handlers (these functions should _not_ be virtual)
144 void OnQuit(wxCommandEvent
& event
);
145 void OnAbout(wxCommandEvent
& event
);
147 void OnSetStatusFields(wxCommandEvent
& event
);
148 void OnRecreateStatusBar(wxCommandEvent
& event
);
157 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
158 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
159 void OnStatusBarToggle(wxCommandEvent
& event
);
160 void DoCreateStatusBar(StatBarKind kind
);
162 wxStatusBar
*m_statbarDefault
;
163 MyStatusBar
*m_statbarCustom
;
165 // any class wishing to process wxWidgets events must use this macro
166 DECLARE_EVENT_TABLE()
169 // Our about dialog ith its status bar
170 class MyAboutDialog
: public wxDialog
173 MyAboutDialog(wxWindow
*parent
);
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 // IDs for the controls and the menu commands
189 StatusBar_Checkbox
= 1000
192 static const int BITMAP_SIZE_X
= 32;
193 static const int BITMAP_SIZE_Y
= 15;
195 // ----------------------------------------------------------------------------
196 // event tables and other macros for wxWidgets
197 // ----------------------------------------------------------------------------
199 // the event tables connect the wxWidgets events with the functions (event
200 // handlers) which process them. It can be also done at run-time, but for the
201 // simple menu events like this the static method is much simpler.
202 #ifdef USE_MDI_PARENT_FRAME
203 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
205 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
207 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
208 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
209 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
210 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
211 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
212 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
213 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
216 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
217 EVT_SIZE(MyStatusBar::OnSize
)
218 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
219 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
220 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
223 // Create a new application object: this macro will allow wxWidgets to create
224 // the application object during program execution (it's better than using a
225 // static object for many reasons) and also declares the accessor function
226 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
230 // ============================================================================
232 // ============================================================================
234 // ----------------------------------------------------------------------------
235 // the application class
236 // ----------------------------------------------------------------------------
238 // `Main program' equivalent: the program execution "starts" here
241 // create the main application window
242 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
243 wxPoint(50, 50), wxSize(450, 340));
245 // and show it (the frames, unlike simple controls, are not shown when
246 // created initially)
249 // success: wxApp::OnRun() will be called which will enter the main message
250 // loop and the application will run. If we returned 'false' here, the
251 // application would exit immediately.
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
260 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
261 #ifdef USE_MDI_PARENT_FRAME
262 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
264 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
267 m_statbarDefault
= NULL
;
268 m_statbarCustom
= NULL
;
271 // we need this in order to allow the about menu relocation, since ABOUT is
272 // not the default id of the about menu
273 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
277 wxMenu
*menuFile
= new wxMenu
;
278 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
280 wxMenu
*statbarMenu
= new wxMenu
;
281 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
282 _T("Set the number of status bar fields"));
283 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
284 _T("Toggle the status bar display"), true);
285 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
286 _T("Toggle status bar format"));
288 wxMenu
*helpMenu
= new wxMenu
;
289 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
291 // now append the freshly created menu to the menu bar...
292 wxMenuBar
*menuBar
= new wxMenuBar();
293 menuBar
->Append(menuFile
, _T("&File"));
294 menuBar
->Append(statbarMenu
, _T("&Status bar"));
295 menuBar
->Append(helpMenu
, _T("&Help"));
297 // ... and attach this menu bar to the frame
300 // create default status bar to start with
302 m_statbarKind
= StatBar_Default
;
303 SetStatusText(_T("Welcome to wxWidgets!"));
305 m_statbarDefault
= GetStatusBar();
312 delete m_statbarDefault
;
313 delete m_statbarCustom
;
316 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
318 wxStatusBar
*statbarOld
= GetStatusBar();
326 case StatBar_Default
:
327 SetStatusBar(m_statbarDefault
);
331 if ( !m_statbarCustom
)
333 m_statbarCustom
= new MyStatusBar(this);
335 SetStatusBar(m_statbarCustom
);
339 wxFAIL_MSG(wxT("unknown stat bar kind"));
342 GetStatusBar()->Show();
345 m_statbarKind
= kind
;
348 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
350 // only allow the setting of the number of status fields for the default
352 wxStatusBar
*sb
= GetStatusBar();
353 event
.Enable(sb
== m_statbarDefault
);
358 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
360 wxStatusBar
*sb
= GetStatusBar();
362 long nFields
= wxGetNumberFromUser
364 _T("Select the number of fields in the status bar"),
366 _T("wxWidgets statusbar sample"),
367 sb
->GetFieldsCount(),
372 // we don't check if the number changed at all on purpose: calling
373 // SetFieldsCount() with the same number of fields should be ok
376 static const int widthsFor2Fields
[] = { 200, -1 };
377 static const int widthsFor3Fields
[] = { -1, -2, -1 };
378 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
380 static const int *widthsAll
[] =
382 NULL
, // 1 field: default
383 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
384 widthsFor3Fields
, // 3 fields: 3 var
385 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
386 NULL
// 5 fields: default (all have same width)
389 const int * const widths
= widthsAll
[nFields
- 1];
390 sb
->SetFieldsCount(nFields
, widths
);
393 for ( long n
= 0; n
< nFields
; n
++ )
398 s
.Printf(_T("fixed (%d)"), widths
[n
]);
400 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
412 wxLogStatus(this, wxT("Cancelled"));
416 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
418 event
.Check(GetStatusBar() != 0);
421 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
423 wxStatusBar
*statbarOld
= GetStatusBar();
431 DoCreateStatusBar(m_statbarKind
);
434 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWidgets
435 // authors) while we look for a proper fix..
440 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
442 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
446 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
448 // true is to force the frame to close
452 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
454 MyAboutDialog
dlg(this);
458 // ----------------------------------------------------------------------------
460 // ----------------------------------------------------------------------------
462 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
463 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
464 wxDefaultPosition
, wxDefaultSize
,
465 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
467 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
468 _T("wxStatusBar sample\n")
469 _T("(c) 2000 Vadim Zeitlin"));
471 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
473 // create the top status bar without the size grip (default style),
474 // otherwise it looks weird
475 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
476 statbarTop
->SetFieldsCount(3);
477 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
478 statbarTop
->SetStatusText(_T("in a dialog"), 1);
479 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
481 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
482 statbarBottom
->SetFieldsCount(2);
483 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
484 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
486 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
487 sizerTop
->Add(statbarTop
, 0, wxGROW
);
488 sizerTop
->Add(-1, 10, 1, wxGROW
);
489 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
490 sizerTop
->Add(-1, 10, 1, wxGROW
);
491 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
492 sizerTop
->Add(-1, 10, 1, wxGROW
);
493 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
499 sizerTop
->SetSizeHints(this);
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
507 // 'this' : used in base member initializer list -- so what??
508 #pragma warning(disable: 4355)
511 MyStatusBar::MyStatusBar(wxWindow
*parent
)
512 : wxStatusBar(parent
, wxID_ANY
), m_timer(this), m_checkbox(NULL
)
514 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
516 SetFieldsCount(Field_Max
);
517 SetStatusWidths(Field_Max
, widths
);
519 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
520 m_checkbox
->SetValue(true);
522 #ifdef USE_STATIC_BITMAP
523 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
525 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
526 wxDefaultPosition
, wxDefaultSize
,
532 SetMinHeight(BITMAP_SIZE_Y
);
538 #pragma warning(default: 4355)
541 MyStatusBar::~MyStatusBar()
543 if ( m_timer
.IsRunning() )
549 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
551 static const int BMP_BUTTON_SIZE_X
= 10;
552 static const int BMP_BUTTON_SIZE_Y
= 9;
554 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
556 dc
.SelectObject(bitmap
);
557 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
558 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
560 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
561 dc
.SelectObject(wxNullBitmap
);
566 void MyStatusBar::OnSize(wxSizeEvent
& event
)
572 GetFieldRect(Field_Checkbox
, rect
);
574 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
576 GetFieldRect(Field_Bitmap
, rect
);
577 wxSize size
= m_statbmp
->GetSize();
579 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
580 rect
.y
+ (rect
.height
- size
.y
) / 2);
585 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
587 m_checkbox
->SetValue(!m_checkbox
->GetValue());
592 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
597 void MyStatusBar::DoToggle()
599 if ( m_checkbox
->GetValue() )
603 #ifdef USE_STATIC_BITMAP
604 m_statbmp
->SetIcon(wxIcon(green_xpm
));
606 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
607 m_statbmp
->Refresh();
612 else // don't show clock
616 #ifdef USE_STATIC_BITMAP
617 m_statbmp
->SetIcon(wxIcon(red_xpm
));
619 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
620 m_statbmp
->Refresh();
623 SetStatusText(_T(""), Field_Clock
);
627 void MyStatusBar::UpdateClock()
629 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);