]>
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 // ----------------------------------------------------------------------------
21 #pragma implementation "statbar.cpp"
22 #pragma interface "statbar.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
33 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
34 #endif // wxUSE_STATUSBAR
36 // for all others, include the necessary headers
40 #include "wx/statusbr.h"
42 #include "wx/checkbox.h"
43 #include "wx/statbmp.h"
45 #include "wx/msgdlg.h"
46 #include "wx/textdlg.h"
48 #include "wx/stattext.h"
49 #include "wx/bmpbuttn.h"
50 #include "wx/dcmemory.h"
53 #include "wx/datetime.h"
55 // define this for the platforms which don't support wxBitmapButton (such as
56 // Motif), else a wxBitmapButton will be used
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
);
137 // event handlers (these functions should _not_ be virtual)
138 void OnQuit(wxCommandEvent
& event
);
139 void OnAbout(wxCommandEvent
& event
);
141 void OnSetStatusFields(wxCommandEvent
& event
);
142 void OnRecreateStatusBar(wxCommandEvent
& event
);
152 void DoCreateStatusBar(StatBarKind kind
);
154 wxStatusBar
*m_statbarDefault
;
155 MyStatusBar
*m_statbarCustom
;
157 // any class wishing to process wxWindows events must use this macro
158 DECLARE_EVENT_TABLE()
161 // Our about dialog ith its status bar
162 class MyAboutDialog
: public wxDialog
165 MyAboutDialog(wxWindow
*parent
);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 // IDs for the controls and the menu commands
180 StatusBar_Checkbox
= 1000
183 static const int BITMAP_SIZE_X
= 32;
184 static const int BITMAP_SIZE_Y
= 15;
186 // ----------------------------------------------------------------------------
187 // event tables and other macros for wxWindows
188 // ----------------------------------------------------------------------------
190 // the event tables connect the wxWindows events with the functions (event
191 // handlers) which process them. It can be also done at run-time, but for the
192 // simple menu events like this the static method is much simpler.
193 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
194 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
195 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
196 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
197 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
200 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
201 EVT_SIZE(MyStatusBar::OnSize
)
202 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
203 EVT_BUTTON(-1, MyStatusBar::OnButton
)
204 EVT_TIMER(-1, MyStatusBar::OnTimer
)
207 // Create a new application object: this macro will allow wxWindows to create
208 // the application object during program execution (it's better than using a
209 // static object for many reasons) and also declares the accessor function
210 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
214 // ============================================================================
216 // ============================================================================
218 // ----------------------------------------------------------------------------
219 // the application class
220 // ----------------------------------------------------------------------------
222 // `Main program' equivalent: the program execution "starts" here
225 // create the main application window
226 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
227 wxPoint(50, 50), wxSize(450, 340));
229 // and show it (the frames, unlike simple controls, are not shown when
230 // created initially)
233 // success: wxApp::OnRun() will be called which will enter the main message
234 // loop and the application will run. If we returned FALSE here, the
235 // application would exit immediately.
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
244 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
245 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
247 m_statbarDefault
= NULL
;
248 m_statbarCustom
= NULL
;
251 // we need this in order to allow the about menu relocation, since ABOUT is
252 // not the default id of the about menu
253 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
257 wxMenu
*menuFile
= new wxMenu
;
258 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
260 wxMenu
*statbarMenu
= new wxMenu
;
261 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
262 "Set the number of status bar fields");
263 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
264 "Toggle status bar format");
266 wxMenu
*helpMenu
= new wxMenu
;
267 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
269 // now append the freshly created menu to the menu bar...
270 wxMenuBar
*menuBar
= new wxMenuBar();
271 menuBar
->Append(menuFile
, "&File");
272 menuBar
->Append(statbarMenu
, "&Status bar");
273 menuBar
->Append(helpMenu
, "&Help");
275 // ... and attach this menu bar to the frame
278 // create default status bar to start with
280 SetStatusText("Welcome to wxWindows!");
282 m_statbarDefault
= GetStatusBar();
289 delete m_statbarDefault
;
290 delete m_statbarCustom
;
293 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
295 wxStatusBar
*statbarOld
= GetStatusBar();
303 case StatBar_Default
:
304 SetStatusBar(m_statbarDefault
);
308 if ( !m_statbarCustom
)
310 m_statbarCustom
= new MyStatusBar(this);
312 SetStatusBar(m_statbarCustom
);
316 wxFAIL_MSG("unknown stat bar kind");
319 GetStatusBar()->Show();
322 m_statbarKind
= kind
;
326 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
328 wxStatusBar
*sb
= GetStatusBar();
330 long nFields
= wxGetNumberFromUser
332 "Select the number of fields in the status bar",
334 "wxWindows statusbar sample",
335 sb
->GetFieldsCount(),
340 // we don't check if the number changed at all on purpose: calling
341 // SetFieldsCount() with the same number of fields should be ok
344 // we set the widths only for 2 of them, otherwise let all the fields
345 // have equal width (the default behaviour)
346 const int *widths
= NULL
;
349 static const int widthsFor2Fields
[2] = { 200, -1 };
350 widths
= widthsFor2Fields
;
353 sb
->SetFieldsCount(nFields
, widths
);
356 wxString::Format(wxT("Status bar now has %ld fields"),
361 wxLogStatus(this, wxT("Cancelled"));
365 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
367 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
371 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
373 // TRUE is to force the frame to close
377 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
379 MyAboutDialog
dlg(this);
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
388 : wxDialog(parent
, -1, wxString("About statbar"),
389 wxDefaultPosition
, wxDefaultSize
,
390 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
392 wxStaticText
*text
= new wxStaticText(this, -1,
393 "wxStatusBar sample\n"
394 "(c) 2000 Vadim Zeitlin");
396 wxButton
*btn
= new wxButton(this, wxID_OK
, "&Close");
398 // create the top status bar without the size grip (default style),
399 // otherwise it looks weird
400 wxStatusBar
*statbarTop
= new wxStatusBar(this, -1, 0);
401 statbarTop
->SetFieldsCount(3);
402 statbarTop
->SetStatusText("This is a top status bar", 0);
403 statbarTop
->SetStatusText("in a dialog", 1);
404 statbarTop
->SetStatusText("Great, isn't it?", 2);
406 wxStatusBar
*statbarBottom
= new wxStatusBar(this, -1);
407 statbarBottom
->SetFieldsCount(2);
408 statbarBottom
->SetStatusText("This is a bottom status bar", 0);
409 statbarBottom
->SetStatusText("in a dialog", 1);
411 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
412 sizerTop
->Add(statbarTop
, 0, wxGROW
);
413 sizerTop
->Add(-1, 10, 1, wxGROW
);
414 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
415 sizerTop
->Add(-1, 10, 1, wxGROW
);
416 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
417 sizerTop
->Add(-1, 10, 1, wxGROW
);
418 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
424 sizerTop
->SetSizeHints(this);
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
432 // 'this' : used in base member initializer list -- so what??
433 #pragma warning(disable: 4355)
436 MyStatusBar::MyStatusBar(wxWindow
*parent
)
437 : wxStatusBar(parent
, -1), m_timer(this), m_checkbox(NULL
)
439 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
441 SetFieldsCount(Field_Max
);
442 SetStatusWidths(Field_Max
, widths
);
444 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
445 m_checkbox
->SetValue(TRUE
);
447 #ifdef USE_STATIC_BITMAP
448 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
450 m_statbmp
= new wxBitmapButton(this, -1, CreateBitmapForButton());
455 SetMinHeight(BITMAP_SIZE_Y
);
461 #pragma warning(default: 4355)
464 MyStatusBar::~MyStatusBar()
466 if ( m_timer
.IsRunning() )
472 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
474 static const int BMP_BUTTON_SIZE_X
= 10;
475 static const int BMP_BUTTON_SIZE_Y
= 9;
477 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
479 dc
.SelectObject(bitmap
);
480 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
481 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
483 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
484 dc
.SelectObject(wxNullBitmap
);
489 void MyStatusBar::OnSize(wxSizeEvent
& event
)
495 GetFieldRect(Field_Checkbox
, rect
);
497 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
499 GetFieldRect(Field_Bitmap
, rect
);
500 #ifdef USE_BUTTON_FOR_BITMAP
501 wxSize
size(BITMAP_SIZE_X
, BITMAP_SIZE_Y
);
503 wxSize size
= m_statbmp
->GetSize();
506 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
507 rect
.y
+ (rect
.height
- size
.y
) / 2);
512 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
514 m_checkbox
->SetValue(!m_checkbox
->GetValue());
519 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
524 void MyStatusBar::DoToggle()
526 if ( m_checkbox
->GetValue() )
530 #ifdef USE_STATIC_BITMAP
531 m_statbmp
->SetIcon(wxIcon(green_xpm
));
533 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(FALSE
));
534 m_statbmp
->Refresh();
539 else // don't show clock
543 #ifdef USE_STATIC_BITMAP
544 m_statbmp
->SetIcon(wxIcon(red_xpm
));
546 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(TRUE
));
547 m_statbmp
->Refresh();
550 SetStatusText("", Field_Clock
);
554 void MyStatusBar::UpdateClock()
556 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);