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 wxTextEntryBase
*GetTextEntry() const { return m_text
; }
142 virtual void RecreateWidget() { CreateText(); }
144 // lazy creation of the content
145 virtual void CreateContent();
148 // create an info text contorl
149 wxTextCtrl
*CreateInfoText();
151 // create a horz sizer holding a static text and this text control
152 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
154 const wxString
& label2
= wxEmptyString
,
155 wxTextCtrl
*text2
= NULL
);
158 void OnButtonReset(wxCommandEvent
& event
);
159 void OnButtonClearLog(wxCommandEvent
& event
);
161 void OnButtonSet(wxCommandEvent
& event
);
162 void OnButtonAdd(wxCommandEvent
& event
);
163 void OnButtonInsert(wxCommandEvent
& event
);
164 void OnButtonClear(wxCommandEvent
& event
);
165 void OnButtonLoad(wxCommandEvent
& event
);
167 void OnStreamRedirector(wxCommandEvent
& event
);
168 void OnButtonQuit(wxCommandEvent
& event
);
170 void OnText(wxCommandEvent
& event
);
171 void OnTextEnter(wxCommandEvent
& event
);
173 void OnCheckOrRadioBox(wxCommandEvent
& event
);
175 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
177 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
178 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
180 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
182 void OnIdle(wxIdleEvent
& event
);
184 // reset the textctrl parameters
187 // (re)create the textctrl
190 // is the control currently single line?
191 bool IsSingleLine() const
193 return m_radioTextLines
->GetSelection() == TextLines_Single
;
199 // the radiobox to choose between single and multi line
200 wxRadioBox
*m_radioTextLines
;
202 // and another one to choose the wrapping style
203 wxRadioBox
*m_radioWrap
;
205 // the checkboxes controlling text ctrl styles
206 wxCheckBox
*m_chkPassword
,
210 // under MSW we test rich edit controls as well here
212 wxRadioBox
*m_radioKind
;
215 // the textctrl itself and the sizer it is in
217 wxSizer
*m_sizerText
;
219 // the information text zones
220 wxTextCtrl
*m_textPosCur
,
229 // and the data to show in them
235 wxString m_range10_20
;
238 // any class wishing to process wxWidgets events must use this macro
239 DECLARE_EVENT_TABLE()
240 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 class WidgetsTextCtrl
: public wxTextCtrl
250 WidgetsTextCtrl(wxWindow
*parent
,
252 const wxString
& value
,
254 : wxTextCtrl(parent
, id
, value
, wxDefaultPosition
, wxDefaultSize
, flags
)
259 void OnRightClick(wxMouseEvent
& event
)
263 switch ( HitTest(event
.GetPosition(), &x
, &y
) )
266 wxFAIL_MSG( wxT("unexpected HitTest() result") );
269 case wxTE_HT_UNKNOWN
:
271 where
= wxT("nowhere near");
275 where
= wxT("before");
279 where
= wxT("below");
283 where
= wxT("beyond");
286 case wxTE_HT_ON_TEXT
:
291 wxLogMessage(wxT("Mouse is %s (%ld, %ld)"), where
.c_str(), x
, y
);
297 DECLARE_EVENT_TABLE()
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
305 EVT_IDLE(TextWidgetsPage::OnIdle
)
307 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
309 EVT_BUTTON(TextPage_StreamRedirector
, TextWidgetsPage::OnStreamRedirector
)
311 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
312 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
313 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
314 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
315 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
317 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
319 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
320 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
322 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
324 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
325 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
327 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
328 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
331 BEGIN_EVENT_TABLE(WidgetsTextCtrl
, wxTextCtrl
)
332 EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick
)
335 // ============================================================================
337 // ============================================================================
339 #if defined(__WXX11__)
340 #define FAMILY_CTRLS NATIVE_CTRLS
341 #elif defined(__WXUNIVERSAL__)
342 #define FAMILY_CTRLS UNIVERSAL_CTRLS
344 #define FAMILY_CTRLS NATIVE_CTRLS
347 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, wxT("Text"),
348 FAMILY_CTRLS
| EDITABLE_CTRLS
351 // ----------------------------------------------------------------------------
352 // TextWidgetsPage creation
353 // ----------------------------------------------------------------------------
355 TextWidgetsPage::TextWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
)
356 : WidgetsPage(book
, imaglist
, text_xpm
)
363 m_radioTextLines
= (wxRadioBox
*)NULL
;
367 m_chkFilename
= (wxCheckBox
*)NULL
;
377 m_textRange
= (wxTextCtrl
*)NULL
;
379 m_sizerText
= (wxSizer
*)NULL
;
384 m_selTo
= -2; // not -1 which means "no selection"
387 void TextWidgetsPage::CreateContent()
390 static const wxString modes
[] =
396 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("&Set textctrl parameters"));
397 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, wxT("&Number of lines:"),
398 wxDefaultPosition
, wxDefaultSize
,
399 WXSIZEOF(modes
), modes
,
400 1, wxRA_SPECIFY_COLS
);
402 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
404 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
405 sizerLeft
->AddSpacer(5);
407 m_chkPassword
= CreateCheckBoxAndAddToSizer(
408 sizerLeft
, wxT("&Password control"), TextPage_Password
410 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
411 sizerLeft
, wxT("&Read-only mode")
413 m_chkFilename
= CreateCheckBoxAndAddToSizer(
414 sizerLeft
, wxT("&Filename control")
416 m_chkFilename
->Disable(); // not implemented yet
417 sizerLeft
->AddSpacer(5);
419 static const wxString wrap
[] =
427 m_radioWrap
= new wxRadioBox(this, wxID_ANY
, wxT("&Wrap style:"),
428 wxDefaultPosition
, wxDefaultSize
,
429 WXSIZEOF(wrap
), wrap
,
430 1, wxRA_SPECIFY_COLS
);
431 sizerLeft
->Add(m_radioWrap
, 0, wxGROW
| wxALL
, 5);
434 static const wxString kinds
[] =
438 wxT("rich edit 2.0"),
441 m_radioKind
= new wxRadioBox(this, wxID_ANY
, wxT("Control &kind"),
442 wxDefaultPosition
, wxDefaultSize
,
443 WXSIZEOF(kinds
), kinds
,
444 1, wxRA_SPECIFY_COLS
);
446 sizerLeft
->AddSpacer(5);
447 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
450 wxButton
*btn
= new wxButton(this, TextPage_Reset
, wxT("&Reset"));
451 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
452 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
455 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, wxT("&Change contents:"));
456 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
458 btn
= new wxButton(this, TextPage_Set
, wxT("&Set text value"));
459 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
461 btn
= new wxButton(this, TextPage_Add
, wxT("&Append text"));
462 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
464 btn
= new wxButton(this, TextPage_Insert
, wxT("&Insert text"));
465 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
467 btn
= new wxButton(this, TextPage_Load
, wxT("&Load file"));
468 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
470 btn
= new wxButton(this, TextPage_Clear
, wxT("&Clear"));
471 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
473 btn
= new wxButton(this, TextPage_StreamRedirector
, wxT("St&ream redirection"));
474 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
476 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, wxT("&Info:"));
477 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
479 m_textPosCur
= CreateInfoText();
480 m_textRowCur
= CreateInfoText();
481 m_textColCur
= CreateInfoText();
483 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
484 sizerRow
->Add(CreateTextWithLabelSizer
490 sizerRow
->Add(CreateTextWithLabelSizer
495 0, wxLEFT
| wxRIGHT
, 5);
496 sizerRow
->Add(CreateTextWithLabelSizer
502 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
504 m_textLineLast
= CreateInfoText();
505 m_textPosLast
= CreateInfoText();
508 CreateTextWithLabelSizer
510 wxT("Number of lines:"),
512 wxT("Last position:"),
518 m_textSelFrom
= CreateInfoText();
519 m_textSelTo
= CreateInfoText();
522 CreateTextWithLabelSizer
524 wxT("Selection: from"),
532 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
533 wxDefaultPosition
, wxDefaultSize
,
537 CreateTextWithLabelSizer
539 wxT("Range 10..20:"),
545 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
546 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
547 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
550 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, wxT("&Text:"));
551 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
554 m_sizerText
->SetMinSize(150, 0);
556 // the 3 panes panes compose the upper part of the window
557 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
558 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
559 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
560 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
571 static int s_maxWidth
= 0;
575 GetTextExtent(wxT("9999999"), &s_maxWidth
, NULL
);
578 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
580 wxSize(s_maxWidth
, wxDefaultCoord
),
585 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
587 const wxString
& label2
,
590 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
591 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
592 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
593 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
596 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
597 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
598 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
604 // ----------------------------------------------------------------------------
606 // ----------------------------------------------------------------------------
608 void TextWidgetsPage::Reset()
610 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
612 m_chkPassword
->SetValue(DEFAULTS
.password
);
613 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
614 m_chkFilename
->SetValue(DEFAULTS
.filename
);
616 m_radioWrap
->SetSelection(DEFAULTS
.wrapStyle
);
619 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
623 void TextWidgetsPage::CreateText()
625 int flags
= ms_defaultFlags
;
626 switch ( m_radioTextLines
->GetSelection() )
629 wxFAIL_MSG( wxT("unexpected lines radio box selection") );
631 case TextLines_Single
:
634 case TextLines_Multi
:
635 flags
|= wxTE_MULTILINE
;
636 m_chkPassword
->SetValue(false);
640 if ( m_chkPassword
->GetValue() )
641 flags
|= wxTE_PASSWORD
;
642 if ( m_chkReadonly
->GetValue() )
643 flags
|= wxTE_READONLY
;
645 switch ( m_radioWrap
->GetSelection() )
648 wxFAIL_MSG( wxT("unexpected wrap style radio box selection") );
651 flags
|= wxTE_DONTWRAP
; // same as wxHSCROLL
655 flags
|= wxTE_WORDWRAP
;
659 flags
|= wxTE_CHARWRAP
;
663 // this is default but use symbolic file name for consistency
664 flags
|= wxTE_BESTWRAP
;
669 switch ( m_radioKind
->GetSelection() )
672 wxFAIL_MSG( wxT("unexpected kind radio box selection") );
690 valueOld
= m_text
->GetValue();
692 m_sizerText
->Detach( m_text
);
697 valueOld
= wxT("Hello, Universe!");
700 m_text
= new WidgetsTextCtrl(this, TextPage_Textctrl
, valueOld
, flags
);
703 if ( m_chkFilename
->GetValue() )
707 // cast to int needed to silence gcc warning about different enums
708 m_sizerText
->Add(m_text
, 1, wxALL
|
709 (flags
& wxTE_MULTILINE
? (int)wxGROW
711 m_sizerText
->Layout();
714 // ----------------------------------------------------------------------------
716 // ----------------------------------------------------------------------------
718 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
720 // update all info texts
724 long posCur
= m_text
->GetInsertionPoint();
725 if ( posCur
!= m_posCur
)
727 m_textPosCur
->Clear();
728 m_textRowCur
->Clear();
729 m_textColCur
->Clear();
732 m_text
->PositionToXY(posCur
, &col
, &row
);
734 *m_textPosCur
<< posCur
;
735 *m_textRowCur
<< row
;
736 *m_textColCur
<< col
;
744 long posLast
= m_text
->GetLastPosition();
745 if ( posLast
!= m_posLast
)
747 m_textPosLast
->Clear();
748 *m_textPosLast
<< posLast
;
754 if ( m_textLineLast
)
756 m_textLineLast
->SetValue(
757 wxString::Format(wxT("%d"), m_text
->GetNumberOfLines()) );
760 if ( m_textSelFrom
&& m_textSelTo
)
763 m_text
->GetSelection(&selFrom
, &selTo
);
764 if ( selFrom
!= m_selFrom
)
766 m_textSelFrom
->Clear();
767 *m_textSelFrom
<< selFrom
;
772 if ( selTo
!= m_selTo
)
774 m_textSelTo
->Clear();
775 *m_textSelTo
<< selTo
;
783 wxString range
= m_text
->GetRange(10, 20);
784 if ( range
!= m_range10_20
)
786 m_range10_20
= range
;
787 m_textRange
->SetValue(range
);
792 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
799 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
801 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
802 ? wxT("Here,\nthere and\neverywhere")
803 : wxT("Yellow submarine"));
808 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
810 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
812 m_text
->AppendText(wxT("We all live in a\n"));
815 m_text
->AppendText(wxT("Yellow submarine"));
818 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
820 m_text
->WriteText(wxT("Is there anybody going to listen to my story"));
821 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
823 m_text
->WriteText(wxT("\nall about the girl who came to stay"));
827 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
833 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
835 // search for the file in several dirs where it's likely to be
837 pathlist
.Add(wxT("."));
838 pathlist
.Add(wxT(".."));
839 pathlist
.Add(wxT("../../../samples/widgets"));
841 wxString filename
= pathlist
.FindValidPath(wxT("textctrl.cpp"));
844 wxLogError(wxT("File textctrl.cpp not found."));
849 if ( !m_text
->LoadFile(filename
) )
851 // this is not supposed to happen ...
852 wxLogError(wxT("Error loading file."));
856 long elapsed
= sw
.Time();
857 wxLogMessage(wxT("Loaded file '%s' in %lu.%us"),
858 filename
.c_str(), elapsed
/ 1000,
859 (unsigned int) elapsed
% 1000);
864 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
866 event
.Enable(!m_text
->GetValue().empty());
869 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
871 event
.Enable( !IsSingleLine() );
874 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
876 // can't put multiline control in password mode
877 event
.Enable( IsSingleLine() );
880 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
882 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
884 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
886 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
887 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
888 (m_chkFilename
->GetValue() != DEFAULTS
.filename
) ||
889 (m_radioWrap
->GetSelection() != DEFAULTS
.wrapStyle
) );
892 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
894 // small hack to suppress the very first message: by then the logging is
895 // not yet redirected and so initial setting of the text value results in
896 // an annoying message box
897 static bool s_firstTime
= true;
904 wxLogMessage(wxT("Text ctrl value changed"));
907 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
909 wxLogMessage(wxT("Text entered: '%s'"), event
.GetString().c_str());
913 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))
918 void TextWidgetsPage::OnStreamRedirector(wxCommandEvent
& WXUNUSED(event
))
920 #if wxHAS_TEXT_WINDOW_STREAM
921 wxStreamToTextRedirector
redirect(m_text
);
922 wxString
str( wxT("Outputed to cout, appears in wxTextCtrl!") );
923 wxSTD cout
<< str
<< wxSTD endl
;
925 wxMessageBox(wxT("This wxWidgets build does not support wxStreamToTextRedirector"));