]> git.saurik.com Git - wxWidgets.git/blame - samples/statbar/statbar.cpp
Reorganize and improve wxSizer::SetItemMinSize() documentation.
[wxWidgets.git] / samples / statbar / statbar.cpp
CommitLineData
2286341c
VZ
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
2286341c
VZ
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
cbc66a27 31// for all others, include the necessary headers
2286341c
VZ
32#ifndef WX_PRECOMP
33 #include "wx/app.h"
788233da 34 #include "wx/log.h"
2286341c
VZ
35 #include "wx/frame.h"
36 #include "wx/statusbr.h"
2286341c
VZ
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"
633d67bb 42 #include "wx/textdlg.h"
cbc66a27 43 #include "wx/sizer.h"
f6c881b4 44 #include "wx/stattext.h"
f6bcfd97
BP
45 #include "wx/bmpbuttn.h"
46 #include "wx/dcmemory.h"
2286341c
VZ
47#endif
48
85401ffe 49#include "wx/datetime.h"
e4f3eb42 50#include "wx/numdlg.h"
2a90b100 51#include "wx/fontdlg.h"
85401ffe 52
41f02b9a
FM
53#ifndef __WXMSW__
54 #include "../sample.xpm"
55#endif
56
54e18afc 57
f6bcfd97
BP
58// define this for the platforms which don't support wxBitmapButton (such as
59// Motif), else a wxBitmapButton will be used
60#ifdef __WXMOTIF__
54e18afc
FM
61 #define USE_STATIC_BITMAP
62#endif
63
ffd0623c
JS
64//#define USE_MDI_PARENT_FRAME 1
65
66#ifdef USE_MDI_PARENT_FRAME
67 #include "wx/mdi.h"
68#endif // USE_MDI_PARENT_FRAME
54e18afc 69
dac831bb 70static const char *SAMPLE_DIALOGS_TITLE = "wxWidgets statbar sample";
f6bcfd97 71
2286341c
VZ
72// ----------------------------------------------------------------------------
73// resources
74// ----------------------------------------------------------------------------
75
f6bcfd97
BP
76#ifdef USE_STATIC_BITMAP
77 #include "green.xpm"
78 #include "red.xpm"
79#endif // USE_STATIC_BITMAP
2286341c
VZ
80
81// ----------------------------------------------------------------------------
82// private classes
83// ----------------------------------------------------------------------------
84
85// Define a new application type, each program should derive a class from wxApp
86class MyApp : public wxApp
87{
88public:
89 // override base class virtuals
90 // ----------------------------
91
92 // this one is called on application startup and is a good place for the app
93 // initialization (doing it here and not in the ctor allows to have an error
94 // return: if OnInit() returns false, the application terminates)
95 virtual bool OnInit();
96};
97
98// A custom status bar which contains controls, icons &c
99class MyStatusBar : public wxStatusBar
100{
101public:
c4c178c1 102 MyStatusBar(wxWindow *parent, long style = wxSTB_DEFAULT_STYLE);
2286341c
VZ
103 virtual ~MyStatusBar();
104
105 void UpdateClock();
106
107 // event handlers
a6ebd559 108#if wxUSE_TIMER
d1f47235 109 void OnTimer(wxTimerEvent& WXUNUSED(event)) { UpdateClock(); }
a6ebd559 110#endif
2286341c
VZ
111 void OnSize(wxSizeEvent& event);
112 void OnToggleClock(wxCommandEvent& event);
f6bcfd97 113 void OnButton(wxCommandEvent& event);
2286341c
VZ
114
115private:
f6bcfd97
BP
116 // toggle the state of the status bar controls
117 void DoToggle();
118
ee1a622d 119 wxBitmap CreateBitmapForButton(bool on = false);
f6bcfd97 120
2286341c
VZ
121 enum
122 {
123 Field_Text,
124 Field_Checkbox,
125 Field_Bitmap,
126 Field_Clock,
127 Field_Max
128 };
129
a6ebd559 130#if wxUSE_TIMER
633d67bb 131 wxTimer m_timer;
a6ebd559 132#endif
2286341c 133
a6ebd559 134#if wxUSE_CHECKBOX
2286341c 135 wxCheckBox *m_checkbox;
a6ebd559 136#endif
f6bcfd97 137#ifdef USE_STATIC_BITMAP
2286341c 138 wxStaticBitmap *m_statbmp;
f6bcfd97
BP
139#else
140 wxBitmapButton *m_statbmp;
141#endif
2286341c
VZ
142
143 DECLARE_EVENT_TABLE()
144};
145
146// Define a new frame type: this is going to be our main frame
147class MyFrame : public wxFrame
148{
149public:
150 // ctor(s)
151 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
ffd0623c
JS
152#ifdef USE_MDI_PARENT_FRAME
153class MyFrame : public wxMDIParentFrame
154#else
2286341c 155 virtual ~MyFrame();
ffd0623c 156#endif
2286341c
VZ
157
158 // event handlers (these functions should _not_ be virtual)
159 void OnQuit(wxCommandEvent& event);
160 void OnAbout(wxCommandEvent& event);
633d67bb 161
dac831bb
VZ
162 void OnSetStatusField(wxCommandEvent& event);
163 void OnSetStatusText(wxCommandEvent& event);
1454b442
VZ
164 void OnPushStatusText(wxCommandEvent& event);
165 void OnPopStatusText(wxCommandEvent& event);
dac831bb 166
fd3ece57 167 void OnResetFieldsWidth(wxCommandEvent& event);
633d67bb 168 void OnSetStatusFields(wxCommandEvent& event);
2a90b100 169 void OnSetStatusFont(wxCommandEvent& event);
2286341c 170 void OnRecreateStatusBar(wxCommandEvent& event);
41d6e8b6 171
c4c178c1
FM
172 void OnSetPaneStyle(wxCommandEvent& event);
173 void OnSetStyle(wxCommandEvent& event);
2286341c
VZ
174
175private:
c4c178c1 176 enum StatusBarKind
2286341c
VZ
177 {
178 StatBar_Default,
179 StatBar_Custom,
180 StatBar_Max
181 } m_statbarKind;
fd3ece57 182
9df6b3b0 183
ea6e41e4 184 void OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event);
ffd0623c 185 void OnUpdateStatusBarToggle(wxUpdateUIEvent& event);
c4c178c1
FM
186 void OnUpdateSetPaneStyle(wxUpdateUIEvent& event);
187 void OnUpdateSetStyle(wxUpdateUIEvent& event);
ffd0623c 188 void OnStatusBarToggle(wxCommandEvent& event);
c4c178c1
FM
189 void DoCreateStatusBar(StatusBarKind kind, long style);
190 void ApplyPaneStyle();
2286341c 191
c4c178c1 192 int m_statbarPaneStyle;
c2919ab3 193
dac831bb
VZ
194 // the index of the field used by some commands
195 int m_field;
196
be5a51fb 197 // any class wishing to process wxWidgets events must use this macro
2286341c
VZ
198 DECLARE_EVENT_TABLE()
199};
200
cbc66a27
VZ
201// Our about dialog ith its status bar
202class MyAboutDialog : public wxDialog
203{
204public:
205 MyAboutDialog(wxWindow *parent);
206};
207
2286341c
VZ
208// ----------------------------------------------------------------------------
209// constants
210// ----------------------------------------------------------------------------
211
212// IDs for the controls and the menu commands
213enum
214{
215 // menu items
c4c178c1
FM
216 StatusBar_Quit = wxID_EXIT,
217 StatusBar_About = wxID_ABOUT,
41d6e8b6 218
c4c178c1 219 StatusBar_SetFields = wxID_HIGHEST+1,
dac831bb
VZ
220 StatusBar_SetField,
221 StatusBar_SetText,
1454b442
VZ
222 StatusBar_PushText,
223 StatusBar_PopText,
2a90b100 224 StatusBar_SetFont,
fd3ece57 225 StatusBar_ResetFieldsWidth,
ea6e41e4 226
2286341c 227 StatusBar_Recreate,
ffd0623c 228 StatusBar_Toggle,
c4c178c1
FM
229 StatusBar_Checkbox,
230 StatusBar_SetPaneStyle,
231 StatusBar_SetPaneStyleNormal,
232 StatusBar_SetPaneStyleFlat,
233 StatusBar_SetPaneStyleRaised,
41d6e8b6 234
c4c178c1
FM
235 StatusBar_SetStyleSizeGrip,
236 StatusBar_SetStyleEllipsizeStart,
237 StatusBar_SetStyleEllipsizeMiddle,
238 StatusBar_SetStyleEllipsizeEnd,
239 StatusBar_SetStyleShowTips
2286341c
VZ
240};
241
242static const int BITMAP_SIZE_X = 32;
243static const int BITMAP_SIZE_Y = 15;
244
245// ----------------------------------------------------------------------------
be5a51fb 246// event tables and other macros for wxWidgets
2286341c
VZ
247// ----------------------------------------------------------------------------
248
be5a51fb 249// the event tables connect the wxWidgets events with the functions (event
2286341c
VZ
250// handlers) which process them. It can be also done at run-time, but for the
251// simple menu events like this the static method is much simpler.
ffd0623c
JS
252#ifdef USE_MDI_PARENT_FRAME
253BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
254#else
2286341c 255BEGIN_EVENT_TABLE(MyFrame, wxFrame)
ffd0623c 256#endif
2286341c 257 EVT_MENU(StatusBar_Quit, MyFrame::OnQuit)
633d67bb 258 EVT_MENU(StatusBar_SetFields, MyFrame::OnSetStatusFields)
dac831bb
VZ
259 EVT_MENU(StatusBar_SetField, MyFrame::OnSetStatusField)
260 EVT_MENU(StatusBar_SetText, MyFrame::OnSetStatusText)
1454b442
VZ
261 EVT_MENU(StatusBar_PushText, MyFrame::OnPushStatusText)
262 EVT_MENU(StatusBar_PopText, MyFrame::OnPopStatusText)
2a90b100 263 EVT_MENU(StatusBar_SetFont, MyFrame::OnSetStatusFont)
54e18afc 264 EVT_MENU(StatusBar_ResetFieldsWidth, MyFrame::OnResetFieldsWidth)
2286341c
VZ
265 EVT_MENU(StatusBar_Recreate, MyFrame::OnRecreateStatusBar)
266 EVT_MENU(StatusBar_About, MyFrame::OnAbout)
ffd0623c 267 EVT_MENU(StatusBar_Toggle, MyFrame::OnStatusBarToggle)
c4c178c1
FM
268 EVT_MENU(StatusBar_SetPaneStyleNormal, MyFrame::OnSetPaneStyle)
269 EVT_MENU(StatusBar_SetPaneStyleFlat, MyFrame::OnSetPaneStyle)
270 EVT_MENU(StatusBar_SetPaneStyleRaised, MyFrame::OnSetPaneStyle)
41d6e8b6 271
c4c178c1
FM
272 EVT_MENU(StatusBar_SetStyleSizeGrip, MyFrame::OnSetStyle)
273 EVT_MENU(StatusBar_SetStyleEllipsizeStart, MyFrame::OnSetStyle)
274 EVT_MENU(StatusBar_SetStyleEllipsizeMiddle, MyFrame::OnSetStyle)
275 EVT_MENU(StatusBar_SetStyleEllipsizeEnd, MyFrame::OnSetStyle)
276 EVT_MENU(StatusBar_SetStyleShowTips, MyFrame::OnSetStyle)
fd3ece57 277
ea6e41e4
FM
278 EVT_UPDATE_UI_RANGE(StatusBar_SetFields, StatusBar_ResetFieldsWidth,
279 MyFrame::OnUpdateForDefaultStatusbar)
ffd0623c 280 EVT_UPDATE_UI(StatusBar_Toggle, MyFrame::OnUpdateStatusBarToggle)
c4c178c1
FM
281 EVT_UPDATE_UI_RANGE(StatusBar_SetPaneStyleNormal, StatusBar_SetPaneStyleRaised,
282 MyFrame::OnUpdateSetPaneStyle)
283 EVT_UPDATE_UI_RANGE(StatusBar_SetStyleSizeGrip, StatusBar_SetStyleShowTips,
284 MyFrame::OnUpdateSetStyle)
2286341c
VZ
285END_EVENT_TABLE()
286
287BEGIN_EVENT_TABLE(MyStatusBar, wxStatusBar)
288 EVT_SIZE(MyStatusBar::OnSize)
a6ebd559 289#if wxUSE_CHECKBOX
2286341c 290 EVT_CHECKBOX(StatusBar_Checkbox, MyStatusBar::OnToggleClock)
a6ebd559 291#endif
ee1a622d 292 EVT_BUTTON(wxID_ANY, MyStatusBar::OnButton)
a6ebd559 293#if wxUSE_TIMER
ee1a622d 294 EVT_TIMER(wxID_ANY, MyStatusBar::OnTimer)
a6ebd559 295#endif
2286341c
VZ
296END_EVENT_TABLE()
297
be5a51fb 298// Create a new application object: this macro will allow wxWidgets to create
2286341c
VZ
299// the application object during program execution (it's better than using a
300// static object for many reasons) and also declares the accessor function
301// wxGetApp() which will return the reference of the right type (i.e. MyApp and
302// not wxApp)
303IMPLEMENT_APP(MyApp)
304
305// ============================================================================
306// implementation
307// ============================================================================
308
309// ----------------------------------------------------------------------------
310// the application class
311// ----------------------------------------------------------------------------
312
313// `Main program' equivalent: the program execution "starts" here
314bool MyApp::OnInit()
315{
45e6e6f8
VZ
316 if ( !wxApp::OnInit() )
317 return false;
318
2286341c 319 // create the main application window
9a83f860 320 MyFrame *frame = new MyFrame(wxT("wxStatusBar sample"),
fd3ece57 321 wxPoint(50, 50), wxSize(450, 340));
2286341c
VZ
322
323 // and show it (the frames, unlike simple controls, are not shown when
324 // created initially)
ee1a622d 325 frame->Show(true);
2286341c
VZ
326
327 // success: wxApp::OnRun() will be called which will enter the main message
ee1a622d 328 // loop and the application will run. If we returned 'false' here, the
2286341c 329 // application would exit immediately.
ee1a622d 330 return true;
2286341c
VZ
331}
332
333// ----------------------------------------------------------------------------
334// main frame
335// ----------------------------------------------------------------------------
336
337// frame constructor
338MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
ffd0623c 339#ifdef USE_MDI_PARENT_FRAME
fd3ece57 340 : wxMDIParentFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
ffd0623c 341#else
fd3ece57 342 : wxFrame((wxWindow *)NULL, wxID_ANY, title, pos, size)
ffd0623c 343#endif
2286341c 344{
41f02b9a
FM
345 SetIcon(wxICON(sample));
346
c4c178c1 347 m_statbarPaneStyle = wxSB_NORMAL;
dac831bb 348 m_field = 1;
c2919ab3 349
2286341c
VZ
350#ifdef __WXMAC__
351 // we need this in order to allow the about menu relocation, since ABOUT is
352 // not the default id of the about menu
353 wxApp::s_macAboutMenuItemId = StatusBar_About;
354#endif
355
356 // create a menu bar
357 wxMenu *menuFile = new wxMenu;
9a83f860 358 menuFile->Append(StatusBar_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
2286341c
VZ
359
360 wxMenu *statbarMenu = new wxMenu;
41d6e8b6 361
c4c178c1 362 wxMenu *statbarStyleMenu = new wxMenu;
9a83f860
VZ
363 statbarStyleMenu->Append(StatusBar_SetStyleSizeGrip, wxT("wxSTB_SIZE_GRIP"), wxT("Toggles the wxSTB_SIZE_GRIP style"), true);
364 statbarStyleMenu->Append(StatusBar_SetStyleShowTips, wxT("wxSTB_SHOW_TIPS"), wxT("Toggles the wxSTB_SHOW_TIPS style"), true);
c4c178c1 365 statbarStyleMenu->AppendSeparator();
9a83f860
VZ
366 statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeStart, wxT("wxSTB_ELLIPSIZE_START"), wxT("Toggles the wxSTB_ELLIPSIZE_START style"), true);
367 statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeMiddle, wxT("wxSTB_ELLIPSIZE_MIDDLE"), wxT("Toggles the wxSTB_ELLIPSIZE_MIDDLE style"), true);
368 statbarStyleMenu->Append(StatusBar_SetStyleEllipsizeEnd, wxT("wxSTB_ELLIPSIZE_END"), wxT("Toggles the wxSTB_ELLIPSIZE_END style"), true);
369 statbarMenu->Append(StatusBar_SetPaneStyle, wxT("Status bar style"), statbarStyleMenu);
c4c178c1
FM
370 statbarMenu->AppendSeparator();
371
dac831bb
VZ
372 statbarMenu->Append(StatusBar_SetField, "Set active field &number\tCtrl-N",
373 "Set the number of field used by the next commands.");
374 statbarMenu->Append(StatusBar_SetText, wxT("Set field &text\tCtrl-T"),
375 wxT("Set the text of the selected field."));
1454b442
VZ
376 statbarMenu->Append(StatusBar_PushText, "P&ush field text\tCtrl-P",
377 "Push a message on top the selected field.");
378 statbarMenu->Append(StatusBar_PopText, "&Pop field text\tShift-Ctrl-P",
379 "Restore the previous contents of the selected field.");
dac831bb
VZ
380 statbarMenu->AppendSeparator();
381
9a83f860
VZ
382 statbarMenu->Append(StatusBar_SetFields, wxT("&Set field count\tCtrl-C"),
383 wxT("Set the number of status bar fields"));
9a83f860
VZ
384 statbarMenu->Append(StatusBar_SetFont, wxT("&Set field font\tCtrl-F"),
385 wxT("Set the font to use for rendering status bar fields"));
2286341c 386
c4c178c1 387 wxMenu *statbarPaneStyleMenu = new wxMenu;
9a83f860
VZ
388 statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleNormal, wxT("&Normal"), wxT("Sets the style of the first field to normal (sunken) look"), true);
389 statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleFlat, wxT("&Flat"), wxT("Sets the style of the first field to flat look"), true);
390 statbarPaneStyleMenu->Append(StatusBar_SetPaneStyleRaised, wxT("&Raised"), wxT("Sets the style of the first field to raised look"), true);
391 statbarMenu->Append(StatusBar_SetPaneStyle, wxT("Field style"), statbarPaneStyleMenu);
c2919ab3 392
9a83f860
VZ
393 statbarMenu->Append(StatusBar_ResetFieldsWidth, wxT("Reset field widths"),
394 wxT("Sets all fields to the same width"));
fd3ece57 395 statbarMenu->AppendSeparator();
54e18afc 396
9a83f860
VZ
397 statbarMenu->Append(StatusBar_Toggle, wxT("&Toggle Status Bar"),
398 wxT("Toggle the status bar display"), true);
399 statbarMenu->Append(StatusBar_Recreate, wxT("&Recreate\tCtrl-R"),
400 wxT("Toggle status bar format"));
54e18afc 401
2286341c 402 wxMenu *helpMenu = new wxMenu;
9a83f860 403 helpMenu->Append(StatusBar_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
2286341c
VZ
404
405 // now append the freshly created menu to the menu bar...
406 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
407 menuBar->Append(menuFile, wxT("&File"));
408 menuBar->Append(statbarMenu, wxT("&Status bar"));
409 menuBar->Append(helpMenu, wxT("&Help"));
2286341c
VZ
410
411 // ... and attach this menu bar to the frame
412 SetMenuBar(menuBar);
413
414 // create default status bar to start with
c4c178c1 415 DoCreateStatusBar(StatBar_Default, wxSTB_DEFAULT_STYLE);
9a83f860 416 SetStatusText(wxT("Welcome to wxWidgets!"));
2286341c
VZ
417}
418
419MyFrame::~MyFrame()
420{
2286341c
VZ
421}
422
c4c178c1 423void MyFrame::DoCreateStatusBar(MyFrame::StatusBarKind kind, long style)
2286341c
VZ
424{
425 wxStatusBar *statbarOld = GetStatusBar();
426 if ( statbarOld )
427 {
c4c178c1
FM
428 SetStatusBar(NULL);
429 delete statbarOld;
2286341c
VZ
430 }
431
c4c178c1 432 wxStatusBar *statbarNew = NULL;
2286341c
VZ
433 switch ( kind )
434 {
435 case StatBar_Default:
c4c178c1
FM
436 statbarNew = new wxStatusBar(this, wxID_ANY, style, "wxStatusBar");
437 statbarNew->SetFieldsCount(2);
2286341c 438 break;
41d6e8b6 439
2286341c 440 case StatBar_Custom:
c4c178c1 441 statbarNew = new MyStatusBar(this, style);
2286341c
VZ
442 break;
443
444 default:
c4c178c1 445 wxFAIL_MSG(wxT("unknown status bar kind"));
2286341c
VZ
446 }
447
c4c178c1
FM
448 SetStatusBar(statbarNew);
449 ApplyPaneStyle();
f6bcfd97 450 PositionStatusBar();
2286341c
VZ
451
452 m_statbarKind = kind;
453}
454
9df6b3b0
FM
455
456// ----------------------------------------------------------------------------
457// main frame - event handlers
458// ----------------------------------------------------------------------------
459
ea6e41e4 460void MyFrame::OnUpdateForDefaultStatusbar(wxUpdateUIEvent& event)
9df6b3b0 461{
ea6e41e4 462 // only allow this feature for the default status bar
9df6b3b0 463 wxStatusBar *sb = GetStatusBar();
c4c178c1
FM
464 if (!sb)
465 return;
41d6e8b6 466
c4c178c1 467 event.Enable(sb->GetName() == "wxStatusBar");
9df6b3b0
FM
468}
469
dac831bb 470void MyFrame::OnSetStatusField(wxCommandEvent& WXUNUSED(event))
9df6b3b0
FM
471{
472 wxStatusBar *sb = GetStatusBar();
c4c178c1
FM
473 if (!sb)
474 return;
9df6b3b0 475
dac831bb
VZ
476 long rc = wxGetNumberFromUser
477 (
478 "Configure the field index to be used by the set, push "
479 "and pop text commands in the menu.\n"
480 "\n"
481 "0 corresponds to the first field, 1 to the second one "
482 "and so on.",
483 "Field &index:",
484 SAMPLE_DIALOGS_TITLE,
485 m_field,
486 0,
487 sb->GetFieldsCount() - 1,
488 NULL
489 );
490
491 if ( rc == -1 )
492 return;
9df6b3b0 493
dac831bb
VZ
494 m_field = rc;
495
496 wxLogStatus("Status bar text will be set for field #%d", m_field);
497}
498
499void MyFrame::OnSetStatusText(wxCommandEvent& WXUNUSED(event))
500{
501 wxStatusBar *sb = GetStatusBar();
502 if (!sb)
503 return;
504
505 wxString txt = wxGetTextFromUser
506 (
507 wxString::Format
508 (
509 "Enter the text from for the field #%d",
510 m_field
511 ),
512 SAMPLE_DIALOGS_TITLE,
513 sb->GetStatusText(m_field),
514 this
515 );
516
517 if ( txt.empty() )
518 return;
519
520 sb->SetStatusText(txt, m_field);
9df6b3b0
FM
521}
522
1454b442
VZ
523// the current depth of the stack used by Push/PopStatusText()
524static int gs_depth = 0;
525
526void MyFrame::OnPushStatusText(wxCommandEvent& WXUNUSED(event))
527{
528 wxStatusBar *sb = GetStatusBar();
529 if (!sb)
530 return;
531
532 static int s_countPush = 0;
533 sb->PushStatusText(wxString::Format
534 (
535 "Pushed message #%d (depth = %d)",
536 ++s_countPush, ++gs_depth
537 ), m_field);
538}
539
540void MyFrame::OnPopStatusText(wxCommandEvent& WXUNUSED(event))
541{
542 wxStatusBar *sb = GetStatusBar();
543 if (!sb)
544 return;
545
546 if ( !gs_depth )
547 {
548 wxLogStatus("No message to pop.");
549 return;
550 }
551
552 gs_depth--;
553 sb->PopStatusText(m_field);
554}
555
2a90b100
FM
556void MyFrame::OnSetStatusFont(wxCommandEvent& WXUNUSED(event))
557{
558 wxStatusBar *sb = GetStatusBar();
c4c178c1
FM
559 if (!sb)
560 return;
2a90b100
FM
561
562 wxFont fnt = wxGetFontFromUser(this, sb->GetFont(), "Choose statusbar font");
563 if (fnt.IsOk())
564 {
565 sb->SetFont(fnt);
566 sb->SetSize(sb->GetBestSize());
567 }
568}
569
633d67bb
VZ
570void MyFrame::OnSetStatusFields(wxCommandEvent& WXUNUSED(event))
571{
572 wxStatusBar *sb = GetStatusBar();
c4c178c1
FM
573 if (!sb)
574 return;
633d67bb
VZ
575
576 long nFields = wxGetNumberFromUser
fd3ece57 577 (
9a83f860
VZ
578 wxT("Select the number of fields in the status bar"),
579 wxT("Fields:"),
dac831bb 580 SAMPLE_DIALOGS_TITLE,
633d67bb
VZ
581 sb->GetFieldsCount(),
582 1, 5,
583 this
fd3ece57 584 );
633d67bb
VZ
585
586 // we don't check if the number changed at all on purpose: calling
587 // SetFieldsCount() with the same number of fields should be ok
588 if ( nFields != -1 )
589 {
71e03035
VZ
590 static const int widthsFor2Fields[] = { 200, -1 };
591 static const int widthsFor3Fields[] = { -1, -2, -1 };
592 static const int widthsFor4Fields[] = { 100, -1, 100, -2, 100 };
593
bf69498a 594 static const int *widthsAll[] =
633d67bb 595 {
71e03035
VZ
596 NULL, // 1 field: default
597 widthsFor2Fields, // 2 fields: 1 fixed, 1 var
598 widthsFor3Fields, // 3 fields: 3 var
599 widthsFor4Fields, // 4 fields: 3 fixed, 2 vars
600 NULL // 5 fields: default (all have same width)
601 };
633d67bb 602
bf69498a
VZ
603 const int * const widths = widthsAll[nFields - 1];
604 sb->SetFieldsCount(nFields, widths);
633d67bb 605
bf69498a
VZ
606 wxString s;
607 for ( long n = 0; n < nFields; n++ )
608 {
609 if ( widths )
610 {
611 if ( widths[n] > 0 )
9a83f860 612 s.Printf(wxT("fixed (%d)"), widths[n]);
bf69498a 613 else
9a83f860 614 s.Printf(wxT("variable (*%d)"), -widths[n]);
bf69498a
VZ
615 }
616 else
617 {
9a83f860 618 s = wxT("default");
bf69498a
VZ
619 }
620
621 SetStatusText(s, n);
622 }
dac831bb
VZ
623
624 if ( m_field >= nFields )
625 m_field = nFields - 1;
633d67bb
VZ
626 }
627 else
628 {
4693b20c 629 wxLogStatus(this, wxT("Cancelled"));
633d67bb
VZ
630 }
631}
632
54e18afc
FM
633void MyFrame::OnResetFieldsWidth(wxCommandEvent& WXUNUSED(event))
634{
fd3ece57 635 wxStatusBar *pStat = GetStatusBar();
c4c178c1
FM
636 if (!pStat)
637 return;
41d6e8b6 638
c4c178c1
FM
639 int n = pStat->GetFieldsCount();
640 pStat->SetStatusWidths(n, NULL);
641 for (int i=0; i<n; i++)
642 pStat->SetStatusText("same size", i);
54e18afc
FM
643}
644
ffd0623c
JS
645void MyFrame::OnUpdateStatusBarToggle(wxUpdateUIEvent& event)
646{
2dcd83af 647 event.Check(GetStatusBar() != NULL);
ffd0623c
JS
648}
649
650void MyFrame::OnStatusBarToggle(wxCommandEvent& WXUNUSED(event))
651{
652 wxStatusBar *statbarOld = GetStatusBar();
653 if ( statbarOld )
654 {
2dcd83af 655 SetStatusBar(NULL);
c4c178c1 656 delete statbarOld;
ffd0623c
JS
657 }
658 else
659 {
c4c178c1 660 DoCreateStatusBar(m_statbarKind, wxSTB_DEFAULT_STYLE);
ffd0623c 661 }
ffd0623c
JS
662}
663
2286341c
VZ
664void MyFrame::OnRecreateStatusBar(wxCommandEvent& WXUNUSED(event))
665{
666 DoCreateStatusBar(m_statbarKind == StatBar_Custom ? StatBar_Default
c4c178c1
FM
667 : StatBar_Custom,
668 wxSTB_DEFAULT_STYLE);
2286341c
VZ
669}
670
671void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
672{
ee1a622d
WS
673 // true is to force the frame to close
674 Close(true);
2286341c
VZ
675}
676
677void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
678{
cbc66a27
VZ
679 MyAboutDialog dlg(this);
680 dlg.ShowModal();
681}
682
c4c178c1 683void MyFrame::OnUpdateSetPaneStyle(wxUpdateUIEvent& event)
c2919ab3 684{
c4c178c1
FM
685 switch (event.GetId())
686 {
687 case StatusBar_SetPaneStyleNormal:
688 event.Check(m_statbarPaneStyle == wxSB_NORMAL);
689 break;
690 case StatusBar_SetPaneStyleFlat:
691 event.Check(m_statbarPaneStyle == wxSB_FLAT);
692 break;
693 case StatusBar_SetPaneStyleRaised:
694 event.Check(m_statbarPaneStyle == wxSB_RAISED);
695 break;
696 }
c2919ab3
VZ
697}
698
c4c178c1 699void MyFrame::OnSetPaneStyle(wxCommandEvent& event)
c2919ab3 700{
c4c178c1
FM
701 switch (event.GetId())
702 {
703 case StatusBar_SetPaneStyleNormal:
704 m_statbarPaneStyle = wxSB_NORMAL;
705 break;
706 case StatusBar_SetPaneStyleFlat:
707 m_statbarPaneStyle = wxSB_FLAT;
708 break;
709 case StatusBar_SetPaneStyleRaised:
710 m_statbarPaneStyle = wxSB_RAISED;
711 break;
712 }
41d6e8b6 713
c4c178c1 714 ApplyPaneStyle();
c2919ab3
VZ
715}
716
c4c178c1 717void MyFrame::ApplyPaneStyle()
c2919ab3
VZ
718{
719 wxStatusBar *sb = GetStatusBar();
c4c178c1
FM
720 if (!sb)
721 return;
41d6e8b6 722
c2919ab3
VZ
723 int fields = sb->GetFieldsCount();
724 int *styles = new int[fields];
725
726 for (int i = 1; i < fields; i++)
727 styles[i] = wxSB_NORMAL;
728
c4c178c1 729 styles[0] = m_statbarPaneStyle;
c2919ab3
VZ
730
731 sb->SetStatusStyles(fields, styles);
732
733 delete [] styles;
734}
735
c4c178c1
FM
736void MyFrame::OnUpdateSetStyle(wxUpdateUIEvent& event)
737{
738 long currentStyle = wxSTB_DEFAULT_STYLE;
739 if (GetStatusBar())
740 currentStyle = GetStatusBar()->GetWindowStyle();
741
742 switch (event.GetId())
743 {
744 case StatusBar_SetStyleSizeGrip:
745 event.Check((currentStyle & wxSTB_SIZEGRIP) != 0);
746 break;
747 case StatusBar_SetStyleShowTips:
748 event.Check((currentStyle & wxSTB_SHOW_TIPS) != 0);
749 break;
750
751 case StatusBar_SetStyleEllipsizeStart:
752 event.Check((currentStyle & wxSTB_ELLIPSIZE_START) != 0);
753 break;
754 case StatusBar_SetStyleEllipsizeMiddle:
755 event.Check((currentStyle & wxSTB_ELLIPSIZE_MIDDLE) != 0);
756 break;
757 case StatusBar_SetStyleEllipsizeEnd:
758 event.Check((currentStyle & wxSTB_ELLIPSIZE_END) != 0);
759 break;
760 }
761}
762
763void MyFrame::OnSetStyle(wxCommandEvent& event)
764{
765 long oldStyle = wxSTB_DEFAULT_STYLE;
766 if (GetStatusBar())
767 oldStyle = GetStatusBar()->GetWindowStyle();
768
769#define STB_ELLIPSIZE_MASK (wxSTB_ELLIPSIZE_START|wxSTB_ELLIPSIZE_MIDDLE|wxSTB_ELLIPSIZE_END)
41d6e8b6 770
c4c178c1
FM
771 long newStyle = oldStyle;
772 long newStyleBit = 0;
773 switch (event.GetId())
774 {
775 case StatusBar_SetStyleSizeGrip:
776 newStyleBit = wxSTB_SIZEGRIP;
777 break;
778 case StatusBar_SetStyleShowTips:
779 newStyleBit = wxSTB_SHOW_TIPS;
780 break;
781
782 case StatusBar_SetStyleEllipsizeStart:
783 newStyleBit = wxSTB_ELLIPSIZE_START;
784 newStyle &= ~STB_ELLIPSIZE_MASK;
785 break;
786 case StatusBar_SetStyleEllipsizeMiddle:
787 newStyleBit = wxSTB_ELLIPSIZE_MIDDLE;
788 newStyle &= ~STB_ELLIPSIZE_MASK;
789 break;
790 case StatusBar_SetStyleEllipsizeEnd:
791 newStyleBit = wxSTB_ELLIPSIZE_END;
792 newStyle &= ~STB_ELLIPSIZE_MASK;
793 break;
794 }
41d6e8b6 795
c4c178c1
FM
796 newStyle = event.IsChecked() ? (newStyle | newStyleBit) :
797 (newStyle & ~newStyleBit);
798 if (newStyle != oldStyle)
799 {
800 DoCreateStatusBar(m_statbarKind, newStyle);
801 SetStatusText("Status bar recreated with a new style");
802 }
803}
804
cbc66a27
VZ
805// ----------------------------------------------------------------------------
806// MyAboutDialog
807// ----------------------------------------------------------------------------
808
809MyAboutDialog::MyAboutDialog(wxWindow *parent)
9a83f860 810 : wxDialog(parent, wxID_ANY, wxString(wxT("About statbar")),
cbc66a27
VZ
811 wxDefaultPosition, wxDefaultSize,
812 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
813{
ee1a622d 814 wxStaticText *text = new wxStaticText(this, wxID_ANY,
9a83f860
VZ
815 wxT("wxStatusBar sample\n")
816 wxT("(c) 2000 Vadim Zeitlin"));
cbc66a27 817
9a83f860 818 wxButton *btn = new wxButton(this, wxID_OK, wxT("&Close"));
a1a43961 819
8b8bff20
VZ
820 // create the top status bar without the size grip (default style),
821 // otherwise it looks weird
ee1a622d 822 wxStatusBar *statbarTop = new wxStatusBar(this, wxID_ANY, 0);
8b8bff20 823 statbarTop->SetFieldsCount(3);
9a83f860
VZ
824 statbarTop->SetStatusText(wxT("This is a top status bar"), 0);
825 statbarTop->SetStatusText(wxT("in a dialog"), 1);
826 statbarTop->SetStatusText(wxT("Great, isn't it?"), 2);
8b8bff20 827
ee1a622d 828 wxStatusBar *statbarBottom = new wxStatusBar(this, wxID_ANY);
8b8bff20 829 statbarBottom->SetFieldsCount(2);
9a83f860
VZ
830 statbarBottom->SetStatusText(wxT("This is a bottom status bar"), 0);
831 statbarBottom->SetStatusText(wxT("in a dialog"), 1);
cbc66a27
VZ
832
833 wxBoxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
8b8bff20 834 sizerTop->Add(statbarTop, 0, wxGROW);
cbc66a27 835 sizerTop->Add(-1, 10, 1, wxGROW);
a1a43961
VZ
836 sizerTop->Add(text, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
837 sizerTop->Add(-1, 10, 1, wxGROW);
838 sizerTop->Add(btn, 0, wxCENTRE | wxRIGHT | wxLEFT, 20);
cbc66a27 839 sizerTop->Add(-1, 10, 1, wxGROW);
8b8bff20 840 sizerTop->Add(statbarBottom, 0, wxGROW);
cbc66a27 841
92c01615 842 SetSizerAndFit(sizerTop);
2286341c
VZ
843}
844
845// ----------------------------------------------------------------------------
846// MyStatusBar
847// ----------------------------------------------------------------------------
848
85401ffe
VZ
849#ifdef __VISUALC__
850 // 'this' : used in base member initializer list -- so what??
851 #pragma warning(disable: 4355)
852#endif
853
c4c178c1
FM
854MyStatusBar::MyStatusBar(wxWindow *parent, long style)
855 : wxStatusBar(parent, wxID_ANY, style, "MyStatusBar")
a6ebd559 856#if wxUSE_TIMER
fd3ece57 857 , m_timer(this)
a6ebd559
WS
858#endif
859#if wxUSE_CHECKBOX
fd3ece57 860 , m_checkbox(NULL)
a6ebd559 861#endif
2286341c
VZ
862{
863 static const int widths[Field_Max] = { -1, 150, BITMAP_SIZE_X, 100 };
864
865 SetFieldsCount(Field_Max);
866 SetStatusWidths(Field_Max, widths);
867
a6ebd559 868#if wxUSE_CHECKBOX
9a83f860 869 m_checkbox = new wxCheckBox(this, StatusBar_Checkbox, wxT("&Toggle clock"));
ee1a622d 870 m_checkbox->SetValue(true);
a6ebd559 871#endif
2286341c 872
f6bcfd97 873#ifdef USE_STATIC_BITMAP
ee1a622d 874 m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxIcon(green_xpm));
f6bcfd97 875#else
ee1a622d 876 m_statbmp = new wxBitmapButton(this, wxID_ANY, CreateBitmapForButton(),
fd3ece57
FM
877 wxDefaultPosition, wxDefaultSize,
878 wxBU_EXACTFIT);
f6bcfd97 879#endif
2286341c 880
a6ebd559 881#if wxUSE_TIMER
2286341c 882 m_timer.Start(1000);
a6ebd559 883#endif
2286341c 884
fd3ece57
FM
885 SetMinHeight(wxMax(m_statbmp->GetBestSize().GetHeight(),
886 m_checkbox->GetBestSize().GetHeight()));
85401ffe 887
2286341c
VZ
888 UpdateClock();
889}
890
85401ffe
VZ
891#ifdef __VISUALC__
892 #pragma warning(default: 4355)
893#endif
894
2286341c
VZ
895MyStatusBar::~MyStatusBar()
896{
a6ebd559 897#if wxUSE_TIMER
2286341c
VZ
898 if ( m_timer.IsRunning() )
899 {
900 m_timer.Stop();
901 }
a6ebd559 902#endif
2286341c
VZ
903}
904
54e18afc
FM
905#define BMP_BUTTON_SIZE_X 10
906#define BMP_BUTTON_SIZE_Y 10
907
f6bcfd97
BP
908wxBitmap MyStatusBar::CreateBitmapForButton(bool on)
909{
54e18afc 910 wxBitmap bitmap(BMP_BUTTON_SIZE_X+1, BMP_BUTTON_SIZE_Y+1);
f6bcfd97
BP
911 wxMemoryDC dc;
912 dc.SelectObject(bitmap);
913 dc.SetBrush(on ? *wxGREEN_BRUSH : *wxRED_BRUSH);
54e18afc 914 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
f6bcfd97
BP
915 dc.Clear();
916 dc.DrawEllipse(0, 0, BMP_BUTTON_SIZE_X, BMP_BUTTON_SIZE_Y);
917 dc.SelectObject(wxNullBitmap);
918
919 return bitmap;
920}
921
2286341c
VZ
922void MyStatusBar::OnSize(wxSizeEvent& event)
923{
a6ebd559 924#if wxUSE_CHECKBOX
f6bcfd97
BP
925 if ( !m_checkbox )
926 return;
a6ebd559 927#endif
f6bcfd97 928
778c9b9f
FM
929 // TEMPORARY HACK: TODO find a more general solution
930#ifdef wxStatusBarGeneric
931 wxStatusBar::OnSize(event);
932#endif
933
2286341c 934 wxRect rect;
ea6e41e4
FM
935 if (!GetFieldRect(Field_Checkbox, rect))
936 {
937 event.Skip();
938 return;
939 }
2286341c 940
a6ebd559 941#if wxUSE_CHECKBOX
2286341c 942 m_checkbox->SetSize(rect.x + 2, rect.y + 2, rect.width - 4, rect.height - 4);
a6ebd559 943#endif
2286341c
VZ
944
945 GetFieldRect(Field_Bitmap, rect);
f6bcfd97 946 wxSize size = m_statbmp->GetSize();
f6bcfd97
BP
947
948 m_statbmp->Move(rect.x + (rect.width - size.x) / 2,
949 rect.y + (rect.height - size.y) / 2);
2286341c
VZ
950
951 event.Skip();
952}
953
f6bcfd97
BP
954void MyStatusBar::OnButton(wxCommandEvent& WXUNUSED(event))
955{
a6ebd559 956#if wxUSE_CHECKBOX
f6bcfd97 957 m_checkbox->SetValue(!m_checkbox->GetValue());
a6ebd559 958#endif
f6bcfd97
BP
959
960 DoToggle();
961}
962
963void MyStatusBar::OnToggleClock(wxCommandEvent& WXUNUSED(event))
964{
965 DoToggle();
966}
967
968void MyStatusBar::DoToggle()
2286341c 969{
a6ebd559 970#if wxUSE_CHECKBOX
2286341c
VZ
971 if ( m_checkbox->GetValue() )
972 {
a6ebd559 973#if wxUSE_TIMER
2286341c 974 m_timer.Start(1000);
a6ebd559 975#endif
2286341c 976
f6bcfd97 977#ifdef USE_STATIC_BITMAP
85401ffe 978 m_statbmp->SetIcon(wxIcon(green_xpm));
f6bcfd97 979#else
ee1a622d 980 m_statbmp->SetBitmapLabel(CreateBitmapForButton(false));
f6bcfd97
BP
981 m_statbmp->Refresh();
982#endif
2286341c
VZ
983
984 UpdateClock();
985 }
986 else // don't show clock
987 {
a6ebd559 988#if wxUSE_TIMER
2286341c 989 m_timer.Stop();
a6ebd559 990#endif
2286341c 991
f6bcfd97 992#ifdef USE_STATIC_BITMAP
85401ffe 993 m_statbmp->SetIcon(wxIcon(red_xpm));
f6bcfd97 994#else
ee1a622d 995 m_statbmp->SetBitmapLabel(CreateBitmapForButton(true));
f6bcfd97
BP
996 m_statbmp->Refresh();
997#endif
2286341c 998
dabbc6a5 999 SetStatusText(wxEmptyString, Field_Clock);
2286341c 1000 }
a6ebd559 1001#endif
2286341c
VZ
1002}
1003
1004void MyStatusBar::UpdateClock()
1005{
1006 SetStatusText(wxDateTime::Now().FormatTime(), Field_Clock);
1007}