]>
Commit | Line | Data |
---|---|---|
2286341c VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: statbar.cpp | |
3 | // Purpose: wxStatusBar sample | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 04.02.00 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2286341c VZ |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if !wxUSE_STATUSBAR | |
28 | #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample" | |
29 | #endif // wxUSE_STATUSBAR | |
30 | ||
cbc66a27 | 31 | // for all others, include the necessary headers |
2286341c VZ |
32 | #ifndef WX_PRECOMP |
33 | #include "wx/app.h" | |
788233da | 34 | #include "wx/log.h" |
2286341c VZ |
35 | #include "wx/frame.h" |
36 | #include "wx/statusbr.h" | |
2286341c VZ |
37 | #include "wx/timer.h" |
38 | #include "wx/checkbox.h" | |
39 | #include "wx/statbmp.h" | |
40 | #include "wx/menu.h" | |
41 | #include "wx/msgdlg.h" | |
633d67bb | 42 | #include "wx/textdlg.h" |
cbc66a27 | 43 | #include "wx/sizer.h" |
f6c881b4 | 44 | #include "wx/stattext.h" |
f6bcfd97 BP |
45 | #include "wx/bmpbuttn.h" |
46 | #include "wx/dcmemory.h" | |
2286341c VZ |
47 | #endif |
48 | ||
85401ffe | 49 | #include "wx/datetime.h" |
e4f3eb42 | 50 | #include "wx/numdlg.h" |
2a90b100 | 51 | #include "wx/fontdlg.h" |
85401ffe | 52 | |
41f02b9a FM |
53 | #ifndef __WXMSW__ |
54 | #include "../sample.xpm" | |
55 | #endif | |
56 | ||
54e18afc | 57 | |
f6bcfd97 BP |
58 | // define this for the platforms which don't support wxBitmapButton (such as |
59 | // Motif), else a wxBitmapButton will be used | |
60 | #ifdef __WXMOTIF__ | |
54e18afc FM |
61 | #define USE_STATIC_BITMAP |
62 | #endif | |
63 | ||
ffd0623c JS |
64 | //#define USE_MDI_PARENT_FRAME 1 |
65 | ||
66 | #ifdef USE_MDI_PARENT_FRAME | |
67 | #include "wx/mdi.h" | |
68 | #endif // USE_MDI_PARENT_FRAME | |
54e18afc | 69 | |
f6bcfd97 | 70 | |
2286341c VZ |
71 | // ---------------------------------------------------------------------------- |
72 | // resources | |
73 | // ---------------------------------------------------------------------------- | |
74 | ||
f6bcfd97 BP |
75 | #ifdef USE_STATIC_BITMAP |
76 | #include "green.xpm" | |
77 | #include "red.xpm" | |
78 | #endif // USE_STATIC_BITMAP | |
2286341c VZ |
79 | |
80 | // ---------------------------------------------------------------------------- | |
81 | // private classes | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | // Define a new application type, each program should derive a class from wxApp | |
85 | class MyApp : public wxApp | |
86 | { | |
87 | public: | |
88 | // override base class virtuals | |
89 | // ---------------------------- | |
90 | ||
91 | // this one is called on application startup and is a good place for the app | |
92 | // initialization (doing it here and not in the ctor allows to have an error | |
93 | // return: if OnInit() returns false, the application terminates) | |
94 | virtual bool OnInit(); | |
95 | }; | |
96 | ||
97 | // A custom status bar which contains controls, icons &c | |
98 | class MyStatusBar : public wxStatusBar | |
99 | { | |
100 | public: | |
c4c178c1 | 101 | MyStatusBar(wxWindow *parent, long style = wxSTB_DEFAULT_STYLE); |
2286341c VZ |
102 | virtual ~MyStatusBar(); |
103 | ||
104 | void UpdateClock(); | |
105 | ||
106 | // event handlers | |
a6ebd559 | 107 | #if wxUSE_TIMER |
d1f47235 | 108 | void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); } |
a6ebd559 | 109 | #endif |
2286341c VZ |
110 | void OnSize(wxSizeEvent& event); |
111 | void OnToggleClock(wxCommandEvent& event); | |
f6bcfd97 | 112 | void OnButton(wxCommandEvent& event); |
2286341c VZ |
113 | |
114 | private: | |
f6bcfd97 BP |
115 | // toggle the state of the status bar controls |
116 | void DoToggle(); | |
117 | ||
ee1a622d | 118 | wxBitmap CreateBitmapForButton(bool on = false); |
f6bcfd97 | 119 | |
2286341c VZ |
120 | enum |
121 | { | |
122 | Field_Text, | |
123 | Field_Checkbox, | |
124 | Field_Bitmap, | |
125 | Field_Clock, | |
126 | Field_Max | |
127 | }; | |
128 | ||
a6ebd559 | 129 | #if wxUSE_TIMER |
633d67bb | 130 | wxTimer m_timer; |
a6ebd559 | 131 | #endif |
2286341c | 132 | |
a6ebd559 | 133 | #if wxUSE_CHECKBOX |
2286341c | 134 | wxCheckBox *m_checkbox; |
a6ebd559 | 135 | #endif |
f6bcfd97 | 136 | #ifdef USE_STATIC_BITMAP |
2286341c | 137 | wxStaticBitmap *m_statbmp; |
f6bcfd97 BP |
138 | #else |
139 | wxBitmapButton *m_statbmp; | |
140 | #endif | |
2286341c VZ |
141 | |
142 | DECLARE_EVENT_TABLE() | |
143 | }; | |
144 | ||
145 | // Define a new frame type: this is going to be our main frame | |
146 | class MyFrame : public wxFrame | |
147 | { | |
148 | public: | |
149 | // ctor(s) | |
150 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
ffd0623c JS |
151 | #ifdef USE_MDI_PARENT_FRAME |
152 | class MyFrame : public wxMDIParentFrame | |
153 | #else | |
2286341c | 154 | virtual ~MyFrame(); |
ffd0623c | 155 | #endif |
2286341c VZ |
156 | |
157 | // event handlers (these functions should _not_ be virtual) | |
158 | void OnQuit(wxCommandEvent& event); | |
159 | void OnAbout(wxCommandEvent& event); | |
633d67bb | 160 | |
fd3ece57 | 161 | void OnResetFieldsWidth(wxCommandEvent& event); |
633d67bb | 162 | void OnSetStatusFields(wxCommandEvent& event); |
9df6b3b0 | 163 | void OnSetStatusTexts(wxCommandEvent& event); |
2a90b100 | 164 | void OnSetStatusFont(wxCommandEvent& event); |
2286341c | 165 | void OnRecreateStatusBar(wxCommandEvent& event); |
c4c178c1 FM |
166 | |
167 | void OnSetPaneStyle(wxCommandEvent& event); | |
168 | void OnSetStyle(wxCommandEvent& event); | |
2286341c VZ |
169 | |
170 | private: | |
c4c178c1 | 171 | enum StatusBarKind |
2286341c VZ |
172 | { |
173 | StatBar_Default, | |
174 | StatBar_Custom, | |
175 | StatBar_Max | |
176 | } m_statbarKind; | |
fd3ece57 | 177 | |
9df6b3b0 | 178 | |
ea6e41e4 | 179 | void OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event); |
ffd0623c | 180 | void OnUpdateStatusBarToggle(wxUpdateUIEvent& event); |
c4c178c1 FM |
181 | void OnUpdateSetPaneStyle(wxUpdateUIEvent& event); |
182 | void OnUpdateSetStyle(wxUpdateUIEvent& event); | |
ffd0623c | 183 | void OnStatusBarToggle(wxCommandEvent& event); |
c4c178c1 FM |
184 | void DoCreateStatusBar(StatusBarKind kind, long style); |
185 | void ApplyPaneStyle(); | |
2286341c | 186 | |
c4c178c1 | 187 | int m_statbarPaneStyle; |
c2919ab3 | 188 | |
be5a51fb | 189 | // any class wishing to process wxWidgets events must use this macro |
2286341c VZ |
190 | DECLARE_EVENT_TABLE() |
191 | }; | |
192 | ||
cbc66a27 VZ |
193 | // Our about dialog ith its status bar |
194 | class MyAboutDialog : public wxDialog | |
195 | { | |
196 | public: | |
197 | MyAboutDialog(wxWindow *parent); | |
198 | }; | |
199 | ||
2286341c VZ |
200 | // ---------------------------------------------------------------------------- |
201 | // constants | |
202 | // ---------------------------------------------------------------------------- | |
203 | ||
204 | // IDs for the controls and the menu commands | |
205 | enum | |
206 | { | |
207 | // menu items | |
c4c178c1 FM |
208 | StatusBar_Quit = wxID_EXIT, |
209 | StatusBar_About = wxID_ABOUT, | |
210 | ||
211 | StatusBar_SetFields = wxID_HIGHEST+1, | |
9df6b3b0 | 212 | StatusBar_SetTexts, |
2a90b100 | 213 | StatusBar_SetFont, |
fd3ece57 | 214 | StatusBar_ResetFieldsWidth, |
ea6e41e4 | 215 | |
2286341c | 216 | StatusBar_Recreate, |
ffd0623c | 217 | StatusBar_Toggle, |
c4c178c1 FM |
218 | StatusBar_Checkbox, |
219 | StatusBar_SetPaneStyle, | |
220 | StatusBar_SetPaneStyleNormal, | |
221 | StatusBar_SetPaneStyleFlat, | |
222 | StatusBar_SetPaneStyleRaised, | |
223 | ||
224 | StatusBar_SetStyleSizeGrip, | |
225 | StatusBar_SetStyleEllipsizeStart, | |
226 | StatusBar_SetStyleEllipsizeMiddle, | |
227 | StatusBar_SetStyleEllipsizeEnd, | |
228 | StatusBar_SetStyleShowTips | |
2286341c VZ |
229 | }; |
230 | ||
231 | static const int BITMAP_SIZE_X = 32; | |
232 | static const int BITMAP_SIZE_Y = 15; | |
233 | ||
234 | // ---------------------------------------------------------------------------- | |
be5a51fb | 235 | // event tables and other macros for wxWidgets |
2286341c VZ |
236 | // ---------------------------------------------------------------------------- |
237 | ||
be5a51fb | 238 | // the event tables connect the wxWidgets events with the functions (event |
2286341c VZ |
239 | // handlers) which process them. It can be also done at run-time, but for the |
240 | // simple menu events like this the static method is much simpler. | |
ffd0623c JS |
241 | #ifdef USE_MDI_PARENT_FRAME |
242 | BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame) | |
243 | #else | |
2286341c | 244 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
ffd0623c | 245 | #endif |
2286341c | 246 | EVT_MENU(StatusBar_Quit, MyFrame::OnQuit) |
633d67bb | 247 | EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields) |
9df6b3b0 | 248 | EVT_MENU(StatusBar_SetTexts, MyFrame::OnSetStatusTexts) |
2a90b100 | 249 | EVT_MENU(StatusBar_SetFont, MyFrame::OnSetStatusFont) |
54e18afc | 250 | EVT_MENU(StatusBar_ResetFieldsWidth, MyFrame::OnResetFieldsWidth) |
2286341c VZ |
251 | EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar) |
252 | EVT_MENU(StatusBar_About, MyFrame::OnAbout) | |
ffd0623c | 253 | EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle) |
c4c178c1 FM |
254 | EVT_MENU(StatusBar_SetPaneStyleNormal, MyFrame::OnSetPaneStyle) |
255 | EVT_MENU(StatusBar_SetPaneStyleFlat, MyFrame::OnSetPaneStyle) | |
256 | EVT_MENU(StatusBar_SetPaneStyleRaised, MyFrame::OnSetPaneStyle) | |
257 | ||
258 | EVT_MENU(StatusBar_SetStyleSizeGrip, MyFrame::OnSetStyle) | |
259 | EVT_MENU(StatusBar_SetStyleEllipsizeStart, MyFrame::OnSetStyle) | |
260 | EVT_MENU(StatusBar_SetStyleEllipsizeMiddle, MyFrame::OnSetStyle) | |
261 | EVT_MENU(StatusBar_SetStyleEllipsizeEnd, MyFrame::OnSetStyle) | |
262 | EVT_MENU(StatusBar_SetStyleShowTips, MyFrame::OnSetStyle) | |
fd3ece57 | 263 | |
ea6e41e4 FM |
264 | EVT_UPDATE_UI_RANGE(StatusBar_SetFields, StatusBar_ResetFieldsWidth, |
265 | MyFrame::OnUpdateForDefaultStatusbar) | |
ffd0623c | 266 | EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle) |
c4c178c1 FM |
267 | EVT_UPDATE_UI_RANGE(StatusBar_SetPaneStyleNormal, StatusBar_SetPaneStyleRaised, |
268 | MyFrame::OnUpdateSetPaneStyle) | |
269 | EVT_UPDATE_UI_RANGE(StatusBar_SetStyleSizeGrip, StatusBar_SetStyleShowTips, | |
270 | MyFrame::OnUpdateSetStyle) | |
2286341c VZ |
271 | END_EVENT_TABLE() |
272 | ||
273 | BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar) | |
274 | EVT_SIZE(MyStatusBar::OnSize) | |
a6ebd559 | 275 | #if wxUSE_CHECKBOX |
2286341c | 276 | EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock) |
a6ebd559 | 277 | #endif |
ee1a622d | 278 | EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton) |
a6ebd559 | 279 | #if wxUSE_TIMER |
ee1a622d | 280 | EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer) |
a6ebd559 | 281 | #endif |
2286341c VZ |
282 | END_EVENT_TABLE() |
283 | ||
be5a51fb | 284 | // Create a new application object: this macro will allow wxWidgets to create |
2286341c VZ |
285 | // the application object during program execution (it's better than using a |
286 | // static object for many reasons) and also declares the accessor function | |
287 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
288 | // not wxApp) | |
289 | IMPLEMENT_APP(MyApp) | |
290 | ||
291 | // ============================================================================ | |
292 | // implementation | |
293 | // ============================================================================ | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // the application class | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | // `Main program' equivalent: the program execution "starts" here | |
300 | bool MyApp::OnInit() | |
301 | { | |
45e6e6f8 VZ |
302 | if ( !wxApp::OnInit() ) |
303 | return false; | |
304 | ||
2286341c | 305 | // create the main application window |
ab1ca7b3 | 306 | MyFrame *frame = new MyFrame(_T("wxStatusBar sample"), |
fd3ece57 | 307 | wxPoint(50, 50), wxSize(450, 340)); |
2286341c VZ |
308 | |
309 | // and show it (the frames, unlike simple controls, are not shown when | |
310 | // created initially) | |
ee1a622d | 311 | frame->Show(true); |
2286341c VZ |
312 | |
313 | // success: wxApp::OnRun() will be called which will enter the main message | |
ee1a622d | 314 | // loop and the application will run. If we returned 'false' here, the |
2286341c | 315 | // application would exit immediately. |
ee1a622d | 316 | return true; |
2286341c VZ |
317 | } |
318 | ||
319 | // ---------------------------------------------------------------------------- | |
320 | // main frame | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | // frame constructor | |
324 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
ffd0623c | 325 | #ifdef USE_MDI_PARENT_FRAME |
fd3ece57 | 326 | : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size) |
ffd0623c | 327 | #else |
fd3ece57 | 328 | : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size) |
ffd0623c | 329 | #endif |
2286341c | 330 | { |
41f02b9a FM |
331 | SetIcon(wxICON(sample)); |
332 | ||
c4c178c1 | 333 | m_statbarPaneStyle = wxSB_NORMAL; |
c2919ab3 | 334 | |
2286341c VZ |
335 | #ifdef __WXMAC__ |
336 | // we need this in order to allow the about menu relocation, since ABOUT is | |
337 | // not the default id of the about menu | |
338 | wxApp::s_macAboutMenuItemId = StatusBar_About; | |
339 | #endif | |
340 | ||
341 | // create a menu bar | |
342 | wxMenu *menuFile = new wxMenu; | |
ab1ca7b3 | 343 | menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
2286341c VZ |
344 | |
345 | wxMenu *statbarMenu = new wxMenu; | |
c4c178c1 FM |
346 | |
347 | wxMenu *statbarStyleMenu = new wxMenu; | |
348 | statbarStyleMenu->Append(StatusBar_SetStyleSizeGrip, _T("wxSTB_SIZE_GRIP"), _T("Toggles the wxSTB_SIZE_GRIP style"), true); | |
349 | statbarStyleMenu->Append(StatusBar_SetStyleShowTips, _T("wxSTB_SHOW_TIPS"), _T("Toggles the wxSTB_SHOW_TIPS style"), true); | |
350 | statbarStyleMenu->AppendSeparator(); | |
351 | statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeStart, _T("wxSTB_ELLIPSIZE_START"), _T("Toggles the wxSTB_ELLIPSIZE_START style"), true); | |
352 | statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeMiddle, _T("wxSTB_ELLIPSIZE_MIDDLE"), _T("Toggles the wxSTB_ELLIPSIZE_MIDDLE style"), true); | |
353 | statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeEnd, _T("wxSTB_ELLIPSIZE_END"), _T("Toggles the wxSTB_ELLIPSIZE_END style"), true); | |
354 | statbarMenu->Append(StatusBar_SetPaneStyle, _T("Status bar style"), statbarStyleMenu); | |
355 | statbarMenu->AppendSeparator(); | |
356 | ||
ab1ca7b3 MB |
357 | statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"), |
358 | _T("Set the number of status bar fields")); | |
9df6b3b0 FM |
359 | statbarMenu->Append(StatusBar_SetTexts, _T("&Set field text\tCtrl-T"), |
360 | _T("Set the text to display for each status bar field")); | |
2a90b100 FM |
361 | statbarMenu->Append(StatusBar_SetFont, _T("&Set field font\tCtrl-F"), |
362 | _T("Set the font to use for rendering status bar fields")); | |
2286341c | 363 | |
c4c178c1 FM |
364 | wxMenu *statbarPaneStyleMenu = new wxMenu; |
365 | statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleNormal, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true); | |
366 | statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleFlat, _T("&Flat"), _T("Sets the style of the first field to flat look"), true); | |
367 | statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleRaised, _T("&Raised"), _T("Sets the style of the first field to raised look"), true); | |
368 | statbarMenu->Append(StatusBar_SetPaneStyle, _T("Field style"), statbarPaneStyleMenu); | |
c2919ab3 | 369 | |
54e18afc FM |
370 | statbarMenu->Append(StatusBar_ResetFieldsWidth, _T("Reset field widths"), |
371 | _T("Sets all fields to the same width")); | |
fd3ece57 | 372 | statbarMenu->AppendSeparator(); |
54e18afc FM |
373 | |
374 | statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"), | |
375 | _T("Toggle the status bar display"), true); | |
376 | statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"), | |
377 | _T("Toggle status bar format")); | |
378 | ||
2286341c | 379 | wxMenu *helpMenu = new wxMenu; |
ab1ca7b3 | 380 | helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
2286341c VZ |
381 | |
382 | // now append the freshly created menu to the menu bar... | |
383 | wxMenuBar *menuBar = new wxMenuBar(); | |
ab1ca7b3 MB |
384 | menuBar->Append(menuFile, _T("&File")); |
385 | menuBar->Append(statbarMenu, _T("&Status bar")); | |
386 | menuBar->Append(helpMenu, _T("&Help")); | |
2286341c VZ |
387 | |
388 | // ... and attach this menu bar to the frame | |
389 | SetMenuBar(menuBar); | |
390 | ||
391 | // create default status bar to start with | |
c4c178c1 | 392 | DoCreateStatusBar(StatBar_Default, wxSTB_DEFAULT_STYLE); |
be5a51fb | 393 | SetStatusText(_T("Welcome to wxWidgets!")); |
2286341c VZ |
394 | } |
395 | ||
396 | MyFrame::~MyFrame() | |
397 | { | |
2286341c VZ |
398 | } |
399 | ||
c4c178c1 | 400 | void MyFrame::DoCreateStatusBar(MyFrame::StatusBarKind kind, long style) |
2286341c VZ |
401 | { |
402 | wxStatusBar *statbarOld = GetStatusBar(); | |
403 | if ( statbarOld ) | |
404 | { | |
c4c178c1 FM |
405 | SetStatusBar(NULL); |
406 | delete statbarOld; | |
2286341c VZ |
407 | } |
408 | ||
c4c178c1 | 409 | wxStatusBar *statbarNew = NULL; |
2286341c VZ |
410 | switch ( kind ) |
411 | { | |
412 | case StatBar_Default: | |
c4c178c1 FM |
413 | statbarNew = new wxStatusBar(this, wxID_ANY, style, "wxStatusBar"); |
414 | statbarNew->SetFieldsCount(2); | |
2286341c | 415 | break; |
c4c178c1 | 416 | |
2286341c | 417 | case StatBar_Custom: |
c4c178c1 | 418 | statbarNew = new MyStatusBar(this, style); |
2286341c VZ |
419 | break; |
420 | ||
421 | default: | |
c4c178c1 | 422 | wxFAIL_MSG(wxT("unknown status bar kind")); |
2286341c VZ |
423 | } |
424 | ||
c4c178c1 FM |
425 | SetStatusBar(statbarNew); |
426 | ApplyPaneStyle(); | |
f6bcfd97 | 427 | PositionStatusBar(); |
2286341c VZ |
428 | |
429 | m_statbarKind = kind; | |
430 | } | |
431 | ||
9df6b3b0 FM |
432 | |
433 | // ---------------------------------------------------------------------------- | |
434 | // main frame - event handlers | |
435 | // ---------------------------------------------------------------------------- | |
436 | ||
ea6e41e4 | 437 | void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event) |
9df6b3b0 | 438 | { |
ea6e41e4 | 439 | // only allow this feature for the default status bar |
9df6b3b0 | 440 | wxStatusBar *sb = GetStatusBar(); |
c4c178c1 FM |
441 | if (!sb) |
442 | return; | |
443 | ||
444 | event.Enable(sb->GetName() == "wxStatusBar"); | |
9df6b3b0 FM |
445 | } |
446 | ||
447 | void MyFrame::OnSetStatusTexts(wxCommandEvent& WXUNUSED(event)) | |
448 | { | |
449 | wxStatusBar *sb = GetStatusBar(); | |
c4c178c1 FM |
450 | if (!sb) |
451 | return; | |
9df6b3b0 FM |
452 | |
453 | wxString txt; | |
454 | for (int i=0; i<sb->GetFieldsCount(); i++) | |
455 | { | |
456 | txt = | |
457 | wxGetTextFromUser(wxString::Format("Enter the text for the %d-th field:", i+1), | |
458 | "Input field text", "A dummy test string", this); | |
459 | ||
460 | sb->SetStatusText(txt, i); | |
461 | } | |
462 | } | |
463 | ||
2a90b100 FM |
464 | void MyFrame::OnSetStatusFont(wxCommandEvent& WXUNUSED(event)) |
465 | { | |
466 | wxStatusBar *sb = GetStatusBar(); | |
c4c178c1 FM |
467 | if (!sb) |
468 | return; | |
2a90b100 FM |
469 | |
470 | wxFont fnt = wxGetFontFromUser(this, sb->GetFont(), "Choose statusbar font"); | |
471 | if (fnt.IsOk()) | |
472 | { | |
473 | sb->SetFont(fnt); | |
474 | sb->SetSize(sb->GetBestSize()); | |
475 | } | |
476 | } | |
477 | ||
633d67bb VZ |
478 | void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event)) |
479 | { | |
480 | wxStatusBar *sb = GetStatusBar(); | |
c4c178c1 FM |
481 | if (!sb) |
482 | return; | |
633d67bb VZ |
483 | |
484 | long nFields = wxGetNumberFromUser | |
fd3ece57 | 485 | ( |
bf69498a VZ |
486 | _T("Select the number of fields in the status bar"), |
487 | _T("Fields:"), | |
be5a51fb | 488 | _T("wxWidgets statusbar sample"), |
633d67bb VZ |
489 | sb->GetFieldsCount(), |
490 | 1, 5, | |
491 | this | |
fd3ece57 | 492 | ); |
633d67bb VZ |
493 | |
494 | // we don't check if the number changed at all on purpose: calling | |
495 | // SetFieldsCount() with the same number of fields should be ok | |
496 | if ( nFields != -1 ) | |
497 | { | |
71e03035 VZ |
498 | static const int widthsFor2Fields[] = { 200, -1 }; |
499 | static const int widthsFor3Fields[] = { -1, -2, -1 }; | |
500 | static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 }; | |
501 | ||
bf69498a | 502 | static const int *widthsAll[] = |
633d67bb | 503 | { |
71e03035 VZ |
504 | NULL, // 1 field: default |
505 | widthsFor2Fields, // 2 fields: 1 fixed, 1 var | |
506 | widthsFor3Fields, // 3 fields: 3 var | |
507 | widthsFor4Fields, // 4 fields: 3 fixed, 2 vars | |
508 | NULL // 5 fields: default (all have same width) | |
509 | }; | |
633d67bb | 510 | |
bf69498a VZ |
511 | const int * const widths = widthsAll[nFields - 1]; |
512 | sb->SetFieldsCount(nFields, widths); | |
633d67bb | 513 | |
bf69498a VZ |
514 | wxString s; |
515 | for ( long n = 0; n < nFields; n++ ) | |
516 | { | |
517 | if ( widths ) | |
518 | { | |
519 | if ( widths[n] > 0 ) | |
520 | s.Printf(_T("fixed (%d)"), widths[n]); | |
521 | else | |
522 | s.Printf(_T("variable (*%d)"), -widths[n]); | |
523 | } | |
524 | else | |
525 | { | |
526 | s = _T("default"); | |
527 | } | |
528 | ||
529 | SetStatusText(s, n); | |
530 | } | |
633d67bb VZ |
531 | } |
532 | else | |
533 | { | |
4693b20c | 534 | wxLogStatus(this, wxT("Cancelled")); |
633d67bb VZ |
535 | } |
536 | } | |
537 | ||
54e18afc FM |
538 | void MyFrame::OnResetFieldsWidth(wxCommandEvent& WXUNUSED(event)) |
539 | { | |
fd3ece57 | 540 | wxStatusBar *pStat = GetStatusBar(); |
c4c178c1 FM |
541 | if (!pStat) |
542 | return; | |
543 | ||
544 | int n = pStat->GetFieldsCount(); | |
545 | pStat->SetStatusWidths(n, NULL); | |
546 | for (int i=0; i<n; i++) | |
547 | pStat->SetStatusText("same size", i); | |
54e18afc FM |
548 | } |
549 | ||
ffd0623c JS |
550 | void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event) |
551 | { | |
2dcd83af | 552 | event.Check(GetStatusBar() != NULL); |
ffd0623c JS |
553 | } |
554 | ||
555 | void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event)) | |
556 | { | |
557 | wxStatusBar *statbarOld = GetStatusBar(); | |
558 | if ( statbarOld ) | |
559 | { | |
2dcd83af | 560 | SetStatusBar(NULL); |
c4c178c1 | 561 | delete statbarOld; |
ffd0623c JS |
562 | } |
563 | else | |
564 | { | |
c4c178c1 | 565 | DoCreateStatusBar(m_statbarKind, wxSTB_DEFAULT_STYLE); |
ffd0623c | 566 | } |
ffd0623c JS |
567 | } |
568 | ||
2286341c VZ |
569 | void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event)) |
570 | { | |
571 | DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default | |
c4c178c1 FM |
572 | : StatBar_Custom, |
573 | wxSTB_DEFAULT_STYLE); | |
2286341c VZ |
574 | } |
575 | ||
576 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
577 | { | |
ee1a622d WS |
578 | // true is to force the frame to close |
579 | Close(true); | |
2286341c VZ |
580 | } |
581 | ||
582 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
583 | { | |
cbc66a27 VZ |
584 | MyAboutDialog dlg(this); |
585 | dlg.ShowModal(); | |
586 | } | |
587 | ||
c4c178c1 | 588 | void MyFrame::OnUpdateSetPaneStyle(wxUpdateUIEvent& event) |
c2919ab3 | 589 | { |
c4c178c1 FM |
590 | switch (event.GetId()) |
591 | { | |
592 | case StatusBar_SetPaneStyleNormal: | |
593 | event.Check(m_statbarPaneStyle == wxSB_NORMAL); | |
594 | break; | |
595 | case StatusBar_SetPaneStyleFlat: | |
596 | event.Check(m_statbarPaneStyle == wxSB_FLAT); | |
597 | break; | |
598 | case StatusBar_SetPaneStyleRaised: | |
599 | event.Check(m_statbarPaneStyle == wxSB_RAISED); | |
600 | break; | |
601 | } | |
c2919ab3 VZ |
602 | } |
603 | ||
c4c178c1 | 604 | void MyFrame::OnSetPaneStyle(wxCommandEvent& event) |
c2919ab3 | 605 | { |
c4c178c1 FM |
606 | switch (event.GetId()) |
607 | { | |
608 | case StatusBar_SetPaneStyleNormal: | |
609 | m_statbarPaneStyle = wxSB_NORMAL; | |
610 | break; | |
611 | case StatusBar_SetPaneStyleFlat: | |
612 | m_statbarPaneStyle = wxSB_FLAT; | |
613 | break; | |
614 | case StatusBar_SetPaneStyleRaised: | |
615 | m_statbarPaneStyle = wxSB_RAISED; | |
616 | break; | |
617 | } | |
618 | ||
619 | ApplyPaneStyle(); | |
c2919ab3 VZ |
620 | } |
621 | ||
c4c178c1 | 622 | void MyFrame::ApplyPaneStyle() |
c2919ab3 VZ |
623 | { |
624 | wxStatusBar *sb = GetStatusBar(); | |
c4c178c1 FM |
625 | if (!sb) |
626 | return; | |
627 | ||
c2919ab3 VZ |
628 | int fields = sb->GetFieldsCount(); |
629 | int *styles = new int[fields]; | |
630 | ||
631 | for (int i = 1; i < fields; i++) | |
632 | styles[i] = wxSB_NORMAL; | |
633 | ||
c4c178c1 | 634 | styles[0] = m_statbarPaneStyle; |
c2919ab3 VZ |
635 | |
636 | sb->SetStatusStyles(fields, styles); | |
637 | ||
638 | delete [] styles; | |
639 | } | |
640 | ||
c4c178c1 FM |
641 | void MyFrame::OnUpdateSetStyle(wxUpdateUIEvent& event) |
642 | { | |
643 | long currentStyle = wxSTB_DEFAULT_STYLE; | |
644 | if (GetStatusBar()) | |
645 | currentStyle = GetStatusBar()->GetWindowStyle(); | |
646 | ||
647 | switch (event.GetId()) | |
648 | { | |
649 | case StatusBar_SetStyleSizeGrip: | |
650 | event.Check((currentStyle & wxSTB_SIZEGRIP) != 0); | |
651 | break; | |
652 | case StatusBar_SetStyleShowTips: | |
653 | event.Check((currentStyle & wxSTB_SHOW_TIPS) != 0); | |
654 | break; | |
655 | ||
656 | case StatusBar_SetStyleEllipsizeStart: | |
657 | event.Check((currentStyle & wxSTB_ELLIPSIZE_START) != 0); | |
658 | break; | |
659 | case StatusBar_SetStyleEllipsizeMiddle: | |
660 | event.Check((currentStyle & wxSTB_ELLIPSIZE_MIDDLE) != 0); | |
661 | break; | |
662 | case StatusBar_SetStyleEllipsizeEnd: | |
663 | event.Check((currentStyle & wxSTB_ELLIPSIZE_END) != 0); | |
664 | break; | |
665 | } | |
666 | } | |
667 | ||
668 | void MyFrame::OnSetStyle(wxCommandEvent& event) | |
669 | { | |
670 | long oldStyle = wxSTB_DEFAULT_STYLE; | |
671 | if (GetStatusBar()) | |
672 | oldStyle = GetStatusBar()->GetWindowStyle(); | |
673 | ||
674 | #define STB_ELLIPSIZE_MASK (wxSTB_ELLIPSIZE_START|wxSTB_ELLIPSIZE_MIDDLE|wxSTB_ELLIPSIZE_END) | |
675 | ||
676 | long newStyle = oldStyle; | |
677 | long newStyleBit = 0; | |
678 | switch (event.GetId()) | |
679 | { | |
680 | case StatusBar_SetStyleSizeGrip: | |
681 | newStyleBit = wxSTB_SIZEGRIP; | |
682 | break; | |
683 | case StatusBar_SetStyleShowTips: | |
684 | newStyleBit = wxSTB_SHOW_TIPS; | |
685 | break; | |
686 | ||
687 | case StatusBar_SetStyleEllipsizeStart: | |
688 | newStyleBit = wxSTB_ELLIPSIZE_START; | |
689 | newStyle &= ~STB_ELLIPSIZE_MASK; | |
690 | break; | |
691 | case StatusBar_SetStyleEllipsizeMiddle: | |
692 | newStyleBit = wxSTB_ELLIPSIZE_MIDDLE; | |
693 | newStyle &= ~STB_ELLIPSIZE_MASK; | |
694 | break; | |
695 | case StatusBar_SetStyleEllipsizeEnd: | |
696 | newStyleBit = wxSTB_ELLIPSIZE_END; | |
697 | newStyle &= ~STB_ELLIPSIZE_MASK; | |
698 | break; | |
699 | } | |
700 | ||
701 | newStyle = event.IsChecked() ? (newStyle | newStyleBit) : | |
702 | (newStyle & ~newStyleBit); | |
703 | if (newStyle != oldStyle) | |
704 | { | |
705 | DoCreateStatusBar(m_statbarKind, newStyle); | |
706 | SetStatusText("Status bar recreated with a new style"); | |
707 | } | |
708 | } | |
709 | ||
cbc66a27 VZ |
710 | // ---------------------------------------------------------------------------- |
711 | // MyAboutDialog | |
712 | // ---------------------------------------------------------------------------- | |
713 | ||
714 | MyAboutDialog::MyAboutDialog(wxWindow *parent) | |
fd3ece57 | 715 | : wxDialog(parent, wxID_ANY, wxString(_T("About statbar")), |
cbc66a27 VZ |
716 | wxDefaultPosition, wxDefaultSize, |
717 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) | |
718 | { | |
ee1a622d | 719 | wxStaticText *text = new wxStaticText(this, wxID_ANY, |
fd3ece57 FM |
720 | _T("wxStatusBar sample\n") |
721 | _T("(c) 2000 Vadim Zeitlin")); | |
cbc66a27 | 722 | |
ab1ca7b3 | 723 | wxButton *btn = new wxButton(this, wxID_OK, _T("&Close")); |
a1a43961 | 724 | |
8b8bff20 VZ |
725 | // create the top status bar without the size grip (default style), |
726 | // otherwise it looks weird | |
ee1a622d | 727 | wxStatusBar *statbarTop = new wxStatusBar(this, wxID_ANY, 0); |
8b8bff20 | 728 | statbarTop->SetFieldsCount(3); |
ab1ca7b3 MB |
729 | statbarTop->SetStatusText(_T("This is a top status bar"), 0); |
730 | statbarTop->SetStatusText(_T("in a dialog"), 1); | |
731 | statbarTop->SetStatusText(_T("Great, isn't it?"), 2); | |
8b8bff20 | 732 | |
ee1a622d | 733 | wxStatusBar *statbarBottom = new wxStatusBar(this, wxID_ANY); |
8b8bff20 | 734 | statbarBottom->SetFieldsCount(2); |
ab1ca7b3 MB |
735 | statbarBottom->SetStatusText(_T("This is a bottom status bar"), 0); |
736 | statbarBottom->SetStatusText(_T("in a dialog"), 1); | |
cbc66a27 VZ |
737 | |
738 | wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
8b8bff20 | 739 | sizerTop->Add(statbarTop, 0, wxGROW); |
cbc66a27 | 740 | sizerTop->Add(-1, 10, 1, wxGROW); |
a1a43961 VZ |
741 | sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20); |
742 | sizerTop->Add(-1, 10, 1, wxGROW); | |
743 | sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20); | |
cbc66a27 | 744 | sizerTop->Add(-1, 10, 1, wxGROW); |
8b8bff20 | 745 | sizerTop->Add(statbarBottom, 0, wxGROW); |
cbc66a27 | 746 | |
92c01615 | 747 | SetSizerAndFit(sizerTop); |
2286341c VZ |
748 | } |
749 | ||
750 | // ---------------------------------------------------------------------------- | |
751 | // MyStatusBar | |
752 | // ---------------------------------------------------------------------------- | |
753 | ||
85401ffe VZ |
754 | #ifdef __VISUALC__ |
755 | // 'this' : used in base member initializer list -- so what?? | |
756 | #pragma warning(disable: 4355) | |
757 | #endif | |
758 | ||
c4c178c1 FM |
759 | MyStatusBar::MyStatusBar(wxWindow *parent, long style) |
760 | : wxStatusBar(parent, wxID_ANY, style, "MyStatusBar") | |
a6ebd559 | 761 | #if wxUSE_TIMER |
fd3ece57 | 762 | , m_timer(this) |
a6ebd559 WS |
763 | #endif |
764 | #if wxUSE_CHECKBOX | |
fd3ece57 | 765 | , m_checkbox(NULL) |
a6ebd559 | 766 | #endif |
2286341c VZ |
767 | { |
768 | static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 }; | |
769 | ||
770 | SetFieldsCount(Field_Max); | |
771 | SetStatusWidths(Field_Max, widths); | |
772 | ||
a6ebd559 | 773 | #if wxUSE_CHECKBOX |
2286341c | 774 | m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock")); |
ee1a622d | 775 | m_checkbox->SetValue(true); |
a6ebd559 | 776 | #endif |
2286341c | 777 | |
f6bcfd97 | 778 | #ifdef USE_STATIC_BITMAP |
ee1a622d | 779 | m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm)); |
f6bcfd97 | 780 | #else |
ee1a622d | 781 | m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(), |
fd3ece57 FM |
782 | wxDefaultPosition, wxDefaultSize, |
783 | wxBU_EXACTFIT); | |
f6bcfd97 | 784 | #endif |
2286341c | 785 | |
a6ebd559 | 786 | #if wxUSE_TIMER |
2286341c | 787 | m_timer.Start(1000); |
a6ebd559 | 788 | #endif |
2286341c | 789 | |
fd3ece57 FM |
790 | SetMinHeight(wxMax(m_statbmp->GetBestSize().GetHeight(), |
791 | m_checkbox->GetBestSize().GetHeight())); | |
85401ffe | 792 | |
2286341c VZ |
793 | UpdateClock(); |
794 | } | |
795 | ||
85401ffe VZ |
796 | #ifdef __VISUALC__ |
797 | #pragma warning(default: 4355) | |
798 | #endif | |
799 | ||
2286341c VZ |
800 | MyStatusBar::~MyStatusBar() |
801 | { | |
a6ebd559 | 802 | #if wxUSE_TIMER |
2286341c VZ |
803 | if ( m_timer.IsRunning() ) |
804 | { | |
805 | m_timer.Stop(); | |
806 | } | |
a6ebd559 | 807 | #endif |
2286341c VZ |
808 | } |
809 | ||
54e18afc FM |
810 | #define BMP_BUTTON_SIZE_X 10 |
811 | #define BMP_BUTTON_SIZE_Y 10 | |
812 | ||
f6bcfd97 BP |
813 | wxBitmap MyStatusBar::CreateBitmapForButton(bool on) |
814 | { | |
54e18afc | 815 | wxBitmap bitmap(BMP_BUTTON_SIZE_X+1, BMP_BUTTON_SIZE_Y+1); |
f6bcfd97 BP |
816 | wxMemoryDC dc; |
817 | dc.SelectObject(bitmap); | |
818 | dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH); | |
54e18afc | 819 | dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT); |
f6bcfd97 BP |
820 | dc.Clear(); |
821 | dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y); | |
822 | dc.SelectObject(wxNullBitmap); | |
823 | ||
824 | return bitmap; | |
825 | } | |
826 | ||
2286341c VZ |
827 | void MyStatusBar::OnSize(wxSizeEvent& event) |
828 | { | |
a6ebd559 | 829 | #if wxUSE_CHECKBOX |
f6bcfd97 BP |
830 | if ( !m_checkbox ) |
831 | return; | |
a6ebd559 | 832 | #endif |
f6bcfd97 | 833 | |
778c9b9f FM |
834 | // TEMPORARY HACK: TODO find a more general solution |
835 | #ifdef wxStatusBarGeneric | |
836 | wxStatusBar::OnSize(event); | |
837 | #endif | |
838 | ||
2286341c | 839 | wxRect rect; |
ea6e41e4 FM |
840 | if (!GetFieldRect(Field_Checkbox, rect)) |
841 | { | |
842 | event.Skip(); | |
843 | return; | |
844 | } | |
2286341c | 845 | |
a6ebd559 | 846 | #if wxUSE_CHECKBOX |
2286341c | 847 | m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4); |
a6ebd559 | 848 | #endif |
2286341c VZ |
849 | |
850 | GetFieldRect(Field_Bitmap, rect); | |
f6bcfd97 | 851 | wxSize size = m_statbmp->GetSize(); |
f6bcfd97 BP |
852 | |
853 | m_statbmp->Move(rect.x + (rect.width - size.x) / 2, | |
854 | rect.y + (rect.height - size.y) / 2); | |
2286341c VZ |
855 | |
856 | event.Skip(); | |
857 | } | |
858 | ||
f6bcfd97 BP |
859 | void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event)) |
860 | { | |
a6ebd559 | 861 | #if wxUSE_CHECKBOX |
f6bcfd97 | 862 | m_checkbox->SetValue(!m_checkbox->GetValue()); |
a6ebd559 | 863 | #endif |
f6bcfd97 BP |
864 | |
865 | DoToggle(); | |
866 | } | |
867 | ||
868 | void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event)) | |
869 | { | |
870 | DoToggle(); | |
871 | } | |
872 | ||
873 | void MyStatusBar::DoToggle() | |
2286341c | 874 | { |
a6ebd559 | 875 | #if wxUSE_CHECKBOX |
2286341c VZ |
876 | if ( m_checkbox->GetValue() ) |
877 | { | |
a6ebd559 | 878 | #if wxUSE_TIMER |
2286341c | 879 | m_timer.Start(1000); |
a6ebd559 | 880 | #endif |
2286341c | 881 | |
f6bcfd97 | 882 | #ifdef USE_STATIC_BITMAP |
85401ffe | 883 | m_statbmp->SetIcon(wxIcon(green_xpm)); |
f6bcfd97 | 884 | #else |
ee1a622d | 885 | m_statbmp->SetBitmapLabel(CreateBitmapForButton(false)); |
f6bcfd97 BP |
886 | m_statbmp->Refresh(); |
887 | #endif | |
2286341c VZ |
888 | |
889 | UpdateClock(); | |
890 | } | |
891 | else // don't show clock | |
892 | { | |
a6ebd559 | 893 | #if wxUSE_TIMER |
2286341c | 894 | m_timer.Stop(); |
a6ebd559 | 895 | #endif |
2286341c | 896 | |
f6bcfd97 | 897 | #ifdef USE_STATIC_BITMAP |
85401ffe | 898 | m_statbmp->SetIcon(wxIcon(red_xpm)); |
f6bcfd97 | 899 | #else |
ee1a622d | 900 | m_statbmp->SetBitmapLabel(CreateBitmapForButton(true)); |
f6bcfd97 BP |
901 | m_statbmp->Refresh(); |
902 | #endif | |
2286341c | 903 | |
dabbc6a5 | 904 | SetStatusText(wxEmptyString, Field_Clock); |
2286341c | 905 | } |
a6ebd559 | 906 | #endif |
2286341c VZ |
907 | } |
908 | ||
909 | void MyStatusBar::UpdateClock() | |
910 | { | |
911 | SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock); | |
912 | } |