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