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