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"
44 #include "icons/text.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
66 // textctrl line number radiobox values
73 // default values for the controls
74 static const struct ControlValues
82 TextLines_Multi
, // multiline
83 FALSE
, // not password
84 TRUE
, // do wrap lines
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // Define a new frame type: this is going to be our main frame
93 class TextWidgetsPage
: public WidgetsPage
97 TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
);
98 virtual ~TextWidgetsPage();
101 // create an info text contorl
102 wxTextCtrl
*CreateInfoText();
104 // create a horz sizer holding a static text and this text control
105 wxSizer
*CreateTextWithLabelSizer(const wxString
& label
,
107 const wxString
& label2
= wxEmptyString
,
108 wxTextCtrl
*text2
= NULL
);
111 void OnButtonReset(wxCommandEvent
& event
);
112 void OnButtonClearLog(wxCommandEvent
& event
);
114 void OnButtonSet(wxCommandEvent
& event
);
115 void OnButtonAdd(wxCommandEvent
& event
);
116 void OnButtonInsert(wxCommandEvent
& event
);
117 void OnButtonClear(wxCommandEvent
& event
);
118 void OnButtonLoad(wxCommandEvent
& event
);
120 void OnButtonQuit(wxCommandEvent
& event
);
122 void OnText(wxCommandEvent
& event
);
123 void OnTextEnter(wxCommandEvent
& event
);
125 void OnCheckOrRadioBox(wxCommandEvent
& event
);
127 void OnUpdateUIClearButton(wxUpdateUIEvent
& event
);
129 void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
);
130 void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
);
132 void OnUpdateUIResetButton(wxUpdateUIEvent
& event
);
134 void OnIdle(wxIdleEvent
& event
);
136 // reset the textctrl parameters
139 // (re)create the textctrl
142 // is the control currently single line?
143 bool IsSingleLine() const
145 return m_radioTextLines
->GetSelection() == TextLines_Single
;
151 // the radiobox to choose between single and multi line
152 wxRadioBox
*m_radioTextLines
;
154 // the checkboxes controlling text ctrl styles
155 wxCheckBox
*m_chkPassword
,
159 // the textctrl itself and the sizer it is in
161 wxSizer
*m_sizerText
;
163 // the information text zones
164 wxTextCtrl
*m_textPosCur
,
172 // and the data to show in them
179 // any class wishing to process wxWindows events must use this macro
180 DECLARE_EVENT_TABLE()
181 DECLARE_WIDGETS_PAGE(TextWidgetsPage
)
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
189 EVT_IDLE(TextWidgetsPage::OnIdle
)
191 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
193 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
194 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
195 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
196 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
197 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
199 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
201 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
202 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
204 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
206 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
207 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
209 EVT_CHECKBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
210 EVT_RADIOBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
213 // ============================================================================
215 // ============================================================================
217 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
219 // ----------------------------------------------------------------------------
220 // TextWidgetsPage creation
221 // ----------------------------------------------------------------------------
223 TextWidgetsPage::TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
)
224 : WidgetsPage(notebook
)
226 imaglist
->Add(wxBitmap(text_xpm
));
229 m_radioTextLines
= (wxRadioBox
*)NULL
;
233 m_chkReadonly
= (wxCheckBox
*)NULL
;
242 m_textSelTo
= (wxTextCtrl
*)NULL
;
243 m_sizerText
= (wxSizer
*)NULL
;
248 m_selTo
= -2; // not -1 which means "no selection"
251 static const wxString modes
[] =
257 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set textctrl parameters"));
258 m_radioTextLines
= new wxRadioBox(this, -1, _T("&Number of lines:"),
259 wxDefaultPosition
, wxDefaultSize
,
260 WXSIZEOF(modes
), modes
,
261 1, wxRA_SPECIFY_COLS
);
263 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
265 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
266 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
268 m_chkPassword
= CreateCheckBoxAndAddToSizer(
269 sizerLeft
, _T("&Password control"), TextPage_Password
271 m_chkWrapLines
= CreateCheckBoxAndAddToSizer(
272 sizerLeft
, _T("Line &wrap"), TextPage_WrapLines
274 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
275 sizerLeft
, _T("&Read-only mode")
278 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
279 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
282 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change contents:"));
283 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
285 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
286 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
288 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
289 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
291 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
292 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
294 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
295 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
297 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
298 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
300 wxStaticBox
*box4
= new wxStaticBox(this, -1, _T("&Info:"));
301 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
303 m_textPosCur
= CreateInfoText();
304 m_textRowCur
= CreateInfoText();
305 m_textColCur
= CreateInfoText();
307 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
308 sizerRow
->Add(CreateTextWithLabelSizer
314 sizerRow
->Add(CreateTextWithLabelSizer
319 0, wxLEFT
| wxRIGHT
, 5);
320 sizerRow
->Add(CreateTextWithLabelSizer
326 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
328 m_textLineLast
= CreateInfoText();
329 m_textPosLast
= CreateInfoText();
332 CreateTextWithLabelSizer
334 _T("Number of lines:"),
336 _T("Last position:"),
342 m_textSelFrom
= CreateInfoText();
343 m_textSelTo
= CreateInfoText();
346 CreateTextWithLabelSizer
348 _T("Selection: from"),
355 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
356 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
357 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
360 wxStaticBox
*box3
= new wxStaticBox(this, -1, _T("&Text:"));
361 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
364 m_sizerText
->SetMinSize(250, 0);
366 // the 3 panes panes compose the upper part of the window
367 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
368 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
369 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
370 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
378 TextWidgetsPage::~TextWidgetsPage()
382 // ----------------------------------------------------------------------------
384 // ----------------------------------------------------------------------------
386 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
388 static int s_maxWidth
= 0;
392 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
395 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
397 wxSize(s_maxWidth
, -1),
402 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
404 const wxString
& label2
,
407 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
408 sizerRow
->Add(new wxStaticText(this, -1, label
), 0,
409 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
410 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
413 sizerRow
->Add(new wxStaticText(this, -1, label2
), 0,
414 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
415 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 void TextWidgetsPage::Reset()
427 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
428 m_chkPassword
->SetValue(DEFAULTS
.password
);
429 m_chkWrapLines
->SetValue(DEFAULTS
.wraplines
);
430 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
433 void TextWidgetsPage::CreateText()
436 switch ( m_radioTextLines
->GetSelection() )
439 wxFAIL_MSG( _T("unexpected radio box selection") );
441 case TextLines_Single
:
444 case TextLines_Multi
:
445 flags
|= wxTE_MULTILINE
;
446 m_chkPassword
->SetValue(FALSE
);
450 if ( m_chkPassword
->GetValue() )
451 flags
|= wxTE_PASSWORD
;
452 if ( m_chkReadonly
->GetValue() )
453 flags
|= wxTE_READONLY
;
454 if ( !m_chkWrapLines
->GetValue() )
460 valueOld
= m_text
->GetValue();
462 m_sizerText
->Remove(m_text
);
467 valueOld
= _T("Hello, Universe!");
470 m_text
= new wxTextCtrl(this, TextPage_Textctrl
,
472 wxDefaultPosition
, wxDefaultSize
,
474 m_sizerText
->Add(m_text
, 1, wxALL
|
475 (flags
& wxTE_MULTILINE
? wxGROW
477 m_sizerText
->Layout();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
486 // update all info texts
490 long posCur
= m_text
->GetInsertionPoint();
491 if ( posCur
!= m_posCur
)
493 m_textPosCur
->Clear();
494 m_textRowCur
->Clear();
495 m_textColCur
->Clear();
498 m_text
->PositionToXY(posCur
, &col
, &row
);
500 *m_textPosCur
<< posCur
;
501 *m_textRowCur
<< row
;
502 *m_textColCur
<< col
;
510 long posLast
= m_text
->GetLastPosition();
511 if ( posLast
!= m_posLast
)
513 m_textPosLast
->Clear();
514 *m_textPosLast
<< posLast
;
520 if ( m_textLineLast
)
522 m_textLineLast
->SetValue(
523 wxString::Format(_T("%ld"), m_text
->GetNumberOfLines()));
526 if ( m_textSelFrom
&& m_textSelTo
)
529 m_text
->GetSelection(&selFrom
, &selTo
);
530 if ( selFrom
!= m_selFrom
)
532 m_textSelFrom
->Clear();
533 *m_textSelFrom
<< selFrom
;
538 if ( selTo
!= m_selTo
)
540 m_textSelTo
->Clear();
541 *m_textSelTo
<< selTo
;
548 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
555 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
557 m_text
->SetValue(_T("Yellow submarine"));
561 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
563 m_text
->AppendText(_T("here, there and everywhere"));
567 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
569 m_text
->WriteText(_T("is there anybody going to listen to my story"));
573 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
579 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
581 // search for the file in several dirs where it's likely to be
583 pathlist
.Add(_T("."));
584 pathlist
.Add(_T(".."));
585 pathlist
.Add(_T("../../../samples/widgets"));
587 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
590 wxLogError(_T("File textctrl.cpp not found."));
595 if ( !m_text
->LoadFile(filename
) )
597 // this is not supposed to happen ...
598 wxLogError(_T("Error loading file."));
602 long elapsed
= sw
.Time();
603 wxLogMessage(_T("Loaded file '%s' in %u.%us"),
604 filename
.c_str(), elapsed
/ 1000, elapsed
% 1000);
609 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
611 event
.Enable(!m_text
->GetValue().empty());
614 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
616 event
.Enable( !IsSingleLine() );
619 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
621 // can't put multiline control in password mode
622 event
.Enable( IsSingleLine() );
625 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
627 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
628 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
629 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
630 (m_chkWrapLines
->GetValue() != DEFAULTS
.wraplines
) );
633 void TextWidgetsPage::OnText(wxCommandEvent
& event
)
635 // small hack to suppress the very first message: by then the logging is
636 // not yet redirected and so initial setting of the text value results in
637 // an annoying message box
638 static bool s_firstTime
= TRUE
;
645 wxLogMessage(_T("Text ctrl value changed"));
648 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
650 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
653 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)