]>
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
35 #include "wx/statusbr.h"
37 #include "wx/checkbox.h"
38 #include "wx/statbmp.h"
40 #include "wx/msgdlg.h"
41 #include "wx/textdlg.h"
43 #include "wx/stattext.h"
44 #include "wx/bmpbuttn.h"
45 #include "wx/dcmemory.h"
48 #include "wx/datetime.h"
50 // define this for the platforms which don't support wxBitmapButton (such as
51 // Motif), else a wxBitmapButton will be used
53 //#define USE_MDI_PARENT_FRAME 1
55 #ifdef USE_MDI_PARENT_FRAME
57 #endif // USE_MDI_PARENT_FRAME
58 #define USE_STATIC_BITMAP
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #ifdef USE_STATIC_BITMAP
68 #endif // USE_STATIC_BITMAP
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // Define a new application type, each program should derive a class from wxApp
75 class MyApp
: public wxApp
78 // override base class virtuals
79 // ----------------------------
81 // this one is called on application startup and is a good place for the app
82 // initialization (doing it here and not in the ctor allows to have an error
83 // return: if OnInit() returns false, the application terminates)
84 virtual bool OnInit();
87 // A custom status bar which contains controls, icons &c
88 class MyStatusBar
: public wxStatusBar
91 MyStatusBar(wxWindow
*parent
);
92 virtual ~MyStatusBar();
97 void OnTimer(wxTimerEvent
& event
) { UpdateClock(); }
98 void OnSize(wxSizeEvent
& event
);
99 void OnToggleClock(wxCommandEvent
& event
);
100 void OnButton(wxCommandEvent
& event
);
103 // toggle the state of the status bar controls
106 wxBitmap
CreateBitmapForButton(bool on
= FALSE
);
119 wxCheckBox
*m_checkbox
;
120 #ifdef USE_STATIC_BITMAP
121 wxStaticBitmap
*m_statbmp
;
123 wxBitmapButton
*m_statbmp
;
126 DECLARE_EVENT_TABLE()
129 // Define a new frame type: this is going to be our main frame
130 class MyFrame
: public wxFrame
134 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
135 #ifdef USE_MDI_PARENT_FRAME
136 class MyFrame
: public wxMDIParentFrame
141 // event handlers (these functions should _not_ be virtual)
142 void OnQuit(wxCommandEvent
& event
);
143 void OnAbout(wxCommandEvent
& event
);
145 void OnSetStatusFields(wxCommandEvent
& event
);
146 void OnRecreateStatusBar(wxCommandEvent
& event
);
155 void OnUpdateSetStatusFields(wxUpdateUIEvent
& event
);
156 void OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
);
157 void OnStatusBarToggle(wxCommandEvent
& event
);
158 void DoCreateStatusBar(StatBarKind kind
);
160 wxStatusBar
*m_statbarDefault
;
161 MyStatusBar
*m_statbarCustom
;
163 // any class wishing to process wxWindows events must use this macro
164 DECLARE_EVENT_TABLE()
167 // Our about dialog ith its status bar
168 class MyAboutDialog
: public wxDialog
171 MyAboutDialog(wxWindow
*parent
);
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 // IDs for the controls and the menu commands
187 StatusBar_Checkbox
= 1000
190 static const int BITMAP_SIZE_X
= 32;
191 static const int BITMAP_SIZE_Y
= 15;
193 // ----------------------------------------------------------------------------
194 // event tables and other macros for wxWindows
195 // ----------------------------------------------------------------------------
197 // the event tables connect the wxWindows events with the functions (event
198 // handlers) which process them. It can be also done at run-time, but for the
199 // simple menu events like this the static method is much simpler.
200 #ifdef USE_MDI_PARENT_FRAME
201 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
203 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
205 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
206 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
207 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
208 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
209 EVT_MENU(StatusBar_Toggle
, MyFrame::OnStatusBarToggle
)
210 EVT_UPDATE_UI(StatusBar_Toggle
, MyFrame::OnUpdateStatusBarToggle
)
211 EVT_UPDATE_UI(StatusBar_SetFields
, MyFrame::OnUpdateSetStatusFields
)
214 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
215 EVT_SIZE(MyStatusBar::OnSize
)
216 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
217 EVT_BUTTON(-1, MyStatusBar::OnButton
)
218 EVT_TIMER(-1, MyStatusBar::OnTimer
)
221 // Create a new application object: this macro will allow wxWindows to create
222 // the application object during program execution (it's better than using a
223 // static object for many reasons) and also declares the accessor function
224 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
228 // ============================================================================
230 // ============================================================================
232 // ----------------------------------------------------------------------------
233 // the application class
234 // ----------------------------------------------------------------------------
236 // `Main program' equivalent: the program execution "starts" here
239 // create the main application window
240 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
241 wxPoint(50, 50), wxSize(450, 340));
243 // and show it (the frames, unlike simple controls, are not shown when
244 // created initially)
247 // success: wxApp::OnRun() will be called which will enter the main message
248 // loop and the application will run. If we returned FALSE here, the
249 // application would exit immediately.
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
258 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
259 #ifdef USE_MDI_PARENT_FRAME
260 : wxMDIParentFrame((wxWindow
*)NULL
, -1, title
, pos
, size
)
262 : wxFrame((wxWindow
*)NULL
, -1, title
, pos
, size
)
265 m_statbarDefault
= NULL
;
266 m_statbarCustom
= NULL
;
269 // we need this in order to allow the about menu relocation, since ABOUT is
270 // not the default id of the about menu
271 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
275 wxMenu
*menuFile
= new wxMenu
;
276 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
278 wxMenu
*statbarMenu
= new wxMenu
;
279 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
280 "Set the number of status bar fields");
281 statbarMenu
->Append(StatusBar_Toggle
, "&Toggle Status Bar",
282 "Toggle the status bar display", true);
283 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
284 "Toggle status bar format");
286 wxMenu
*helpMenu
= new wxMenu
;
287 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
289 // now append the freshly created menu to the menu bar...
290 wxMenuBar
*menuBar
= new wxMenuBar();
291 menuBar
->Append(menuFile
, "&File");
292 menuBar
->Append(statbarMenu
, "&Status bar");
293 menuBar
->Append(helpMenu
, "&Help");
295 // ... and attach this menu bar to the frame
298 // create default status bar to start with
300 m_statbarKind
= StatBar_Default
;
301 SetStatusText("Welcome to wxWindows!");
303 m_statbarDefault
= GetStatusBar();
310 delete m_statbarDefault
;
311 delete m_statbarCustom
;
314 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
316 wxStatusBar
*statbarOld
= GetStatusBar();
324 case StatBar_Default
:
325 SetStatusBar(m_statbarDefault
);
329 if ( !m_statbarCustom
)
331 m_statbarCustom
= new MyStatusBar(this);
333 SetStatusBar(m_statbarCustom
);
337 wxFAIL_MSG(wxT("unknown stat bar kind"));
340 GetStatusBar()->Show();
343 m_statbarKind
= kind
;
346 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent
& event
)
348 // only allow the setting of the number of status fields for the default
350 wxStatusBar
*sb
= GetStatusBar();
351 event
.Enable(sb
== m_statbarDefault
);
356 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
358 wxStatusBar
*sb
= GetStatusBar();
360 long nFields
= wxGetNumberFromUser
362 _T("Select the number of fields in the status bar"),
364 _T("wxWindows statusbar sample"),
365 sb
->GetFieldsCount(),
370 // we don't check if the number changed at all on purpose: calling
371 // SetFieldsCount() with the same number of fields should be ok
374 static const int widthsFor2Fields
[] = { 200, -1 };
375 static const int widthsFor3Fields
[] = { -1, -2, -1 };
376 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
378 static const int *widthsAll
[] =
380 NULL
, // 1 field: default
381 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
382 widthsFor3Fields
, // 3 fields: 3 var
383 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
384 NULL
// 5 fields: default (all have same width)
387 const int * const widths
= widthsAll
[nFields
- 1];
388 sb
->SetFieldsCount(nFields
, widths
);
391 for ( long n
= 0; n
< nFields
; n
++ )
396 s
.Printf(_T("fixed (%d)"), widths
[n
]);
398 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
410 wxLogStatus(this, wxT("Cancelled"));
414 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent
& event
)
416 event
.Check(GetStatusBar() != 0);
419 void MyFrame::OnStatusBarToggle(wxCommandEvent
& WXUNUSED(event
))
421 wxStatusBar
*statbarOld
= GetStatusBar();
429 DoCreateStatusBar(m_statbarKind
);
432 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWindows
433 // authors) while we look for a proper fix..
438 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
440 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
444 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
446 // TRUE is to force the frame to close
450 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
452 MyAboutDialog
dlg(this);
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
461 : wxDialog(parent
, -1, wxString("About statbar"),
462 wxDefaultPosition
, wxDefaultSize
,
463 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
465 wxStaticText
*text
= new wxStaticText(this, -1,
466 "wxStatusBar sample\n"
467 "(c) 2000 Vadim Zeitlin");
469 wxButton
*btn
= new wxButton(this, wxID_OK
, "&Close");
471 // create the top status bar without the size grip (default style),
472 // otherwise it looks weird
473 wxStatusBar
*statbarTop
= new wxStatusBar(this, -1, 0);
474 statbarTop
->SetFieldsCount(3);
475 statbarTop
->SetStatusText("This is a top status bar", 0);
476 statbarTop
->SetStatusText("in a dialog", 1);
477 statbarTop
->SetStatusText("Great, isn't it?", 2);
479 wxStatusBar
*statbarBottom
= new wxStatusBar(this, -1);
480 statbarBottom
->SetFieldsCount(2);
481 statbarBottom
->SetStatusText("This is a bottom status bar", 0);
482 statbarBottom
->SetStatusText("in a dialog", 1);
484 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
485 sizerTop
->Add(statbarTop
, 0, wxGROW
);
486 sizerTop
->Add(-1, 10, 1, wxGROW
);
487 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
488 sizerTop
->Add(-1, 10, 1, wxGROW
);
489 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
490 sizerTop
->Add(-1, 10, 1, wxGROW
);
491 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
497 sizerTop
->SetSizeHints(this);
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
505 // 'this' : used in base member initializer list -- so what??
506 #pragma warning(disable: 4355)
509 MyStatusBar::MyStatusBar(wxWindow
*parent
)
510 : wxStatusBar(parent
, -1), m_timer(this), m_checkbox(NULL
)
512 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
514 SetFieldsCount(Field_Max
);
515 SetStatusWidths(Field_Max
, widths
);
517 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
518 m_checkbox
->SetValue(TRUE
);
520 #ifdef USE_STATIC_BITMAP
521 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
523 m_statbmp
= new wxBitmapButton(this, -1, CreateBitmapForButton(),
524 wxDefaultPosition
, wxDefaultSize
,
530 SetMinHeight(BITMAP_SIZE_Y
);
536 #pragma warning(default: 4355)
539 MyStatusBar::~MyStatusBar()
541 if ( m_timer
.IsRunning() )
547 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
549 static const int BMP_BUTTON_SIZE_X
= 10;
550 static const int BMP_BUTTON_SIZE_Y
= 9;
552 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
554 dc
.SelectObject(bitmap
);
555 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
556 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
558 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
559 dc
.SelectObject(wxNullBitmap
);
564 void MyStatusBar::OnSize(wxSizeEvent
& event
)
570 GetFieldRect(Field_Checkbox
, rect
);
572 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
574 GetFieldRect(Field_Bitmap
, rect
);
575 wxSize size
= m_statbmp
->GetSize();
577 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
578 rect
.y
+ (rect
.height
- size
.y
) / 2);
583 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
585 m_checkbox
->SetValue(!m_checkbox
->GetValue());
590 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
595 void MyStatusBar::DoToggle()
597 if ( m_checkbox
->GetValue() )
601 #ifdef USE_STATIC_BITMAP
602 m_statbmp
->SetIcon(wxIcon(green_xpm
));
604 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(FALSE
));
605 m_statbmp
->Refresh();
610 else // don't show clock
614 #ifdef USE_STATIC_BITMAP
615 m_statbmp
->SetIcon(wxIcon(red_xpm
));
617 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(TRUE
));
618 m_statbmp
->Refresh();
621 SetStatusText("", Field_Clock
);
625 void MyStatusBar::UpdateClock()
627 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);