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