]>
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_STATIC_BITMAP
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 #ifdef USE_STATIC_BITMAP
63 #endif // USE_STATIC_BITMAP
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // Define a new application type, each program should derive a class from wxApp
70 class MyApp
: public wxApp
73 // override base class virtuals
74 // ----------------------------
76 // this one is called on application startup and is a good place for the app
77 // initialization (doing it here and not in the ctor allows to have an error
78 // return: if OnInit() returns false, the application terminates)
79 virtual bool OnInit();
82 // A custom status bar which contains controls, icons &c
83 class MyStatusBar
: public wxStatusBar
86 MyStatusBar(wxWindow
*parent
);
87 virtual ~MyStatusBar();
92 void OnTimer(wxTimerEvent
& event
) { UpdateClock(); }
93 void OnSize(wxSizeEvent
& event
);
94 void OnToggleClock(wxCommandEvent
& event
);
95 void OnButton(wxCommandEvent
& event
);
98 // toggle the state of the status bar controls
101 wxBitmap
CreateBitmapForButton(bool on
= FALSE
);
114 wxCheckBox
*m_checkbox
;
115 #ifdef USE_STATIC_BITMAP
116 wxStaticBitmap
*m_statbmp
;
118 wxBitmapButton
*m_statbmp
;
121 DECLARE_EVENT_TABLE()
124 // Define a new frame type: this is going to be our main frame
125 class MyFrame
: public wxFrame
129 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
132 // event handlers (these functions should _not_ be virtual)
133 void OnQuit(wxCommandEvent
& event
);
134 void OnAbout(wxCommandEvent
& event
);
136 void OnSetStatusFields(wxCommandEvent
& event
);
137 void OnRecreateStatusBar(wxCommandEvent
& event
);
147 void DoCreateStatusBar(StatBarKind kind
);
149 wxStatusBar
*m_statbarDefault
;
150 MyStatusBar
*m_statbarCustom
;
152 // any class wishing to process wxWindows events must use this macro
153 DECLARE_EVENT_TABLE()
156 // Our about dialog ith its status bar
157 class MyAboutDialog
: public wxDialog
160 MyAboutDialog(wxWindow
*parent
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 // IDs for the controls and the menu commands
175 StatusBar_Checkbox
= 1000
178 static const int BITMAP_SIZE_X
= 32;
179 static const int BITMAP_SIZE_Y
= 15;
181 // ----------------------------------------------------------------------------
182 // event tables and other macros for wxWindows
183 // ----------------------------------------------------------------------------
185 // the event tables connect the wxWindows events with the functions (event
186 // handlers) which process them. It can be also done at run-time, but for the
187 // simple menu events like this the static method is much simpler.
188 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
189 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
190 EVT_MENU(StatusBar_SetFields
, MyFrame::OnSetStatusFields
)
191 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
192 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
195 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
196 EVT_SIZE(MyStatusBar::OnSize
)
197 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
198 EVT_BUTTON(-1, MyStatusBar::OnButton
)
199 EVT_TIMER(-1, MyStatusBar::OnTimer
)
202 // Create a new application object: this macro will allow wxWindows to create
203 // the application object during program execution (it's better than using a
204 // static object for many reasons) and also declares the accessor function
205 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
209 // ============================================================================
211 // ============================================================================
213 // ----------------------------------------------------------------------------
214 // the application class
215 // ----------------------------------------------------------------------------
217 // `Main program' equivalent: the program execution "starts" here
220 // create the main application window
221 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
222 wxPoint(50, 50), wxSize(450, 340));
224 // and show it (the frames, unlike simple controls, are not shown when
225 // created initially)
228 // success: wxApp::OnRun() will be called which will enter the main message
229 // loop and the application will run. If we returned FALSE here, the
230 // application would exit immediately.
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
239 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
240 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
242 m_statbarDefault
= NULL
;
243 m_statbarCustom
= NULL
;
246 // we need this in order to allow the about menu relocation, since ABOUT is
247 // not the default id of the about menu
248 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
252 wxMenu
*menuFile
= new wxMenu
;
253 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
255 wxMenu
*statbarMenu
= new wxMenu
;
256 statbarMenu
->Append(StatusBar_SetFields
, "&Set field count\tCtrl-C",
257 "Set the number of status bar fields");
258 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
259 "Toggle status bar format");
261 wxMenu
*helpMenu
= new wxMenu
;
262 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
264 // now append the freshly created menu to the menu bar...
265 wxMenuBar
*menuBar
= new wxMenuBar();
266 menuBar
->Append(menuFile
, "&File");
267 menuBar
->Append(statbarMenu
, "&Status bar");
268 menuBar
->Append(helpMenu
, "&Help");
270 // ... and attach this menu bar to the frame
273 // create default status bar to start with
275 SetStatusText("Welcome to wxWindows!");
277 m_statbarDefault
= GetStatusBar();
284 delete m_statbarDefault
;
285 delete m_statbarCustom
;
288 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
290 wxStatusBar
*statbarOld
= GetStatusBar();
298 case StatBar_Default
:
299 SetStatusBar(m_statbarDefault
);
303 if ( !m_statbarCustom
)
305 m_statbarCustom
= new MyStatusBar(this);
307 SetStatusBar(m_statbarCustom
);
311 wxFAIL_MSG("unknown stat bar kind");
314 GetStatusBar()->Show();
317 m_statbarKind
= kind
;
321 void MyFrame::OnSetStatusFields(wxCommandEvent
& WXUNUSED(event
))
323 wxStatusBar
*sb
= GetStatusBar();
325 long nFields
= wxGetNumberFromUser
327 _T("Select the number of fields in the status bar"),
329 _T("wxWindows statusbar sample"),
330 sb
->GetFieldsCount(),
335 // we don't check if the number changed at all on purpose: calling
336 // SetFieldsCount() with the same number of fields should be ok
339 static const int widthsFor2Fields
[] = { 200, -1 };
340 static const int widthsFor3Fields
[] = { -1, -2, -1 };
341 static const int widthsFor4Fields
[] = { 100, -1, 100, -2, 100 };
343 static const int *widthsAll
[] =
345 NULL
, // 1 field: default
346 widthsFor2Fields
, // 2 fields: 1 fixed, 1 var
347 widthsFor3Fields
, // 3 fields: 3 var
348 widthsFor4Fields
, // 4 fields: 3 fixed, 2 vars
349 NULL
// 5 fields: default (all have same width)
352 const int * const widths
= widthsAll
[nFields
- 1];
353 sb
->SetFieldsCount(nFields
, widths
);
356 for ( long n
= 0; n
< nFields
; n
++ )
361 s
.Printf(_T("fixed (%d)"), widths
[n
]);
363 s
.Printf(_T("variable (*%d)"), -widths
[n
]);
375 wxLogStatus(this, wxT("Cancelled"));
379 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
381 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
385 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
387 // TRUE is to force the frame to close
391 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
393 MyAboutDialog
dlg(this);
397 // ----------------------------------------------------------------------------
399 // ----------------------------------------------------------------------------
401 MyAboutDialog::MyAboutDialog(wxWindow
*parent
)
402 : wxDialog(parent
, -1, wxString("About statbar"),
403 wxDefaultPosition
, wxDefaultSize
,
404 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
406 wxStaticText
*text
= new wxStaticText(this, -1,
407 "wxStatusBar sample\n"
408 "(c) 2000 Vadim Zeitlin");
410 wxButton
*btn
= new wxButton(this, wxID_OK
, "&Close");
412 // create the top status bar without the size grip (default style),
413 // otherwise it looks weird
414 wxStatusBar
*statbarTop
= new wxStatusBar(this, -1, 0);
415 statbarTop
->SetFieldsCount(3);
416 statbarTop
->SetStatusText("This is a top status bar", 0);
417 statbarTop
->SetStatusText("in a dialog", 1);
418 statbarTop
->SetStatusText("Great, isn't it?", 2);
420 wxStatusBar
*statbarBottom
= new wxStatusBar(this, -1);
421 statbarBottom
->SetFieldsCount(2);
422 statbarBottom
->SetStatusText("This is a bottom status bar", 0);
423 statbarBottom
->SetStatusText("in a dialog", 1);
425 wxBoxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
426 sizerTop
->Add(statbarTop
, 0, wxGROW
);
427 sizerTop
->Add(-1, 10, 1, wxGROW
);
428 sizerTop
->Add(text
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
429 sizerTop
->Add(-1, 10, 1, wxGROW
);
430 sizerTop
->Add(btn
, 0, wxCENTRE
| wxRIGHT
| wxLEFT
, 20);
431 sizerTop
->Add(-1, 10, 1, wxGROW
);
432 sizerTop
->Add(statbarBottom
, 0, wxGROW
);
438 sizerTop
->SetSizeHints(this);
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
446 // 'this' : used in base member initializer list -- so what??
447 #pragma warning(disable: 4355)
450 MyStatusBar::MyStatusBar(wxWindow
*parent
)
451 : wxStatusBar(parent
, -1), m_timer(this), m_checkbox(NULL
)
453 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
455 SetFieldsCount(Field_Max
);
456 SetStatusWidths(Field_Max
, widths
);
458 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
459 m_checkbox
->SetValue(TRUE
);
461 #ifdef USE_STATIC_BITMAP
462 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
464 m_statbmp
= new wxBitmapButton(this, -1, CreateBitmapForButton(),
465 wxDefaultPosition
, wxDefaultSize
,
471 SetMinHeight(BITMAP_SIZE_Y
);
477 #pragma warning(default: 4355)
480 MyStatusBar::~MyStatusBar()
482 if ( m_timer
.IsRunning() )
488 wxBitmap
MyStatusBar::CreateBitmapForButton(bool on
)
490 static const int BMP_BUTTON_SIZE_X
= 10;
491 static const int BMP_BUTTON_SIZE_Y
= 9;
493 wxBitmap
bitmap(BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
495 dc
.SelectObject(bitmap
);
496 dc
.SetBrush(on
? *wxGREEN_BRUSH
: *wxRED_BRUSH
);
497 dc
.SetBackground(*wxLIGHT_GREY_BRUSH
);
499 dc
.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X
, BMP_BUTTON_SIZE_Y
);
500 dc
.SelectObject(wxNullBitmap
);
505 void MyStatusBar::OnSize(wxSizeEvent
& event
)
511 GetFieldRect(Field_Checkbox
, rect
);
513 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
515 GetFieldRect(Field_Bitmap
, rect
);
516 wxSize size
= m_statbmp
->GetSize();
518 m_statbmp
->Move(rect
.x
+ (rect
.width
- size
.x
) / 2,
519 rect
.y
+ (rect
.height
- size
.y
) / 2);
524 void MyStatusBar::OnButton(wxCommandEvent
& WXUNUSED(event
))
526 m_checkbox
->SetValue(!m_checkbox
->GetValue());
531 void MyStatusBar::OnToggleClock(wxCommandEvent
& WXUNUSED(event
))
536 void MyStatusBar::DoToggle()
538 if ( m_checkbox
->GetValue() )
542 #ifdef USE_STATIC_BITMAP
543 m_statbmp
->SetIcon(wxIcon(green_xpm
));
545 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(FALSE
));
546 m_statbmp
->Refresh();
551 else // don't show clock
555 #ifdef USE_STATIC_BITMAP
556 m_statbmp
->SetIcon(wxIcon(red_xpm
));
558 m_statbmp
->SetBitmapLabel(CreateBitmapForButton(TRUE
));
559 m_statbmp
->Refresh();
562 SetStatusText("", Field_Clock
);
566 void MyStatusBar::UpdateClock()
568 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);