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