1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWindows 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"
45 #include "icons/text.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
67 // textctrl line number radiobox values
76 // textctrl kind values
86 // default values for the controls
87 static const struct ControlValues
98 TextLines_Multi
, // multiline
99 false, // not password
100 true, // do wrap lines
101 false, // not readonly
103 TextKind_Plain
// plain EDIT control
107 // ----------------------------------------------------------------------------
109 // ----------------------------------------------------------------------------
111 // Define a new frame type: this is going to be our main frame
112 class TextWidgetsPage
: public WidgetsPage
116 TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
117 virtual ~TextWidgetsPage();
120 // create an info text contorl
121 wxTextCtrl
*CreateInfoText();
123 // create a horz sizer holding a static text and this text control
124 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
126 const wxString
& label2
= wxEmptyString
,
127 wxTextCtrl
*text2
= NULL
);
130 void OnButtonReset(wxCommandEvent
& event
);
131 void OnButtonClearLog(wxCommandEvent
& event
);
133 void OnButtonSet(wxCommandEvent
& event
);
134 void OnButtonAdd(wxCommandEvent
& event
);
135 void OnButtonInsert(wxCommandEvent
& event
);
136 void OnButtonClear(wxCommandEvent
& event
);
137 void OnButtonLoad(wxCommandEvent
& event
);
139 void OnButtonQuit(wxCommandEvent
& event
);
141 void OnText(wxCommandEvent
& event
);
142 void OnTextEnter(wxCommandEvent
& event
);
144 void OnCheckOrRadioBox(wxCommandEvent
& event
);
146 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
148 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
149 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
151 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
153 void OnIdle(wxIdleEvent
& event
);
155 // reset the textctrl parameters
158 // (re)create the textctrl
161 // is the control currently single line?
162 bool IsSingleLine() const
164 return m_radioTextLines
->GetSelection() == TextLines_Single
;
170 // the radiobox to choose between single and multi line
171 wxRadioBox
*m_radioTextLines
;
173 // the checkboxes controlling text ctrl styles
174 wxCheckBox
*m_chkPassword
,
178 // under MSW we test rich edit controls as well here
180 wxRadioBox
*m_radioKind
;
183 // the textctrl itself and the sizer it is in
185 wxSizer
*m_sizerText
;
187 // the information text zones
188 wxTextCtrl
*m_textPosCur
,
197 // and the data to show in them
203 wxString m_range10_20
;
206 // any class wishing to process wxWindows events must use this macro
207 DECLARE_EVENT_TABLE()
208 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
211 // ----------------------------------------------------------------------------
213 // ----------------------------------------------------------------------------
215 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
216 EVT_IDLE(TextWidgetsPage::OnIdle
)
218 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
220 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
221 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
222 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
223 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
224 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
226 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
228 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
229 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
231 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
233 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
234 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
236 EVT_CHECKBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
237 EVT_RADIOBOX(wxID_ANY
, TextWidgetsPage::OnCheckOrRadioBox
)
240 // ============================================================================
242 // ============================================================================
244 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
246 // ----------------------------------------------------------------------------
247 // TextWidgetsPage creation
248 // ----------------------------------------------------------------------------
250 TextWidgetsPage::TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
)
251 : WidgetsPage(notebook
)
253 imaglist
->Add(wxBitmap(text_xpm
));
259 m_radioTextLines
= (wxRadioBox
*)NULL
;
263 m_chkReadonly
= (wxCheckBox
*)NULL
;
273 m_textRange
= (wxTextCtrl
*)NULL
;
275 m_sizerText
= (wxSizer
*)NULL
;
280 m_selTo
= -2; // not -1 which means "no selection"
283 static const wxString modes
[] =
289 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, _T("&Set textctrl parameters"));
290 m_radioTextLines
= new wxRadioBox(this, wxID_ANY
, _T("&Number of lines:"),
291 wxDefaultPosition
, wxDefaultSize
,
292 WXSIZEOF(modes
), modes
,
293 1, wxRA_SPECIFY_COLS
);
295 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
297 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
298 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
300 m_chkPassword
= CreateCheckBoxAndAddToSizer(
301 sizerLeft
, _T("&Password control"), TextPage_Password
303 m_chkWrapLines
= CreateCheckBoxAndAddToSizer(
304 sizerLeft
, _T("Line &wrap"), TextPage_WrapLines
306 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
307 sizerLeft
, _T("&Read-only mode")
311 static const wxString kinds
[] =
318 m_radioKind
= new wxRadioBox(this, wxID_ANY
, _T("Control &kind"),
319 wxDefaultPosition
, wxDefaultSize
,
320 WXSIZEOF(kinds
), kinds
,
321 1, wxRA_SPECIFY_COLS
);
323 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
324 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
327 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
328 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
329 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
332 wxStaticBox
*box2
= new wxStaticBox(this, wxID_ANY
, _T("&Change contents:"));
333 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
335 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
336 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
338 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
339 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
341 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
342 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
344 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
345 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
347 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
348 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
350 wxStaticBox
*box4
= new wxStaticBox(this, wxID_ANY
, _T("&Info:"));
351 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
353 m_textPosCur
= CreateInfoText();
354 m_textRowCur
= CreateInfoText();
355 m_textColCur
= CreateInfoText();
357 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
358 sizerRow
->Add(CreateTextWithLabelSizer
364 sizerRow
->Add(CreateTextWithLabelSizer
369 0, wxLEFT
| wxRIGHT
, 5);
370 sizerRow
->Add(CreateTextWithLabelSizer
376 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
378 m_textLineLast
= CreateInfoText();
379 m_textPosLast
= CreateInfoText();
382 CreateTextWithLabelSizer
384 _T("Number of lines:"),
386 _T("Last position:"),
392 m_textSelFrom
= CreateInfoText();
393 m_textSelTo
= CreateInfoText();
396 CreateTextWithLabelSizer
398 _T("Selection: from"),
406 m_textRange
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
407 wxDefaultPosition
, wxDefaultSize
,
411 CreateTextWithLabelSizer
419 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
420 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
421 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
424 wxStaticBox
*box3
= new wxStaticBox(this, wxID_ANY
, _T("&Text:"));
425 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
428 m_sizerText
->SetMinSize(150, 0);
430 // the 3 panes panes compose the upper part of the window
431 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
432 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
433 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
434 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
441 TextWidgetsPage::~TextWidgetsPage()
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
451 static int s_maxWidth
= 0;
455 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
458 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
460 wxSize(s_maxWidth
, -1),
465 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
467 const wxString
& label2
,
470 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
471 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label
), 0,
472 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
473 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
476 sizerRow
->Add(new wxStaticText(this, wxID_ANY
, label2
), 0,
477 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
478 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 void TextWidgetsPage::Reset()
490 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
491 m_chkPassword
->SetValue(DEFAULTS
.password
);
492 m_chkWrapLines
->SetValue(DEFAULTS
.wraplines
);
493 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
495 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
499 void TextWidgetsPage::CreateText()
502 switch ( m_radioTextLines
->GetSelection() )
505 wxFAIL_MSG( _T("unexpected lines radio box selection") );
507 case TextLines_Single
:
510 case TextLines_Multi
:
511 flags
|= wxTE_MULTILINE
;
512 m_chkPassword
->SetValue(false);
516 if ( m_chkPassword
->GetValue() )
517 flags
|= wxTE_PASSWORD
;
518 if ( m_chkReadonly
->GetValue() )
519 flags
|= wxTE_READONLY
;
520 if ( !m_chkWrapLines
->GetValue() )
524 switch ( m_radioKind
->GetSelection() )
527 wxFAIL_MSG( _T("unexpected kind radio box selection") );
545 valueOld
= m_text
->GetValue();
547 m_sizerText
->Detach( m_text
);
552 valueOld
= _T("Hello, Universe!");
555 m_text
= new wxTextCtrl(this, TextPage_Textctrl
,
557 wxDefaultPosition
, wxDefaultSize
,
560 // cast to int needed to silence gcc warning about different enums
561 m_sizerText
->Add(m_text
, 1, wxALL
|
562 (flags
& wxTE_MULTILINE
? (int)wxGROW
564 m_sizerText
->Layout();
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
573 // update all info texts
577 long posCur
= m_text
->GetInsertionPoint();
578 if ( posCur
!= m_posCur
)
580 m_textPosCur
->Clear();
581 m_textRowCur
->Clear();
582 m_textColCur
->Clear();
585 m_text
->PositionToXY(posCur
, &col
, &row
);
587 *m_textPosCur
<< posCur
;
588 *m_textRowCur
<< row
;
589 *m_textColCur
<< col
;
597 long posLast
= m_text
->GetLastPosition();
598 if ( posLast
!= m_posLast
)
600 m_textPosLast
->Clear();
601 *m_textPosLast
<< posLast
;
607 if ( m_textLineLast
)
609 m_textLineLast
->SetValue(
610 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
613 if ( m_textSelFrom
&& m_textSelTo
)
616 m_text
->GetSelection(&selFrom
, &selTo
);
617 if ( selFrom
!= m_selFrom
)
619 m_textSelFrom
->Clear();
620 *m_textSelFrom
<< selFrom
;
625 if ( selTo
!= m_selTo
)
627 m_textSelTo
->Clear();
628 *m_textSelTo
<< selTo
;
636 wxString range
= m_text
->GetRange(10, 20);
637 if ( range
!= m_range10_20
)
639 m_range10_20
= range
;
640 m_textRange
->SetValue(range
);
645 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
652 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
654 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
655 ? _T("Here,\nthere and\neverywhere")
656 : _T("Yellow submarine"));
661 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
663 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
665 m_text
->AppendText(_T("We all live in a\n"));
668 m_text
->AppendText(_T("Yellow submarine"));
671 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
673 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
674 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
676 m_text
->WriteText(_T("\nall about the girl who came to stay"));
680 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
686 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
688 // search for the file in several dirs where it's likely to be
690 pathlist
.Add(_T("."));
691 pathlist
.Add(_T(".."));
692 pathlist
.Add(_T("../../../samples/widgets"));
694 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
697 wxLogError(_T("File textctrl.cpp not found."));
702 if ( !m_text
->LoadFile(filename
) )
704 // this is not supposed to happen ...
705 wxLogError(_T("Error loading file."));
709 long elapsed
= sw
.Time();
710 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
711 filename
.c_str(), elapsed
/ 1000,
712 (unsigned int) elapsed
% 1000);
717 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
719 event
.Enable(!m_text
->GetValue().empty());
722 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
724 event
.Enable( !IsSingleLine() );
727 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
729 // can't put multiline control in password mode
730 event
.Enable( IsSingleLine() );
733 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
735 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
737 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
739 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
740 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
741 (m_chkWrapLines
->GetValue() != DEFAULTS
.wraplines
) );
744 void TextWidgetsPage::OnText(wxCommandEvent
& WXUNUSED(event
))
746 // small hack to suppress the very first message: by then the logging is
747 // not yet redirected and so initial setting of the text value results in
748 // an annoying message box
749 static bool s_firstTime
= true;
756 wxLogMessage(_T("Text ctrl value changed"));
759 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
761 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
764 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& WXUNUSED(event
))