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
; }
139 virtual void RecreateWidget() { CreateText(); }
142 // create an info text contorl
143 wxTextCtrl
*CreateInfoText();
145 // create a horz sizer holding a static text and this text control
146 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
148 const wxString
& label2
= wxEmptyString
,
149 wxTextCtrl
*text2
= NULL
);
152 void OnButtonReset(wxCommandEvent
& event
);
153 void OnButtonClearLog(wxCommandEvent
& event
);
155 void OnButtonSet(wxCommandEvent
& event
);
156 void OnButtonAdd(wxCommandEvent
& event
);
157 void OnButtonInsert(wxCommandEvent
& event
);
158 void OnButtonClear(wxCommandEvent
& event
);
159 void OnButtonLoad(wxCommandEvent
& event
);
161 void OnStreamRedirector(wxCommandEvent
& event
);
162 void OnButtonQuit(wxCommandEvent
& event
);
164 void OnText(wxCommandEvent
& event
);
165 void OnTextEnter(wxCommandEvent
& event
);
167 void OnCheckOrRadioBox(wxCommandEvent
& event
);
169 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
171 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
172 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
174 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
176 void OnIdle(wxIdleEvent
& event
);
178 // reset the textctrl parameters
181 // (re)create the textctrl
184 // is the control currently single line?
185 bool IsSingleLine() const
187 return m_radioTextLines
->GetSelection() == TextLines_Single
;
193 // the radiobox to choose between single and multi line
194 wxRadioBox
*m_radioTextLines
;
196 // and another one to choose the wrapping style
197 wxRadioBox
*m_radioWrap
;
199 // the checkboxes controlling text ctrl styles
200 wxCheckBox
*m_chkPassword
,
203 // under MSW we test rich edit controls as well here
205 wxRadioBox
*m_radioKind
;
208 // the textctrl itself and the sizer it is in
210 wxSizer
*m_sizerText
;
212 // the information text zones
213 wxTextCtrl
*m_textPosCur
,
222 // and the data to show in them
228 wxString m_range10_20
;
231 // any class wishing to process wxWidgets events must use this macro
232 DECLARE_EVENT_TABLE()
233 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 class WidgetsTextCtrl
: public wxTextCtrl
243 WidgetsTextCtrl(wxWindow
*parent
,
245 const wxString
& value
,
247 : wxTextCtrl(parent
, id
, value
, wxDefaultPosition
, wxDefaultSize
, flags
)
252 void OnRightClick(wxMouseEvent
& event
)
256 switch ( HitTest(event
.GetPosition(), &x
, &y
) )
259 wxFAIL_MSG( _T("unexpected HitTest() result") );
262 case wxTE_HT_UNKNOWN
:
264 where
= _T("nowhere near");
268 where
= _T("before");
276 where
= _T("beyond");
279 case wxTE_HT_ON_TEXT
:
284 wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where
.c_str(), x
, y
);
290 DECLARE_EVENT_TABLE()
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
298 EVT_IDLE(TextWidgetsPage::OnIdle
)
300 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
302 EVT_BUTTON(TextPage_StreamRedirector
, TextWidgetsPage::OnStreamRedirector
)
304 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
305 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
306 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
307 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
308 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
310 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
312 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
313 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
315 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
317 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
318 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
320 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
321 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
324 BEGIN_EVENT_TABLE(WidgetsTextCtrl
, wxTextCtrl
)
325 EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick
)
328 // ============================================================================
330 // ============================================================================
332 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
334 // ----------------------------------------------------------------------------
335 // TextWidgetsPage creation
336 // ----------------------------------------------------------------------------
338 TextWidgetsPage::TextWidgetsPage(wxBookCtrlBase
*book
, wxImageList
*imaglist
)
341 imaglist
->Add(wxBitmap(text_xpm
));
348 m_radioTextLines
= (wxRadioBox
*)NULL
;
351 m_chkReadonly
= (wxCheckBox
*)NULL
;
361 m_textRange
= (wxTextCtrl
*)NULL
;
363 m_sizerText
= (wxSizer
*)NULL
;
368 m_selTo
= -2; // not -1 which means "no selection"
371 static const wxString modes
[] =
377 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
378 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
379 wxDefaultPosition
, wxDefaultSize
,
380 WXSIZEOF(modes
), modes
,
381 1, wxRA_SPECIFY_COLS
);
383 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
385 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
386 sizerLeft
->AddSpacer(5);
388 m_chkPassword
= CreateCheckBoxAndAddToSizer(
389 sizerLeft
, _T("&Password control"), TextPage_Password
391 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
392 sizerLeft
, _T("&Read-only mode")
394 sizerLeft
->AddSpacer(5);
396 static const wxString wrap
[] =
404 m_radioWrap
= new wxRadioBox(this, wxID_ANY
, _T("&Wrap style:"),
405 wxDefaultPosition
, wxDefaultSize
,
406 WXSIZEOF(wrap
), wrap
,
407 1, wxRA_SPECIFY_COLS
);
408 sizerLeft
->Add(m_radioWrap
, 0, wxGROW
| wxALL
, 5);
411 static const wxString kinds
[] =
418 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
419 wxDefaultPosition
, wxDefaultSize
,
420 WXSIZEOF(kinds
), kinds
,
421 1, wxRA_SPECIFY_COLS
);
423 sizerLeft
->AddSpacer(5);
424 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
427 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
428 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
429 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
432 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
433 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
435 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
436 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
438 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
439 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
441 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
442 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
444 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
445 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
447 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
448 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
450 btn
= new wxButton(this, TextPage_StreamRedirector
, _T("St&ream redirection"));
451 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
453 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
454 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
456 m_textPosCur
= CreateInfoText();
457 m_textRowCur
= CreateInfoText();
458 m_textColCur
= CreateInfoText();
460 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
461 sizerRow
->Add(CreateTextWithLabelSizer
467 sizerRow
->Add(CreateTextWithLabelSizer
472 0, wxLEFT
| wxRIGHT
, 5);
473 sizerRow
->Add(CreateTextWithLabelSizer
479 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
481 m_textLineLast
= CreateInfoText();
482 m_textPosLast
= CreateInfoText();
485 CreateTextWithLabelSizer
487 _T("Number of lines:"),
489 _T("Last position:"),
495 m_textSelFrom
= CreateInfoText();
496 m_textSelTo
= CreateInfoText();
499 CreateTextWithLabelSizer
501 _T("Selection: from"),
509 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
510 wxDefaultPosition
, wxDefaultSize
,
514 CreateTextWithLabelSizer
522 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
523 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
524 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
527 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
528 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
531 m_sizerText
->SetMinSize(150, 0);
533 // the 3 panes panes compose the upper part of the window
534 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
535 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
536 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
537 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
550 static int s_maxWidth
= 0;
554 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
557 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
559 wxSize(s_maxWidth
, wxDefaultCoord
),
564 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
566 const wxString
& label2
,
569 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
570 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
571 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
572 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
575 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
576 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
577 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
583 // ----------------------------------------------------------------------------
585 // ----------------------------------------------------------------------------
587 void TextWidgetsPage::Reset()
589 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
591 m_chkPassword
->SetValue(DEFAULTS
.password
);
592 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
594 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
597 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
601 void TextWidgetsPage::CreateText()
603 int flags
= ms_defaultFlags
;
604 switch ( m_radioTextLines
->GetSelection() )
607 wxFAIL_MSG( _T("unexpected lines radio box selection") );
609 case TextLines_Single
:
612 case TextLines_Multi
:
613 flags
|= wxTE_MULTILINE
;
614 m_chkPassword
->SetValue(false);
618 if ( m_chkPassword
->GetValue() )
619 flags
|= wxTE_PASSWORD
;
620 if ( m_chkReadonly
->GetValue() )
621 flags
|= wxTE_READONLY
;
623 switch ( m_radioWrap
->GetSelection() )
626 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
629 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
633 flags
|= wxTE_WORDWRAP
;
637 flags
|= wxTE_CHARWRAP
;
641 // this is default but use symbolic file name for consistency
642 flags
|= wxTE_BESTWRAP
;
647 switch ( m_radioKind
->GetSelection() )
650 wxFAIL_MSG( _T("unexpected kind radio box selection") );
668 valueOld
= m_text
->GetValue();
670 m_sizerText
->Detach( m_text
);
675 valueOld
= _T("Hello, Universe!");
678 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
680 // cast to int needed to silence gcc warning about different enums
681 m_sizerText
->Add(m_text
, 1, wxALL
|
682 (flags
& wxTE_MULTILINE
? (int)wxGROW
684 m_sizerText
->Layout();
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
693 // update all info texts
697 long posCur
= m_text
->GetInsertionPoint();
698 if ( posCur
!= m_posCur
)
700 m_textPosCur
->Clear();
701 m_textRowCur
->Clear();
702 m_textColCur
->Clear();
705 m_text
->PositionToXY(posCur
, &col
, &row
);
707 *m_textPosCur
<< posCur
;
708 *m_textRowCur
<< row
;
709 *m_textColCur
<< col
;
717 long posLast
= m_text
->GetLastPosition();
718 if ( posLast
!= m_posLast
)
720 m_textPosLast
->Clear();
721 *m_textPosLast
<< posLast
;
727 if ( m_textLineLast
)
729 m_textLineLast
->SetValue(
730 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
733 if ( m_textSelFrom
&& m_textSelTo
)
736 m_text
->GetSelection(&selFrom
, &selTo
);
737 if ( selFrom
!= m_selFrom
)
739 m_textSelFrom
->Clear();
740 *m_textSelFrom
<< selFrom
;
745 if ( selTo
!= m_selTo
)
747 m_textSelTo
->Clear();
748 *m_textSelTo
<< selTo
;
756 wxString range
= m_text
->GetRange(10, 20);
757 if ( range
!= m_range10_20
)
759 m_range10_20
= range
;
760 m_textRange
->SetValue(range
);
765 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
772 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
774 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
775 ? _T("Here,\nthere and\neverywhere")
776 : _T("Yellow submarine"));
781 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
783 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
785 m_text
->AppendText(_T("We all live in a\n"));
788 m_text
->AppendText(_T("Yellow submarine"));
791 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
793 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
794 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
796 m_text
->WriteText(_T("\nall about the girl who came to stay"));
800 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
806 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
808 // search for the file in several dirs where it's likely to be
810 pathlist
.Add(_T("."));
811 pathlist
.Add(_T(".."));
812 pathlist
.Add(_T("../../../samples/widgets"));
814 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
817 wxLogError(_T("File textctrl.cpp not found."));
822 if ( !m_text
->LoadFile(filename
) )
824 // this is not supposed to happen ...
825 wxLogError(_T("Error loading file."));
829 long elapsed
= sw
.Time();
830 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
831 filename
.c_str(), elapsed
/ 1000,
832 (unsigned int) elapsed
% 1000);
837 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
839 event
.Enable(!m_text
->GetValue().empty());
842 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
844 event
.Enable( !IsSingleLine() );
847 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
849 // can't put multiline control in password mode
850 event
.Enable( IsSingleLine() );
853 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
855 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
857 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
859 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
860 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
861 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
864 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
866 // small hack to suppress the very first message: by then the logging is
867 // not yet redirected and so initial setting of the text value results in
868 // an annoying message box
869 static bool s_firstTime
= true;
876 wxLogMessage(_T("Text ctrl value changed"));
879 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
881 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
885 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
890 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
892 #if wxHAS_TEXT_WINDOW_STREAM
893 wxStreamToTextRedirector
redirect(m_text
);
894 wxString
str( _T("Outputed to cout, appears in wxTextCtrl!") );
895 wxSTD cout
<< str
<< wxSTD endl
;
897 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));