]>
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 (this file is usually all you
37 // need because it includes almost all "standard" wxWindows headers
41 #include "wx/statusbr.h"
43 #include "wx/checkbox.h"
44 #include "wx/statbmp.h"
46 #include "wx/msgdlg.h"
47 #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 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 // IDs for the controls and the menu commands
152 StatusBar_Checkbox
= 1000
155 static const int BITMAP_SIZE_X
= 32;
156 static const int BITMAP_SIZE_Y
= 15;
158 // ----------------------------------------------------------------------------
159 // event tables and other macros for wxWindows
160 // ----------------------------------------------------------------------------
162 // the event tables connect the wxWindows events with the functions (event
163 // handlers) which process them. It can be also done at run-time, but for the
164 // simple menu events like this the static method is much simpler.
165 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
166 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
167 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
168 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
169 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
172 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
173 EVT_SIZE(MyStatusBar::OnSize
)
174 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
175 EVT_TIMER(-1, MyStatusBar::OnTimer
)
178 // Create a new application object: this macro will allow wxWindows to create
179 // the application object during program execution (it's better than using a
180 // static object for many reasons) and also declares the accessor function
181 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
185 // ============================================================================
187 // ============================================================================
189 // ----------------------------------------------------------------------------
190 // the application class
191 // ----------------------------------------------------------------------------
193 // `Main program' equivalent: the program execution "starts" here
196 // create the main application window
197 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
198 wxPoint(50, 50), wxSize(450, 340));
200 // and show it (the frames, unlike simple controls, are not shown when
201 // created initially)
204 // success: wxApp::OnRun() will be called which will enter the main message
205 // loop and the application will run. If we returned FALSE here, the
206 // application would exit immediately.
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
215 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
216 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
218 m_statbarDefault
= NULL
;
219 m_statbarCustom
= NULL
;
222 // we need this in order to allow the about menu relocation, since ABOUT is
223 // not the default id of the about menu
224 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
228 wxMenu
*menuFile
= new wxMenu
;
229 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
231 wxMenu
*statbarMenu
= new wxMenu
;
232 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
233 "Set the number of status bar fields");
234 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
235 "Toggle status bar format");
237 wxMenu
*helpMenu
= new wxMenu
;
238 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
240 // now append the freshly created menu to the menu bar...
241 wxMenuBar
*menuBar
= new wxMenuBar();
242 menuBar
->Append(menuFile
, "&File");
243 menuBar
->Append(statbarMenu
, "&Status bar");
244 menuBar
->Append(helpMenu
, "&Help");
246 // ... and attach this menu bar to the frame
249 // create default status bar to start with
251 SetStatusText("Welcome to wxWindows!");
253 m_statbarDefault
= GetStatusBar();
260 delete m_statbarDefault
;
261 delete m_statbarCustom
;
264 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
266 wxStatusBar
*statbarOld
= GetStatusBar();
274 case StatBar_Default
:
275 SetStatusBar(m_statbarDefault
);
279 if ( !m_statbarCustom
)
281 m_statbarCustom
= new MyStatusBar(this);
283 SetStatusBar(m_statbarCustom
);
287 wxFAIL_MSG("unknown stat bar kind");
291 GetStatusBar()->Show();
293 m_statbarKind
= kind
;
297 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
299 wxStatusBar
*sb
= GetStatusBar();
301 long nFields
= wxGetNumberFromUser
303 "Select the number of fields in the status bar",
305 "wxWindows statusbar sample",
306 sb
->GetFieldsCount(),
311 // we don't check if the number changed at all on purpose: calling
312 // SetFieldsCount() with the same number of fields should be ok
315 // we set the widths only for 2 of them, otherwise let all the fields
316 // have equal width (the default behaviour)
317 const int *widths
= NULL
;
320 static const int widthsFor2Fields
[2] = { 200, -1 };
321 widths
= widthsFor2Fields
;
324 sb
->SetFieldsCount(nFields
, widths
);
327 wxString::Format("Status bar now has %ld fields", nFields
));
331 wxLogStatus(this, "Cancelled");
335 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
337 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
341 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
343 // TRUE is to force the frame to close
347 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
349 wxMessageBox("wxStatusBar sample\n(c) 2000 Vadim Zeitlin",
350 "About statbar", wxOK
| wxICON_INFORMATION
, this);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
358 // 'this' : used in base member initializer list -- so what??
359 #pragma warning(disable: 4355)
362 MyStatusBar::MyStatusBar(wxWindow
*parent
)
363 : wxStatusBar(parent
, -1), m_timer(this)
365 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
367 SetFieldsCount(Field_Max
);
368 SetStatusWidths(Field_Max
, widths
);
370 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
371 m_checkbox
->SetValue(TRUE
);
373 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
377 SetMinHeight(BITMAP_SIZE_Y
);
383 #pragma warning(default: 4355)
386 MyStatusBar::~MyStatusBar()
388 if ( m_timer
.IsRunning() )
394 void MyStatusBar::OnSize(wxSizeEvent
& event
)
397 GetFieldRect(Field_Checkbox
, rect
);
399 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
401 GetFieldRect(Field_Bitmap
, rect
);
402 m_statbmp
->Move(rect
.x
+ (rect
.width
- BITMAP_SIZE_X
) / 2,
403 rect
.y
+ (rect
.height
- BITMAP_SIZE_Y
) / 2);
408 void MyStatusBar::OnToggleClock(wxCommandEvent
& event
)
410 if ( m_checkbox
->GetValue() )
414 m_statbmp
->SetIcon(wxIcon(green_xpm
));
418 else // don't show clock
422 m_statbmp
->SetIcon(wxIcon(red_xpm
));
424 SetStatusText("", Field_Clock
);
428 void MyStatusBar::UpdateClock()
430 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);