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