]>
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"
48 #include "wx/stattext.h"
51 #include "wx/datetime.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp
: public wxApp
68 // override base class virtuals
69 // ----------------------------
71 // this one is called on application startup and is a good place for the app
72 // initialization (doing it here and not in the ctor allows to have an error
73 // return: if OnInit() returns false, the application terminates)
74 virtual bool OnInit();
77 // A custom status bar which contains controls, icons &c
78 class MyStatusBar
: public wxStatusBar
81 MyStatusBar(wxWindow
*parent
);
82 virtual ~MyStatusBar();
87 void OnTimer(wxTimerEvent
& event
) { UpdateClock(); }
88 void OnSize(wxSizeEvent
& event
);
89 void OnToggleClock(wxCommandEvent
& event
);
103 wxCheckBox
*m_checkbox
;
104 wxStaticBitmap
*m_statbmp
;
106 DECLARE_EVENT_TABLE()
109 // Define a new frame type: this is going to be our main frame
110 class MyFrame
: public wxFrame
114 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
117 // event handlers (these functions should _not_ be virtual)
118 void OnQuit(wxCommandEvent
& event
);
119 void OnAbout(wxCommandEvent
& event
);
121 void OnSetStatusFields(wxCommandEvent
& event
);
122 void OnRecreateStatusBar(wxCommandEvent
& event
);
132 void DoCreateStatusBar(StatBarKind kind
);
134 wxStatusBar
*m_statbarDefault
;
135 MyStatusBar
*m_statbarCustom
;
137 // any class wishing to process wxWindows events must use this macro
138 DECLARE_EVENT_TABLE()
141 // Our about dialog ith its status bar
142 class MyAboutDialog
: public wxDialog
145 MyAboutDialog(wxWindow
*parent
);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 // IDs for the controls and the menu commands
160 StatusBar_Checkbox
= 1000
163 static const int BITMAP_SIZE_X
= 32;
164 static const int BITMAP_SIZE_Y
= 15;
166 // ----------------------------------------------------------------------------
167 // event tables and other macros for wxWindows
168 // ----------------------------------------------------------------------------
170 // the event tables connect the wxWindows events with the functions (event
171 // handlers) which process them. It can be also done at run-time, but for the
172 // simple menu events like this the static method is much simpler.
173 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
174 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
175 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
176 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
177 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
180 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
181 EVT_SIZE(MyStatusBar::OnSize
)
182 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
183 EVT_TIMER(-1, MyStatusBar::OnTimer
)
186 // Create a new application object: this macro will allow wxWindows to create
187 // the application object during program execution (it's better than using a
188 // static object for many reasons) and also declares the accessor function
189 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
193 // ============================================================================
195 // ============================================================================
197 // ----------------------------------------------------------------------------
198 // the application class
199 // ----------------------------------------------------------------------------
201 // `Main program' equivalent: the program execution "starts" here
204 // create the main application window
205 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
206 wxPoint(50, 50), wxSize(450, 340));
208 // and show it (the frames, unlike simple controls, are not shown when
209 // created initially)
212 // success: wxApp::OnRun() will be called which will enter the main message
213 // loop and the application will run. If we returned FALSE here, the
214 // application would exit immediately.
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
223 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
224 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
226 m_statbarDefault
= NULL
;
227 m_statbarCustom
= NULL
;
230 // we need this in order to allow the about menu relocation, since ABOUT is
231 // not the default id of the about menu
232 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
236 wxMenu
*menuFile
= new wxMenu
;
237 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
239 wxMenu
*statbarMenu
= new wxMenu
;
240 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
241 "Set the number of status bar fields");
242 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
243 "Toggle status bar format");
245 wxMenu
*helpMenu
= new wxMenu
;
246 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
248 // now append the freshly created menu to the menu bar...
249 wxMenuBar
*menuBar
= new wxMenuBar();
250 menuBar
->Append(menuFile
, "&File");
251 menuBar
->Append(statbarMenu
, "&Status bar");
252 menuBar
->Append(helpMenu
, "&Help");
254 // ... and attach this menu bar to the frame
257 // create default status bar to start with
259 SetStatusText("Welcome to wxWindows!");
261 m_statbarDefault
= GetStatusBar();
268 delete m_statbarDefault
;
269 delete m_statbarCustom
;
272 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
274 wxStatusBar
*statbarOld
= GetStatusBar();
282 case StatBar_Default
:
283 SetStatusBar(m_statbarDefault
);
287 if ( !m_statbarCustom
)
289 m_statbarCustom
= new MyStatusBar(this);
291 SetStatusBar(m_statbarCustom
);
295 wxFAIL_MSG("unknown stat bar kind");
299 GetStatusBar()->Show();
301 m_statbarKind
= kind
;
305 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
307 wxStatusBar
*sb
= GetStatusBar();
309 long nFields
= wxGetNumberFromUser
311 "Select the number of fields in the status bar",
313 "wxWindows statusbar sample",
314 sb
->GetFieldsCount(),
319 // we don't check if the number changed at all on purpose: calling
320 // SetFieldsCount() with the same number of fields should be ok
323 // we set the widths only for 2 of them, otherwise let all the fields
324 // have equal width (the default behaviour)
325 const int *widths
= NULL
;
328 static const int widthsFor2Fields
[2] = { 200, -1 };
329 widths
= widthsFor2Fields
;
332 sb
->SetFieldsCount(nFields
, widths
);
335 wxString::Format("Status bar now has %ld fields", nFields
));
339 wxLogStatus(this, "Cancelled");
343 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
345 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
349 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
351 // TRUE is to force the frame to close
355 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
357 MyAboutDialog
dlg(this);
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
365 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
366 : wxDialog(parent
, -1, wxString("About statbar"),
367 wxDefaultPosition
, wxDefaultSize
,
368 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
370 wxStaticText
*text
= new wxStaticText(this, -1,
371 "wxStatusBar sample\n"
372 "(c) 2000 Vadim Zeitlin");
374 wxButton
*btn
= new wxButton(this, wxID_OK
, "&Close");
376 // create the top status bar without the size grip (default style),
377 // otherwise it looks weird
378 wxStatusBar
*statbarTop
= new wxStatusBar(this, -1, 0);
379 statbarTop
->SetFieldsCount(3);
380 statbarTop
->SetStatusText("This is a top status bar", 0);
381 statbarTop
->SetStatusText("in a dialog", 1);
382 statbarTop
->SetStatusText("Great, isn't it?", 2);
384 wxStatusBar
*statbarBottom
= new wxStatusBar(this, -1);
385 statbarBottom
->SetFieldsCount(2);
386 statbarBottom
->SetStatusText("This is a bottom status bar", 0);
387 statbarBottom
->SetStatusText("in a dialog", 1);
389 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
390 sizerTop
->Add(statbarTop
, 0, wxGROW
);
391 sizerTop
->Add(-1, 10, 1, wxGROW
);
392 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
393 sizerTop
->Add(-1, 10, 1, wxGROW
);
394 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
395 sizerTop
->Add(-1, 10, 1, wxGROW
);
396 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
402 sizerTop
->SetSizeHints(this);
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
410 // 'this' : used in base member initializer list -- so what??
411 #pragma warning(disable: 4355)
414 MyStatusBar::MyStatusBar(wxWindow
*parent
)
415 : wxStatusBar(parent
, -1), m_timer(this)
417 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
419 SetFieldsCount(Field_Max
);
420 SetStatusWidths(Field_Max
, widths
);
422 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
423 m_checkbox
->SetValue(TRUE
);
425 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
429 SetMinHeight(BITMAP_SIZE_Y
);
435 #pragma warning(default: 4355)
438 MyStatusBar::~MyStatusBar()
440 if ( m_timer
.IsRunning() )
446 void MyStatusBar::OnSize(wxSizeEvent
& event
)
449 GetFieldRect(Field_Checkbox
, rect
);
451 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
453 GetFieldRect(Field_Bitmap
, rect
);
454 m_statbmp
->Move(rect
.x
+ (rect
.width
- BITMAP_SIZE_X
) / 2,
455 rect
.y
+ (rect
.height
- BITMAP_SIZE_Y
) / 2);
460 void MyStatusBar::OnToggleClock(wxCommandEvent
& event
)
462 if ( m_checkbox
->GetValue() )
466 m_statbmp
->SetIcon(wxIcon(green_xpm
));
470 else // don't show clock
474 m_statbmp
->SetIcon(wxIcon(red_xpm
));
476 SetStatusText("", Field_Clock
);
480 void MyStatusBar::UpdateClock()
482 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);