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