statbar.cpp 31 KB

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