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