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