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