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"
46 #include "icons/text.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
63 TextPage_StreamRedirector
,
70 // textctrl line number radiobox values
78 // wrap style radio box
90 // textctrl kind values
101 // default values for the controls
102 static const struct ControlValues
116 TextLines_Multi
, // multiline
117 false, // not password
118 false, // not readonly
119 WrapStyle_Word
, // wrap on word boundaries
121 TextKind_Plain
// plain EDIT control
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // Define a new frame type: this is going to be our main frame
130 class TextWidgetsPage
: public WidgetsPage
134 TextWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
);
135 virtual ~TextWidgetsPage(){};
137 virtual wxControl
*GetWidget() const { return m_text
; }
140 // create an info text contorl
141 wxTextCtrl
*CreateInfoText();
143 // create a horz sizer holding a static text and this text control
144 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
146 const wxString
& label2
= wxEmptyString
,
147 wxTextCtrl
*text2
= NULL
);
150 void OnButtonReset(wxCommandEvent
& event
);
151 void OnButtonClearLog(wxCommandEvent
& event
);
153 void OnButtonSet(wxCommandEvent
& event
);
154 void OnButtonAdd(wxCommandEvent
& event
);
155 void OnButtonInsert(wxCommandEvent
& event
);
156 void OnButtonClear(wxCommandEvent
& event
);
157 void OnButtonLoad(wxCommandEvent
& event
);
159 void OnStreamRedirector(wxCommandEvent
& event
);
160 void OnButtonQuit(wxCommandEvent
& event
);
162 void OnText(wxCommandEvent
& event
);
163 void OnTextEnter(wxCommandEvent
& event
);
165 void OnCheckOrRadioBox(wxCommandEvent
& event
);
167 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
169 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
170 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
172 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
174 void OnIdle(wxIdleEvent
& event
);
176 // reset the textctrl parameters
179 // (re)create the textctrl
182 // is the control currently single line?
183 bool IsSingleLine() const
185 return m_radioTextLines
->GetSelection() == TextLines_Single
;
191 // the radiobox to choose between single and multi line
192 wxRadioBox
*m_radioTextLines
;
194 // and another one to choose the wrapping style
195 wxRadioBox
*m_radioWrap
;
197 // the checkboxes controlling text ctrl styles
198 wxCheckBox
*m_chkPassword
,
201 // under MSW we test rich edit controls as well here
203 wxRadioBox
*m_radioKind
;
206 // the textctrl itself and the sizer it is in
208 wxSizer
*m_sizerText
;
210 // the information text zones
211 wxTextCtrl
*m_textPosCur
,
220 // and the data to show in them
226 wxString m_range10_20
;
229 // any class wishing to process wxWidgets events must use this macro
230 DECLARE_EVENT_TABLE()
231 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 class WidgetsTextCtrl
: public wxTextCtrl
241 WidgetsTextCtrl(wxWindow
*parent
,
243 const wxString
& value
,
245 : wxTextCtrl(parent
, id
, value
, wxDefaultPosition
, wxDefaultSize
, flags
)
250 void OnRightClick(wxMouseEvent
& event
)
254 switch ( HitTest(event
.GetPosition(), &x
, &y
) )
257 wxFAIL_MSG( _T("unexpected HitTest() result") );
260 case wxTE_HT_UNKNOWN
:
262 where
= _T("nowhere near");
266 where
= _T("before");
274 where
= _T("beyond");
277 case wxTE_HT_ON_TEXT
:
282 wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where
.c_str(), x
, y
);
288 DECLARE_EVENT_TABLE()
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
296 EVT_IDLE(TextWidgetsPage::OnIdle
)
298 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
300 EVT_BUTTON(TextPage_StreamRedirector
, TextWidgetsPage::OnStreamRedirector
)
302 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
303 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
304 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
305 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
306 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
308 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
310 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
311 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
313 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
315 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
316 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
318 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
319 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
322 BEGIN_EVENT_TABLE(WidgetsTextCtrl
, wxTextCtrl
)
323 EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick
)
326 // ============================================================================
328 // ============================================================================
330 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
332 // ----------------------------------------------------------------------------
333 // TextWidgetsPage creation
334 // ----------------------------------------------------------------------------
336 TextWidgetsPage::TextWidgetsPage(wxBookCtrl
*book
, wxImageList
*imaglist
)
339 imaglist
->Add(wxBitmap(text_xpm
));
346 m_radioTextLines
= (wxRadioBox
*)NULL
;
349 m_chkReadonly
= (wxCheckBox
*)NULL
;
359 m_textRange
= (wxTextCtrl
*)NULL
;
361 m_sizerText
= (wxSizer
*)NULL
;
366 m_selTo
= -2; // not -1 which means "no selection"
369 static const wxString modes
[] =
375 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
376 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
377 wxDefaultPosition
, wxDefaultSize
,
378 WXSIZEOF(modes
), modes
,
379 1, wxRA_SPECIFY_COLS
);
381 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
383 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
384 sizerLeft
->AddSpacer(5);
386 m_chkPassword
= CreateCheckBoxAndAddToSizer(
387 sizerLeft
, _T("&Password control"), TextPage_Password
389 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
390 sizerLeft
, _T("&Read-only mode")
392 sizerLeft
->AddSpacer(5);
394 static const wxString wrap
[] =
402 m_radioWrap
= new wxRadioBox(this, wxID_ANY
, _T("&Wrap style:"),
403 wxDefaultPosition
, wxDefaultSize
,
404 WXSIZEOF(wrap
), wrap
,
405 1, wxRA_SPECIFY_COLS
);
406 sizerLeft
->Add(m_radioWrap
, 0, wxGROW
| wxALL
, 5);
409 static const wxString kinds
[] =
416 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
417 wxDefaultPosition
, wxDefaultSize
,
418 WXSIZEOF(kinds
), kinds
,
419 1, wxRA_SPECIFY_COLS
);
421 sizerLeft
->AddSpacer(5);
422 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
425 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
426 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
427 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
430 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
431 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
433 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
434 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
436 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
437 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
439 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
440 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
442 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
443 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
445 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
446 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
448 btn
= new wxButton(this, TextPage_StreamRedirector
, _T("St&ream redirection"));
449 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
451 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
452 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
454 m_textPosCur
= CreateInfoText();
455 m_textRowCur
= CreateInfoText();
456 m_textColCur
= CreateInfoText();
458 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
459 sizerRow
->Add(CreateTextWithLabelSizer
465 sizerRow
->Add(CreateTextWithLabelSizer
470 0, wxLEFT
| wxRIGHT
, 5);
471 sizerRow
->Add(CreateTextWithLabelSizer
477 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
479 m_textLineLast
= CreateInfoText();
480 m_textPosLast
= CreateInfoText();
483 CreateTextWithLabelSizer
485 _T("Number of lines:"),
487 _T("Last position:"),
493 m_textSelFrom
= CreateInfoText();
494 m_textSelTo
= CreateInfoText();
497 CreateTextWithLabelSizer
499 _T("Selection: from"),
507 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
508 wxDefaultPosition
, wxDefaultSize
,
512 CreateTextWithLabelSizer
520 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
521 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
522 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
525 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
526 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
529 m_sizerText
->SetMinSize(150, 0);
531 // the 3 panes panes compose the upper part of the window
532 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
533 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
534 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
535 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
548 static int s_maxWidth
= 0;
552 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
555 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
557 wxSize(s_maxWidth
, -1),
562 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
564 const wxString
& label2
,
567 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
568 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
569 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
570 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
573 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
574 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
575 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 void TextWidgetsPage::Reset()
587 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
589 m_chkPassword
->SetValue(DEFAULTS
.password
);
590 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
592 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
595 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
599 void TextWidgetsPage::CreateText()
602 switch ( m_radioTextLines
->GetSelection() )
605 wxFAIL_MSG( _T("unexpected lines radio box selection") );
607 case TextLines_Single
:
610 case TextLines_Multi
:
611 flags
|= wxTE_MULTILINE
;
612 m_chkPassword
->SetValue(false);
616 if ( m_chkPassword
->GetValue() )
617 flags
|= wxTE_PASSWORD
;
618 if ( m_chkReadonly
->GetValue() )
619 flags
|= wxTE_READONLY
;
621 switch ( m_radioWrap
->GetSelection() )
624 wxFAIL_MSG( _T("unexpected wrap style radio box selection") );
627 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
631 flags
|= wxTE_WORDWRAP
;
635 flags
|= wxTE_LINEWRAP
;
639 // this is default but use symbolic file name for consistency
640 flags
|= wxTE_BESTWRAP
;
645 switch ( m_radioKind
->GetSelection() )
648 wxFAIL_MSG( _T("unexpected kind radio box selection") );
666 valueOld
= m_text
->GetValue();
668 m_sizerText
->Detach( m_text
);
673 valueOld
= _T("Hello, Universe!");
676 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
678 // cast to int needed to silence gcc warning about different enums
679 m_sizerText
->Add(m_text
, 1, wxALL
|
680 (flags
& wxTE_MULTILINE
? (int)wxGROW
682 m_sizerText
->Layout();
685 // ----------------------------------------------------------------------------
687 // ----------------------------------------------------------------------------
689 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
691 // update all info texts
695 long posCur
= m_text
->GetInsertionPoint();
696 if ( posCur
!= m_posCur
)
698 m_textPosCur
->Clear();
699 m_textRowCur
->Clear();
700 m_textColCur
->Clear();
703 m_text
->PositionToXY(posCur
, &col
, &row
);
705 *m_textPosCur
<< posCur
;
706 *m_textRowCur
<< row
;
707 *m_textColCur
<< col
;
715 long posLast
= m_text
->GetLastPosition();
716 if ( posLast
!= m_posLast
)
718 m_textPosLast
->Clear();
719 *m_textPosLast
<< posLast
;
725 if ( m_textLineLast
)
727 m_textLineLast
->SetValue(
728 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
731 if ( m_textSelFrom
&& m_textSelTo
)
734 m_text
->GetSelection(&selFrom
, &selTo
);
735 if ( selFrom
!= m_selFrom
)
737 m_textSelFrom
->Clear();
738 *m_textSelFrom
<< selFrom
;
743 if ( selTo
!= m_selTo
)
745 m_textSelTo
->Clear();
746 *m_textSelTo
<< selTo
;
754 wxString range
= m_text
->GetRange(10, 20);
755 if ( range
!= m_range10_20
)
757 m_range10_20
= range
;
758 m_textRange
->SetValue(range
);
763 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
770 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
772 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
773 ? _T("Here,\nthere and\neverywhere")
774 : _T("Yellow submarine"));
779 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
781 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
783 m_text
->AppendText(_T("We all live in a\n"));
786 m_text
->AppendText(_T("Yellow submarine"));
789 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
791 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
792 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
794 m_text
->WriteText(_T("\nall about the girl who came to stay"));
798 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
804 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
806 // search for the file in several dirs where it's likely to be
808 pathlist
.Add(_T("."));
809 pathlist
.Add(_T(".."));
810 pathlist
.Add(_T("../../../samples/widgets"));
812 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
815 wxLogError(_T("File textctrl.cpp not found."));
820 if ( !m_text
->LoadFile(filename
) )
822 // this is not supposed to happen ...
823 wxLogError(_T("Error loading file."));
827 long elapsed
= sw
.Time();
828 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
829 filename
.c_str(), elapsed
/ 1000,
830 (unsigned int) elapsed
% 1000);
835 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
837 event
.Enable(!m_text
->GetValue().empty());
840 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
842 event
.Enable( !IsSingleLine() );
845 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
847 // can't put multiline control in password mode
848 event
.Enable( IsSingleLine() );
851 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
853 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
855 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
857 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
858 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
859 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
862 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
864 // small hack to suppress the very first message: by then the logging is
865 // not yet redirected and so initial setting of the text value results in
866 // an annoying message box
867 static bool s_firstTime
= true;
874 wxLogMessage(_T("Text ctrl value changed"));
877 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
879 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
883 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
888 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
890 // Note, NO_TEXT_WINDOW_STREAM is private flag of wxWidgets header
891 // it's simpler to check it rather than duplicate whole
892 #ifdef NO_TEXT_WINDOW_STREAM
893 wxMessageBox(_T("This wxWidgets build does not support wxStreamToTextRedirector"));
895 wxStreamToTextRedirector
redirect(m_text
);
896 wxString
str( _T("Outputed to cout, appears in wxTextCtrl!") );