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