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