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 // ----------------------------------------------------------------------------
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
117 TextLines_Multi
, // multiline
118 false, // not password
119 false, // not readonly
120 WrapStyle_Word
, // wrap on word boundaries
122 TextKind_Plain
// plain EDIT control
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 // Define a new frame type: this is going to be our main frame
131 class TextWidgetsPage
: public WidgetsPage
135 TextWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
);
136 virtual ~TextWidgetsPage(){};
138 virtual wxControl
*GetWidget() const { return m_text
; }
141 // create an info text contorl
142 wxTextCtrl
*CreateInfoText();
144 // create a horz sizer holding a static text and this text control
145 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
147 const wxString
& label2
= wxEmptyString
,
148 wxTextCtrl
*text2
= NULL
);
151 void OnButtonReset(wxCommandEvent
& event
);
152 void OnButtonClearLog(wxCommandEvent
& event
);
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
);
160 void OnStreamRedirector(wxCommandEvent
& event
);
161 void OnButtonQuit(wxCommandEvent
& event
);
163 void OnText(wxCommandEvent
& event
);
164 void OnTextEnter(wxCommandEvent
& event
);
166 void OnCheckOrRadioBox(wxCommandEvent
& event
);
168 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
170 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
171 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
173 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
175 void OnIdle(wxIdleEvent
& event
);
177 // reset the textctrl parameters
180 // (re)create the textctrl
183 // is the control currently single line?
184 bool IsSingleLine() const
186 return m_radioTextLines
->GetSelection() == TextLines_Single
;
192 // the radiobox to choose between single and multi line
193 wxRadioBox
*m_radioTextLines
;
195 // and another one to choose the wrapping style
196 wxRadioBox
*m_radioWrap
;
198 // the checkboxes controlling text ctrl styles
199 wxCheckBox
*m_chkPassword
,
202 // under MSW we test rich edit controls as well here
204 wxRadioBox
*m_radioKind
;
207 // the textctrl itself and the sizer it is in
209 wxSizer
*m_sizerText
;
211 // the information text zones
212 wxTextCtrl
*m_textPosCur
,
221 // and the data to show in them
227 wxString m_range10_20
;
230 // any class wishing to process wxWidgets events must use this macro
231 DECLARE_EVENT_TABLE()
232 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 class WidgetsTextCtrl
: public wxTextCtrl
242 WidgetsTextCtrl(wxWindow
*parent
,
244 const wxString
& value
,
246 : wxTextCtrl(parent
, id
, value
, wxDefaultPosition
, wxDefaultSize
, flags
)
251 void OnRightClick(wxMouseEvent
& event
)
255 switch ( HitTest(event
.GetPosition(), &x
, &y
) )
258 wxFAIL_MSG( _T("unexpected HitTest() result") );
261 case wxTE_HT_UNKNOWN
:
263 where
= _T("nowhere near");
267 where
= _T("before");
275 where
= _T("beyond");
278 case wxTE_HT_ON_TEXT
:
283 wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where
.c_str(), x
, y
);
289 DECLARE_EVENT_TABLE()
292 // ----------------------------------------------------------------------------
294 // ----------------------------------------------------------------------------
296 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
297 EVT_IDLE(TextWidgetsPage::OnIdle
)
299 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
301 EVT_BUTTON(TextPage_StreamRedirector
, TextWidgetsPage::OnStreamRedirector
)
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
)
309 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
311 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
312 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
314 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
316 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
317 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
319 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
320 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
323 BEGIN_EVENT_TABLE(WidgetsTextCtrl
, wxTextCtrl
)
324 EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick
)
327 // ============================================================================
329 // ============================================================================
331 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
333 // ----------------------------------------------------------------------------
334 // TextWidgetsPage creation
335 // ----------------------------------------------------------------------------
337 TextWidgetsPage::TextWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
)
340 imaglist
->Add(wxBitmap(text_xpm
));
347 m_radioTextLines
= (wxRadioBox
*)NULL
;
350 m_chkReadonly
= (wxCheckBox
*)NULL
;
360 m_textRange
= (wxTextCtrl
*)NULL
;
362 m_sizerText
= (wxSizer
*)NULL
;
367 m_selTo
= -2; // not -1 which means "no selection"
370 static const wxString modes
[] =
376 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
377 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
378 wxDefaultPosition
, wxDefaultSize
,
379 WXSIZEOF(modes
), modes
,
380 1, wxRA_SPECIFY_COLS
);
382 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
384 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
385 sizerLeft
->AddSpacer(5);
387 m_chkPassword
= CreateCheckBoxAndAddToSizer(
388 sizerLeft
, _T("&Password control"), TextPage_Password
390 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
391 sizerLeft
, _T("&Read-only mode")
393 sizerLeft
->AddSpacer(5);
395 static const wxString wrap
[] =
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);
410 static const wxString kinds
[] =
417 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
418 wxDefaultPosition
, wxDefaultSize
,
419 WXSIZEOF(kinds
), kinds
,
420 1, wxRA_SPECIFY_COLS
);
422 sizerLeft
->AddSpacer(5);
423 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
426 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
427 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
428 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
431 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
432 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
434 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
435 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
437 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
438 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
440 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
441 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
443 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
444 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
446 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
447 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
449 btn
= new wxButton(this, TextPage_StreamRedirector
, _T("St&ream redirection"));
450 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
452 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
453 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
455 m_textPosCur
= CreateInfoText();
456 m_textRowCur
= CreateInfoText();
457 m_textColCur
= CreateInfoText();
459 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
460 sizerRow
->Add(CreateTextWithLabelSizer
466 sizerRow
->Add(CreateTextWithLabelSizer
471 0, wxLEFT
| wxRIGHT
, 5);
472 sizerRow
->Add(CreateTextWithLabelSizer
478 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
480 m_textLineLast
= CreateInfoText();
481 m_textPosLast
= CreateInfoText();
484 CreateTextWithLabelSizer
486 _T("Number of lines:"),
488 _T("Last position:"),
494 m_textSelFrom
= CreateInfoText();
495 m_textSelTo
= CreateInfoText();
498 CreateTextWithLabelSizer
500 _T("Selection: from"),
508 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
509 wxDefaultPosition
, wxDefaultSize
,
513 CreateTextWithLabelSizer
521 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
522 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
523 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
526 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
527 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
530 m_sizerText
->SetMinSize(150, 0);
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);
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
549 static int s_maxWidth
= 0;
553 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
556 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
558 wxSize(s_maxWidth
, wxDefaultCoord
),
563 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
565 const wxString
& label2
,
568 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
569 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
570 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
571 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
574 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
575 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
576 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 void TextWidgetsPage::Reset()
588 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
590 m_chkPassword
->SetValue(DEFAULTS
.password
);
591 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
593 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
596 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
600 void TextWidgetsPage::CreateText()
603 switch ( m_radioTextLines
->GetSelection() )
606 wxFAIL_MSG( _T("unexpected lines radio box selection") );
608 case TextLines_Single
:
611 case TextLines_Multi
:
612 flags
|= wxTE_MULTILINE
;
613 m_chkPassword
->SetValue(false);
617 if ( m_chkPassword
->GetValue() )
618 flags
|= wxTE_PASSWORD
;
619 if ( m_chkReadonly
->GetValue() )
620 flags
|= wxTE_READONLY
;
622 switch ( m_radioWrap
->GetSelection() )
625 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
628 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
632 flags
|= wxTE_WORDWRAP
;
636 flags
|= wxTE_LINEWRAP
;
640 // this is default but use symbolic file name for consistency
641 flags
|= wxTE_BESTWRAP
;
646 switch ( m_radioKind
->GetSelection() )
649 wxFAIL_MSG( _T("unexpected kind radio box selection") );
667 valueOld
= m_text
->GetValue();
669 m_sizerText
->Detach( m_text
);
674 valueOld
= _T("Hello, Universe!");
677 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
679 // cast to int needed to silence gcc warning about different enums
680 m_sizerText
->Add(m_text
, 1, wxALL
|
681 (flags
& wxTE_MULTILINE
? (int)wxGROW
683 m_sizerText
->Layout();
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
692 // update all info texts
696 long posCur
= m_text
->GetInsertionPoint();
697 if ( posCur
!= m_posCur
)
699 m_textPosCur
->Clear();
700 m_textRowCur
->Clear();
701 m_textColCur
->Clear();
704 m_text
->PositionToXY(posCur
, &col
, &row
);
706 *m_textPosCur
<< posCur
;
707 *m_textRowCur
<< row
;
708 *m_textColCur
<< col
;
716 long posLast
= m_text
->GetLastPosition();
717 if ( posLast
!= m_posLast
)
719 m_textPosLast
->Clear();
720 *m_textPosLast
<< posLast
;
726 if ( m_textLineLast
)
728 m_textLineLast
->SetValue(
729 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
732 if ( m_textSelFrom
&& m_textSelTo
)
735 m_text
->GetSelection(&selFrom
, &selTo
);
736 if ( selFrom
!= m_selFrom
)
738 m_textSelFrom
->Clear();
739 *m_textSelFrom
<< selFrom
;
744 if ( selTo
!= m_selTo
)
746 m_textSelTo
->Clear();
747 *m_textSelTo
<< selTo
;
755 wxString range
= m_text
->GetRange(10, 20);
756 if ( range
!= m_range10_20
)
758 m_range10_20
= range
;
759 m_textRange
->SetValue(range
);
764 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
771 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
773 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
774 ? _T("Here,\nthere and\neverywhere")
775 : _T("Yellow submarine"));
780 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
782 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
784 m_text
->AppendText(_T("We all live in a\n"));
787 m_text
->AppendText(_T("Yellow submarine"));
790 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
792 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
793 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
795 m_text
->WriteText(_T("\nall about the girl who came to stay"));
799 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
805 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
807 // search for the file in several dirs where it's likely to be
809 pathlist
.Add(_T("."));
810 pathlist
.Add(_T(".."));
811 pathlist
.Add(_T("../../../samples/widgets"));
813 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
816 wxLogError(_T("File textctrl.cpp not found."));
821 if ( !m_text
->LoadFile(filename
) )
823 // this is not supposed to happen ...
824 wxLogError(_T("Error loading file."));
828 long elapsed
= sw
.Time();
829 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
830 filename
.c_str(), elapsed
/ 1000,
831 (unsigned int) elapsed
% 1000);
836 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
838 event
.Enable(!m_text
->GetValue().empty());
841 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
843 event
.Enable( !IsSingleLine() );
846 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
848 // can't put multiline control in password mode
849 event
.Enable( IsSingleLine() );
852 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
854 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
856 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
858 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
859 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
860 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
863 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
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
868 static bool s_firstTime
= true;
875 wxLogMessage(_T("Text ctrl value changed"));
878 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
880 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
884 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
889 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
891 #if wxHAS_TEXT_WINDOW_STREAM
892 wxStreamToTextRedirector
redirect(m_text
);
893 wxString
str( _T("Outputed to cout, appears in wxTextCtrl!") );
894 wxSTD cout
<< str
<< wxSTD endl
;
896 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));