1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: part of the widgets sample showing wxTextCtrl
5 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
32 #include "wx/bitmap.h"
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"
39 #include "wx/msgdlg.h"
43 #include "wx/ioswrap.h"
47 #include "icons/text.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 TextPage_Reset
= wxID_HIGHEST
,
64 TextPage_StreamRedirector
,
71 // textctrl line number radiobox values
79 // wrap style radio box
91 // textctrl kind values
102 // default values for the controls
103 static const struct ControlValues
118 TextLines_Multi
, // multiline
119 false, // not password
120 false, // not readonly
121 false, // not filename
122 WrapStyle_Word
, // wrap on word boundaries
124 TextKind_Plain
// plain EDIT control
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 // Define a new frame type: this is going to be our main frame
133 class TextWidgetsPage
: public WidgetsPage
137 TextWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
138 virtual ~TextWidgetsPage(){};
140 virtual wxControl
*GetWidget() const { return m_text
; }
141 virtual void RecreateWidget() { CreateText(); }
143 // lazy creation of the content
144 virtual void CreateContent();
147 // create an info text contorl
148 wxTextCtrl
*CreateInfoText();
150 // create a horz sizer holding a static text and this text control
151 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
153 const wxString
& label2
= wxEmptyString
,
154 wxTextCtrl
*text2
= NULL
);
157 void OnButtonReset(wxCommandEvent
& event
);
158 void OnButtonClearLog(wxCommandEvent
& event
);
160 void OnButtonSet(wxCommandEvent
& event
);
161 void OnButtonAdd(wxCommandEvent
& event
);
162 void OnButtonInsert(wxCommandEvent
& event
);
163 void OnButtonClear(wxCommandEvent
& event
);
164 void OnButtonLoad(wxCommandEvent
& event
);
166 void OnStreamRedirector(wxCommandEvent
& event
);
167 void OnButtonQuit(wxCommandEvent
& event
);
169 void OnText(wxCommandEvent
& event
);
170 void OnTextEnter(wxCommandEvent
& event
);
172 void OnCheckOrRadioBox(wxCommandEvent
& event
);
174 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
176 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
177 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
179 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
181 void OnIdle(wxIdleEvent
& event
);
183 // reset the textctrl parameters
186 // (re)create the textctrl
189 // is the control currently single line?
190 bool IsSingleLine() const
192 return m_radioTextLines
->GetSelection() == TextLines_Single
;
198 // the radiobox to choose between single and multi line
199 wxRadioBox
*m_radioTextLines
;
201 // and another one to choose the wrapping style
202 wxRadioBox
*m_radioWrap
;
204 // the checkboxes controlling text ctrl styles
205 wxCheckBox
*m_chkPassword
,
209 // under MSW we test rich edit controls as well here
211 wxRadioBox
*m_radioKind
;
214 // the textctrl itself and the sizer it is in
216 wxSizer
*m_sizerText
;
218 // the information text zones
219 wxTextCtrl
*m_textPosCur
,
228 // and the data to show in them
234 wxString m_range10_20
;
237 // any class wishing to process wxWidgets events must use this macro
238 DECLARE_EVENT_TABLE()
239 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 class WidgetsTextCtrl
: public wxTextCtrl
249 WidgetsTextCtrl(wxWindow
*parent
,
251 const wxString
& value
,
253 : wxTextCtrl(parent
, id
, value
, wxDefaultPosition
, wxDefaultSize
, flags
)
258 void OnRightClick(wxMouseEvent
& event
)
262 switch ( HitTest(event
.GetPosition(), &x
, &y
) )
265 wxFAIL_MSG( _T("unexpected HitTest() result") );
268 case wxTE_HT_UNKNOWN
:
270 where
= _T("nowhere near");
274 where
= _T("before");
282 where
= _T("beyond");
285 case wxTE_HT_ON_TEXT
:
290 wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where
.c_str(), x
, y
);
296 DECLARE_EVENT_TABLE()
299 // ----------------------------------------------------------------------------
301 // ----------------------------------------------------------------------------
303 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
304 EVT_IDLE(TextWidgetsPage::OnIdle
)
306 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
308 EVT_BUTTON(TextPage_StreamRedirector
, TextWidgetsPage::OnStreamRedirector
)
310 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
311 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
312 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
313 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
314 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
316 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
318 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
319 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
321 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
323 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
324 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
326 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
327 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
330 BEGIN_EVENT_TABLE(WidgetsTextCtrl
, wxTextCtrl
)
331 EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick
)
334 // ============================================================================
336 // ============================================================================
338 #if defined(__WXX11__)
339 #define FAMILY_CTRLS NATIVE_CTRLS
340 #elif defined(__WXUNIVERSAL__)
341 #define FAMILY_CTRLS UNIVERSAL_CTRLS
343 #define FAMILY_CTRLS NATIVE_CTRLS
346 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"),
347 FAMILY_CTRLS
| EDITABLE_CTRLS
350 // ----------------------------------------------------------------------------
351 // TextWidgetsPage creation
352 // ----------------------------------------------------------------------------
354 TextWidgetsPage::TextWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
355 : WidgetsPage(book
, imaglist
, text_xpm
)
362 m_radioTextLines
= (wxRadioBox
*)NULL
;
366 m_chkFilename
= (wxCheckBox
*)NULL
;
376 m_textRange
= (wxTextCtrl
*)NULL
;
378 m_sizerText
= (wxSizer
*)NULL
;
383 m_selTo
= -2; // not -1 which means "no selection"
386 void TextWidgetsPage::CreateContent()
389 static const wxString modes
[] =
395 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
396 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
397 wxDefaultPosition
, wxDefaultSize
,
398 WXSIZEOF(modes
), modes
,
399 1, wxRA_SPECIFY_COLS
);
401 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
403 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
404 sizerLeft
->AddSpacer(5);
406 m_chkPassword
= CreateCheckBoxAndAddToSizer(
407 sizerLeft
, _T("&Password control"), TextPage_Password
409 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
410 sizerLeft
, _T("&Read-only mode")
412 m_chkFilename
= CreateCheckBoxAndAddToSizer(
413 sizerLeft
, _T("&Filename control")
415 m_chkFilename
->Disable(); // not implemented yet
416 sizerLeft
->AddSpacer(5);
418 static const wxString wrap
[] =
426 m_radioWrap
= new wxRadioBox(this, wxID_ANY
, _T("&Wrap style:"),
427 wxDefaultPosition
, wxDefaultSize
,
428 WXSIZEOF(wrap
), wrap
,
429 1, wxRA_SPECIFY_COLS
);
430 sizerLeft
->Add(m_radioWrap
, 0, wxGROW
| wxALL
, 5);
433 static const wxString kinds
[] =
440 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
441 wxDefaultPosition
, wxDefaultSize
,
442 WXSIZEOF(kinds
), kinds
,
443 1, wxRA_SPECIFY_COLS
);
445 sizerLeft
->AddSpacer(5);
446 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
449 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
450 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
451 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
454 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
455 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
457 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
458 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
460 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
461 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
463 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
464 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
466 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
467 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
469 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
470 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
472 btn
= new wxButton(this, TextPage_StreamRedirector
, _T("St&ream redirection"));
473 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
475 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
476 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
478 m_textPosCur
= CreateInfoText();
479 m_textRowCur
= CreateInfoText();
480 m_textColCur
= CreateInfoText();
482 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
483 sizerRow
->Add(CreateTextWithLabelSizer
489 sizerRow
->Add(CreateTextWithLabelSizer
494 0, wxLEFT
| wxRIGHT
, 5);
495 sizerRow
->Add(CreateTextWithLabelSizer
501 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
503 m_textLineLast
= CreateInfoText();
504 m_textPosLast
= CreateInfoText();
507 CreateTextWithLabelSizer
509 _T("Number of lines:"),
511 _T("Last position:"),
517 m_textSelFrom
= CreateInfoText();
518 m_textSelTo
= CreateInfoText();
521 CreateTextWithLabelSizer
523 _T("Selection: from"),
531 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
532 wxDefaultPosition
, wxDefaultSize
,
536 CreateTextWithLabelSizer
544 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
545 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
546 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
549 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
550 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
553 m_sizerText
->SetMinSize(150, 0);
555 // the 3 panes panes compose the upper part of the window
556 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
557 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
558 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
559 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
572 static int s_maxWidth
= 0;
576 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
579 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
581 wxSize(s_maxWidth
, wxDefaultCoord
),
586 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
588 const wxString
& label2
,
591 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
592 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
593 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
594 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
597 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
598 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
599 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
605 // ----------------------------------------------------------------------------
607 // ----------------------------------------------------------------------------
609 void TextWidgetsPage::Reset()
611 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
613 m_chkPassword
->SetValue(DEFAULTS
.password
);
614 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
615 m_chkFilename
->SetValue(DEFAULTS
.filename
);
617 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
620 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
624 void TextWidgetsPage::CreateText()
626 int flags
= ms_defaultFlags
;
627 switch ( m_radioTextLines
->GetSelection() )
630 wxFAIL_MSG( _T("unexpected lines radio box selection") );
632 case TextLines_Single
:
635 case TextLines_Multi
:
636 flags
|= wxTE_MULTILINE
;
637 m_chkPassword
->SetValue(false);
641 if ( m_chkPassword
->GetValue() )
642 flags
|= wxTE_PASSWORD
;
643 if ( m_chkReadonly
->GetValue() )
644 flags
|= wxTE_READONLY
;
646 switch ( m_radioWrap
->GetSelection() )
649 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
652 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
656 flags
|= wxTE_WORDWRAP
;
660 flags
|= wxTE_CHARWRAP
;
664 // this is default but use symbolic file name for consistency
665 flags
|= wxTE_BESTWRAP
;
670 switch ( m_radioKind
->GetSelection() )
673 wxFAIL_MSG( _T("unexpected kind radio box selection") );
691 valueOld
= m_text
->GetValue();
693 m_sizerText
->Detach( m_text
);
698 valueOld
= _T("Hello, Universe!");
701 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
704 if ( m_chkFilename
->GetValue() )
708 // cast to int needed to silence gcc warning about different enums
709 m_sizerText
->Add(m_text
, 1, wxALL
|
710 (flags
& wxTE_MULTILINE
? (int)wxGROW
712 m_sizerText
->Layout();
715 // ----------------------------------------------------------------------------
717 // ----------------------------------------------------------------------------
719 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
721 // update all info texts
725 long posCur
= m_text
->GetInsertionPoint();
726 if ( posCur
!= m_posCur
)
728 m_textPosCur
->Clear();
729 m_textRowCur
->Clear();
730 m_textColCur
->Clear();
733 m_text
->PositionToXY(posCur
, &col
, &row
);
735 *m_textPosCur
<< posCur
;
736 *m_textRowCur
<< row
;
737 *m_textColCur
<< col
;
745 long posLast
= m_text
->GetLastPosition();
746 if ( posLast
!= m_posLast
)
748 m_textPosLast
->Clear();
749 *m_textPosLast
<< posLast
;
755 if ( m_textLineLast
)
757 m_textLineLast
->SetValue(
758 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
761 if ( m_textSelFrom
&& m_textSelTo
)
764 m_text
->GetSelection(&selFrom
, &selTo
);
765 if ( selFrom
!= m_selFrom
)
767 m_textSelFrom
->Clear();
768 *m_textSelFrom
<< selFrom
;
773 if ( selTo
!= m_selTo
)
775 m_textSelTo
->Clear();
776 *m_textSelTo
<< selTo
;
784 wxString range
= m_text
->GetRange(10, 20);
785 if ( range
!= m_range10_20
)
787 m_range10_20
= range
;
788 m_textRange
->SetValue(range
);
793 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
800 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
802 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
803 ? _T("Here,\nthere and\neverywhere")
804 : _T("Yellow submarine"));
809 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
811 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
813 m_text
->AppendText(_T("We all live in a\n"));
816 m_text
->AppendText(_T("Yellow submarine"));
819 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
821 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
822 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
824 m_text
->WriteText(_T("\nall about the girl who came to stay"));
828 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
834 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
836 // search for the file in several dirs where it's likely to be
838 pathlist
.Add(_T("."));
839 pathlist
.Add(_T(".."));
840 pathlist
.Add(_T("../../../samples/widgets"));
842 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
845 wxLogError(_T("File textctrl.cpp not found."));
850 if ( !m_text
->LoadFile(filename
) )
852 // this is not supposed to happen ...
853 wxLogError(_T("Error loading file."));
857 long elapsed
= sw
.Time();
858 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
859 filename
.c_str(), elapsed
/ 1000,
860 (unsigned int) elapsed
% 1000);
865 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
867 event
.Enable(!m_text
->GetValue().empty());
870 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
872 event
.Enable( !IsSingleLine() );
875 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
877 // can't put multiline control in password mode
878 event
.Enable( IsSingleLine() );
881 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
883 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
885 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
887 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
888 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
889 (m_chkFilename
->GetValue() != DEFAULTS
.filename
) ||
890 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
893 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
895 // small hack to suppress the very first message: by then the logging is
896 // not yet redirected and so initial setting of the text value results in
897 // an annoying message box
898 static bool s_firstTime
= true;
905 wxLogMessage(_T("Text ctrl value changed"));
908 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
910 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
914 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
919 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
921 #if wxHAS_TEXT_WINDOW_STREAM
922 wxStreamToTextRedirector
redirect(m_text
);
923 wxString
str( _T("Outputed to cout, appears in wxTextCtrl!") );
924 wxSTD cout
<< str
<< wxSTD endl
;
926 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));