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();
182 DECLARE_WIDGETS_PAGE(TextWidgetsPage
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 BEGIN_EVENT_TABLE(TextWidgetsPage
, WidgetsPage
)
190 EVT_IDLE(TextWidgetsPage::OnIdle
)
192 EVT_BUTTON(TextPage_Reset
, TextWidgetsPage::OnButtonReset
)
194 EVT_BUTTON(TextPage_Clear
, TextWidgetsPage::OnButtonClear
)
195 EVT_BUTTON(TextPage_Set
, TextWidgetsPage::OnButtonSet
)
196 EVT_BUTTON(TextPage_Add
, TextWidgetsPage::OnButtonAdd
)
197 EVT_BUTTON(TextPage_Insert
, TextWidgetsPage::OnButtonInsert
)
198 EVT_BUTTON(TextPage_Load
, TextWidgetsPage::OnButtonLoad
)
200 EVT_UPDATE_UI(TextPage_Clear
, TextWidgetsPage::OnUpdateUIClearButton
)
202 EVT_UPDATE_UI(TextPage_Password
, TextWidgetsPage::OnUpdateUIPasswordCheckbox
)
203 EVT_UPDATE_UI(TextPage_WrapLines
, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox
)
205 EVT_UPDATE_UI(TextPage_Reset
, TextWidgetsPage::OnUpdateUIResetButton
)
207 EVT_TEXT(TextPage_Textctrl
, TextWidgetsPage::OnText
)
208 EVT_TEXT_ENTER(TextPage_Textctrl
, TextWidgetsPage::OnTextEnter
)
210 EVT_CHECKBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
211 EVT_RADIOBOX(-1, TextWidgetsPage::OnCheckOrRadioBox
)
214 // ============================================================================
216 // ============================================================================
218 IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage
, _T("Text"));
220 // ----------------------------------------------------------------------------
221 // TextWidgetsPage creation
222 // ----------------------------------------------------------------------------
224 TextWidgetsPage::TextWidgetsPage(wxNotebook
*notebook
, wxImageList
*imaglist
)
225 : WidgetsPage(notebook
)
227 imaglist
->Add(wxBitmap(text_xpm
));
230 m_radioTextLines
= (wxRadioBox
*)NULL
;
234 m_chkReadonly
= (wxCheckBox
*)NULL
;
243 m_textSelTo
= (wxTextCtrl
*)NULL
;
244 m_sizerText
= (wxSizer
*)NULL
;
249 m_selTo
= -2; // not -1 which means "no selection"
252 static const wxString modes
[] =
258 wxStaticBox
*box
= new wxStaticBox(this, -1, _T("&Set textctrl parameters"));
259 m_radioTextLines
= new wxRadioBox(this, -1, _T("&Number of lines:"),
260 wxDefaultPosition
, wxDefaultSize
,
261 WXSIZEOF(modes
), modes
,
262 1, wxRA_SPECIFY_COLS
);
264 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
266 sizerLeft
->Add(m_radioTextLines
, 0, wxGROW
| wxALL
, 5);
267 sizerLeft
->Add(5, 5, 0, wxGROW
| wxALL
, 5); // spacer
269 m_chkPassword
= CreateCheckBoxAndAddToSizer(
270 sizerLeft
, _T("&Password control"), TextPage_Password
272 m_chkWrapLines
= CreateCheckBoxAndAddToSizer(
273 sizerLeft
, _T("Line &wrap"), TextPage_WrapLines
275 m_chkReadonly
= CreateCheckBoxAndAddToSizer(
276 sizerLeft
, _T("&Read-only mode")
279 wxButton
*btn
= new wxButton(this, TextPage_Reset
, _T("&Reset"));
280 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
283 wxStaticBox
*box2
= new wxStaticBox(this, -1, _T("&Change contents:"));
284 wxSizer
*sizerMiddleUp
= new wxStaticBoxSizer(box2
, wxVERTICAL
);
286 btn
= new wxButton(this, TextPage_Set
, _T("&Set text value"));
287 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
289 btn
= new wxButton(this, TextPage_Add
, _T("&Append text"));
290 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
292 btn
= new wxButton(this, TextPage_Insert
, _T("&Insert text"));
293 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
295 btn
= new wxButton(this, TextPage_Load
, _T("&Load file"));
296 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
298 btn
= new wxButton(this, TextPage_Clear
, _T("&Clear"));
299 sizerMiddleUp
->Add(btn
, 0, wxALL
| wxGROW
, 5);
301 wxStaticBox
*box4
= new wxStaticBox(this, -1, _T("&Info:"));
302 wxSizer
*sizerMiddleDown
= new wxStaticBoxSizer(box4
, wxVERTICAL
);
304 m_textPosCur
= CreateInfoText();
305 m_textRowCur
= CreateInfoText();
306 m_textColCur
= CreateInfoText();
308 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
309 sizerRow
->Add(CreateTextWithLabelSizer
315 sizerRow
->Add(CreateTextWithLabelSizer
320 0, wxLEFT
| wxRIGHT
, 5);
321 sizerRow
->Add(CreateTextWithLabelSizer
327 sizerMiddleDown
->Add(sizerRow
, 0, wxALL
, 5);
329 m_textLineLast
= CreateInfoText();
330 m_textPosLast
= CreateInfoText();
333 CreateTextWithLabelSizer
335 _T("Number of lines:"),
337 _T("Last position:"),
343 m_textSelFrom
= CreateInfoText();
344 m_textSelTo
= CreateInfoText();
347 CreateTextWithLabelSizer
349 _T("Selection: from"),
356 wxSizer
*sizerMiddle
= new wxBoxSizer(wxVERTICAL
);
357 sizerMiddle
->Add(sizerMiddleUp
, 0, wxGROW
);
358 sizerMiddle
->Add(sizerMiddleDown
, 1, wxGROW
| wxTOP
, 5);
361 wxStaticBox
*box3
= new wxStaticBox(this, -1, _T("&Text:"));
362 m_sizerText
= new wxStaticBoxSizer(box3
, wxHORIZONTAL
);
365 m_sizerText
->SetMinSize(250, 0);
367 // the 3 panes panes compose the upper part of the window
368 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
369 sizerTop
->Add(sizerLeft
, 0, wxGROW
| (wxALL
& ~wxLEFT
), 10);
370 sizerTop
->Add(sizerMiddle
, 0, wxGROW
| wxALL
, 10);
371 sizerTop
->Add(m_sizerText
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
379 TextWidgetsPage::~TextWidgetsPage()
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 wxTextCtrl
*TextWidgetsPage::CreateInfoText()
389 static int s_maxWidth
= 0;
393 GetTextExtent(_T("9999999"), &s_maxWidth
, NULL
);
396 wxTextCtrl
*text
= new wxTextCtrl(this, -1, _T(""),
398 wxSize(s_maxWidth
, -1),
403 wxSizer
*TextWidgetsPage::CreateTextWithLabelSizer(const wxString
& label
,
405 const wxString
& label2
,
408 wxSizer
*sizerRow
= new wxBoxSizer(wxHORIZONTAL
);
409 sizerRow
->Add(new wxStaticText(this, -1, label
), 0,
410 wxALIGN_CENTRE_VERTICAL
| wxRIGHT
, 5);
411 sizerRow
->Add(text
, 0, wxALIGN_CENTRE_VERTICAL
);
414 sizerRow
->Add(new wxStaticText(this, -1, label2
), 0,
415 wxALIGN_CENTRE_VERTICAL
| wxLEFT
| wxRIGHT
, 5);
416 sizerRow
->Add(text2
, 0, wxALIGN_CENTRE_VERTICAL
);
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 void TextWidgetsPage::Reset()
428 m_radioTextLines
->SetSelection(DEFAULTS
.textLines
);
429 m_chkPassword
->SetValue(DEFAULTS
.password
);
430 m_chkWrapLines
->SetValue(DEFAULTS
.wraplines
);
431 m_chkReadonly
->SetValue(DEFAULTS
.readonly
);
434 void TextWidgetsPage::CreateText()
437 switch ( m_radioTextLines
->GetSelection() )
440 wxFAIL_MSG( _T("unexpected radio box selection") );
442 case TextLines_Single
:
445 case TextLines_Multi
:
446 flags
|= wxTE_MULTILINE
;
447 m_chkPassword
->SetValue(FALSE
);
451 if ( m_chkPassword
->GetValue() )
452 flags
|= wxTE_PASSWORD
;
453 if ( m_chkReadonly
->GetValue() )
454 flags
|= wxTE_READONLY
;
455 if ( !m_chkWrapLines
->GetValue() )
461 valueOld
= m_text
->GetValue();
463 m_sizerText
->Remove(m_text
);
468 valueOld
= _T("Hello, Universe!");
471 m_text
= new wxTextCtrl(this, TextPage_Textctrl
,
473 wxDefaultPosition
, wxDefaultSize
,
475 m_sizerText
->Add(m_text
, 1, wxALL
|
476 (flags
& wxTE_MULTILINE
? wxGROW
478 m_sizerText
->Layout();
481 // ----------------------------------------------------------------------------
483 // ----------------------------------------------------------------------------
485 void TextWidgetsPage::OnIdle(wxIdleEvent
& WXUNUSED(event
))
487 // update all info texts
491 long posCur
= m_text
->GetInsertionPoint();
492 if ( posCur
!= m_posCur
)
494 m_textPosCur
->Clear();
495 m_textRowCur
->Clear();
496 m_textColCur
->Clear();
499 m_text
->PositionToXY(posCur
, &col
, &row
);
501 *m_textPosCur
<< posCur
;
502 *m_textRowCur
<< row
;
503 *m_textColCur
<< col
;
511 long posLast
= m_text
->GetLastPosition();
512 if ( posLast
!= m_posLast
)
514 m_textPosLast
->Clear();
515 *m_textPosLast
<< posLast
;
521 if ( m_textLineLast
)
523 m_textLineLast
->SetValue(
524 wxString::Format(_T("%ld"), m_text
->GetNumberOfLines()));
527 if ( m_textSelFrom
&& m_textSelTo
)
530 m_text
->GetSelection(&selFrom
, &selTo
);
531 if ( selFrom
!= m_selFrom
)
533 m_textSelFrom
->Clear();
534 *m_textSelFrom
<< selFrom
;
539 if ( selTo
!= m_selTo
)
541 m_textSelTo
->Clear();
542 *m_textSelTo
<< selTo
;
549 void TextWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
556 void TextWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
558 m_text
->SetValue(_T("Yellow submarine"));
562 void TextWidgetsPage::OnButtonAdd(wxCommandEvent
& WXUNUSED(event
))
564 m_text
->AppendText(_T("here, there and everywhere"));
568 void TextWidgetsPage::OnButtonInsert(wxCommandEvent
& WXUNUSED(event
))
570 m_text
->WriteText(_T("is there anybody going to listen to my story"));
574 void TextWidgetsPage::OnButtonClear(wxCommandEvent
& WXUNUSED(event
))
580 void TextWidgetsPage::OnButtonLoad(wxCommandEvent
& WXUNUSED(event
))
582 // search for the file in several dirs where it's likely to be
584 pathlist
.Add(_T("."));
585 pathlist
.Add(_T(".."));
586 pathlist
.Add(_T("../../../samples/widgets"));
588 wxString filename
= pathlist
.FindValidPath(_T("textctrl.cpp"));
591 wxLogError(_T("File textctrl.cpp not found."));
596 if ( !m_text
->LoadFile(filename
) )
598 // this is not supposed to happen ...
599 wxLogError(_T("Error loading file."));
603 long elapsed
= sw
.Time();
604 wxLogMessage(_T("Loaded file '%s' in %u.%us"),
605 filename
.c_str(), elapsed
/ 1000, elapsed
% 1000);
610 void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent
& event
)
612 event
.Enable(!m_text
->GetValue().empty());
615 void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent
& event
)
617 event
.Enable( !IsSingleLine() );
620 void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent
& event
)
622 // can't put multiline control in password mode
623 event
.Enable( IsSingleLine() );
626 void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent
& event
)
628 event
.Enable( (m_radioTextLines
->GetSelection() != DEFAULTS
.textLines
) ||
629 (m_chkReadonly
->GetValue() != DEFAULTS
.readonly
) ||
630 (m_chkPassword
->GetValue() != DEFAULTS
.password
) ||
631 (m_chkWrapLines
->GetValue() != DEFAULTS
.wraplines
) );
634 void TextWidgetsPage::OnText(wxCommandEvent
& event
)
636 // small hack to suppress the very first message: by then the logging is
637 // not yet redirected and so initial setting of the text value results in
638 // an annoying message box
639 static bool s_firstTime
= TRUE
;
646 wxLogMessage(_T("Text ctrl value changed"));
649 void TextWidgetsPage::OnTextEnter(wxCommandEvent
& event
)
651 wxLogMessage(_T("Text entered: '%s'"), event
.GetString().c_str());
654 void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent
& event
)