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