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