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