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
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(WidgetsBookCtrl
*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 #if defined(__WXX11__)
333 #define FAMILY_CTRLS NATIVE_CTRLS
334 #elif defined(__WXUNIVERSAL__)
335 #define FAMILY_CTRLS UNIVERSAL_CTRLS
337 #define FAMILY_CTRLS NATIVE_CTRLS
340 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"),
341 FAMILY_CTRLS
| EDITABLE_CTRLS
344 // ----------------------------------------------------------------------------
345 // TextWidgetsPage creation
346 // ----------------------------------------------------------------------------
348 TextWidgetsPage::TextWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
349 : WidgetsPage(book
, imaglist
, text_xpm
)
356 m_radioTextLines
= (wxRadioBox
*)NULL
;
359 m_chkReadonly
= (wxCheckBox
*)NULL
;
369 m_textRange
= (wxTextCtrl
*)NULL
;
371 m_sizerText
= (wxSizer
*)NULL
;
376 m_selTo
= -2; // not -1 which means "no selection"
379 static const wxString modes
[] =
385 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
386 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
387 wxDefaultPosition
, wxDefaultSize
,
388 WXSIZEOF(modes
), modes
,
389 1, wxRA_SPECIFY_COLS
);
391 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
393 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
394 sizerLeft
->AddSpacer(5);
396 m_chkPassword
= CreateCheckBoxAndAddToSizer(
397 sizerLeft
, _T("&Password control"), TextPage_Password
399 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
400 sizerLeft
, _T("&Read-only mode")
402 sizerLeft
->AddSpacer(5);
404 static const wxString wrap
[] =
412 m_radioWrap
= new wxRadioBox(this, wxID_ANY
, _T("&Wrap style:"),
413 wxDefaultPosition
, wxDefaultSize
,
414 WXSIZEOF(wrap
), wrap
,
415 1, wxRA_SPECIFY_COLS
);
416 sizerLeft
->Add(m_radioWrap
, 0, wxGROW
| wxALL
, 5);
419 static const wxString kinds
[] =
426 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
427 wxDefaultPosition
, wxDefaultSize
,
428 WXSIZEOF(kinds
), kinds
,
429 1, wxRA_SPECIFY_COLS
);
431 sizerLeft
->AddSpacer(5);
432 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
435 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
436 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
437 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
440 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
441 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
443 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
444 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
446 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
447 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
449 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
450 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
452 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
453 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
455 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
456 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
458 btn
= new wxButton(this, TextPage_StreamRedirector
, _T("St&ream redirection"));
459 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
461 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
462 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
464 m_textPosCur
= CreateInfoText();
465 m_textRowCur
= CreateInfoText();
466 m_textColCur
= CreateInfoText();
468 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
469 sizerRow
->Add(CreateTextWithLabelSizer
475 sizerRow
->Add(CreateTextWithLabelSizer
480 0, wxLEFT
| wxRIGHT
, 5);
481 sizerRow
->Add(CreateTextWithLabelSizer
487 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
489 m_textLineLast
= CreateInfoText();
490 m_textPosLast
= CreateInfoText();
493 CreateTextWithLabelSizer
495 _T("Number of lines:"),
497 _T("Last position:"),
503 m_textSelFrom
= CreateInfoText();
504 m_textSelTo
= CreateInfoText();
507 CreateTextWithLabelSizer
509 _T("Selection: from"),
517 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
518 wxDefaultPosition
, wxDefaultSize
,
522 CreateTextWithLabelSizer
530 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
531 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
532 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
535 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
536 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
539 m_sizerText
->SetMinSize(150, 0);
541 // the 3 panes panes compose the upper part of the window
542 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
543 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
544 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
545 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
552 // ----------------------------------------------------------------------------
554 // ----------------------------------------------------------------------------
556 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
558 static int s_maxWidth
= 0;
562 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
565 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
567 wxSize(s_maxWidth
, wxDefaultCoord
),
572 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
574 const wxString
& label2
,
577 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
578 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
579 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
580 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
583 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
584 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
585 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
591 // ----------------------------------------------------------------------------
593 // ----------------------------------------------------------------------------
595 void TextWidgetsPage::Reset()
597 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
599 m_chkPassword
->SetValue(DEFAULTS
.password
);
600 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
602 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
605 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
609 void TextWidgetsPage::CreateText()
611 int flags
= ms_defaultFlags
;
612 switch ( m_radioTextLines
->GetSelection() )
615 wxFAIL_MSG( _T("unexpected lines radio box selection") );
617 case TextLines_Single
:
620 case TextLines_Multi
:
621 flags
|= wxTE_MULTILINE
;
622 m_chkPassword
->SetValue(false);
626 if ( m_chkPassword
->GetValue() )
627 flags
|= wxTE_PASSWORD
;
628 if ( m_chkReadonly
->GetValue() )
629 flags
|= wxTE_READONLY
;
631 switch ( m_radioWrap
->GetSelection() )
634 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
637 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
641 flags
|= wxTE_WORDWRAP
;
645 flags
|= wxTE_CHARWRAP
;
649 // this is default but use symbolic file name for consistency
650 flags
|= wxTE_BESTWRAP
;
655 switch ( m_radioKind
->GetSelection() )
658 wxFAIL_MSG( _T("unexpected kind radio box selection") );
676 valueOld
= m_text
->GetValue();
678 m_sizerText
->Detach( m_text
);
683 valueOld
= _T("Hello, Universe!");
686 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
688 // cast to int needed to silence gcc warning about different enums
689 m_sizerText
->Add(m_text
, 1, wxALL
|
690 (flags
& wxTE_MULTILINE
? (int)wxGROW
692 m_sizerText
->Layout();
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
699 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
701 // update all info texts
705 long posCur
= m_text
->GetInsertionPoint();
706 if ( posCur
!= m_posCur
)
708 m_textPosCur
->Clear();
709 m_textRowCur
->Clear();
710 m_textColCur
->Clear();
713 m_text
->PositionToXY(posCur
, &col
, &row
);
715 *m_textPosCur
<< posCur
;
716 *m_textRowCur
<< row
;
717 *m_textColCur
<< col
;
725 long posLast
= m_text
->GetLastPosition();
726 if ( posLast
!= m_posLast
)
728 m_textPosLast
->Clear();
729 *m_textPosLast
<< posLast
;
735 if ( m_textLineLast
)
737 m_textLineLast
->SetValue(
738 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
741 if ( m_textSelFrom
&& m_textSelTo
)
744 m_text
->GetSelection(&selFrom
, &selTo
);
745 if ( selFrom
!= m_selFrom
)
747 m_textSelFrom
->Clear();
748 *m_textSelFrom
<< selFrom
;
753 if ( selTo
!= m_selTo
)
755 m_textSelTo
->Clear();
756 *m_textSelTo
<< selTo
;
764 wxString range
= m_text
->GetRange(10, 20);
765 if ( range
!= m_range10_20
)
767 m_range10_20
= range
;
768 m_textRange
->SetValue(range
);
773 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
780 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
782 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
783 ? _T("Here,\nthere and\neverywhere")
784 : _T("Yellow submarine"));
789 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
791 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
793 m_text
->AppendText(_T("We all live in a\n"));
796 m_text
->AppendText(_T("Yellow submarine"));
799 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
801 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
802 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
804 m_text
->WriteText(_T("\nall about the girl who came to stay"));
808 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
814 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
816 // search for the file in several dirs where it's likely to be
818 pathlist
.Add(_T("."));
819 pathlist
.Add(_T(".."));
820 pathlist
.Add(_T("../../../samples/widgets"));
822 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
825 wxLogError(_T("File textctrl.cpp not found."));
830 if ( !m_text
->LoadFile(filename
) )
832 // this is not supposed to happen ...
833 wxLogError(_T("Error loading file."));
837 long elapsed
= sw
.Time();
838 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
839 filename
.c_str(), elapsed
/ 1000,
840 (unsigned int) elapsed
% 1000);
845 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
847 event
.Enable(!m_text
->GetValue().empty());
850 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
852 event
.Enable( !IsSingleLine() );
855 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
857 // can't put multiline control in password mode
858 event
.Enable( IsSingleLine() );
861 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
863 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
865 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
867 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
868 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
869 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
872 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
874 // small hack to suppress the very first message: by then the logging is
875 // not yet redirected and so initial setting of the text value results in
876 // an annoying message box
877 static bool s_firstTime
= true;
884 wxLogMessage(_T("Text ctrl value changed"));
887 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
889 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
893 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
898 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
900 #if wxHAS_TEXT_WINDOW_STREAM
901 wxStreamToTextRedirector
redirect(m_text
);
902 wxString
str( _T("Outputed to cout, appears in wxTextCtrl!") );
903 wxSTD cout
<< str
<< wxSTD endl
;
905 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));