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"
43 #include "wx/checkbox.h"
44 #include "wx/statbmp.h"
46 #include "wx/msgdlg.h"
49 #include "wx/datetime.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 // ----------------------------
69 // this one is called on application startup and is a good place for the app
70 // initialization (doing it here and not in the ctor allows to have an error
71 // return: if OnInit() returns false, the application terminates)
72 virtual bool OnInit();
75 // A custom status bar which contains controls, icons &c
76 class MyStatusBar
: public wxStatusBar
79 MyStatusBar(wxWindow
*parent
);
80 virtual ~MyStatusBar();
85 void OnSize(wxSizeEvent
& event
);
86 void OnToggleClock(wxCommandEvent
& event
);
98 class MyTimer
: public wxTimer
101 MyTimer(MyStatusBar
*statbar
) {m_statbar
= statbar
; }
103 virtual void Notify() { m_statbar
->UpdateClock(); }
106 MyStatusBar
*m_statbar
;
109 wxCheckBox
*m_checkbox
;
110 wxStaticBitmap
*m_statbmp
;
112 DECLARE_EVENT_TABLE()
115 // Define a new frame type: this is going to be our main frame
116 class MyFrame
: public wxFrame
120 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
123 // event handlers (these functions should _not_ be virtual)
124 void OnQuit(wxCommandEvent
& event
);
125 void OnAbout(wxCommandEvent
& event
);
126 void OnRecreateStatusBar(wxCommandEvent
& event
);
136 void DoCreateStatusBar(StatBarKind kind
);
138 wxStatusBar
*m_statbarDefault
;
139 MyStatusBar
*m_statbarCustom
;
141 // any class wishing to process wxWindows events must use this macro
142 DECLARE_EVENT_TABLE()
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // IDs for the controls and the menu commands
156 StatusBar_Checkbox
= 1000
159 static const int BITMAP_SIZE_X
= 32;
160 static const int BITMAP_SIZE_Y
= 15;
162 // ----------------------------------------------------------------------------
163 // event tables and other macros for wxWindows
164 // ----------------------------------------------------------------------------
166 // the event tables connect the wxWindows events with the functions (event
167 // handlers) which process them. It can be also done at run-time, but for the
168 // simple menu events like this the static method is much simpler.
169 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
170 EVT_MENU(StatusBar_Quit
, MyFrame
::OnQuit
)
171 EVT_MENU(StatusBar_Recreate
, MyFrame
::OnRecreateStatusBar
)
172 EVT_MENU(StatusBar_About
, MyFrame
::OnAbout
)
175 BEGIN_EVENT_TABLE(MyStatusBar
, wxStatusBar
)
176 EVT_SIZE(MyStatusBar
::OnSize
)
177 EVT_CHECKBOX(StatusBar_Checkbox
, MyStatusBar
::OnToggleClock
)
180 // Create a new application object: this macro will allow wxWindows to create
181 // the application object during program execution (it's better than using a
182 // static object for many reasons) and also declares the accessor function
183 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
187 // ============================================================================
189 // ============================================================================
191 // ----------------------------------------------------------------------------
192 // the application class
193 // ----------------------------------------------------------------------------
195 // `Main program' equivalent: the program execution "starts" here
198 // create the main application window
199 MyFrame
*frame
= new MyFrame("wxStatusBar sample",
200 wxPoint(50, 50), wxSize(450, 340));
202 // and show it (the frames, unlike simple controls, are not shown when
203 // created initially)
206 // success: wxApp::OnRun() will be called which will enter the main message
207 // loop and the application will run. If we returned FALSE here, the
208 // application would exit immediately.
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
217 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
218 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
220 m_statbarDefault
= NULL
;
221 m_statbarCustom
= NULL
;
224 // we need this in order to allow the about menu relocation, since ABOUT is
225 // not the default id of the about menu
226 wxApp
::s_macAboutMenuItemId
= StatusBar_About
;
230 wxMenu
*menuFile
= new wxMenu
;
231 menuFile
->Append(StatusBar_Quit
, "E&xit\tAlt-X", "Quit this program");
233 wxMenu
*statbarMenu
= new wxMenu
;
234 statbarMenu
->Append(StatusBar_Recreate
, "&Recreate\tCtrl-R",
235 "Toggle status bar format");
237 wxMenu
*helpMenu
= new wxMenu
;
238 helpMenu
->Append(StatusBar_About
, "&About...\tCtrl-A", "Show about dialog");
240 // now append the freshly created menu to the menu bar...
241 wxMenuBar
*menuBar
= new wxMenuBar();
242 menuBar
->Append(menuFile
, "&File");
243 menuBar
->Append(statbarMenu
, "&Status bar");
244 menuBar
->Append(helpMenu
, "&Help");
246 // ... and attach this menu bar to the frame
249 // create default status bar to start with
251 SetStatusText("Welcome to wxWindows!");
253 m_statbarDefault
= GetStatusBar();
260 delete m_statbarDefault
;
261 delete m_statbarCustom
;
264 void MyFrame
::DoCreateStatusBar(MyFrame
::StatBarKind kind
)
266 wxStatusBar
*statbarOld
= GetStatusBar();
274 case StatBar_Default
:
275 SetStatusBar(m_statbarDefault
);
279 if ( !m_statbarCustom
)
281 m_statbarCustom
= new MyStatusBar(this);
283 SetStatusBar(m_statbarCustom
);
287 wxFAIL_MSG("unknown stat bar kind");
291 GetStatusBar()->Show();
293 m_statbarKind
= kind
;
297 void MyFrame
::OnRecreateStatusBar(wxCommandEvent
& WXUNUSED(event
))
299 DoCreateStatusBar(m_statbarKind
== StatBar_Custom ? StatBar_Default
303 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
305 // TRUE is to force the frame to close
309 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
311 wxMessageBox("wxStatusBar sample\n(c) 2000 Vadim Zeitlin",
312 "About statbar", wxOK
| wxICON_INFORMATION
, this);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
320 // 'this' : used in base member initializer list -- so what??
321 #pragma warning(disable: 4355)
324 MyStatusBar
::MyStatusBar(wxWindow
*parent
)
325 : wxStatusBar(parent
, -1), m_timer(this)
327 static const int widths
[Field_Max
] = { -1, 150, BITMAP_SIZE_X
, 100 };
329 SetFieldsCount(Field_Max
);
330 SetStatusWidths(Field_Max
, widths
);
332 m_checkbox
= new wxCheckBox(this, StatusBar_Checkbox
, _T("&Toggle clock"));
333 m_checkbox
->SetValue(TRUE
);
335 m_statbmp
= new wxStaticBitmap(this, -1, wxIcon(green_xpm
));
339 SetMinHeight(BITMAP_SIZE_Y
);
345 #pragma warning(default: 4355)
348 MyStatusBar
::~MyStatusBar()
350 if ( m_timer
.IsRunning() )
356 void MyStatusBar
::OnSize(wxSizeEvent
& event
)
359 GetFieldRect(Field_Checkbox
, rect
);
361 m_checkbox
->SetSize(rect
.x
+ 2, rect
.y
+ 2, rect
.width
- 4, rect
.height
- 4);
363 GetFieldRect(Field_Bitmap
, rect
);
364 m_statbmp
->Move(rect
.x
+ (rect
.width
- BITMAP_SIZE_X
) / 2,
365 rect
.y
+ (rect
.height
- BITMAP_SIZE_Y
) / 2);
370 void MyStatusBar
::OnToggleClock(wxCommandEvent
& event
)
372 if ( m_checkbox
->GetValue() )
376 m_statbmp
->SetIcon(wxIcon(green_xpm
));
380 else // don't show clock
384 m_statbmp
->SetIcon(wxIcon(red_xpm
));
386 SetStatusText("", Field_Clock
);
390 void MyStatusBar
::UpdateClock()
392 SetStatusText(wxDateTime
::Now().FormatTime(), Field_Clock
);