static.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: static.cpp
  4. // Purpose: Part of the widgets sample showing various static controls
  5. // Author: Vadim Zeitlin
  6. // Created: 11.04.01
  7. // Copyright: (c) 2001 Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers
  22. #ifndef WX_PRECOMP
  23. #include "wx/log.h"
  24. #include "wx/bitmap.h"
  25. #include "wx/button.h"
  26. #include "wx/checkbox.h"
  27. #include "wx/radiobox.h"
  28. #include "wx/statbox.h"
  29. #include "wx/stattext.h"
  30. #include "wx/textctrl.h"
  31. #endif
  32. #include "wx/sizer.h"
  33. #include "wx/statline.h"
  34. #include "wx/generic/stattextg.h"
  35. #include "wx/wupdlock.h"
  36. #include "widgets.h"
  37. #include "icons/statbox.xpm"
  38. // ----------------------------------------------------------------------------
  39. // constants
  40. // ----------------------------------------------------------------------------
  41. // control ids
  42. enum
  43. {
  44. StaticPage_Reset = wxID_HIGHEST,
  45. StaticPage_BoxText,
  46. StaticPage_LabelText,
  47. StaticPage_LabelTextWithMarkup
  48. };
  49. // alignment radiobox values
  50. enum
  51. {
  52. StaticHAlign_Left,
  53. StaticHAlign_Centre,
  54. StaticHAlign_Right,
  55. StaticHAlign_Max
  56. };
  57. enum
  58. {
  59. StaticVAlign_Top,
  60. StaticVAlign_Centre,
  61. StaticVAlign_Bottom,
  62. StaticVAlign_Max
  63. };
  64. enum
  65. {
  66. StaticEllipsize_Start,
  67. StaticEllipsize_Middle,
  68. StaticEllipsize_End
  69. };
  70. // ----------------------------------------------------------------------------
  71. // StaticWidgetsPage
  72. // ----------------------------------------------------------------------------
  73. class StaticWidgetsPage : public WidgetsPage
  74. {
  75. public:
  76. StaticWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  77. virtual ~StaticWidgetsPage(){};
  78. virtual wxControl *GetWidget() const { return m_statText; }
  79. virtual Widgets GetWidgets() const
  80. {
  81. Widgets widgets;
  82. widgets.push_back(m_sizerStatBox->GetStaticBox());
  83. widgets.push_back(m_statText);
  84. #if wxUSE_MARKUP
  85. widgets.push_back(m_statMarkup);
  86. #endif // wxUSE_MARKUP
  87. #if wxUSE_STATLINE
  88. widgets.push_back(m_statLine);
  89. #endif // wxUSE_STATLINE
  90. return widgets;
  91. }
  92. virtual void RecreateWidget() { CreateStatic(); }
  93. // lazy creation of the content
  94. virtual void CreateContent();
  95. protected:
  96. // event handlers
  97. void OnCheckOrRadioBox(wxCommandEvent& event);
  98. void OnButtonReset(wxCommandEvent& event);
  99. void OnButtonBoxText(wxCommandEvent& event);
  100. void OnButtonLabelText(wxCommandEvent& event);
  101. #if wxUSE_MARKUP
  102. void OnButtonLabelWithMarkupText(wxCommandEvent& event);
  103. #endif // wxUSE_MARKUP
  104. void OnMouseEvent(wxMouseEvent& event);
  105. // reset all parameters
  106. void Reset();
  107. // (re)create all controls
  108. void CreateStatic();
  109. // the controls
  110. // ------------
  111. // the check/radio boxes for styles
  112. wxCheckBox *m_chkVert,
  113. *m_chkGeneric,
  114. *m_chkAutoResize,
  115. *m_chkEllipsize;
  116. #if wxUSE_MARKUP
  117. wxCheckBox *m_chkMarkup,
  118. *m_chkGreen;
  119. #endif // wxUSE_MARKUP
  120. wxRadioBox *m_radioHAlign,
  121. *m_radioVAlign,
  122. *m_radioEllipsize;
  123. // the controls and the sizer containing them
  124. wxStaticBoxSizer *m_sizerStatBox;
  125. wxStaticTextBase *m_statText;
  126. #if wxUSE_MARKUP
  127. wxStaticTextBase *m_statMarkup;
  128. #endif // wxUSE_MARKUP
  129. #if wxUSE_STATLINE
  130. wxStaticLine *m_statLine;
  131. #endif // wxUSE_STATLINE
  132. wxSizer *m_sizerStatic;
  133. // the text entries for command parameters
  134. wxTextCtrl *m_textBox,
  135. *m_textLabel;
  136. #if wxUSE_MARKUP
  137. wxTextCtrl *m_textLabelWithMarkup;
  138. #endif // wxUSE_MARKUP
  139. private:
  140. wxDECLARE_EVENT_TABLE();
  141. DECLARE_WIDGETS_PAGE(StaticWidgetsPage)
  142. };
  143. // ----------------------------------------------------------------------------
  144. // event tables
  145. // ----------------------------------------------------------------------------
  146. wxBEGIN_EVENT_TABLE(StaticWidgetsPage, WidgetsPage)
  147. EVT_BUTTON(StaticPage_Reset, StaticWidgetsPage::OnButtonReset)
  148. EVT_BUTTON(StaticPage_LabelText, StaticWidgetsPage::OnButtonLabelText)
  149. #if wxUSE_MARKUP
  150. EVT_BUTTON(StaticPage_LabelTextWithMarkup, StaticWidgetsPage::OnButtonLabelWithMarkupText)
  151. #endif // wxUSE_MARKUP
  152. EVT_BUTTON(StaticPage_BoxText, StaticWidgetsPage::OnButtonBoxText)
  153. EVT_CHECKBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
  154. EVT_RADIOBOX(wxID_ANY, StaticWidgetsPage::OnCheckOrRadioBox)
  155. wxEND_EVENT_TABLE()
  156. // ============================================================================
  157. // implementation
  158. // ============================================================================
  159. IMPLEMENT_WIDGETS_PAGE(StaticWidgetsPage, wxT("Static"),
  160. (int)wxPlatform(GENERIC_CTRLS).If(wxOS_WINDOWS,NATIVE_CTRLS)
  161. );
  162. StaticWidgetsPage::StaticWidgetsPage(WidgetsBookCtrl *book,
  163. wxImageList *imaglist)
  164. : WidgetsPage(book, imaglist, statbox_xpm)
  165. {
  166. // init everything
  167. m_chkVert =
  168. m_chkAutoResize =
  169. m_chkGeneric =
  170. #if wxUSE_MARKUP
  171. m_chkGreen =
  172. #endif // wxUSE_MARKUP
  173. NULL;
  174. m_radioHAlign =
  175. m_radioVAlign = (wxRadioBox *)NULL;
  176. m_statText = NULL;
  177. #if wxUSE_STATLINE
  178. m_statLine = (wxStaticLine *)NULL;
  179. #endif // wxUSE_STATLINE
  180. #if wxUSE_MARKUP
  181. m_statMarkup = NULL;
  182. #endif // wxUSE_MARKUP
  183. m_sizerStatBox = (wxStaticBoxSizer *)NULL;
  184. m_sizerStatic = (wxSizer *)NULL;
  185. m_textBox =
  186. m_textLabel =
  187. #if wxUSE_MARKUP
  188. m_textLabelWithMarkup =
  189. #endif // wxUSE_MARKUP
  190. NULL;
  191. }
  192. void StaticWidgetsPage::CreateContent()
  193. {
  194. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  195. // left pane
  196. wxSizer *sizerLeft = new wxStaticBoxSizer(wxVERTICAL, this, "&Set style");
  197. m_chkGeneric = CreateCheckBoxAndAddToSizer(sizerLeft,
  198. "&Generic wxStaticText");
  199. m_chkVert = CreateCheckBoxAndAddToSizer(sizerLeft, "&Vertical line");
  200. m_chkAutoResize = CreateCheckBoxAndAddToSizer(sizerLeft, "&Fit to text");
  201. sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  202. static const wxString halign[] =
  203. {
  204. wxT("left"),
  205. wxT("centre"),
  206. wxT("right"),
  207. };
  208. static const wxString valign[] =
  209. {
  210. wxT("top"),
  211. wxT("centre"),
  212. wxT("bottom"),
  213. };
  214. m_radioHAlign = new wxRadioBox(this, wxID_ANY, wxT("&Horz alignment"),
  215. wxDefaultPosition, wxDefaultSize,
  216. WXSIZEOF(halign), halign, 3);
  217. m_radioVAlign = new wxRadioBox(this, wxID_ANY, wxT("&Vert alignment"),
  218. wxDefaultPosition, wxDefaultSize,
  219. WXSIZEOF(valign), valign, 3);
  220. sizerLeft->Add(m_radioHAlign, 0, wxGROW | wxALL, 5);
  221. sizerLeft->Add(m_radioVAlign, 0, wxGROW | wxALL, 5);
  222. sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer
  223. m_chkEllipsize = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Ellipsize"));
  224. static const wxString ellipsizeMode[] =
  225. {
  226. wxT("&start"),
  227. wxT("&middle"),
  228. wxT("&end"),
  229. };
  230. m_radioEllipsize = new wxRadioBox(this, wxID_ANY, wxT("&Ellipsize mode"),
  231. wxDefaultPosition, wxDefaultSize,
  232. WXSIZEOF(ellipsizeMode), ellipsizeMode,
  233. 3);
  234. sizerLeft->Add(m_radioEllipsize, 0, wxGROW | wxALL, 5);
  235. wxButton *btn = new wxButton(this, StaticPage_Reset, wxT("&Reset"));
  236. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  237. // middle pane
  238. wxSizer *sizerMiddle = new wxStaticBoxSizer(wxVERTICAL, this,
  239. "&Change labels");
  240. m_textBox = new wxTextCtrl(this, wxID_ANY, wxEmptyString);
  241. wxButton *b1 = new wxButton(this, wxID_ANY, "Change &box label");
  242. b1->Connect(wxEVT_BUTTON,
  243. wxCommandEventHandler(StaticWidgetsPage::OnButtonBoxText),
  244. NULL, this);
  245. sizerMiddle->Add(m_textBox, 0, wxEXPAND|wxALL, 5);
  246. sizerMiddle->Add(b1, 0, wxLEFT|wxBOTTOM, 5);
  247. m_textLabel = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  248. wxDefaultPosition, wxDefaultSize,
  249. wxTE_MULTILINE|wxHSCROLL);
  250. wxButton *b2 = new wxButton(this, wxID_ANY, "Change &text label");
  251. b2->Connect(wxEVT_BUTTON,
  252. wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelText),
  253. NULL, this);
  254. sizerMiddle->Add(m_textLabel, 0, wxEXPAND|wxALL, 5);
  255. sizerMiddle->Add(b2, 0, wxLEFT|wxBOTTOM, 5);
  256. #if wxUSE_MARKUP
  257. m_textLabelWithMarkup = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  258. wxDefaultPosition, wxDefaultSize,
  259. wxTE_MULTILINE|wxHSCROLL);
  260. wxButton *b3 = new wxButton(this, wxID_ANY, "Change decorated text label");
  261. b3->Connect(wxEVT_BUTTON,
  262. wxCommandEventHandler(StaticWidgetsPage::OnButtonLabelWithMarkupText),
  263. NULL, this);
  264. sizerMiddle->Add(m_textLabelWithMarkup, 0, wxEXPAND|wxALL, 5);
  265. sizerMiddle->Add(b3, 0, wxLEFT|wxBOTTOM, 5);
  266. m_chkGreen = CreateCheckBoxAndAddToSizer(sizerMiddle,
  267. "Decorated label on g&reen");
  268. #endif // wxUSE_MARKUP
  269. // final initializations
  270. // NB: must be done _before_ calling CreateStatic()
  271. Reset();
  272. m_textBox->SetValue(wxT("This is a &box"));
  273. m_textLabel->SetValue(wxT("And this is a\n\tlabel inside the box with a &mnemonic.\n")
  274. wxT("Only this text is affected by the ellipsize settings."));
  275. #if wxUSE_MARKUP
  276. m_textLabelWithMarkup->SetValue(wxT("Another label, this time <b>decorated</b> ")
  277. wxT("with <u>markup</u>; here you need entities ")
  278. wxT("for the symbols: &lt; &gt; &amp; &apos; &quot; ")
  279. wxT(" but you can still place &mnemonics..."));
  280. #endif // wxUSE_MARKUP
  281. // right pane
  282. wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
  283. sizerRight->SetMinSize(150, 0);
  284. m_sizerStatic = sizerRight;
  285. CreateStatic();
  286. // the 3 panes panes compose the window
  287. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  288. sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
  289. sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  290. SetSizer(sizerTop);
  291. }
  292. // ----------------------------------------------------------------------------
  293. // operations
  294. // ----------------------------------------------------------------------------
  295. void StaticWidgetsPage::Reset()
  296. {
  297. m_chkGeneric->SetValue(false);
  298. m_chkVert->SetValue(false);
  299. m_chkAutoResize->SetValue(true);
  300. m_chkEllipsize->SetValue(true);
  301. m_radioHAlign->SetSelection(StaticHAlign_Left);
  302. m_radioVAlign->SetSelection(StaticVAlign_Top);
  303. }
  304. void StaticWidgetsPage::CreateStatic()
  305. {
  306. wxWindowUpdateLocker lock(this);
  307. bool isVert = m_chkVert->GetValue();
  308. if ( m_sizerStatBox )
  309. {
  310. // delete m_sizerStatBox; -- deleted by Remove()
  311. m_sizerStatic->Remove(m_sizerStatBox);
  312. delete m_statText;
  313. #if wxUSE_MARKUP
  314. delete m_statMarkup;
  315. #endif // wxUSE_MARKUP
  316. #if wxUSE_STATLINE
  317. delete m_statLine;
  318. #endif // wxUSE_STATLINE
  319. }
  320. int flagsBox = 0,
  321. flagsText = ms_defaultFlags,
  322. flagsDummyText = ms_defaultFlags;
  323. if ( !m_chkAutoResize->GetValue() )
  324. {
  325. flagsText |= wxST_NO_AUTORESIZE;
  326. flagsDummyText |= wxST_NO_AUTORESIZE;
  327. }
  328. int align = 0;
  329. switch ( m_radioHAlign->GetSelection() )
  330. {
  331. default:
  332. wxFAIL_MSG(wxT("unexpected radiobox selection"));
  333. // fall through
  334. case StaticHAlign_Left:
  335. align |= wxALIGN_LEFT;
  336. break;
  337. case StaticHAlign_Centre:
  338. align |= wxALIGN_CENTRE_HORIZONTAL;
  339. break;
  340. case StaticHAlign_Right:
  341. align |= wxALIGN_RIGHT;
  342. break;
  343. }
  344. switch ( m_radioVAlign->GetSelection() )
  345. {
  346. default:
  347. wxFAIL_MSG(wxT("unexpected radiobox selection"));
  348. // fall through
  349. case StaticVAlign_Top:
  350. align |= wxALIGN_TOP;
  351. break;
  352. case StaticVAlign_Centre:
  353. align |= wxALIGN_CENTRE_VERTICAL;
  354. break;
  355. case StaticVAlign_Bottom:
  356. align |= wxALIGN_BOTTOM;
  357. break;
  358. }
  359. if ( m_chkEllipsize->GetValue() )
  360. {
  361. switch ( m_radioEllipsize->GetSelection() )
  362. {
  363. default:
  364. wxFAIL_MSG(wxT("unexpected radiobox selection"));
  365. // fall through
  366. case StaticEllipsize_Start:
  367. flagsDummyText |= wxST_ELLIPSIZE_START;
  368. break;
  369. case StaticEllipsize_Middle:
  370. flagsDummyText |= wxST_ELLIPSIZE_MIDDLE;
  371. break;
  372. case StaticEllipsize_End:
  373. flagsDummyText |= wxST_ELLIPSIZE_END;
  374. break;
  375. }
  376. }
  377. flagsDummyText |= align;
  378. flagsText |= align;
  379. flagsBox |= align;
  380. wxStaticBox *staticBox = new wxStaticBox(this, wxID_ANY,
  381. m_textBox->GetValue(),
  382. wxDefaultPosition, wxDefaultSize,
  383. flagsBox);
  384. m_sizerStatBox = new wxStaticBoxSizer(staticBox, isVert ? wxHORIZONTAL
  385. : wxVERTICAL);
  386. if ( m_chkGeneric->GetValue() )
  387. {
  388. m_statText = new wxGenericStaticText(staticBox, wxID_ANY,
  389. m_textLabel->GetValue(),
  390. wxDefaultPosition, wxDefaultSize,
  391. flagsDummyText);
  392. #if wxUSE_MARKUP
  393. m_statMarkup = new wxGenericStaticText(staticBox, wxID_ANY,
  394. wxString(),
  395. wxDefaultPosition, wxDefaultSize,
  396. flagsText);
  397. #endif // wxUSE_MARKUP
  398. }
  399. else // use native versions
  400. {
  401. m_statText = new wxStaticText(staticBox, wxID_ANY,
  402. m_textLabel->GetValue(),
  403. wxDefaultPosition, wxDefaultSize,
  404. flagsDummyText);
  405. #if wxUSE_MARKUP
  406. m_statMarkup = new wxStaticText(staticBox, wxID_ANY,
  407. wxString(),
  408. wxDefaultPosition, wxDefaultSize,
  409. flagsText);
  410. #endif // wxUSE_MARKUP
  411. }
  412. m_statText->SetToolTip("Tooltip for a label inside the box");
  413. #if wxUSE_MARKUP
  414. m_statMarkup->SetLabelMarkup(m_textLabelWithMarkup->GetValue());
  415. if ( m_chkGreen->GetValue() )
  416. m_statMarkup->SetBackgroundColour(*wxGREEN);
  417. #endif // wxUSE_MARKUP
  418. #if wxUSE_STATLINE
  419. m_statLine = new wxStaticLine(staticBox, wxID_ANY,
  420. wxDefaultPosition, wxDefaultSize,
  421. isVert ? wxLI_VERTICAL : wxLI_HORIZONTAL);
  422. #endif // wxUSE_STATLINE
  423. m_sizerStatBox->Add(m_statText, 0, wxGROW | wxALL, 5);
  424. #if wxUSE_STATLINE
  425. m_sizerStatBox->Add(m_statLine, 0, wxGROW | wxALL, 5);
  426. #endif // wxUSE_STATLINE
  427. #if wxUSE_MARKUP
  428. m_sizerStatBox->Add(m_statMarkup, 0, wxALL, 5);
  429. #endif // wxUSE_MARKUP
  430. m_sizerStatic->Add(m_sizerStatBox, 0, wxGROW);
  431. m_sizerStatic->Layout();
  432. m_statText->Connect(wxEVT_LEFT_UP,
  433. wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent),
  434. NULL, this);
  435. staticBox->Connect(wxEVT_LEFT_UP,
  436. wxMouseEventHandler(StaticWidgetsPage::OnMouseEvent),
  437. NULL, this);
  438. }
  439. // ----------------------------------------------------------------------------
  440. // event handlers
  441. // ----------------------------------------------------------------------------
  442. void StaticWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  443. {
  444. Reset();
  445. CreateStatic();
  446. }
  447. void StaticWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event)
  448. {
  449. if (event.GetEventObject() == static_cast<wxObject*>(m_chkEllipsize))
  450. {
  451. m_radioEllipsize->Enable(event.IsChecked());
  452. }
  453. CreateStatic();
  454. }
  455. void StaticWidgetsPage::OnButtonBoxText(wxCommandEvent& WXUNUSED(event))
  456. {
  457. m_sizerStatBox->GetStaticBox()->SetLabel(m_textBox->GetValue());
  458. }
  459. void StaticWidgetsPage::OnButtonLabelText(wxCommandEvent& WXUNUSED(event))
  460. {
  461. m_statText->SetLabel(m_textLabel->GetValue());
  462. // test GetLabel() and GetLabelText(); the first should return the
  463. // label as it is written in the relative text control; the second should
  464. // return the label as it's shown in the wxStaticText
  465. wxLogMessage(wxT("The original label should be '%s'"),
  466. m_statText->GetLabel());
  467. wxLogMessage(wxT("The label text is '%s'"),
  468. m_statText->GetLabelText());
  469. }
  470. #if wxUSE_MARKUP
  471. void StaticWidgetsPage::OnButtonLabelWithMarkupText(wxCommandEvent& WXUNUSED(event))
  472. {
  473. m_statMarkup->SetLabelMarkup(m_textLabelWithMarkup->GetValue());
  474. // test GetLabel() and GetLabelText(); the first should return the
  475. // label as it is written in the relative text control; the second should
  476. // return the label as it's shown in the wxStaticText
  477. wxLogMessage(wxT("The original label should be '%s'"),
  478. m_statMarkup->GetLabel());
  479. wxLogMessage(wxT("The label text is '%s'"),
  480. m_statMarkup->GetLabelText());
  481. }
  482. #endif // wxUSE_MARKUP
  483. void StaticWidgetsPage::OnMouseEvent(wxMouseEvent& event)
  484. {
  485. if ( event.GetEventObject() == m_statText )
  486. {
  487. wxLogMessage("Clicked on static text");
  488. }
  489. else
  490. {
  491. wxLogMessage("Clicked on static box");
  492. }
  493. }