]>
git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
dc3e41156dcf0fd2e959a38ba283daa8127ba64c
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 (this file is usually all you
37 // need because it includes almost all "standard" wxWindows headers
41 #include "wx/statusbr.h"
42 #include "wx/datetime.h"
44 #include "wx/checkbox.h"
45 #include "wx/statbmp.h"
47 #include "wx/msgdlg.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp
: public wxApp
65 // override base class virtuals
66 // ----------------------------
68 // this one is called on application startup and is a good place for the app
69 // initialization (doing it here and not in the ctor allows to have an error
70 // return: if OnInit() returns false, the application terminates)
71 virtual bool OnInit();
74 // A custom status bar which contains controls, icons &c
75 class MyStatusBar
: public wxStatusBar
78 MyStatusBar(wxWindow
*parent
);
79 virtual ~MyStatusBar();
84 void OnSize(wxSizeEvent
& event
);
85 void OnToggleClock(wxCommandEvent
& event
);
97 class MyTimer
: public wxTimer
100 MyTimer(MyStatusBar
*statbar
) {m_statbar
= statbar
; }
102 virtual void Notify() { m_statbar
->UpdateClock(); }
105 MyStatusBar
*m_statbar
;
108 wxCheckBox
*m_checkbox
;
109 wxStaticBitmap
*m_statbmp
;
111 DECLARE_EVENT_TABLE()
114 // Define a new frame type: this is going to be our main frame
115 class MyFrame
: public wxFrame
119 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
122 // event handlers (these functions should _not_ be virtual)
123 void OnQuit(wxCommandEvent
& event
);
124 void OnAbout(wxCommandEvent
& event
);
125 void OnRecreateStatusBar(wxCommandEvent
& event
);
135 void DoCreateStatusBar(StatBarKind kind
);
137 wxStatusBar
*m_statbarDefault
;
138 MyStatusBar
*m_statbarCustom
;
140 // any class wishing to process wxWindows events must use this macro
141 DECLARE_EVENT_TABLE()
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 // IDs for the controls and the menu commands
155 StatusBar_Checkbox
= 1000
158 static const int BITMAP_SIZE_X
= 32;
159 static const int BITMAP_SIZE_Y
= 15;
161 // ----------------------------------------------------------------------------
162 // event tables and other macros for wxWindows
163 // ----------------------------------------------------------------------------
165 // the event tables connect the wxWindows events with the functions (event
166 // handlers) which process them. It can be also done at run-time, but for the
167 // simple menu events like this the static method is much simpler.
168 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
169 EVT_MENU(StatusBar_Quit
, MyFrame::OnQuit
)
170 EVT_MENU(StatusBar_Recreate
, MyFrame::OnRecreateStatusBar
)
171 EVT_MENU(StatusBar_About
, MyFrame::OnAbout
)
174 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
175 EVT_SIZE(MyStatusBar::OnSize
)
176 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar::OnToggleClock
)
179 // Create a new application object: this macro will allow wxWindows to create
180 // the application object during program execution (it's better than using a
181 // static object for many reasons) and also declares the accessor function
182 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
186 // ============================================================================
188 // ============================================================================
190 // ----------------------------------------------------------------------------
191 // the application class
192 // ----------------------------------------------------------------------------
194 // `Main program' equivalent: the program execution "starts" here
197 // create the main application window
198 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
199 wxPoint(50, 50), wxSize(450, 340));
201 // and show it (the frames, unlike simple controls, are not shown when
202 // created initially)
205 // success: wxApp::OnRun() will be called which will enter the main message
206 // loop and the application will run. If we returned FALSE here, the
207 // application would exit immediately.
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
216 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
217 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
219 m_statbarDefault
= NULL
;
220 m_statbarCustom
= NULL
;
223 // we need this in order to allow the about menu relocation, since ABOUT is
224 // not the default id of the about menu
225 wxApp::s_macAboutMenuItemId
= StatusBar_About
;
229 wxMenu
*menuFile
= new wxMenu
;
230 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
232 wxMenu
*statbarMenu
= new wxMenu
;
233 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
234 "Toggle status bar format");
236 wxMenu
*helpMenu
= new wxMenu
;
237 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
239 // now append the freshly created menu to the menu bar...
240 wxMenuBar
*menuBar
= new wxMenuBar();
241 menuBar
->Append(menuFile
, "&File");
242 menuBar
->Append(statbarMenu
, "&Status bar");
243 menuBar
->Append(helpMenu
, "&Help");
245 // ... and attach this menu bar to the frame
248 // create default status bar to start with
250 SetStatusText("Welcome to wxWindows!");
252 m_statbarDefault
= GetStatusBar();
259 delete m_statbarDefault
;
260 delete m_statbarCustom
;
263 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind
)
265 wxStatusBar
*statbarOld
= GetStatusBar();
273 case StatBar_Default
:
274 SetStatusBar(m_statbarDefault
);
278 if ( !m_statbarCustom
)
280 m_statbarCustom
= new MyStatusBar(this);
282 SetStatusBar(m_statbarCustom
);
286 wxFAIL_MSG("unknown stat bar kind");
290 GetStatusBar()->Show();
292 m_statbarKind
= kind
;
296 void MyFrame::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
298 DoCreateStatusBar(m_statbarKind
== StatBar_Custom
? StatBar_Default
302 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
304 // TRUE is to force the frame to close
308 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
310 wxMessageBox("wxStatusBar sample\n(c) 2000 Vadim Zeitlin",
311 "About statbar", wxOK
| wxICON_INFORMATION
, this);
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 MyStatusBar::MyStatusBar(wxWindow
*parent
)
319 : wxStatusBar(parent
, -1), m_timer(this)
321 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
323 SetFieldsCount(Field_Max
);
324 SetStatusWidths(Field_Max
, widths
);
326 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
327 m_checkbox
->SetValue(TRUE
);
329 m_statbmp
= new wxStaticBitmap(this, -1, wxICON(green
));
336 MyStatusBar::~MyStatusBar()
338 if ( m_timer
.IsRunning() )
344 void MyStatusBar::OnSize(wxSizeEvent
& event
)
347 GetFieldRect(Field_Checkbox
, rect
);
349 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
351 GetFieldRect(Field_Bitmap
, rect
);
352 m_statbmp
->Move(rect
.x
+ (rect
.width
- BITMAP_SIZE_X
) / 2,
353 rect
.y
+ (rect
.height
- BITMAP_SIZE_Y
) / 2);
358 void MyStatusBar::OnToggleClock(wxCommandEvent
& event
)
360 if ( m_checkbox
->GetValue() )
364 m_statbmp
->SetIcon(wxICON(green
));
368 else // don't show clock
372 m_statbmp
->SetIcon(wxICON(red
));
374 SetStatusText("", Field_Clock
);
378 void MyStatusBar::UpdateClock()
380 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock
);