]>
git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxStatusBar sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
28 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
29 #endif // wxUSE_STATUSBAR
31 // for all others, include the necessary headers
36 #include "wx/statusbr.h"
38 #include "wx/checkbox.h"
39 #include "wx/statbmp.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textdlg.h"
44 #include "wx/stattext.h"
45 #include "wx/bmpbuttn.h"
46 #include "wx/dcmemory.h"
49 #include "wx/datetime.h"
50 #include "wx/numdlg.h"
52 // define this for the platforms which don't support wxBitmapButton (such as
53 // Motif), else a wxBitmapButton will be used
55 //#define USE_MDI_PARENT_FRAME 1
57 #ifdef USE_MDI_PARENT_FRAME
59 #endif // USE_MDI_PARENT_FRAME
60 #define USE_STATIC_BITMAP
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #ifdef USE_STATIC_BITMAP
70 #endif // USE_STATIC_BITMAP
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // Define a new application type, each program should derive a class from wxApp
77 class MyApp
: public wxApp
80 // override base class virtuals
81 // ----------------------------
83 // this one is called on application startup and is a good place for the app
84 // initialization (doing it here and not in the ctor allows to have an error
85 // return: if OnInit() returns false, the application terminates)
86 virtual bool OnInit();
89 // A custom status bar which contains controls, icons &c
90 class MyStatusBar
: public wxStatusBar
93 MyStatusBar(wxWindow
*parent
);
94 virtual ~MyStatusBar();
99 void OnTimer(wxTimerEvent
& WXUNUSED(event
)) { UpdateClock(); }
100 void OnSize(wxSizeEvent
& event
);
101 void OnToggleClock(wxCommandEvent
& event
);
102 void OnButton(wxCommandEvent
& event
);
105 // toggle the state of the status bar controls
108 wxBitmap
CreateBitmapForButton(bool on
= false);
121 wxCheckBox
*m_checkbox
;
122 #ifdef USE_STATIC_BITMAP
123 wxStaticBitmap
*m_statbmp
;
125 wxBitmapButton
*m_statbmp
;
128 DECLARE_EVENT_TABLE()
131 // Define a new frame type: this is going to be our main frame
132 class MyFrame
: public wxFrame
136 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
137 #ifdef USE_MDI_PARENT_FRAME
138 class MyFrame
: public wxMDIParentFrame
143 // event handlers (these functions should _not_ be virtual)
144 void OnQuit(wxCommandEvent
& event
);
145 void OnAbout(wxCommandEvent
& event
);
147 void OnSetStatusFields(wxCommandEvent
& event
);
148 void OnRecreateStatusBar(wxCommandEvent
& event
);
149 void OnSetStyleNormal(wxCommandEvent
& event
);
150 void OnSetStyleFlat(wxCommandEvent
& event
);
151 void OnSetStyleRaised(wxCommandEvent
& event
);
160 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
161 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
162 void OnUpdateSetStyleNormal(wxUpdateUIEvent
& event
);
163 void OnUpdateSetStyleFlat(wxUpdateUIEvent
& event
);
164 void OnUpdateSetStyleRaised(wxUpdateUIEvent
& event
);
165 void OnStatusBarToggle(wxCommandEvent
& event
);
166 void DoCreateStatusBar(StatBarKind kind
);
169 wxStatusBar
*m_statbarDefault
;
170 MyStatusBar
*m_statbarCustom
;
174 // any class wishing to process wxWidgets events must use this macro
175 DECLARE_EVENT_TABLE()
178 // Our about dialog ith its status bar
179 class MyAboutDialog
: public wxDialog
182 MyAboutDialog(wxWindow
*parent
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 // IDs for the controls and the menu commands
198 StatusBar_Checkbox
= 1000,
200 StatusBar_SetStyleNormal
,
201 StatusBar_SetStyleFlat
,
202 StatusBar_SetStyleRaised
205 static const int BITMAP_SIZE_X
= 32;
206 static const int BITMAP_SIZE_Y
= 15;
208 // ----------------------------------------------------------------------------
209 // event tables and other macros for wxWidgets
210 // ----------------------------------------------------------------------------
212 // the event tables connect the wxWidgets events with the functions (event
213 // handlers) which process them. It can be also done at run-time, but for the
214 // simple menu events like this the static method is much simpler.
215 #ifdef USE_MDI_PARENT_FRAME
216 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
218 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
220 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
221 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
222 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
223 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
224 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
225 EVT_MENU(StatusBar_SetStyleNormal
, MyFrame::OnSetStyleNormal
)
226 EVT_MENU(StatusBar_SetStyleFlat
, MyFrame::OnSetStyleFlat
)
227 EVT_MENU(StatusBar_SetStyleRaised
, MyFrame::OnSetStyleRaised
)
228 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
229 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
230 EVT_UPDATE_UI(StatusBar_SetStyleNormal
, MyFrame::OnUpdateSetStyleNormal
)
231 EVT_UPDATE_UI(StatusBar_SetStyleFlat
, MyFrame::OnUpdateSetStyleFlat
)
232 EVT_UPDATE_UI(StatusBar_SetStyleRaised
, MyFrame::OnUpdateSetStyleRaised
)
235 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
236 EVT_SIZE(MyStatusBar::OnSize
)
237 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
238 EVT_BUTTON(wxID_ANY
, MyStatusBar::OnButton
)
239 EVT_TIMER(wxID_ANY
, MyStatusBar::OnTimer
)
242 // Create a new application object: this macro will allow wxWidgets to create
243 // the application object during program execution (it's better than using a
244 // static object for many reasons) and also declares the accessor function
245 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
249 // ============================================================================
251 // ============================================================================
253 // ----------------------------------------------------------------------------
254 // the application class
255 // ----------------------------------------------------------------------------
257 // `Main program' equivalent: the program execution "starts" here
260 // create the main application window
261 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
262 wxPoint(50, 50), wxSize(450, 340));
264 // and show it (the frames, unlike simple controls, are not shown when
265 // created initially)
268 // success: wxApp::OnRun() will be called which will enter the main message
269 // loop and the application will run. If we returned 'false' here, the
270 // application would exit immediately.
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
279 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
280 #ifdef USE_MDI_PARENT_FRAME
281 : wxMDIParentFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
283 : wxFrame((wxWindow
*)NULL
, wxID_ANY
, title
, pos
, size
)
286 m_statbarDefault
= NULL
;
287 m_statbarCustom
= NULL
;
289 m_statbarStyle
= wxSB_NORMAL
;
292 // we need this in order to allow the about menu relocation, since ABOUT is
293 // not the default id of the about menu
294 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
298 wxMenu
*menuFile
= new wxMenu
;
299 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
301 wxMenu
*statbarMenu
= new wxMenu
;
302 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
303 _T("Set the number of status bar fields"));
304 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
305 _T("Toggle the status bar display"), true);
306 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
307 _T("Toggle status bar format"));
309 wxMenu
*statbarStyleMenu
= new wxMenu
;
310 statbarStyleMenu
->Append(StatusBar_SetStyleNormal
, _T("&Normal"), _T("Sets the style of the first field to normal (sunken) look"), true);
311 statbarStyleMenu
->Append(StatusBar_SetStyleFlat
, _T("&Flat"), _T("Sets the style of the first field to flat look"), true);
312 statbarStyleMenu
->Append(StatusBar_SetStyleRaised
, _T("&Raised"), _T("Sets the style of the first field to raised look"), true);
313 statbarMenu
->Append(StatusBar_SetStyle
, _T("Field style"), statbarStyleMenu
);
315 wxMenu
*helpMenu
= new wxMenu
;
316 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
318 // now append the freshly created menu to the menu bar...
319 wxMenuBar
*menuBar
= new wxMenuBar();
320 menuBar
->Append(menuFile
, _T("&File"));
321 menuBar
->Append(statbarMenu
, _T("&Status bar"));
322 menuBar
->Append(helpMenu
, _T("&Help"));
324 // ... and attach this menu bar to the frame
327 // create default status bar to start with
329 m_statbarKind
= StatBar_Default
;
330 SetStatusText(_T("Welcome to wxWidgets!"));
332 m_statbarDefault
= GetStatusBar();
339 delete m_statbarDefault
;
340 delete m_statbarCustom
;
343 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
345 wxStatusBar
*statbarOld
= GetStatusBar();
353 case StatBar_Default
:
354 SetStatusBar(m_statbarDefault
);
358 if ( !m_statbarCustom
)
360 m_statbarCustom
= new MyStatusBar(this);
362 SetStatusBar(m_statbarCustom
);
366 wxFAIL_MSG(wxT("unknown stat bar kind"));
370 GetStatusBar()->Show();
373 m_statbarKind
= kind
;
376 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
378 // only allow the settings of the number of status fields for the default
380 wxStatusBar
*sb
= GetStatusBar();
381 event
.Enable(sb
== m_statbarDefault
);
385 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
387 wxStatusBar
*sb
= GetStatusBar();
389 long nFields
= wxGetNumberFromUser
391 _T("Select the number of fields in the status bar"),
393 _T("wxWidgets statusbar sample"),
394 sb
->GetFieldsCount(),
399 // we don't check if the number changed at all on purpose: calling
400 // SetFieldsCount() with the same number of fields should be ok
403 static const int widthsFor2Fields
[] = { 200, -1 };
404 static const int widthsFor3Fields
[] = { -1, -2, -1 };
405 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
407 static const int *widthsAll
[] =
409 NULL
, // 1 field: default
410 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
411 widthsFor3Fields
, // 3 fields: 3 var
412 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
413 NULL
// 5 fields: default (all have same width)
416 const int * const widths
= widthsAll
[nFields
- 1];
417 sb
->SetFieldsCount(nFields
, widths
);
420 for ( long n
= 0; n
< nFields
; n
++ )
425 s
.Printf(_T("fixed (%d)"), widths
[n
]);
427 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
439 wxLogStatus(this, wxT("Cancelled"));
443 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
445 event
.Check(GetStatusBar() != 0);
448 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
450 wxStatusBar
*statbarOld
= GetStatusBar();
458 DoCreateStatusBar(m_statbarKind
);
461 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWidgets
462 // authors) while we look for a proper fix..
467 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
469 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
473 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
475 // true is to force the frame to close
479 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
481 MyAboutDialog
dlg(this);
485 void MyFrame::OnUpdateSetStyleNormal(wxUpdateUIEvent
&event
)
487 event
.Check(m_statbarStyle
== wxSB_NORMAL
);
490 void MyFrame::OnUpdateSetStyleFlat(wxUpdateUIEvent
&event
)
492 event
.Check(m_statbarStyle
== wxSB_FLAT
);
495 void MyFrame::OnUpdateSetStyleRaised(wxUpdateUIEvent
&event
)
497 event
.Check(m_statbarStyle
== wxSB_RAISED
);
500 void MyFrame::OnSetStyleNormal(wxCommandEvent
& WXUNUSED(event
))
502 m_statbarStyle
= wxSB_NORMAL
;
506 void MyFrame::OnSetStyleFlat(wxCommandEvent
& WXUNUSED(event
))
508 m_statbarStyle
= wxSB_FLAT
;
512 void MyFrame::OnSetStyleRaised(wxCommandEvent
& WXUNUSED(event
))
514 m_statbarStyle
= wxSB_RAISED
;
518 void MyFrame::ApplyStyle()
520 wxStatusBar
*sb
= GetStatusBar();
521 int fields
= sb
->GetFieldsCount();
522 int *styles
= new int[fields
];
524 for (int i
= 1; i
< fields
; i
++)
525 styles
[i
] = wxSB_NORMAL
;
527 styles
[0] = m_statbarStyle
;
529 sb
->SetStatusStyles(fields
, styles
);
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
539 : wxDialog(parent
, wxID_ANY
, wxString(_T("About statbar")),
540 wxDefaultPosition
, wxDefaultSize
,
541 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
543 wxStaticText
*text
= new wxStaticText(this, wxID_ANY
,
544 _T("wxStatusBar sample\n")
545 _T("(c) 2000 Vadim Zeitlin"));
547 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
549 // create the top status bar without the size grip (default style),
550 // otherwise it looks weird
551 wxStatusBar
*statbarTop
= new wxStatusBar(this, wxID_ANY
, 0);
552 statbarTop
->SetFieldsCount(3);
553 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
554 statbarTop
->SetStatusText(_T("in a dialog"), 1);
555 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
557 wxStatusBar
*statbarBottom
= new wxStatusBar(this, wxID_ANY
);
558 statbarBottom
->SetFieldsCount(2);
559 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
560 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
562 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
563 sizerTop
->Add(statbarTop
, 0, wxGROW
);
564 sizerTop
->Add(-1, 10, 1, wxGROW
);
565 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
566 sizerTop
->Add(-1, 10, 1, wxGROW
);
567 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
568 sizerTop
->Add(-1, 10, 1, wxGROW
);
569 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
574 sizerTop
->SetSizeHints(this);
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
582 // 'this' : used in base member initializer list -- so what??
583 #pragma warning(disable: 4355)
586 MyStatusBar::MyStatusBar(wxWindow
*parent
)
587 : wxStatusBar(parent
, wxID_ANY
), m_timer(this), m_checkbox(NULL
)
589 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
591 SetFieldsCount(Field_Max
);
592 SetStatusWidths(Field_Max
, widths
);
594 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
595 m_checkbox
->SetValue(true);
597 #ifdef USE_STATIC_BITMAP
598 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, wxIcon(green_xpm
));
600 m_statbmp
= new wxBitmapButton(this, wxID_ANY
, CreateBitmapForButton(),
601 wxDefaultPosition
, wxDefaultSize
,
607 SetMinHeight(BITMAP_SIZE_Y
);
613 #pragma warning(default: 4355)
616 MyStatusBar::~MyStatusBar()
618 if ( m_timer
.IsRunning() )
624 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
626 static const int BMP_BUTTON_SIZE_X
= 10;
627 static const int BMP_BUTTON_SIZE_Y
= 9;
629 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
631 dc
.SelectObject(bitmap
);
632 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
633 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
635 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
636 dc
.SelectObject(wxNullBitmap
);
641 void MyStatusBar::OnSize(wxSizeEvent
& event
)
647 GetFieldRect(Field_Checkbox
, rect
);
649 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
651 GetFieldRect(Field_Bitmap
, rect
);
652 wxSize size
= m_statbmp
->GetSize();
654 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
655 rect
.y
+ (rect
.height
- size
.y
) / 2);
660 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
662 m_checkbox
->SetValue(!m_checkbox
->GetValue());
667 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
672 void MyStatusBar::DoToggle()
674 if ( m_checkbox
->GetValue() )
678 #ifdef USE_STATIC_BITMAP
679 m_statbmp
->SetIcon(wxIcon(green_xpm
));
681 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(false));
682 m_statbmp
->Refresh();
687 else // don't show clock
691 #ifdef USE_STATIC_BITMAP
692 m_statbmp
->SetIcon(wxIcon(red_xpm
));
694 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(true));
695 m_statbmp
->Refresh();
698 SetStatusText(wxEmptyString
, Field_Clock
);
702 void MyStatusBar::UpdateClock()
704 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);