]> git.saurik.com Git - wxWidgets.git/blob - samples/statbar/statbar.cpp
Fixed HitTest for columns > 1
[wxWidgets.git] / samples / statbar / statbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: statbar.cpp
3 // Purpose: wxStatusBar sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.02.00
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if !wxUSE_STATUSBAR
28 #error "You need to set wxUSE_STATUSBAR to 1 to compile this sample"
29 #endif // wxUSE_STATUSBAR
30
31 // for all others, include the necessary headers
32 #ifndef WX_PRECOMP
33 #include "wx/app.h"
34 #include "wx/log.h"
35 #include "wx/frame.h"
36 #include "wx/statusbr.h"
37 #include "wx/timer.h"
38 #include "wx/checkbox.h"
39 #include "wx/statbmp.h"
40 #include "wx/menu.h"
41 #include "wx/msgdlg.h"
42 #include "wx/textdlg.h"
43 #include "wx/sizer.h"
44 #include "wx/stattext.h"
45 #include "wx/bmpbuttn.h"
46 #include "wx/dcmemory.h"
47 #endif
48
49 #include "wx/datetime.h"
50 #include "wx/numdlg.h"
51
52 // define this for the platforms which don't support wxBitmapButton (such as
53 // Motif), else a wxBitmapButton will be used
54 #ifdef __WXMOTIF__
55 //#define USE_MDI_PARENT_FRAME 1
56
57 #ifdef USE_MDI_PARENT_FRAME
58 #include "wx/mdi.h"
59 #endif // USE_MDI_PARENT_FRAME
60 #define USE_STATIC_BITMAP
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // resources
65 // ----------------------------------------------------------------------------
66
67 #ifdef USE_STATIC_BITMAP
68 #include "green.xpm"
69 #include "red.xpm"
70 #endif // USE_STATIC_BITMAP
71
72 // ----------------------------------------------------------------------------
73 // private classes
74 // ----------------------------------------------------------------------------
75
76 // Define a new application type, each program should derive a class from wxApp
77 class MyApp : public wxApp
78 {
79 public:
80 // override base class virtuals
81 // ----------------------------
82
83 // this one is called on application startup and is a good place for the app
84 // initialization (doing it here and not in the ctor allows to have an error
85 // return: if OnInit() returns false, the application terminates)
86 virtual bool OnInit();
87 };
88
89 // A custom status bar which contains controls, icons &c
90 class MyStatusBar : public wxStatusBar
91 {
92 public:
93 MyStatusBar(wxWindow *parent);
94 virtual ~MyStatusBar();
95
96 void UpdateClock();
97
98 // event handlers
99 void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); }
100 void OnSize(wxSizeEvent& event);
101 void OnToggleClock(wxCommandEvent& event);
102 void OnButton(wxCommandEvent& event);
103
104 private:
105 // toggle the state of the status bar controls
106 void DoToggle();
107
108 wxBitmap CreateBitmapForButton(bool on = false);
109
110 enum
111 {
112 Field_Text,
113 Field_Checkbox,
114 Field_Bitmap,
115 Field_Clock,
116 Field_Max
117 };
118
119 wxTimer m_timer;
120
121 wxCheckBox *m_checkbox;
122 #ifdef USE_STATIC_BITMAP
123 wxStaticBitmap *m_statbmp;
124 #else
125 wxBitmapButton *m_statbmp;
126 #endif
127
128 DECLARE_EVENT_TABLE()
129 };
130
131 // Define a new frame type: this is going to be our main frame
132 class MyFrame : public wxFrame
133 {
134 public:
135 // ctor(s)
136 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
137 #ifdef USE_MDI_PARENT_FRAME
138 class MyFrame : public wxMDIParentFrame
139 #else
140 virtual ~MyFrame();
141 #endif
142
143 // event handlers (these functions should _not_ be virtual)
144 void OnQuit(wxCommandEvent& event);
145 void OnAbout(wxCommandEvent& event);
146
147 void OnSetStatusFields(wxCommandEvent& event);
148 void OnRecreateStatusBar(wxCommandEvent& event);
149
150 private:
151 enum StatBarKind
152 {
153 StatBar_Default,
154 StatBar_Custom,
155 StatBar_Max
156 } m_statbarKind;
157 void OnUpdateSetStatusFields(wxUpdateUIEvent& event);
158 void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
159 void OnStatusBarToggle(wxCommandEvent& event);
160 void DoCreateStatusBar(StatBarKind kind);
161
162 wxStatusBar *m_statbarDefault;
163 MyStatusBar *m_statbarCustom;
164
165 // any class wishing to process wxWidgets events must use this macro
166 DECLARE_EVENT_TABLE()
167 };
168
169 // Our about dialog ith its status bar
170 class MyAboutDialog : public wxDialog
171 {
172 public:
173 MyAboutDialog(wxWindow *parent);
174 };
175
176 // ----------------------------------------------------------------------------
177 // constants
178 // ----------------------------------------------------------------------------
179
180 // IDs for the controls and the menu commands
181 enum
182 {
183 // menu items
184 StatusBar_Quit = 1,
185 StatusBar_SetFields,
186 StatusBar_Recreate,
187 StatusBar_About,
188 StatusBar_Toggle,
189 StatusBar_Checkbox = 1000
190 };
191
192 static const int BITMAP_SIZE_X = 32;
193 static const int BITMAP_SIZE_Y = 15;
194
195 // ----------------------------------------------------------------------------
196 // event tables and other macros for wxWidgets
197 // ----------------------------------------------------------------------------
198
199 // the event tables connect the wxWidgets events with the functions (event
200 // handlers) which process them. It can be also done at run-time, but for the
201 // simple menu events like this the static method is much simpler.
202 #ifdef USE_MDI_PARENT_FRAME
203 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
204 #else
205 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
206 #endif
207 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
208 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
209 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
210 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
211 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
212 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
213 EVT_UPDATE_UI(StatusBar_SetFields, MyFrame::OnUpdateSetStatusFields)
214 END_EVENT_TABLE()
215
216 BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
217 EVT_SIZE(MyStatusBar::OnSize)
218 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
219 EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton)
220 EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer)
221 END_EVENT_TABLE()
222
223 // Create a new application object: this macro will allow wxWidgets to create
224 // the application object during program execution (it's better than using a
225 // static object for many reasons) and also declares the accessor function
226 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
227 // not wxApp)
228 IMPLEMENT_APP(MyApp)
229
230 // ============================================================================
231 // implementation
232 // ============================================================================
233
234 // ----------------------------------------------------------------------------
235 // the application class
236 // ----------------------------------------------------------------------------
237
238 // `Main program' equivalent: the program execution "starts" here
239 bool MyApp::OnInit()
240 {
241 // create the main application window
242 MyFrame *frame = new MyFrame(_T("wxStatusBar sample"),
243 wxPoint(50, 50), wxSize(450, 340));
244
245 // and show it (the frames, unlike simple controls, are not shown when
246 // created initially)
247 frame->Show(true);
248
249 // success: wxApp::OnRun() will be called which will enter the main message
250 // loop and the application will run. If we returned 'false' here, the
251 // application would exit immediately.
252 return true;
253 }
254
255 // ----------------------------------------------------------------------------
256 // main frame
257 // ----------------------------------------------------------------------------
258
259 // frame constructor
260 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
261 #ifdef USE_MDI_PARENT_FRAME
262 : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
263 #else
264 : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
265 #endif
266 {
267 m_statbarDefault = NULL;
268 m_statbarCustom = NULL;
269
270 #ifdef __WXMAC__
271 // we need this in order to allow the about menu relocation, since ABOUT is
272 // not the default id of the about menu
273 wxApp::s_macAboutMenuItemId = StatusBar_About;
274 #endif
275
276 // create a menu bar
277 wxMenu *menuFile = new wxMenu;
278 menuFile->Append(StatusBar_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
279
280 wxMenu *statbarMenu = new wxMenu;
281 statbarMenu->Append(StatusBar_SetFields, _T("&Set field count\tCtrl-C"),
282 _T("Set the number of status bar fields"));
283 statbarMenu->Append(StatusBar_Toggle, _T("&Toggle Status Bar"),
284 _T("Toggle the status bar display"), true);
285 statbarMenu->Append(StatusBar_Recreate, _T("&Recreate\tCtrl-R"),
286 _T("Toggle status bar format"));
287
288 wxMenu *helpMenu = new wxMenu;
289 helpMenu->Append(StatusBar_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
290
291 // now append the freshly created menu to the menu bar...
292 wxMenuBar *menuBar = new wxMenuBar();
293 menuBar->Append(menuFile, _T("&File"));
294 menuBar->Append(statbarMenu, _T("&Status bar"));
295 menuBar->Append(helpMenu, _T("&Help"));
296
297 // ... and attach this menu bar to the frame
298 SetMenuBar(menuBar);
299
300 // create default status bar to start with
301 CreateStatusBar(2);
302 m_statbarKind = StatBar_Default;
303 SetStatusText(_T("Welcome to wxWidgets!"));
304
305 m_statbarDefault = GetStatusBar();
306 }
307
308 MyFrame::~MyFrame()
309 {
310 SetStatusBar(NULL);
311
312 delete m_statbarDefault;
313 delete m_statbarCustom;
314 }
315
316 void MyFrame::DoCreateStatusBar(MyFrame::StatBarKind kind)
317 {
318 wxStatusBar *statbarOld = GetStatusBar();
319 if ( statbarOld )
320 {
321 statbarOld->Hide();
322 }
323
324 switch ( kind )
325 {
326 case StatBar_Default:
327 SetStatusBar(m_statbarDefault);
328 break;
329
330 case StatBar_Custom:
331 if ( !m_statbarCustom )
332 {
333 m_statbarCustom = new MyStatusBar(this);
334 }
335 SetStatusBar(m_statbarCustom);
336 break;
337
338 default:
339 wxFAIL_MSG(wxT("unknown stat bar kind"));
340 }
341
342 GetStatusBar()->Show();
343 PositionStatusBar();
344
345 m_statbarKind = kind;
346 }
347
348 void MyFrame::OnUpdateSetStatusFields(wxUpdateUIEvent& event)
349 {
350 // only allow the setting of the number of status fields for the default
351 // status bar
352 wxStatusBar *sb = GetStatusBar();
353 event.Enable(sb == m_statbarDefault);
354 }
355
356
357 // event handlers
358 void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
359 {
360 wxStatusBar *sb = GetStatusBar();
361
362 long nFields = wxGetNumberFromUser
363 (
364 _T("Select the number of fields in the status bar"),
365 _T("Fields:"),
366 _T("wxWidgets statusbar sample"),
367 sb->GetFieldsCount(),
368 1, 5,
369 this
370 );
371
372 // we don't check if the number changed at all on purpose: calling
373 // SetFieldsCount() with the same number of fields should be ok
374 if ( nFields != -1 )
375 {
376 static const int widthsFor2Fields[] = { 200, -1 };
377 static const int widthsFor3Fields[] = { -1, -2, -1 };
378 static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 };
379
380 static const int *widthsAll[] =
381 {
382 NULL, // 1 field: default
383 widthsFor2Fields, // 2 fields: 1 fixed, 1 var
384 widthsFor3Fields, // 3 fields: 3 var
385 widthsFor4Fields, // 4 fields: 3 fixed, 2 vars
386 NULL // 5 fields: default (all have same width)
387 };
388
389 const int * const widths = widthsAll[nFields - 1];
390 sb->SetFieldsCount(nFields, widths);
391
392 wxString s;
393 for ( long n = 0; n < nFields; n++ )
394 {
395 if ( widths )
396 {
397 if ( widths[n] > 0 )
398 s.Printf(_T("fixed (%d)"), widths[n]);
399 else
400 s.Printf(_T("variable (*%d)"), -widths[n]);
401 }
402 else
403 {
404 s = _T("default");
405 }
406
407 SetStatusText(s, n);
408 }
409 }
410 else
411 {
412 wxLogStatus(this, wxT("Cancelled"));
413 }
414 }
415
416 void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event)
417 {
418 event.Check(GetStatusBar() != 0);
419 }
420
421 void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event))
422 {
423 wxStatusBar *statbarOld = GetStatusBar();
424 if ( statbarOld )
425 {
426 statbarOld->Hide();
427 SetStatusBar(0);
428 }
429 else
430 {
431 DoCreateStatusBar(m_statbarKind);
432 }
433 #ifdef __WXMSW__
434 // The following is a kludge suggested by Vadim Zeitlin (one of the wxWidgets
435 // authors) while we look for a proper fix..
436 // SendSizeEvent();
437 #endif
438 }
439
440 void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
441 {
442 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
443 : StatBar_Custom);
444 }
445
446 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
447 {
448 // true is to force the frame to close
449 Close(true);
450 }
451
452 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
453 {
454 MyAboutDialog dlg(this);
455 dlg.ShowModal();
456 }
457
458 // ----------------------------------------------------------------------------
459 // MyAboutDialog
460 // ----------------------------------------------------------------------------
461
462 MyAboutDialog::MyAboutDialog(wxWindow *parent)
463 : wxDialog(parent, wxID_ANY, wxString(_T("About statbar")),
464 wxDefaultPosition, wxDefaultSize,
465 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
466 {
467 wxStaticText *text = new wxStaticText(this, wxID_ANY,
468 _T("wxStatusBar sample\n")
469 _T("(c) 2000 Vadim Zeitlin"));
470
471 wxButton *btn = new wxButton(this, wxID_OK, _T("&Close"));
472
473 // create the top status bar without the size grip (default style),
474 // otherwise it looks weird
475 wxStatusBar *statbarTop = new wxStatusBar(this, wxID_ANY, 0);
476 statbarTop->SetFieldsCount(3);
477 statbarTop->SetStatusText(_T("This is a top status bar"), 0);
478 statbarTop->SetStatusText(_T("in a dialog"), 1);
479 statbarTop->SetStatusText(_T("Great, isn't it?"), 2);
480
481 wxStatusBar *statbarBottom = new wxStatusBar(this, wxID_ANY);
482 statbarBottom->SetFieldsCount(2);
483 statbarBottom->SetStatusText(_T("This is a bottom status bar"), 0);
484 statbarBottom->SetStatusText(_T("in a dialog"), 1);
485
486 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
487 sizerTop->Add(statbarTop, 0, wxGROW);
488 sizerTop->Add(-1, 10, 1, wxGROW);
489 sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
490 sizerTop->Add(-1, 10, 1, wxGROW);
491 sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
492 sizerTop->Add(-1, 10, 1, wxGROW);
493 sizerTop->Add(statbarBottom, 0, wxGROW);
494
495 SetSizer(sizerTop);
496
497 sizerTop->Fit(this);
498 sizerTop->SetSizeHints(this);
499 }
500
501 // ----------------------------------------------------------------------------
502 // MyStatusBar
503 // ----------------------------------------------------------------------------
504
505 #ifdef __VISUALC__
506 // 'this' : used in base member initializer list -- so what??
507 #pragma warning(disable: 4355)
508 #endif
509
510 MyStatusBar::MyStatusBar(wxWindow *parent)
511 : wxStatusBar(parent, wxID_ANY), m_timer(this), m_checkbox(NULL)
512 {
513 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
514
515 SetFieldsCount(Field_Max);
516 SetStatusWidths(Field_Max, widths);
517
518 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, _T("&Toggle clock"));
519 m_checkbox->SetValue(true);
520
521 #ifdef USE_STATIC_BITMAP
522 m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm));
523 #else
524 m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(),
525 wxDefaultPosition, wxDefaultSize,
526 wxBU_EXACTFIT);
527 #endif
528
529 m_timer.Start(1000);
530
531 SetMinHeight(BITMAP_SIZE_Y);
532
533 UpdateClock();
534 }
535
536 #ifdef __VISUALC__
537 #pragma warning(default: 4355)
538 #endif
539
540 MyStatusBar::~MyStatusBar()
541 {
542 if ( m_timer.IsRunning() )
543 {
544 m_timer.Stop();
545 }
546 }
547
548 wxBitmap MyStatusBar::CreateBitmapForButton(bool on)
549 {
550 static const int BMP_BUTTON_SIZE_X = 10;
551 static const int BMP_BUTTON_SIZE_Y = 9;
552
553 wxBitmap bitmap(BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
554 wxMemoryDC dc;
555 dc.SelectObject(bitmap);
556 dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH);
557 dc.SetBackground(*wxLIGHT_GREY_BRUSH);
558 dc.Clear();
559 dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
560 dc.SelectObject(wxNullBitmap);
561
562 return bitmap;
563 }
564
565 void MyStatusBar::OnSize(wxSizeEvent& event)
566 {
567 if ( !m_checkbox )
568 return;
569
570 wxRect rect;
571 GetFieldRect(Field_Checkbox, rect);
572
573 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
574
575 GetFieldRect(Field_Bitmap, rect);
576 wxSize size = m_statbmp->GetSize();
577
578 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
579 rect.y + (rect.height - size.y) / 2);
580
581 event.Skip();
582 }
583
584 void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
585 {
586 m_checkbox->SetValue(!m_checkbox->GetValue());
587
588 DoToggle();
589 }
590
591 void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
592 {
593 DoToggle();
594 }
595
596 void MyStatusBar::DoToggle()
597 {
598 if ( m_checkbox->GetValue() )
599 {
600 m_timer.Start(1000);
601
602 #ifdef USE_STATIC_BITMAP
603 m_statbmp->SetIcon(wxIcon(green_xpm));
604 #else
605 m_statbmp->SetBitmapLabel(CreateBitmapForButton(false));
606 m_statbmp->Refresh();
607 #endif
608
609 UpdateClock();
610 }
611 else // don't show clock
612 {
613 m_timer.Stop();
614
615 #ifdef USE_STATIC_BITMAP
616 m_statbmp->SetIcon(wxIcon(red_xpm));
617 #else
618 m_statbmp->SetBitmapLabel(CreateBitmapForButton(true));
619 m_statbmp->Refresh();
620 #endif
621
622 SetStatusText(wxEmptyString, Field_Clock);
623 }
624 }
625
626 void MyStatusBar::UpdateClock()
627 {
628 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
629 }