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