]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/textctrl.cpp
bakefile-generated makefiles for the sample
[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{
56 TextPage_Reset = 100,
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
5378558e 135 TextWidgetsPage(wxBookCtrlBase *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
332IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage, _T("Text"));
333
334// ----------------------------------------------------------------------------
335// TextWidgetsPage creation
336// ----------------------------------------------------------------------------
337
5378558e 338TextWidgetsPage::TextWidgetsPage(wxBookCtrlBase *book, wxImageList *imaglist)
61c083e7 339 : WidgetsPage(book)
32b8ec41
VZ
340{
341 imaglist->Add(wxBitmap(text_xpm));
342
343 // init everything
64b54de8
VZ
344#ifdef __WXMSW__
345 m_radioKind =
346#endif // __WXMSW__
fb4888a6 347 m_radioWrap =
32b8ec41
VZ
348 m_radioTextLines = (wxRadioBox *)NULL;
349
350 m_chkPassword =
32b8ec41
VZ
351 m_chkReadonly = (wxCheckBox *)NULL;
352
353 m_text =
354 m_textPosCur =
355 m_textRowCur =
356 m_textColCur =
357 m_textPosLast =
358 m_textLineLast =
359 m_textSelFrom =
64b54de8
VZ
360 m_textSelTo =
361 m_textRange = (wxTextCtrl *)NULL;
362
32b8ec41
VZ
363 m_sizerText = (wxSizer *)NULL;
364
365 m_posCur =
366 m_posLast =
367 m_selFrom =
368 m_selTo = -2; // not -1 which means "no selection"
369
370 // left pane
371 static const wxString modes[] =
372 {
373 _T("single line"),
374 _T("multi line"),
375 };
376
206d3a16
JS
377 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set textctrl parameters"));
378 m_radioTextLines = new wxRadioBox(this, wxID_ANY, _T("&Number of lines:"),
32b8ec41
VZ
379 wxDefaultPosition, wxDefaultSize,
380 WXSIZEOF(modes), modes,
381 1, wxRA_SPECIFY_COLS);
382
383 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
384
385 sizerLeft->Add(m_radioTextLines, 0, wxGROW | wxALL, 5);
fb4888a6 386 sizerLeft->AddSpacer(5);
32b8ec41
VZ
387
388 m_chkPassword = CreateCheckBoxAndAddToSizer(
389 sizerLeft, _T("&Password control"), TextPage_Password
390 );
32b8ec41
VZ
391 m_chkReadonly = CreateCheckBoxAndAddToSizer(
392 sizerLeft, _T("&Read-only mode")
393 );
fb4888a6
VZ
394 sizerLeft->AddSpacer(5);
395
396 static const wxString wrap[] =
397 {
398 _T("no wrap"),
399 _T("word wrap"),
400 _T("char wrap"),
401 _T("best wrap"),
402 };
403
404 m_radioWrap = new wxRadioBox(this, wxID_ANY, _T("&Wrap style:"),
405 wxDefaultPosition, wxDefaultSize,
406 WXSIZEOF(wrap), wrap,
407 1, wxRA_SPECIFY_COLS);
408 sizerLeft->Add(m_radioWrap, 0, wxGROW | wxALL, 5);
32b8ec41 409
64b54de8
VZ
410#ifdef __WXMSW__
411 static const wxString kinds[] =
412 {
413 _T("plain edit"),
414 _T("rich edit"),
415 _T("rich edit 2.0"),
416 };
417
206d3a16 418 m_radioKind = new wxRadioBox(this, wxID_ANY, _T("Control &kind"),
64b54de8
VZ
419 wxDefaultPosition, wxDefaultSize,
420 WXSIZEOF(kinds), kinds,
421 1, wxRA_SPECIFY_COLS);
422
fb4888a6 423 sizerLeft->AddSpacer(5);
64b54de8
VZ
424 sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5);
425#endif // __WXMSW__
426
32b8ec41 427 wxButton *btn = new wxButton(this, TextPage_Reset, _T("&Reset"));
7b127900 428 sizerLeft->Add(2, 2, 0, wxGROW | wxALL, 1); // spacer
32b8ec41
VZ
429 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
430
431 // middle pane
206d3a16 432 wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change contents:"));
32b8ec41
VZ
433 wxSizer *sizerMiddleUp = new wxStaticBoxSizer(box2, wxVERTICAL);
434
435 btn = new wxButton(this, TextPage_Set, _T("&Set text value"));
7b127900 436 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
32b8ec41
VZ
437
438 btn = new wxButton(this, TextPage_Add, _T("&Append text"));
7b127900 439 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
32b8ec41
VZ
440
441 btn = new wxButton(this, TextPage_Insert, _T("&Insert text"));
7b127900 442 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
32b8ec41
VZ
443
444 btn = new wxButton(this, TextPage_Load, _T("&Load file"));
7b127900 445 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
32b8ec41
VZ
446
447 btn = new wxButton(this, TextPage_Clear, _T("&Clear"));
7b127900 448 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
32b8ec41 449
286ff306
WS
450 btn = new wxButton(this, TextPage_StreamRedirector, _T("St&ream redirection"));
451 sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1);
452
206d3a16 453 wxStaticBox *box4 = new wxStaticBox(this, wxID_ANY, _T("&Info:"));
32b8ec41
VZ
454 wxSizer *sizerMiddleDown = new wxStaticBoxSizer(box4, wxVERTICAL);
455
456 m_textPosCur = CreateInfoText();
457 m_textRowCur = CreateInfoText();
458 m_textColCur = CreateInfoText();
459
460 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
461 sizerRow->Add(CreateTextWithLabelSizer
462 (
463 _T("Current pos:"),
464 m_textPosCur
465 ),
466 0, wxRIGHT, 5);
467 sizerRow->Add(CreateTextWithLabelSizer
468 (
469 _T("Col:"),
470 m_textColCur
471 ),
472 0, wxLEFT | wxRIGHT, 5);
473 sizerRow->Add(CreateTextWithLabelSizer
474 (
475 _T("Row:"),
476 m_textRowCur
477 ),
478 0, wxLEFT, 5);
479 sizerMiddleDown->Add(sizerRow, 0, wxALL, 5);
480
481 m_textLineLast = CreateInfoText();
482 m_textPosLast = CreateInfoText();
483 sizerMiddleDown->Add
484 (
485 CreateTextWithLabelSizer
486 (
487 _T("Number of lines:"),
488 m_textLineLast,
489 _T("Last position:"),
490 m_textPosLast
491 ),
492 0, wxALL, 5
493 );
494
495 m_textSelFrom = CreateInfoText();
496 m_textSelTo = CreateInfoText();
497 sizerMiddleDown->Add
498 (
499 CreateTextWithLabelSizer
500 (
501 _T("Selection: from"),
502 m_textSelFrom,
503 _T("to"),
504 m_textSelTo
505 ),
506 0, wxALL, 5
507 );
64b54de8 508
206d3a16 509 m_textRange = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
64b54de8
VZ
510 wxDefaultPosition, wxDefaultSize,
511 wxTE_READONLY);
512 sizerMiddleDown->Add
513 (
514 CreateTextWithLabelSizer
515 (
516 _T("Range 10..20:"),
517 m_textRange
518 ),
519 0, wxALL, 5
520 );
521
32b8ec41
VZ
522 wxSizer *sizerMiddle = new wxBoxSizer(wxVERTICAL);
523 sizerMiddle->Add(sizerMiddleUp, 0, wxGROW);
524 sizerMiddle->Add(sizerMiddleDown, 1, wxGROW | wxTOP, 5);
525
526 // right pane
206d3a16 527 wxStaticBox *box3 = new wxStaticBox(this, wxID_ANY, _T("&Text:"));
32b8ec41
VZ
528 m_sizerText = new wxStaticBoxSizer(box3, wxHORIZONTAL);
529 Reset();
530 CreateText();
7b127900 531 m_sizerText->SetMinSize(150, 0);
32b8ec41
VZ
532
533 // the 3 panes panes compose the upper part of the window
534 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
535 sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10);
536 sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10);
537 sizerTop->Add(m_sizerText, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
538
32b8ec41
VZ
539 SetSizer(sizerTop);
540
541 sizerTop->Fit(this);
542}
543
32b8ec41
VZ
544// ----------------------------------------------------------------------------
545// creation helpers
546// ----------------------------------------------------------------------------
547
548wxTextCtrl *TextWidgetsPage::CreateInfoText()
549{
550 static int s_maxWidth = 0;
551 if ( !s_maxWidth )
552 {
553 // calc it once only
554 GetTextExtent(_T("9999999"), &s_maxWidth, NULL);
555 }
556
206d3a16 557 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
32b8ec41 558 wxDefaultPosition,
15cdc341 559 wxSize(s_maxWidth, wxDefaultCoord),
32b8ec41
VZ
560 wxTE_READONLY);
561 return text;
562}
563
564wxSizer *TextWidgetsPage::CreateTextWithLabelSizer(const wxString& label,
565 wxTextCtrl *text,
566 const wxString& label2,
567 wxTextCtrl *text2)
568{
569 wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL);
206d3a16 570 sizerRow->Add(new wxStaticText(this, wxID_ANY, label), 0,
32b8ec41
VZ
571 wxALIGN_CENTRE_VERTICAL | wxRIGHT, 5);
572 sizerRow->Add(text, 0, wxALIGN_CENTRE_VERTICAL);
573 if ( text2 )
574 {
206d3a16 575 sizerRow->Add(new wxStaticText(this, wxID_ANY, label2), 0,
32b8ec41
VZ
576 wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 5);
577 sizerRow->Add(text2, 0, wxALIGN_CENTRE_VERTICAL);
578 }
579
580 return sizerRow;
581}
582
583// ----------------------------------------------------------------------------
584// operations
585// ----------------------------------------------------------------------------
586
587void TextWidgetsPage::Reset()
588{
589 m_radioTextLines->SetSelection(DEFAULTS.textLines);
fb4888a6 590
32b8ec41 591 m_chkPassword->SetValue(DEFAULTS.password);
32b8ec41 592 m_chkReadonly->SetValue(DEFAULTS.readonly);
fb4888a6
VZ
593
594 m_radioWrap->SetSelection(DEFAULTS.wrapStyle);
595
64b54de8
VZ
596#ifdef __WXMSW__
597 m_radioKind->SetSelection(DEFAULTS.textKind);
598#endif // __WXMSW__
32b8ec41
VZ
599}
600
601void TextWidgetsPage::CreateText()
602{
1301e228 603 int flags = ms_defaultFlags;
32b8ec41
VZ
604 switch ( m_radioTextLines->GetSelection() )
605 {
606 default:
64b54de8 607 wxFAIL_MSG( _T("unexpected lines radio box selection") );
32b8ec41
VZ
608
609 case TextLines_Single:
610 break;
611
612 case TextLines_Multi:
613 flags |= wxTE_MULTILINE;
206d3a16 614 m_chkPassword->SetValue(false);
32b8ec41
VZ
615 break;
616 }
617
618 if ( m_chkPassword->GetValue() )
619 flags |= wxTE_PASSWORD;
620 if ( m_chkReadonly->GetValue() )
621 flags |= wxTE_READONLY;
fb4888a6
VZ
622
623 switch ( m_radioWrap->GetSelection() )
624 {
625 default:
626 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
627
628 case WrapStyle_None:
629 flags |= wxTE_DONTWRAP; // same as wxHSCROLL
630 break;
631
632 case WrapStyle_Word:
633 flags |= wxTE_WORDWRAP;
634 break;
635
636 case WrapStyle_Char:
40ff126a 637 flags |= wxTE_CHARWRAP;
fb4888a6
VZ
638 break;
639
640 case WrapStyle_Best:
641 // this is default but use symbolic file name for consistency
642 flags |= wxTE_BESTWRAP;
643 break;
644 }
32b8ec41 645
64b54de8
VZ
646#ifdef __WXMSW__
647 switch ( m_radioKind->GetSelection() )
648 {
649 default:
650 wxFAIL_MSG( _T("unexpected kind radio box selection") );
651
652 case TextKind_Plain:
653 break;
654
655 case TextKind_Rich:
656 flags |= wxTE_RICH;
657 break;
658
659 case TextKind_Rich2:
660 flags |= wxTE_RICH2;
661 break;
662 }
663#endif // __WXMSW__
664
32b8ec41
VZ
665 wxString valueOld;
666 if ( m_text )
667 {
668 valueOld = m_text->GetValue();
669
12a3f227 670 m_sizerText->Detach( m_text );
32b8ec41
VZ
671 delete m_text;
672 }
673 else
674 {
675 valueOld = _T("Hello, Universe!");
676 }
677
efe66bbc 678 m_text = new WidgetsTextCtrl(this, TextPage_Textctrl, valueOld, flags);
72383a72
VZ
679
680 // cast to int needed to silence gcc warning about different enums
32b8ec41 681 m_sizerText->Add(m_text, 1, wxALL |
72383a72 682 (flags & wxTE_MULTILINE ? (int)wxGROW
32b8ec41
VZ
683 : wxALIGN_TOP), 5);
684 m_sizerText->Layout();
685}
686
687// ----------------------------------------------------------------------------
688// event handlers
689// ----------------------------------------------------------------------------
690
691void TextWidgetsPage::OnIdle(wxIdleEvent& WXUNUSED(event))
692{
693 // update all info texts
694
695 if ( m_textPosCur )
696 {
697 long posCur = m_text->GetInsertionPoint();
698 if ( posCur != m_posCur )
699 {
700 m_textPosCur->Clear();
701 m_textRowCur->Clear();
702 m_textColCur->Clear();
703
704 long col, row;
705 m_text->PositionToXY(posCur, &col, &row);
706
707 *m_textPosCur << posCur;
708 *m_textRowCur << row;
709 *m_textColCur << col;
710
711 m_posCur = posCur;
712 }
713 }
714
715 if ( m_textPosLast )
716 {
717 long posLast = m_text->GetLastPosition();
718 if ( posLast != m_posLast )
719 {
720 m_textPosLast->Clear();
721 *m_textPosLast << posLast;
722
723 m_posLast = posLast;
724 }
725 }
726
727 if ( m_textLineLast )
728 {
729 m_textLineLast->SetValue(
aec18ff7 730 wxString::Format(_T("%d"), m_text->GetNumberOfLines()) );
32b8ec41
VZ
731 }
732
733 if ( m_textSelFrom && m_textSelTo )
734 {
735 long selFrom, selTo;
736 m_text->GetSelection(&selFrom, &selTo);
737 if ( selFrom != m_selFrom )
738 {
739 m_textSelFrom->Clear();
740 *m_textSelFrom << selFrom;
741
742 m_selFrom = selFrom;
743 }
744
745 if ( selTo != m_selTo )
746 {
747 m_textSelTo->Clear();
748 *m_textSelTo << selTo;
749
750 m_selTo = selTo;
751 }
752 }
64b54de8
VZ
753
754 if ( m_textRange )
755 {
626117f7
VZ
756 wxString range = m_text->GetRange(10, 20);
757 if ( range != m_range10_20 )
758 {
759 m_range10_20 = range;
760 m_textRange->SetValue(range);
761 }
64b54de8 762 }
32b8ec41
VZ
763}
764
765void TextWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
766{
767 Reset();
768
769 CreateText();
770}
771
772void TextWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
773{
64b54de8
VZ
774 m_text->SetValue(m_text->GetWindowStyle() & wxTE_MULTILINE
775 ? _T("Here,\nthere and\neverywhere")
776 : _T("Yellow submarine"));
777
32b8ec41
VZ
778 m_text->SetFocus();
779}
780
781void TextWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
782{
64b54de8
VZ
783 if ( m_text->GetWindowStyle() & wxTE_MULTILINE )
784 {
785 m_text->AppendText(_T("We all live in a\n"));
786 }
787
788 m_text->AppendText(_T("Yellow submarine"));
32b8ec41
VZ
789}
790
791void TextWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event))
792{
64b54de8
VZ
793 m_text->WriteText(_T("Is there anybody going to listen to my story"));
794 if ( m_text->GetWindowStyle() & wxTE_MULTILINE )
795 {
796 m_text->WriteText(_T("\nall about the girl who came to stay"));
797 }
32b8ec41
VZ
798}
799
800void TextWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event))
801{
802 m_text->Clear();
803 m_text->SetFocus();
804}
805
806void TextWidgetsPage::OnButtonLoad(wxCommandEvent& WXUNUSED(event))
807{
808 // search for the file in several dirs where it's likely to be
809 wxPathList pathlist;
810 pathlist.Add(_T("."));
811 pathlist.Add(_T(".."));
812 pathlist.Add(_T("../../../samples/widgets"));
813
814 wxString filename = pathlist.FindValidPath(_T("textctrl.cpp"));
815 if ( !filename )
816 {
817 wxLogError(_T("File textctrl.cpp not found."));
818 }
819 else // load it
820 {
821 wxStopWatch sw;
822 if ( !m_text->LoadFile(filename) )
823 {
824 // this is not supposed to happen ...
825 wxLogError(_T("Error loading file."));
826 }
827 else
828 {
829 long elapsed = sw.Time();
aec18ff7 830 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
8b1d8f36
VZ
831 filename.c_str(), elapsed / 1000,
832 (unsigned int) elapsed % 1000);
32b8ec41
VZ
833 }
834 }
835}
836
837void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event)
838{
839 event.Enable(!m_text->GetValue().empty());
840}
841
842void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event)
843{
844 event.Enable( !IsSingleLine() );
845}
846
847void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event)
848{
849 // can't put multiline control in password mode
850 event.Enable( IsSingleLine() );
851}
852
853void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event)
854{
855 event.Enable( (m_radioTextLines->GetSelection() != DEFAULTS.textLines) ||
64b54de8
VZ
856#ifdef __WXMSW__
857 (m_radioKind->GetSelection() != DEFAULTS.textKind) ||
858#endif // __WXMSW__
32b8ec41
VZ
859 (m_chkReadonly->GetValue() != DEFAULTS.readonly) ||
860 (m_chkPassword->GetValue() != DEFAULTS.password) ||
fb4888a6 861 (m_radioWrap->GetSelection() != DEFAULTS.wrapStyle) );
32b8ec41
VZ
862}
863
c02e5a31 864void TextWidgetsPage::OnText(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
865{
866 // small hack to suppress the very first message: by then the logging is
867 // not yet redirected and so initial setting of the text value results in
868 // an annoying message box
206d3a16 869 static bool s_firstTime = true;
32b8ec41
VZ
870 if ( s_firstTime )
871 {
206d3a16 872 s_firstTime = false;
32b8ec41
VZ
873 return;
874 }
875
876 wxLogMessage(_T("Text ctrl value changed"));
877}
878
879void TextWidgetsPage::OnTextEnter(wxCommandEvent& event)
880{
881 wxLogMessage(_T("Text entered: '%s'"), event.GetString().c_str());
cd2e10d6 882 event.Skip();
32b8ec41
VZ
883}
884
c02e5a31 885void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event))
32b8ec41
VZ
886{
887 CreateText();
888}
72383a72 889
286ff306
WS
890void TextWidgetsPage::OnStreamRedirector(wxCommandEvent& WXUNUSED(event))
891{
15cdc341 892#if wxHAS_TEXT_WINDOW_STREAM
286ff306
WS
893 wxStreamToTextRedirector redirect(m_text);
894 wxString str( _T("Outputed to cout, appears in wxTextCtrl!") );
05772b84 895 wxSTD cout << str << wxSTD endl;
15cdc341
WS
896#else
897 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));
286ff306
WS
898#endif
899}