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