textctrl.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: textctrl.cpp
  4. // Purpose: part of the widgets sample showing wxTextCtrl
  5. // Author: Vadim Zeitlin
  6. // Created: 27.03.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/timer.h"
  25. #include "wx/bitmap.h"
  26. #include "wx/button.h"
  27. #include "wx/checkbox.h"
  28. #include "wx/radiobox.h"
  29. #include "wx/statbox.h"
  30. #include "wx/stattext.h"
  31. #include "wx/textctrl.h"
  32. #include "wx/msgdlg.h"
  33. #endif
  34. #include "wx/sizer.h"
  35. #include "wx/ioswrap.h"
  36. #include "widgets.h"
  37. #include "icons/text.xpm"
  38. // ----------------------------------------------------------------------------
  39. // constants
  40. // ----------------------------------------------------------------------------
  41. // control ids
  42. enum
  43. {
  44. TextPage_Reset = wxID_HIGHEST,
  45. TextPage_Set,
  46. TextPage_Add,
  47. TextPage_Insert,
  48. TextPage_Clear,
  49. TextPage_Load,
  50. TextPage_StreamRedirector,
  51. TextPage_Password,
  52. TextPage_WrapLines,
  53. TextPage_Textctrl
  54. };
  55. // textctrl line number radiobox values
  56. enum TextLines
  57. {
  58. TextLines_Single,
  59. TextLines_Multi,
  60. TextLines_Max
  61. };
  62. // wrap style radio box
  63. enum WrapStyle
  64. {
  65. WrapStyle_None,
  66. WrapStyle_Word,
  67. WrapStyle_Char,
  68. WrapStyle_Best,
  69. WrapStyle_Max
  70. };
  71. #ifdef __WXMSW__
  72. // textctrl kind values
  73. enum TextKind
  74. {
  75. TextKind_Plain,
  76. TextKind_Rich,
  77. TextKind_Rich2,
  78. TextKind_Max
  79. };
  80. #endif // __WXMSW__
  81. // default values for the controls
  82. static const struct ControlValues
  83. {
  84. TextLines textLines;
  85. bool password;
  86. bool readonly;
  87. bool processEnter;
  88. bool filename;
  89. WrapStyle wrapStyle;
  90. #ifdef __WXMSW__
  91. TextKind textKind;
  92. #endif // __WXMSW__
  93. } DEFAULTS =
  94. {
  95. TextLines_Multi, // multiline
  96. false, // not password
  97. false, // not readonly
  98. true, // do process enter
  99. false, // not filename
  100. WrapStyle_Word, // wrap on word boundaries
  101. #ifdef __WXMSW__
  102. TextKind_Plain // plain EDIT control
  103. #endif // __WXMSW__
  104. };
  105. // ----------------------------------------------------------------------------
  106. // TextWidgetsPage
  107. // ----------------------------------------------------------------------------
  108. // Define a new frame type: this is going to be our main frame
  109. class TextWidgetsPage : public WidgetsPage
  110. {
  111. public:
  112. // ctor(s) and dtor
  113. TextWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  114. virtual ~TextWidgetsPage(){};
  115. virtual wxControl *GetWidget() const { return m_text; }
  116. virtual wxTextEntryBase *GetTextEntry() const { return m_text; }
  117. virtual void RecreateWidget() { CreateText(); }
  118. // lazy creation of the content
  119. virtual void CreateContent();
  120. protected:
  121. // create an info text contorl
  122. wxTextCtrl *CreateInfoText();
  123. // create a horz sizer holding a static text and this text control
  124. wxSizer *CreateTextWithLabelSizer(const wxString& label,
  125. wxTextCtrl *text,
  126. const wxString& label2 = wxEmptyString,
  127. wxTextCtrl *text2 = NULL);
  128. // event handlers
  129. void OnButtonReset(wxCommandEvent& event);
  130. void OnButtonClearLog(wxCommandEvent& event);
  131. void OnButtonSet(wxCommandEvent& event);
  132. void OnButtonAdd(wxCommandEvent& event);
  133. void OnButtonInsert(wxCommandEvent& event);
  134. void OnButtonClear(wxCommandEvent& event);
  135. void OnButtonLoad(wxCommandEvent& event);
  136. void OnStreamRedirector(wxCommandEvent& event);
  137. void OnButtonQuit(wxCommandEvent& event);
  138. void OnText(wxCommandEvent& event);
  139. void OnTextEnter(wxCommandEvent& event);
  140. void OnTextPasted(wxClipboardTextEvent& event);
  141. void OnCheckOrRadioBox(wxCommandEvent& event);
  142. void OnUpdateUIClearButton(wxUpdateUIEvent& event);
  143. void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event);
  144. void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event);
  145. void OnUpdateUIResetButton(wxUpdateUIEvent& event);
  146. void OnIdle(wxIdleEvent& event);
  147. // reset the textctrl parameters
  148. void Reset();
  149. // (re)create the textctrl
  150. void CreateText();
  151. // is the control currently single line?
  152. bool IsSingleLine() const
  153. {
  154. return m_radioTextLines->GetSelection() == TextLines_Single;
  155. }
  156. // the controls
  157. // ------------
  158. // the radiobox to choose between single and multi line
  159. wxRadioBox *m_radioTextLines;
  160. // and another one to choose the wrapping style
  161. wxRadioBox *m_radioWrap;
  162. // the checkboxes controlling text ctrl styles
  163. wxCheckBox *m_chkPassword,
  164. *m_chkReadonly,
  165. *m_chkProcessEnter,
  166. *m_chkFilename;
  167. // under MSW we test rich edit controls as well here
  168. #ifdef __WXMSW__
  169. wxRadioBox *m_radioKind;
  170. #endif // __WXMSW__
  171. // the textctrl itself and the sizer it is in
  172. wxTextCtrl *m_text;
  173. wxSizer *m_sizerText;
  174. // the information text zones
  175. wxTextCtrl *m_textPosCur,
  176. *m_textRowCur,
  177. *m_textColCur,
  178. *m_textPosLast,
  179. *m_textLineLast,
  180. *m_textSelFrom,
  181. *m_textSelTo,
  182. *m_textRange;
  183. // and the data to show in them
  184. long m_posCur,
  185. m_posLast,
  186. m_selFrom,
  187. m_selTo;
  188. wxString m_range10_20;
  189. private:
  190. // any class wishing to process wxWidgets events must use this macro
  191. wxDECLARE_EVENT_TABLE();
  192. DECLARE_WIDGETS_PAGE(TextWidgetsPage)
  193. };
  194. // ----------------------------------------------------------------------------
  195. // WidgetsTextCtrl
  196. // ----------------------------------------------------------------------------
  197. class WidgetsTextCtrl : public wxTextCtrl
  198. {
  199. public:
  200. WidgetsTextCtrl(wxWindow *parent,
  201. wxWindowID id,
  202. const wxString& value,
  203. int flags)
  204. : wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize, flags)
  205. {
  206. }
  207. protected:
  208. void OnRightClick(wxMouseEvent& event)
  209. {
  210. wxString where;
  211. wxTextCoord x, y;
  212. switch ( HitTest(event.GetPosition(), &x, &y) )
  213. {
  214. default:
  215. wxFAIL_MSG( wxT("unexpected HitTest() result") );
  216. // fall through
  217. case wxTE_HT_UNKNOWN:
  218. x = y = -1;
  219. where = wxT("nowhere near");
  220. break;
  221. case wxTE_HT_BEFORE:
  222. where = wxT("before");
  223. break;
  224. case wxTE_HT_BELOW:
  225. where = wxT("below");
  226. break;
  227. case wxTE_HT_BEYOND:
  228. where = wxT("beyond");
  229. break;
  230. case wxTE_HT_ON_TEXT:
  231. where = wxT("at");
  232. break;
  233. }
  234. wxLogMessage(wxT("Mouse is %s (%ld, %ld)"), where.c_str(), x, y);
  235. event.Skip();
  236. }
  237. private:
  238. wxDECLARE_EVENT_TABLE();
  239. };
  240. // ----------------------------------------------------------------------------
  241. // event tables
  242. // ----------------------------------------------------------------------------
  243. wxBEGIN_EVENT_TABLE(TextWidgetsPage, WidgetsPage)
  244. EVT_IDLE(TextWidgetsPage::OnIdle)
  245. EVT_BUTTON(TextPage_Reset, TextWidgetsPage::OnButtonReset)
  246. EVT_BUTTON(TextPage_StreamRedirector, TextWidgetsPage::OnStreamRedirector)
  247. EVT_BUTTON(TextPage_Clear, TextWidgetsPage::OnButtonClear)
  248. EVT_BUTTON(TextPage_Set, TextWidgetsPage::OnButtonSet)
  249. EVT_BUTTON(TextPage_Add, TextWidgetsPage::OnButtonAdd)
  250. EVT_BUTTON(TextPage_Insert, TextWidgetsPage::OnButtonInsert)
  251. EVT_BUTTON(TextPage_Load, TextWidgetsPage::OnButtonLoad)
  252. EVT_UPDATE_UI(TextPage_Clear, TextWidgetsPage::OnUpdateUIClearButton)
  253. EVT_UPDATE_UI(TextPage_Password, TextWidgetsPage::OnUpdateUIPasswordCheckbox)
  254. EVT_UPDATE_UI(TextPage_WrapLines, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox)
  255. EVT_UPDATE_UI(TextPage_Reset, TextWidgetsPage::OnUpdateUIResetButton)
  256. EVT_TEXT(TextPage_Textctrl, TextWidgetsPage::OnText)
  257. EVT_TEXT_ENTER(TextPage_Textctrl, TextWidgetsPage::OnTextEnter)
  258. EVT_TEXT_PASTE(TextPage_Textctrl, TextWidgetsPage::OnTextPasted)
  259. EVT_CHECKBOX(wxID_ANY, TextWidgetsPage::OnCheckOrRadioBox)
  260. EVT_RADIOBOX(wxID_ANY, TextWidgetsPage::OnCheckOrRadioBox)
  261. wxEND_EVENT_TABLE()
  262. wxBEGIN_EVENT_TABLE(WidgetsTextCtrl, wxTextCtrl)
  263. EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick)
  264. wxEND_EVENT_TABLE()
  265. // ============================================================================
  266. // implementation
  267. // ============================================================================
  268. #if defined(__WXX11__)
  269. #define FAMILY_CTRLS NATIVE_CTRLS
  270. #elif defined(__WXUNIVERSAL__)
  271. #define FAMILY_CTRLS UNIVERSAL_CTRLS
  272. #else
  273. #define FAMILY_CTRLS NATIVE_CTRLS
  274. #endif
  275. IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage, wxT("Text"),
  276. FAMILY_CTRLS | EDITABLE_CTRLS
  277. );
  278. // ----------------------------------------------------------------------------
  279. // TextWidgetsPage creation
  280. // ----------------------------------------------------------------------------
  281. TextWidgetsPage::TextWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist)
  282. : WidgetsPage(book, imaglist, text_xpm)
  283. {
  284. // init everything
  285. #ifdef __WXMSW__
  286. m_radioKind =
  287. #endif // __WXMSW__
  288. m_radioWrap =
  289. m_radioTextLines = (wxRadioBox *)NULL;
  290. m_chkPassword =
  291. m_chkReadonly =
  292. m_chkProcessEnter =
  293. m_chkFilename = (wxCheckBox *)NULL;
  294. m_text =
  295. m_textPosCur =
  296. m_textRowCur =
  297. m_textColCur =
  298. m_textPosLast =
  299. m_textLineLast =
  300. m_textSelFrom =
  301. m_textSelTo =
  302. m_textRange = (wxTextCtrl *)NULL;
  303. m_sizerText = (wxSizer *)NULL;
  304. m_posCur =
  305. m_posLast =
  306. m_selFrom =
  307. m_selTo = -2; // not -1 which means "no selection"
  308. }
  309. void TextWidgetsPage::CreateContent()
  310. {
  311. // left pane
  312. static const wxString modes[] =
  313. {
  314. wxT("single line"),
  315. wxT("multi line"),
  316. };
  317. wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set textctrl parameters"));
  318. m_radioTextLines = new wxRadioBox(this, wxID_ANY, wxT("&Number of lines:"),
  319. wxDefaultPosition, wxDefaultSize,
  320. WXSIZEOF(modes), modes,
  321. 1, wxRA_SPECIFY_COLS);
  322. wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
  323. sizerLeft->Add(m_radioTextLines, 0, wxGROW | wxALL, 5);
  324. sizerLeft->AddSpacer(5);
  325. m_chkPassword = CreateCheckBoxAndAddToSizer(
  326. sizerLeft, wxT("&Password control"), TextPage_Password
  327. );
  328. m_chkReadonly = CreateCheckBoxAndAddToSizer(
  329. sizerLeft, wxT("&Read-only mode")
  330. );
  331. m_chkProcessEnter = CreateCheckBoxAndAddToSizer(
  332. sizerLeft, wxT("Process &Enter")
  333. );
  334. m_chkFilename = CreateCheckBoxAndAddToSizer(
  335. sizerLeft, wxT("&Filename control")
  336. );
  337. m_chkFilename->Disable(); // not implemented yet
  338. sizerLeft->AddSpacer(5);
  339. static const wxString wrap[] =
  340. {
  341. wxT("no wrap"),
  342. wxT("word wrap"),
  343. wxT("char wrap"),
  344. wxT("best wrap"),
  345. };
  346. m_radioWrap = new wxRadioBox(this, wxID_ANY, wxT("&Wrap style:"),
  347. wxDefaultPosition, wxDefaultSize,
  348. WXSIZEOF(wrap), wrap,
  349. 1, wxRA_SPECIFY_COLS);
  350. sizerLeft->Add(m_radioWrap, 0, wxGROW | wxALL, 5);
  351. #ifdef __WXMSW__
  352. static const wxString kinds[] =
  353. {
  354. wxT("plain edit"),
  355. wxT("rich edit"),
  356. wxT("rich edit 2.0"),
  357. };
  358. m_radioKind = new wxRadioBox(this, wxID_ANY, wxT("Control &kind"),
  359. wxDefaultPosition, wxDefaultSize,
  360. WXSIZEOF(kinds), kinds,
  361. 1, wxRA_SPECIFY_COLS);
  362. sizerLeft->AddSpacer(5);
  363. sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
  364. #endif // __WXMSW__
  365. wxButton *btn = new wxButton(this, TextPage_Reset, wxT("&Reset"));
  366. sizerLeft->Add(2, 2, 0, wxGROW | wxALL, 1); // spacer
  367. sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  368. // middle pane
  369. wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, wxT("&Change contents:"));
  370. wxSizer *sizerMiddleUp = new wxStaticBoxSizer(box2, wxVERTICAL);
  371. btn = new wxButton(this, TextPage_Set, wxT("&Set text value"));
  372. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  373. btn = new wxButton(this, TextPage_Add, wxT("&Append text"));
  374. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  375. btn = new wxButton(this, TextPage_Insert, wxT("&Insert text"));
  376. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  377. btn = new wxButton(this, TextPage_Load, wxT("&Load file"));
  378. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  379. btn = new wxButton(this, TextPage_Clear, wxT("&Clear"));
  380. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  381. btn = new wxButton(this, TextPage_StreamRedirector, wxT("St&ream redirection"));
  382. sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
  383. wxStaticBox *box4 = new wxStaticBox(this, wxID_ANY, wxT("&Info:"));
  384. wxSizer *sizerMiddleDown = new wxStaticBoxSizer(box4, wxVERTICAL);
  385. m_textPosCur = CreateInfoText();
  386. m_textRowCur = CreateInfoText();
  387. m_textColCur = CreateInfoText();
  388. wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
  389. sizerRow->Add(CreateTextWithLabelSizer
  390. (
  391. wxT("Current pos:"),
  392. m_textPosCur
  393. ),
  394. 0, wxRIGHT, 5);
  395. sizerRow->Add(CreateTextWithLabelSizer
  396. (
  397. wxT("Col:"),
  398. m_textColCur
  399. ),
  400. 0, wxLEFT | wxRIGHT, 5);
  401. sizerRow->Add(CreateTextWithLabelSizer
  402. (
  403. wxT("Row:"),
  404. m_textRowCur
  405. ),
  406. 0, wxLEFT, 5);
  407. sizerMiddleDown->Add(sizerRow, 0, wxALL, 5);
  408. m_textLineLast = CreateInfoText();
  409. m_textPosLast = CreateInfoText();
  410. sizerMiddleDown->Add
  411. (
  412. CreateTextWithLabelSizer
  413. (
  414. wxT("Number of lines:"),
  415. m_textLineLast,
  416. wxT("Last position:"),
  417. m_textPosLast
  418. ),
  419. 0, wxALL, 5
  420. );
  421. m_textSelFrom = CreateInfoText();
  422. m_textSelTo = CreateInfoText();
  423. sizerMiddleDown->Add
  424. (
  425. CreateTextWithLabelSizer
  426. (
  427. wxT("Selection: from"),
  428. m_textSelFrom,
  429. wxT("to"),
  430. m_textSelTo
  431. ),
  432. 0, wxALL, 5
  433. );
  434. m_textRange = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  435. wxDefaultPosition, wxDefaultSize,
  436. wxTE_READONLY);
  437. sizerMiddleDown->Add
  438. (
  439. CreateTextWithLabelSizer
  440. (
  441. wxT("Range 10..20:"),
  442. m_textRange
  443. ),
  444. 0, wxALL, 5
  445. );
  446. wxSizer *sizerMiddle = new wxBoxSizer(wxVERTICAL);
  447. sizerMiddle->Add(sizerMiddleUp, 0, wxGROW);
  448. sizerMiddle->Add(sizerMiddleDown, 1, wxGROW | wxTOP, 5);
  449. // right pane
  450. wxStaticBox *box3 = new wxStaticBox(this, wxID_ANY, wxT("&Text:"));
  451. m_sizerText = new wxStaticBoxSizer(box3, wxHORIZONTAL);
  452. Reset();
  453. CreateText();
  454. m_sizerText->SetMinSize(150, 0);
  455. // the 3 panes panes compose the upper part of the window
  456. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  457. sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
  458. sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
  459. sizerTop->Add(m_sizerText, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  460. SetSizer(sizerTop);
  461. }
  462. // ----------------------------------------------------------------------------
  463. // creation helpers
  464. // ----------------------------------------------------------------------------
  465. wxTextCtrl *TextWidgetsPage::CreateInfoText()
  466. {
  467. static int s_maxWidth = 0;
  468. if ( !s_maxWidth )
  469. {
  470. // calc it once only
  471. GetTextExtent(wxT("9999999"), &s_maxWidth, NULL);
  472. }
  473. wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
  474. wxDefaultPosition,
  475. wxSize(s_maxWidth, wxDefaultCoord),
  476. wxTE_READONLY);
  477. return text;
  478. }
  479. wxSizer *TextWidgetsPage::CreateTextWithLabelSizer(const wxString& label,
  480. wxTextCtrl *text,
  481. const wxString& label2,
  482. wxTextCtrl *text2)
  483. {
  484. wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
  485. sizerRow->Add(new wxStaticText(this, wxID_ANY, label), 0,
  486. wxALIGN_CENTRE_VERTICAL | wxRIGHT, 5);
  487. sizerRow->Add(text, 0, wxALIGN_CENTRE_VERTICAL);
  488. if ( text2 )
  489. {
  490. sizerRow->Add(new wxStaticText(this, wxID_ANY, label2), 0,
  491. wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 5);
  492. sizerRow->Add(text2, 0, wxALIGN_CENTRE_VERTICAL);
  493. }
  494. return sizerRow;
  495. }
  496. // ----------------------------------------------------------------------------
  497. // operations
  498. // ----------------------------------------------------------------------------
  499. void TextWidgetsPage::Reset()
  500. {
  501. m_radioTextLines->SetSelection(DEFAULTS.textLines);
  502. m_chkPassword->SetValue(DEFAULTS.password);
  503. m_chkReadonly->SetValue(DEFAULTS.readonly);
  504. m_chkProcessEnter->SetValue(DEFAULTS.processEnter);
  505. m_chkFilename->SetValue(DEFAULTS.filename);
  506. m_radioWrap->SetSelection(DEFAULTS.wrapStyle);
  507. #ifdef __WXMSW__
  508. m_radioKind->SetSelection(DEFAULTS.textKind);
  509. #endif // __WXMSW__
  510. }
  511. void TextWidgetsPage::CreateText()
  512. {
  513. int flags = ms_defaultFlags;
  514. switch ( m_radioTextLines->GetSelection() )
  515. {
  516. default:
  517. wxFAIL_MSG( wxT("unexpected lines radio box selection") );
  518. case TextLines_Single:
  519. break;
  520. case TextLines_Multi:
  521. flags |= wxTE_MULTILINE;
  522. m_chkPassword->SetValue(false);
  523. break;
  524. }
  525. if ( m_chkPassword->GetValue() )
  526. flags |= wxTE_PASSWORD;
  527. if ( m_chkReadonly->GetValue() )
  528. flags |= wxTE_READONLY;
  529. if ( m_chkProcessEnter->GetValue() )
  530. flags |= wxTE_PROCESS_ENTER;
  531. switch ( m_radioWrap->GetSelection() )
  532. {
  533. default:
  534. wxFAIL_MSG( wxT("unexpected wrap style radio box selection") );
  535. case WrapStyle_None:
  536. flags |= wxTE_DONTWRAP; // same as wxHSCROLL
  537. break;
  538. case WrapStyle_Word:
  539. flags |= wxTE_WORDWRAP;
  540. break;
  541. case WrapStyle_Char:
  542. flags |= wxTE_CHARWRAP;
  543. break;
  544. case WrapStyle_Best:
  545. // this is default but use symbolic file name for consistency
  546. flags |= wxTE_BESTWRAP;
  547. break;
  548. }
  549. #ifdef __WXMSW__
  550. switch ( m_radioKind->GetSelection() )
  551. {
  552. default:
  553. wxFAIL_MSG( wxT("unexpected kind radio box selection") );
  554. case TextKind_Plain:
  555. break;
  556. case TextKind_Rich:
  557. flags |= wxTE_RICH;
  558. break;
  559. case TextKind_Rich2:
  560. flags |= wxTE_RICH2;
  561. break;
  562. }
  563. #endif // __WXMSW__
  564. wxString valueOld;
  565. if ( m_text )
  566. {
  567. valueOld = m_text->GetValue();
  568. m_sizerText->Detach( m_text );
  569. delete m_text;
  570. }
  571. else
  572. {
  573. valueOld = wxT("Hello, Universe!");
  574. }
  575. m_text = new WidgetsTextCtrl(this, TextPage_Textctrl, valueOld, flags);
  576. #if 0
  577. if ( m_chkFilename->GetValue() )
  578. ;
  579. #endif // TODO
  580. // cast to int needed to silence gcc warning about different enums
  581. m_sizerText->Add(m_text, 1, wxALL |
  582. (flags & wxTE_MULTILINE ? (int)wxGROW
  583. : wxALIGN_TOP), 5);
  584. m_sizerText->Layout();
  585. }
  586. // ----------------------------------------------------------------------------
  587. // event handlers
  588. // ----------------------------------------------------------------------------
  589. void TextWidgetsPage::OnIdle(wxIdleEvent& WXUNUSED(event))
  590. {
  591. // update all info texts
  592. if ( m_textPosCur )
  593. {
  594. long posCur = m_text->GetInsertionPoint();
  595. if ( posCur != m_posCur )
  596. {
  597. m_textPosCur->Clear();
  598. m_textRowCur->Clear();
  599. m_textColCur->Clear();
  600. long col, row;
  601. m_text->PositionToXY(posCur, &col, &row);
  602. *m_textPosCur << posCur;
  603. *m_textRowCur << row;
  604. *m_textColCur << col;
  605. m_posCur = posCur;
  606. }
  607. }
  608. if ( m_textPosLast )
  609. {
  610. long posLast = m_text->GetLastPosition();
  611. if ( posLast != m_posLast )
  612. {
  613. m_textPosLast->Clear();
  614. *m_textPosLast << posLast;
  615. m_posLast = posLast;
  616. }
  617. }
  618. if ( m_textLineLast )
  619. {
  620. m_textLineLast->SetValue(
  621. wxString::Format(wxT("%d"), m_text->GetNumberOfLines()) );
  622. }
  623. if ( m_textSelFrom && m_textSelTo )
  624. {
  625. long selFrom, selTo;
  626. m_text->GetSelection(&selFrom, &selTo);
  627. if ( selFrom != m_selFrom )
  628. {
  629. m_textSelFrom->Clear();
  630. *m_textSelFrom << selFrom;
  631. m_selFrom = selFrom;
  632. }
  633. if ( selTo != m_selTo )
  634. {
  635. m_textSelTo->Clear();
  636. *m_textSelTo << selTo;
  637. m_selTo = selTo;
  638. }
  639. }
  640. if ( m_textRange )
  641. {
  642. wxString range = m_text->GetRange(10, 20);
  643. if ( range != m_range10_20 )
  644. {
  645. m_range10_20 = range;
  646. m_textRange->SetValue(range);
  647. }
  648. }
  649. }
  650. void TextWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  651. {
  652. Reset();
  653. CreateText();
  654. }
  655. void TextWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
  656. {
  657. m_text->SetValue(m_text->GetWindowStyle() & wxTE_MULTILINE
  658. ? wxT("Here,\nthere and\neverywhere")
  659. : wxT("Yellow submarine"));
  660. m_text->SetFocus();
  661. }
  662. void TextWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
  663. {
  664. if ( m_text->GetWindowStyle() & wxTE_MULTILINE )
  665. {
  666. m_text->AppendText(wxT("We all live in a\n"));
  667. }
  668. m_text->AppendText(wxT("Yellow submarine"));
  669. }
  670. void TextWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
  671. {
  672. m_text->WriteText(wxT("Is there anybody going to listen to my story"));
  673. if ( m_text->GetWindowStyle() & wxTE_MULTILINE )
  674. {
  675. m_text->WriteText(wxT("\nall about the girl who came to stay"));
  676. }
  677. }
  678. void TextWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
  679. {
  680. m_text->Clear();
  681. m_text->SetFocus();
  682. }
  683. void TextWidgetsPage::OnButtonLoad(wxCommandEvent& WXUNUSED(event))
  684. {
  685. // search for the file in several dirs where it's likely to be
  686. wxPathList pathlist;
  687. pathlist.Add(wxT("."));
  688. pathlist.Add(wxT(".."));
  689. pathlist.Add(wxT("../../../samples/widgets"));
  690. wxString filename = pathlist.FindValidPath(wxT("textctrl.cpp"));
  691. if ( !filename )
  692. {
  693. wxLogError(wxT("File textctrl.cpp not found."));
  694. }
  695. else // load it
  696. {
  697. wxStopWatch sw;
  698. if ( !m_text->LoadFile(filename) )
  699. {
  700. // this is not supposed to happen ...
  701. wxLogError(wxT("Error loading file."));
  702. }
  703. else
  704. {
  705. long elapsed = sw.Time();
  706. wxLogMessage(wxT("Loaded file '%s' in %lu.%us"),
  707. filename.c_str(), elapsed / 1000,
  708. (unsigned int) elapsed % 1000);
  709. }
  710. }
  711. }
  712. void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
  713. {
  714. event.Enable(!m_text->GetValue().empty());
  715. }
  716. void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event)
  717. {
  718. event.Enable( !IsSingleLine() );
  719. }
  720. void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event)
  721. {
  722. // can't put multiline control in password mode
  723. event.Enable( IsSingleLine() );
  724. }
  725. void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
  726. {
  727. event.Enable( (m_radioTextLines->GetSelection() != DEFAULTS.textLines) ||
  728. #ifdef __WXMSW__
  729. (m_radioKind->GetSelection() != DEFAULTS.textKind) ||
  730. #endif // __WXMSW__
  731. (m_chkPassword->GetValue() != DEFAULTS.password) ||
  732. (m_chkReadonly->GetValue() != DEFAULTS.readonly) ||
  733. (m_chkProcessEnter->GetValue() != DEFAULTS.processEnter) ||
  734. (m_chkFilename->GetValue() != DEFAULTS.filename) ||
  735. (m_radioWrap->GetSelection() != DEFAULTS.wrapStyle) );
  736. }
  737. void TextWidgetsPage::OnText(wxCommandEvent& WXUNUSED(event))
  738. {
  739. // small hack to suppress the very first message: by then the logging is
  740. // not yet redirected and so initial setting of the text value results in
  741. // an annoying message box
  742. static bool s_firstTime = true;
  743. if ( s_firstTime )
  744. {
  745. s_firstTime = false;
  746. return;
  747. }
  748. wxLogMessage(wxT("Text ctrl value changed"));
  749. }
  750. void TextWidgetsPage::OnTextEnter(wxCommandEvent& event)
  751. {
  752. wxLogMessage(wxT("Text entered: '%s'"), event.GetString().c_str());
  753. event.Skip();
  754. }
  755. void TextWidgetsPage::OnTextPasted(wxClipboardTextEvent& event)
  756. {
  757. wxLogMessage("Text pasted from clipboard.");
  758. event.Skip();
  759. }
  760. void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
  761. {
  762. CreateText();
  763. }
  764. void TextWidgetsPage::OnStreamRedirector(wxCommandEvent& WXUNUSED(event))
  765. {
  766. #if wxHAS_TEXT_WINDOW_STREAM
  767. wxStreamToTextRedirector redirect(m_text);
  768. wxString str( wxT("Outputed to cout, appears in wxTextCtrl!") );
  769. wxSTD cout << str << wxSTD endl;
  770. #else
  771. wxMessageBox(wxT("This wxWidgets build does not support wxStreamToTextRedirector"));
  772. #endif
  773. }