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/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/statbox.h"
36 #include "wx/stattext.h"
37 #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
204 // any class wishing to process wxWindows events must use this macro
205 DECLARE_EVENT_TABLE()
206 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
213 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
214 EVT_IDLE(TextWidgetsPage::OnIdle
)
216 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
218 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
219 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
220 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
221 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
222 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
224 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
226 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
227 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
229 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
231 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
232 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
234 EVT_CHECKBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
235 EVT_RADIOBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
238 // ============================================================================
240 // ============================================================================
242 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
244 // ----------------------------------------------------------------------------
245 // TextWidgetsPage creation
246 // ----------------------------------------------------------------------------
248 TextWidgetsPage::TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
)
249 : WidgetsPage(notebook
)
251 imaglist
->Add(wxBitmap(text_xpm
));
257 m_radioTextLines
= (wxRadioBox
*)NULL
;
261 m_chkReadonly
= (wxCheckBox
*)NULL
;
271 m_textRange
= (wxTextCtrl
*)NULL
;
273 m_sizerText
= (wxSizer
*)NULL
;
278 m_selTo
= -2; // not -1 which means "no selection"
281 static const wxString modes
[] =
287 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set textctrl parameters"));
288 m_radioTextLines
= new wxRadioBox(this, -1, _T("&Number of lines:"),
289 wxDefaultPosition
, wxDefaultSize
,
290 WXSIZEOF(modes
), modes
,
291 1, wxRA_SPECIFY_COLS
);
293 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
295 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
296 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
298 m_chkPassword
= CreateCheckBoxAndAddToSizer(
299 sizerLeft
, _T("&Password control"), TextPage_Password
301 m_chkWrapLines
= CreateCheckBoxAndAddToSizer(
302 sizerLeft
, _T("Line &wrap"), TextPage_WrapLines
304 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
305 sizerLeft
, _T("&Read-only mode")
309 static const wxString kinds
[] =
316 m_radioKind
= new wxRadioBox(this, -1, _T("Control &kind"),
317 wxDefaultPosition
, wxDefaultSize
,
318 WXSIZEOF(kinds
), kinds
,
319 1, wxRA_SPECIFY_COLS
);
321 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
322 sizerLeft
->Add(m_radioKind
, 0, wxGROW
| wxALL
, 5);
325 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
326 sizerLeft
->Add(2, 2, 0, wxGROW
| wxALL
, 1); // spacer
327 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
330 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change contents:"));
331 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
333 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
334 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
336 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
337 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
339 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
340 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
342 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
343 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
345 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
346 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 1);
348 wxStaticBox
*box4
= new wxStaticBox(this, -1, _T("&Info:"));
349 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
351 m_textPosCur
= CreateInfoText();
352 m_textRowCur
= CreateInfoText();
353 m_textColCur
= CreateInfoText();
355 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
356 sizerRow
->Add(CreateTextWithLabelSizer
362 sizerRow
->Add(CreateTextWithLabelSizer
367 0, wxLEFT
| wxRIGHT
, 5);
368 sizerRow
->Add(CreateTextWithLabelSizer
374 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
376 m_textLineLast
= CreateInfoText();
377 m_textPosLast
= CreateInfoText();
380 CreateTextWithLabelSizer
382 _T("Number of lines:"),
384 _T("Last position:"),
390 m_textSelFrom
= CreateInfoText();
391 m_textSelTo
= CreateInfoText();
394 CreateTextWithLabelSizer
396 _T("Selection: from"),
404 m_textRange
= new wxTextCtrl(this, -1, _T(""),
405 wxDefaultPosition
, wxDefaultSize
,
409 CreateTextWithLabelSizer
417 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
418 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
419 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
422 wxStaticBox
*box3
= new wxStaticBox(this, -1, _T("&Text:"));
423 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
426 m_sizerText
->SetMinSize(150, 0);
428 // the 3 panes panes compose the upper part of the window
429 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
430 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
431 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
432 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
440 TextWidgetsPage::~TextWidgetsPage()
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
450 static int s_maxWidth
= 0;
454 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
457 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
459 wxSize(s_maxWidth
, -1),
464 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
466 const wxString
& label2
,
469 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
470 sizerRow
->Add(new wxStaticText(this, -1, label
), 0,
471 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
472 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
475 sizerRow
->Add(new wxStaticText(this, -1, label2
), 0,
476 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
477 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
483 // ----------------------------------------------------------------------------
485 // ----------------------------------------------------------------------------
487 void TextWidgetsPage::Reset()
489 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
490 m_chkPassword
->SetValue(DEFAULTS
.password
);
491 m_chkWrapLines
->SetValue(DEFAULTS
.wraplines
);
492 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
494 m_radioKind
->SetSelection(DEFAULTS
.textKind
);
498 void TextWidgetsPage::CreateText()
501 switch ( m_radioTextLines
->GetSelection() )
504 wxFAIL_MSG( _T("unexpected lines radio box selection") );
506 case TextLines_Single
:
509 case TextLines_Multi
:
510 flags
|= wxTE_MULTILINE
;
511 m_chkPassword
->SetValue(FALSE
);
515 if ( m_chkPassword
->GetValue() )
516 flags
|= wxTE_PASSWORD
;
517 if ( m_chkReadonly
->GetValue() )
518 flags
|= wxTE_READONLY
;
519 if ( !m_chkWrapLines
->GetValue() )
523 switch ( m_radioKind
->GetSelection() )
526 wxFAIL_MSG( _T("unexpected kind radio box selection") );
544 valueOld
= m_text
->GetValue();
546 m_sizerText
->Remove(m_text
);
551 valueOld
= _T("Hello, Universe!");
554 m_text
= new wxTextCtrl(this, TextPage_Textctrl
,
556 wxDefaultPosition
, wxDefaultSize
,
558 m_sizerText
->Add(m_text
, 1, wxALL
|
559 (flags
& wxTE_MULTILINE
? wxGROW
561 m_sizerText
->Layout();
564 // ----------------------------------------------------------------------------
566 // ----------------------------------------------------------------------------
568 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
570 // update all info texts
574 long posCur
= m_text
->GetInsertionPoint();
575 if ( posCur
!= m_posCur
)
577 m_textPosCur
->Clear();
578 m_textRowCur
->Clear();
579 m_textColCur
->Clear();
582 m_text
->PositionToXY(posCur
, &col
, &row
);
584 *m_textPosCur
<< posCur
;
585 *m_textRowCur
<< row
;
586 *m_textColCur
<< col
;
594 long posLast
= m_text
->GetLastPosition();
595 if ( posLast
!= m_posLast
)
597 m_textPosLast
->Clear();
598 *m_textPosLast
<< posLast
;
604 if ( m_textLineLast
)
606 m_textLineLast
->SetValue(
607 wxString::Format(_T("%d"), m_text
->GetNumberOfLines()) );
610 if ( m_textSelFrom
&& m_textSelTo
)
613 m_text
->GetSelection(&selFrom
, &selTo
);
614 if ( selFrom
!= m_selFrom
)
616 m_textSelFrom
->Clear();
617 *m_textSelFrom
<< selFrom
;
622 if ( selTo
!= m_selTo
)
624 m_textSelTo
->Clear();
625 *m_textSelTo
<< selTo
;
633 m_textRange
->SetValue(m_text
->GetRange(10, 20));
637 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
644 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
646 m_text
->SetValue(m_text
->GetWindowStyle() & wxTE_MULTILINE
647 ? _T("Here,\nthere and\neverywhere")
648 : _T("Yellow submarine"));
653 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
655 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
657 m_text
->AppendText(_T("We all live in a\n"));
660 m_text
->AppendText(_T("Yellow submarine"));
663 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
665 m_text
->WriteText(_T("Is there anybody going to listen to my story"));
666 if ( m_text
->GetWindowStyle() & wxTE_MULTILINE
)
668 m_text
->WriteText(_T("\nall about the girl who came to stay"));
672 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
678 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
680 // search for the file in several dirs where it's likely to be
682 pathlist
.Add(_T("."));
683 pathlist
.Add(_T(".."));
684 pathlist
.Add(_T("../../../samples/widgets"));
686 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
689 wxLogError(_T("File textctrl.cpp not found."));
694 if ( !m_text
->LoadFile(filename
) )
696 // this is not supposed to happen ...
697 wxLogError(_T("Error loading file."));
701 long elapsed
= sw
.Time();
702 wxLogMessage(_T("Loaded file '%s' in %lu.%us"),
703 filename
.c_str(), elapsed
/ 1000, elapsed
% 1000);
708 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
710 event
.Enable(!m_text
->GetValue().empty());
713 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
715 event
.Enable( !IsSingleLine() );
718 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
720 // can't put multiline control in password mode
721 event
.Enable( IsSingleLine() );
724 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
726 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
728 (m_radioKind
->GetSelection() != DEFAULTS
.textKind
) ||
730 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
731 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
732 (m_chkWrapLines
->GetValue() != DEFAULTS
.wraplines
) );
735 void TextWidgetsPage::OnText(wxCommandEvent
& event
)
737 // small hack to suppress the very first message: by then the logging is
738 // not yet redirected and so initial setting of the text value results in
739 // an annoying message box
740 static bool s_firstTime
= TRUE
;
747 wxLogMessage(_T("Text ctrl value changed"));
750 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
752 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
755 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)