]>
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 // ----------------------------------------------------------------------------
21 #pragma implementation "statbar.cpp"
22 #pragma interface "statbar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
33 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
34 #endif // wxUSE_STATUSBAR
36 // for all others, include the necessary headers
40 #include "wx/statusbr.h"
42 #include "wx/checkbox.h"
43 #include "wx/statbmp.h"
45 #include "wx/msgdlg.h"
46 #include "wx/textdlg.h"
50 #include "wx/datetime.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Define a new application type, each program should derive a class from wxApp
64 class MyApp
: public wxApp
67 // override base class virtuals
68 // ----------------------------
70 // this one is called on application startup and is a good place for the app
71 // initialization (doing it here and not in the ctor allows to have an error
72 // return: if OnInit() returns false, the application terminates)
73 virtual bool OnInit();
76 // A custom status bar which contains controls, icons &c
77 class MyStatusBar
: public wxStatusBar
80 MyStatusBar(wxWindow
*parent
);
81 virtual ~MyStatusBar();
86 void OnTimer(wxTimerEvent
& event
) { UpdateClock(); }
87 void OnSize(wxSizeEvent
& event
);
88 void OnToggleClock(wxCommandEvent
& event
);
102 wxCheckBox
*m_checkbox
;
103 wxStaticBitmap
*m_statbmp
;
105 DECLARE_EVENT_TABLE()
108 // Define a new frame type: this is going to be our main frame
109 class MyFrame
: public wxFrame
113 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
116 // event handlers (these functions should _not_ be virtual)
117 void OnQuit(wxCommandEvent
& event
);
118 void OnAbout(wxCommandEvent
& event
);
120 void OnSetStatusFields(wxCommandEvent
& event
);
121 void OnRecreateStatusBar(wxCommandEvent
& event
);
131 void DoCreateStatusBar(StatBarKind kind
);
133 wxStatusBar
*m_statbarDefault
;
134 MyStatusBar
*m_statbarCustom
;
136 // any class wishing to process wxWindows events must use this macro
137 DECLARE_EVENT_TABLE()
140 // Our about dialog ith its status bar
141 class MyAboutDialog
: public wxDialog
144 MyAboutDialog(wxWindow
*parent
);
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 // IDs for the controls and the menu commands
159 StatusBar_Checkbox
= 1000
162 static const int BITMAP_SIZE_X
= 32;
163 static const int BITMAP_SIZE_Y
= 15;
165 // ----------------------------------------------------------------------------
166 // event tables and other macros for wxWindows
167 // ----------------------------------------------------------------------------
169 // the event tables connect the wxWindows events with the functions (event
170 // handlers) which process them. It can be also done at run-time, but for the
171 // simple menu events like this the static method is much simpler.
172 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
173 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
174 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
175 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
176 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
179 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
180 EVT_SIZE(MyStatusBar::OnSize
)
181 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
182 EVT_TIMER(-1, MyStatusBar::OnTimer
)
185 // Create a new application object: this macro will allow wxWindows to create
186 // the application object during program execution (it's better than using a
187 // static object for many reasons) and also declares the accessor function
188 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
192 // ============================================================================
194 // ============================================================================
196 // ----------------------------------------------------------------------------
197 // the application class
198 // ----------------------------------------------------------------------------
200 // `Main program' equivalent: the program execution "starts" here
203 // create the main application window
204 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
205 wxPoint(50, 50), wxSize(450, 340));
207 // and show it (the frames, unlike simple controls, are not shown when
208 // created initially)
211 // success: wxApp::OnRun() will be called which will enter the main message
212 // loop and the application will run. If we returned FALSE here, the
213 // application would exit immediately.
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
222 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
223 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
225 m_statbarDefault
= NULL
;
226 m_statbarCustom
= NULL
;
229 // we need this in order to allow the about menu relocation, since ABOUT is
230 // not the default id of the about menu
231 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
235 wxMenu
*menuFile
= new wxMenu
;
236 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
238 wxMenu
*statbarMenu
= new wxMenu
;
239 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
240 "Set the number of status bar fields");
241 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
242 "Toggle status bar format");
244 wxMenu
*helpMenu
= new wxMenu
;
245 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
247 // now append the freshly created menu to the menu bar...
248 wxMenuBar
*menuBar
= new wxMenuBar();
249 menuBar
->Append(menuFile
, "&File");
250 menuBar
->Append(statbarMenu
, "&Status bar");
251 menuBar
->Append(helpMenu
, "&Help");
253 // ... and attach this menu bar to the frame
256 // create default status bar to start with
258 SetStatusText("Welcome to wxWindows!");
260 m_statbarDefault
= GetStatusBar();
267 delete m_statbarDefault
;
268 delete m_statbarCustom
;
271 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
273 wxStatusBar
*statbarOld
= GetStatusBar();
281 case StatBar_Default
:
282 SetStatusBar(m_statbarDefault
);
286 if ( !m_statbarCustom
)
288 m_statbarCustom
= new MyStatusBar(this);
290 SetStatusBar(m_statbarCustom
);
294 wxFAIL_MSG("unknown stat bar kind");
298 GetStatusBar()->Show();
300 m_statbarKind
= kind
;
304 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
306 wxStatusBar
*sb
= GetStatusBar();
308 long nFields
= wxGetNumberFromUser
310 "Select the number of fields in the status bar",
312 "wxWindows statusbar sample",
313 sb
->GetFieldsCount(),
318 // we don't check if the number changed at all on purpose: calling
319 // SetFieldsCount() with the same number of fields should be ok
322 // we set the widths only for 2 of them, otherwise let all the fields
323 // have equal width (the default behaviour)
324 const int *widths
= NULL
;
327 static const int widthsFor2Fields
[2] = { 200, -1 };
328 widths
= widthsFor2Fields
;
331 sb
->SetFieldsCount(nFields
, widths
);
334 wxString::Format("Status bar now has %ld fields", nFields
));
338 wxLogStatus(this, "Cancelled");
342 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
344 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
348 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
350 // TRUE is to force the frame to close
354 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
356 MyAboutDialog
dlg(this);
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
365 : wxDialog(parent
, -1, wxString("About statbar"),
366 wxDefaultPosition
, wxDefaultSize
,
367 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
369 wxStaticText
*text
= new wxStaticText(this, -1,
370 "wxStatusBar sample\n"
371 "(c) 2000 Vadim Zeitlin");
373 wxStatusBar
*statbar
= new wxStatusBar(this, -1);
374 statbar
->SetFieldsCount(2);
375 statbar
->SetStatusText("This is a status bar", 0);
376 statbar
->SetStatusText("in a dialog", 1);
378 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
379 sizerTop
->Add(-1, 10, 1, wxGROW
);
380 sizerTop
->Add(text
, 0, wxCENTRE
);
381 sizerTop
->Add(-1, 10, 1, wxGROW
);
382 sizerTop
->Add(statbar
, 0, wxGROW
);
388 sizerTop
->SetSizeHints(this);
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
396 // 'this' : used in base member initializer list -- so what??
397 #pragma warning(disable: 4355)
400 MyStatusBar::MyStatusBar(wxWindow
*parent
)
401 : wxStatusBar(parent
, -1), m_timer(this)
403 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
405 SetFieldsCount(Field_Max
);
406 SetStatusWidths(Field_Max
, widths
);
408 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
409 m_checkbox
->SetValue(TRUE
);
411 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
415 SetMinHeight(BITMAP_SIZE_Y
);
421 #pragma warning(default: 4355)
424 MyStatusBar::~MyStatusBar()
426 if ( m_timer
.IsRunning() )
432 void MyStatusBar::OnSize(wxSizeEvent
& event
)
435 GetFieldRect(Field_Checkbox
, rect
);
437 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
439 GetFieldRect(Field_Bitmap
, rect
);
440 m_statbmp
->Move(rect
.x
+ (rect
.width
- BITMAP_SIZE_X
) / 2,
441 rect
.y
+ (rect
.height
- BITMAP_SIZE_Y
) / 2);
446 void MyStatusBar::OnToggleClock(wxCommandEvent
& event
)
448 if ( m_checkbox
->GetValue() )
452 m_statbmp
->SetIcon(wxIcon(green_xpm
));
456 else // don't show clock
460 m_statbmp
->SetIcon(wxIcon(red_xpm
));
462 SetStatusText("", Field_Clock
);
466 void MyStatusBar::UpdateClock()
468 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);