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