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