]>
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"
51 // define this for the platforms which don't support wxBitmapButton (such as
52 // Motif), else a wxBitmapButton will be used
54 //#define USE_MDI_PARENT_FRAME 1
56 #ifdef USE_MDI_PARENT_FRAME
58 #endif // USE_MDI_PARENT_FRAME
59 #define USE_STATIC_BITMAP
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 #ifdef USE_STATIC_BITMAP
69 #endif // USE_STATIC_BITMAP
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // Define a new application type, each program should derive a class from wxApp
76 class MyApp
: public wxApp
79 // override base class virtuals
80 // ----------------------------
82 // this one is called on application startup and is a good place for the app
83 // initialization (doing it here and not in the ctor allows to have an error
84 // return: if OnInit() returns false, the application terminates)
85 virtual bool OnInit();
88 // A custom status bar which contains controls, icons &c
89 class MyStatusBar
: public wxStatusBar
92 MyStatusBar(wxWindow
*parent
);
93 virtual ~MyStatusBar();
98 void OnTimer(wxTimerEvent
& event
) { UpdateClock(); }
99 void OnSize(wxSizeEvent
& event
);
100 void OnToggleClock(wxCommandEvent
& event
);
101 void OnButton(wxCommandEvent
& event
);
104 // toggle the state of the status bar controls
107 wxBitmap
CreateBitmapForButton(bool on
= FALSE
);
120 wxCheckBox
*m_checkbox
;
121 #ifdef USE_STATIC_BITMAP
122 wxStaticBitmap
*m_statbmp
;
124 wxBitmapButton
*m_statbmp
;
127 DECLARE_EVENT_TABLE()
130 // Define a new frame type: this is going to be our main frame
131 class MyFrame
: public wxFrame
135 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
136 #ifdef USE_MDI_PARENT_FRAME
137 class MyFrame
: public wxMDIParentFrame
142 // event handlers (these functions should _not_ be virtual)
143 void OnQuit(wxCommandEvent
& event
);
144 void OnAbout(wxCommandEvent
& event
);
146 void OnSetStatusFields(wxCommandEvent
& event
);
147 void OnRecreateStatusBar(wxCommandEvent
& event
);
156 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
157 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
158 void OnStatusBarToggle(wxCommandEvent
& event
);
159 void DoCreateStatusBar(StatBarKind kind
);
161 wxStatusBar
*m_statbarDefault
;
162 MyStatusBar
*m_statbarCustom
;
164 // any class wishing to process wxWindows events must use this macro
165 DECLARE_EVENT_TABLE()
168 // Our about dialog ith its status bar
169 class MyAboutDialog
: public wxDialog
172 MyAboutDialog(wxWindow
*parent
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 // IDs for the controls and the menu commands
188 StatusBar_Checkbox
= 1000
191 static const int BITMAP_SIZE_X
= 32;
192 static const int BITMAP_SIZE_Y
= 15;
194 // ----------------------------------------------------------------------------
195 // event tables and other macros for wxWindows
196 // ----------------------------------------------------------------------------
198 // the event tables connect the wxWindows events with the functions (event
199 // handlers) which process them. It can be also done at run-time, but for the
200 // simple menu events like this the static method is much simpler.
201 #ifdef USE_MDI_PARENT_FRAME
202 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
204 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
206 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
207 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
208 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
209 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
210 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
211 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
212 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
215 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
216 EVT_SIZE(MyStatusBar::OnSize
)
217 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
218 EVT_BUTTON(-1, MyStatusBar::OnButton
)
219 EVT_TIMER(-1, MyStatusBar::OnTimer
)
222 // Create a new application object: this macro will allow wxWindows to create
223 // the application object during program execution (it's better than using a
224 // static object for many reasons) and also declares the accessor function
225 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
229 // ============================================================================
231 // ============================================================================
233 // ----------------------------------------------------------------------------
234 // the application class
235 // ----------------------------------------------------------------------------
237 // `Main program' equivalent: the program execution "starts" here
240 // create the main application window
241 MyFrame
*frame
= new MyFrame(_T("wxStatusBar sample"),
242 wxPoint(50, 50), wxSize(450, 340));
244 // and show it (the frames, unlike simple controls, are not shown when
245 // created initially)
248 // success: wxApp::OnRun() will be called which will enter the main message
249 // loop and the application will run. If we returned FALSE here, the
250 // application would exit immediately.
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
259 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
260 #ifdef USE_MDI_PARENT_FRAME
261 : wxMDIParentFrame((wxWindow
*)NULL
, -1, title
, pos
, size
)
263 : wxFrame((wxWindow
*)NULL
, -1, title
, pos
, size
)
266 m_statbarDefault
= NULL
;
267 m_statbarCustom
= NULL
;
270 // we need this in order to allow the about menu relocation, since ABOUT is
271 // not the default id of the about menu
272 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
276 wxMenu
*menuFile
= new wxMenu
;
277 menuFile
->Append(StatusBar_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
279 wxMenu
*statbarMenu
= new wxMenu
;
280 statbarMenu
->Append(StatusBar_SetFields
, _T("&Set field count\tCtrl-C"),
281 _T("Set the number of status bar fields"));
282 statbarMenu
->Append(StatusBar_Toggle
, _T("&Toggle Status Bar"),
283 _T("Toggle the status bar display"), true);
284 statbarMenu
->Append(StatusBar_Recreate
, _T("&Recreate\tCtrl-R"),
285 _T("Toggle status bar format"));
287 wxMenu
*helpMenu
= new wxMenu
;
288 helpMenu
->Append(StatusBar_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
290 // now append the freshly created menu to the menu bar...
291 wxMenuBar
*menuBar
= new wxMenuBar();
292 menuBar
->Append(menuFile
, _T("&File"));
293 menuBar
->Append(statbarMenu
, _T("&Status bar"));
294 menuBar
->Append(helpMenu
, _T("&Help"));
296 // ... and attach this menu bar to the frame
299 // create default status bar to start with
301 m_statbarKind
= StatBar_Default
;
302 SetStatusText(_T("Welcome to wxWindows!"));
304 m_statbarDefault
= GetStatusBar();
311 delete m_statbarDefault
;
312 delete m_statbarCustom
;
315 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
317 wxStatusBar
*statbarOld
= GetStatusBar();
325 case StatBar_Default
:
326 SetStatusBar(m_statbarDefault
);
330 if ( !m_statbarCustom
)
332 m_statbarCustom
= new MyStatusBar(this);
334 SetStatusBar(m_statbarCustom
);
338 wxFAIL_MSG(wxT("unknown stat bar kind"));
341 GetStatusBar()->Show();
344 m_statbarKind
= kind
;
347 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
349 // only allow the setting of the number of status fields for the default
351 wxStatusBar
*sb
= GetStatusBar();
352 event
.Enable(sb
== m_statbarDefault
);
357 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
359 wxStatusBar
*sb
= GetStatusBar();
361 long nFields
= wxGetNumberFromUser
363 _T("Select the number of fields in the status bar"),
365 _T("wxWindows statusbar sample"),
366 sb
->GetFieldsCount(),
371 // we don't check if the number changed at all on purpose: calling
372 // SetFieldsCount() with the same number of fields should be ok
375 static const int widthsFor2Fields
[] = { 200, -1 };
376 static const int widthsFor3Fields
[] = { -1, -2, -1 };
377 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
379 static const int *widthsAll
[] =
381 NULL
, // 1 field: default
382 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
383 widthsFor3Fields
, // 3 fields: 3 var
384 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
385 NULL
// 5 fields: default (all have same width)
388 const int * const widths
= widthsAll
[nFields
- 1];
389 sb
->SetFieldsCount(nFields
, widths
);
392 for ( long n
= 0; n
< nFields
; n
++ )
397 s
.Printf(_T("fixed (%d)"), widths
[n
]);
399 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
411 wxLogStatus(this, wxT("Cancelled"));
415 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
417 event
.Check(GetStatusBar() != 0);
420 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
422 wxStatusBar
*statbarOld
= GetStatusBar();
430 DoCreateStatusBar(m_statbarKind
);
433 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWindows
434 // authors) while we look for a proper fix..
439 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
441 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
445 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
447 // TRUE is to force the frame to close
451 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
453 MyAboutDialog
dlg(this);
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
462 : wxDialog(parent
, -1, wxString(_T("About statbar")),
463 wxDefaultPosition
, wxDefaultSize
,
464 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
466 wxStaticText
*text
= new wxStaticText(this, -1,
467 _T("wxStatusBar sample\n")
468 _T("(c) 2000 Vadim Zeitlin"));
470 wxButton
*btn
= new wxButton(this, wxID_OK
, _T("&Close"));
472 // create the top status bar without the size grip (default style),
473 // otherwise it looks weird
474 wxStatusBar
*statbarTop
= new wxStatusBar(this, -1, 0);
475 statbarTop
->SetFieldsCount(3);
476 statbarTop
->SetStatusText(_T("This is a top status bar"), 0);
477 statbarTop
->SetStatusText(_T("in a dialog"), 1);
478 statbarTop
->SetStatusText(_T("Great, isn't it?"), 2);
480 wxStatusBar
*statbarBottom
= new wxStatusBar(this, -1);
481 statbarBottom
->SetFieldsCount(2);
482 statbarBottom
->SetStatusText(_T("This is a bottom status bar"), 0);
483 statbarBottom
->SetStatusText(_T("in a dialog"), 1);
485 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
486 sizerTop
->Add(statbarTop
, 0, wxGROW
);
487 sizerTop
->Add(-1, 10, 1, wxGROW
);
488 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
489 sizerTop
->Add(-1, 10, 1, wxGROW
);
490 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
491 sizerTop
->Add(-1, 10, 1, wxGROW
);
492 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
498 sizerTop
->SetSizeHints(this);
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
506 // 'this' : used in base member initializer list -- so what??
507 #pragma warning(disable: 4355)
510 MyStatusBar::MyStatusBar(wxWindow
*parent
)
511 : wxStatusBar(parent
, -1), m_timer(this), m_checkbox(NULL
)
513 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
515 SetFieldsCount(Field_Max
);
516 SetStatusWidths(Field_Max
, widths
);
518 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
519 m_checkbox
->SetValue(TRUE
);
521 #ifdef USE_STATIC_BITMAP
522 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
524 m_statbmp
= new wxBitmapButton(this, -1, CreateBitmapForButton(),
525 wxDefaultPosition
, wxDefaultSize
,
531 SetMinHeight(BITMAP_SIZE_Y
);
537 #pragma warning(default: 4355)
540 MyStatusBar::~MyStatusBar()
542 if ( m_timer
.IsRunning() )
548 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
550 static const int BMP_BUTTON_SIZE_X
= 10;
551 static const int BMP_BUTTON_SIZE_Y
= 9;
553 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
555 dc
.SelectObject(bitmap
);
556 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
557 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
559 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
560 dc
.SelectObject(wxNullBitmap
);
565 void MyStatusBar::OnSize(wxSizeEvent
& event
)
571 GetFieldRect(Field_Checkbox
, rect
);
573 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
575 GetFieldRect(Field_Bitmap
, rect
);
576 wxSize size
= m_statbmp
->GetSize();
578 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
579 rect
.y
+ (rect
.height
- size
.y
) / 2);
584 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
586 m_checkbox
->SetValue(!m_checkbox
->GetValue());
591 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
596 void MyStatusBar::DoToggle()
598 if ( m_checkbox
->GetValue() )
602 #ifdef USE_STATIC_BITMAP
603 m_statbmp
->SetIcon(wxIcon(green_xpm
));
605 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(FALSE
));
606 m_statbmp
->Refresh();
611 else // don't show clock
615 #ifdef USE_STATIC_BITMAP
616 m_statbmp
->SetIcon(wxIcon(red_xpm
));
618 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(TRUE
));
619 m_statbmp
->Refresh();
622 SetStatusText(_T(""), Field_Clock
);
626 void MyStatusBar::UpdateClock()
628 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);