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