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