1 \section{\class{wxTextCtrl
}}\label{wxtextctrl
}
3 A text control allows text to be displayed and edited. It may be
4 single line or multi-line.
6 \wxheading{Derived from
}
9 \helpref{wxControl
}{wxcontrol
}\\
10 \helpref{wxWindow
}{wxwindow
}\\
11 \helpref{wxEvtHandler
}{wxevthandler
}\\
12 \helpref{wxObject
}{wxobject
}
14 \wxheading{Include files
}
18 \wxheading{Window styles
}
21 \begin{twocollist
}\itemsep=
0pt
22 \twocolitem{\windowstyle{wxTE
\_PROCESS\_ENTER}}{The control will generate
23 the message wxEVENT
\_TYPE\_TEXT\_ENTER\_COMMAND (otherwise pressing <Enter> is
24 either processed internally by the control or used for navigation between
26 \twocolitem{\windowstyle{wxTE
\_PROCESS\_TAB}}{The control will receieve
27 EVT
\_CHAR messages for TAB pressed - normally, TAB is used for passing to the
28 next control in a dialog instead. For the control created with this style,
29 you can still use Ctrl-Enter to pass to the next control from the keyboard.
}
30 \twocolitem{\windowstyle{wxTE
\_MULTILINE}}{The text control allows multiple lines.
}
31 \twocolitem{\windowstyle{wxTE
\_PASSWORD}}{The text will be echoed as asterisks.
}
32 \twocolitem{\windowstyle{wxTE
\_READONLY}}{The text will not be user-editable.
}
33 \twocolitem{\windowstyle{wxTE
\_RICH}}{Use rich text control under Win32, this
34 allows to have more than
64Kb of text in the control even under Win9x. This
35 style is ignored under other platforms.
}
36 \twocolitem{\windowstyle{wxTE
\_AUTO\_URL}}{Highlight the URLs and generate the
37 wxTextUrlEvents when mouse events occur over them. This style is supported
38 under Win32 only and requires wxTE
\_RICH.
}
39 \twocolitem{\windowstyle{wxTE
\_NOHIDESEL}}{By default, the Windows text control
40 doesn't show the selection when it doesn't have focus - use this style to force
41 it to always show it. It doesn't do anything under other platforms.
}
42 \twocolitem{\windowstyle{wxHSCROLL
}}{A horizontal scrollbar will be created. No effect under GTK+.
}
45 See also
\helpref{window styles overview
}{windowstyles
} and
46 \helpref{wxTextCtrl::wxTextCtrl
}{wxtextctrlconstr
}.
48 \wxheading{wxTextCtrl and C++ streams
}
50 This class multiply-inherits from
{\bf streambuf
} where compilers allow,
51 allowing code such as the following:
55 wxTextCtrl *control = new wxTextCtrl(...);
57 ostream stream(control)
59 stream <<
123.456 << " some text
\n";
64 If your compiler does not support derivation from
{\bf streambuf
} and gives a
65 compile error, define the symbol
{\bf NO
\_TEXT\_WINDOW\_STREAM} in the
66 wxTextCtrl header file.
68 Note that independently of this setting you can always use wxTextCtrl itself
69 in a stream-like manner:
73 wxTextCtrl *control = new wxTextCtrl(...);
75 *control <<
123.456 << " some text
\n";
79 always works. However the possibility to create an ostream associated with
80 wxTextCtrl may be useful if you need to redirect the output of a function
81 taking an ostream as parameter to a text control.
83 Another commonly requested need is to redirect
{\bf std::cout
} to the text
84 control. This could be done in the following way:
90 wxTextCtrl *control = new wxTextCtrl(...);
92 std::streambuf *sbOld = std::cout.rdbuf();
93 std::cout.rdbuf
(*control);
95 // use cout as usual, the output appears in the text control
98 std::cout.rdbuf(sbOld);
102 But wxWindows provides a convenient class to make it even simpler so instead
109 wxTextCtrl *control = new wxTextCtrl(...);
111 wxStreamToTextRedirector redirect(control);
113 // all output to cout goes into the text control until the exit from current
118 See \helpref{wxStreamToTextRedirector}{wxstreamtotextredirector} for more
121 \wxheading{Event handling}
123 The following commands are processed by default event handlers in wxTextCtrl: wxID\_CUT, wxID\_COPY,
124 wxID\_PASTE, wxID\_UNDO, wxID\_REDO. The associated UI update events are also processed
125 automatically, when the control has the focus.
127 To process input from a text control, use these event handler macros to direct input to member
128 functions that take a \helpref{wxCommandEvent}{wxcommandevent} argument.
131 \begin{twocollist}\itemsep=0pt
132 \twocolitem{{\bf EVT\_TEXT(id, func)}}{Respond to a wxEVT\_COMMAND\_TEXT\_UPDATED event,
133 generated when the text changes. Notice that this event will always be sent
134 when the text controls contents changes - whether this is due to user input or
135 comes from the program itself (for example, if SetValue() is called)}
136 \twocolitem{{\bf EVT\_TEXT\_ENTER(id, func)}}{Respond to a wxEVT\_COMMAND\_TEXT\_ENTER event,
137 generated when enter is pressed in a single-line text control.}
138 \twocolitem{{\bf EVT\_TEXT\_URL(id, func)}}{A mouse event occured over an URL
139 in the text control (Win32 only)}
140 \twocolitem{{\bf EVT\_TEXT\_MAXLEN(id, func)}}{User tried to enter more text
141 into the control than the limit set by
142 \helpref{SetMaxLength}{wxtextctrlsetmaxlength}.}
145 \latexignore{\rtfignore{\wxheading{Members}}}
147 \membersection{wxTextCtrl::wxTextCtrl}\label{wxtextctrlconstr}
149 \func{}{wxTextCtrl}{\void}
153 \func{}{wxTextCtrl}{\param{wxWindow* }{parent}, \param{wxWindowID}{ id},\rtfsp
154 \param{const wxString\& }{value = ``"}, \param{const wxPoint\& }{pos}, \param{const wxSize\& }{size = wxDefaultSize},\rtfsp
155 \param{long}{ style = 0}, \param{const wxValidator\& }{validator}, \param{const wxString\& }{name = ``text"}}
157 Constructor, creating and showing a text control.
159 \wxheading{Parameters}
161 \docparam{parent}{Parent window. Should not be NULL.}
163 \docparam{id}{Control identifier. A value of -1 denotes a default value.}
165 \docparam{value}{Default text value.}
167 \docparam{pos}{Text control position.}
169 \docparam{size}{Text control size.}
171 \docparam{style}{Window style. See \helpref{wxTextCtrl}{wxtextctrl}.}
173 \docparam{validator}{Window validator.}
175 \docparam{name}{Window name.}
179 The horizontal scrollbar ({\bf wxTE\_HSCROLL} style flag) will only be created for multi-line text controls.
180 Without a horizontal scrollbar, text lines that don't fit in the control's
181 size will be wrapped (but no newline character is inserted). Single line
182 controls don't have a horizontal scrollbar, the text is automatically scrolled
183 so that the \helpref{insertion point}{wxtextctrlgetinsertionpoint} is always
186 % VZ: this is no longer true
187 %Under Windows, if the {\bf wxTE\_MULTILINE} style is used, the window is implemented
188 %as a Windows rich text control with unlimited capacity. Otherwise, normal edit control limits
193 \helpref{wxTextCtrl::Create}{wxtextctrlcreate}, \helpref{wxValidator}{wxvalidator}
195 \membersection{wxTextCtrl::\destruct{wxTextCtrl}}
197 \func{}{\destruct{wxTextCtrl}}{\void}
199 Destructor, destroying the text control.
201 \membersection{wxTextCtrl::AppendText}\label{wxtextctrlappendtext}
203 \func{void}{AppendText}{\param{const wxString\& }{ text}}
205 Appends the text to the end of the text control.
207 \wxheading{Parameters}
209 \docparam{text}{Text to write to the text control.}
213 After the text is appended, the insertion point will be at the end of the text control. If this behaviour is not desired,
214 the programmer should use \helpref{GetInsertionPoint}{wxtextctrlgetinsertionpoint} and \helpref{SetInsertionPoint}{wxtextctrlsetinsertionpoint}.
218 \helpref{wxTextCtrl::WriteText}{wxtextctrlwritetext}
220 \membersection{wxTextCtrl::CanCopy}\label{wxtextctrlcancopy}
222 \func{virtual bool}{CanCopy}{\void}
224 Returns TRUE if the selection can be copied to the clipboard.
226 \membersection{wxTextCtrl::CanCut}\label{wxtextctrlcancut}
228 \func{virtual bool}{CanCut}{\void}
230 Returns TRUE if the selection can be cut to the clipboard.
232 \membersection{wxTextCtrl::CanPaste}\label{wxtextctrlcanpaste}
234 \func{virtual bool}{CanPaste}{\void}
236 Returns TRUE if the contents of the clipboard can be pasted into the
237 text control. On some platforms (Motif, GTK) this is an approximation
238 and returns TRUE if the control is editable, FALSE otherwise.
240 \membersection{wxTextCtrl::CanRedo}\label{wxtextctrlcanredo}
242 \func{virtual bool}{CanRedo}{\void}
244 Returns TRUE if there is a redo facility available and the last operation
247 \membersection{wxTextCtrl::CanUndo}\label{wxtextctrlcanundo}
249 \func{virtual bool}{CanUndo}{\void}
251 Returns TRUE if there is an undo facility available and the last operation
254 \membersection{wxTextCtrl::Clear}\label{wxtextctrlclear}
256 \func{virtual void}{Clear}{\void}
258 Clears the text in the control.
260 \membersection{wxTextCtrl::Copy}\label{wxtextctrlcopy}
262 \func{virtual void}{Copy}{\void}
264 Copies the selected text to the clipboard under Motif and MS Windows.
266 \membersection{wxTextCtrl::Create}\label{wxtextctrlcreate}
268 \func{bool}{Create}{\param{wxWindow* }{parent}, \param{wxWindowID}{ id},\rtfsp
269 \param{const wxString\& }{value = ``"}, \param{const wxPoint\& }{pos}, \param{const wxSize\& }{size = wxDefaultSize},\rtfsp
270 \param{long}{ style = 0}, \param{const wxValidator\& }{validator}, \param{const wxString\& }{name = ``text"}}
272 Creates the text control for two-step construction. Derived classes
273 should call or replace this function. See \helpref{wxTextCtrl::wxTextCtrl}{wxtextctrlconstr}\rtfsp
276 \membersection{wxTextCtrl::Cut}\label{wxtextctrlcut}
278 \func{virtual void}{Cut}{\void}
280 Copies the selected text to the clipboard and removes the selection.
282 \membersection{wxTextCtrl::DiscardEdits}
284 \func{void}{DiscardEdits}{\void}
286 Resets the internal `modified' flag as if the current edits had been saved.
288 \membersection{wxTextCtrl::GetInsertionPoint}\label{wxtextctrlgetinsertionpoint}
290 \constfunc{virtual long}{GetInsertionPoint}{\void}
292 Returns the insertion point. This is defined as the zero based index of the
293 character position to the right of the insertion point. For example, if
294 the insertion point is at the end of the text control, it is equal to
295 both \helpref{GetValue()}{wxtextctrlgetvalue}.Length() and
296 \helpref{GetLastPosition()}{wxtextctrlgetlastposition}.
298 The following code snippet safely returns the character at the insertion
299 point or the zero character if the point is at the end of the control.
303 char GetCurrentChar(wxTextCtrl *tc) {
304 if (tc->GetInsertionPoint() == tc->GetLastPosition())
306 return tc->GetValue[tc->GetInsertionPoint()];
311 \membersection{wxTextCtrl::GetLastPosition}\label{wxtextctrlgetlastposition}
313 \constfunc{virtual long}{GetLastPosition}{\void}
315 Returns the zero based index of the last position in the text control,
316 which is equal to the number of characters in the control.
318 \membersection{wxTextCtrl::GetLineLength}\label{wxtextctrlgetlinelength}
320 \constfunc{int}{GetLineLength}{\param{long}{ lineNo}}
322 Gets the length of the specified line, not including any trailing newline
325 \wxheading{Parameters}
327 \docparam{lineNo}{Line number (starting from zero).}
329 \wxheading{Return value}
331 The length of the line, or -1 if {\it lineNo} was invalid.
333 \membersection{wxTextCtrl::GetLineText}\label{wxtextctrlgetlinetext}
335 \constfunc{wxString}{GetLineText}{\param{long}{ lineNo}}
337 Returns the contents of a given line in the text control, not including
338 any trailing newline character(s).
340 \wxheading{Parameters}
342 \docparam{lineNo}{The line number, starting from zero.}
344 \wxheading{Return value}
346 The contents of the line.
348 \membersection{wxTextCtrl::GetNumberOfLines}\label{wxtextctrlgetnumberoflines}
350 \constfunc{int}{GetNumberOfLines}{\void}
352 Returns the number of lines in the text control buffer.
356 Note that even empty text controls have one line (where the insertion point
357 is), so GetNumberOfLines() never returns 0.
359 For gtk\_text (multi-line) controls, the number of lines is
360 calculated by actually counting newline characters in the buffer. You
361 may wish to avoid using functions that work with line numbers if you are
362 working with controls that contain large amounts of text.
364 \membersection{wxTextCtrl::GetSelection}\label{wxtextctrlgetselection}
366 \func{virtual void}{GetSelection}{\param{long*}{ from}, \param{long*}{ to}}
368 Gets the current selection span. If the returned values are equal, there was
371 Please note that the indices returned may be used with the other wxTextctrl
372 methods but don't necessarily represent the correct indices into the string
373 returned by \helpref{GetValue()}{wxtextctrlgetvalue} for multiline controls
374 under Windows (at least,) you should use
375 \helpref{GetStringSelection()}{wxtextctrlgetstringselection} to get the selected
378 \wxheading{Parameters}
380 \docparam{from}{The returned first position.}
382 \docparam{to}{The returned last position.}
384 \pythonnote{The wxPython version of this method returns a tuple
385 consisting of the from and to values.}
387 \perlnote{In wxPerl this method takes no parameter and returns a
388 2-element list {\tt ( from, to )}.}
390 \membersection{wxTextCtrl::GetStringSelection}\label{wxtextctrlgetstringselection}
392 \func{virtual wxString}{GetStringSelection}{\void}
394 Gets the text currently selected in the control. If there is no selection, the
395 returned string is empty.
397 \membersection{wxTextCtrl::GetValue}\label{wxtextctrlgetvalue}
399 \constfunc{wxString}{GetValue}{\void}
401 Gets the contents of the control. Notice that for a multiline text control,
402 the lines will be separated by (Unix-style) $\backslash$n characters, even under
403 Windows where they are separated by a $\backslash$r$\backslash$n sequence in the native control.
405 \membersection{wxTextCtrl::IsModified}\label{wxtextctrlismodified}
407 \constfunc{bool}{IsModified}{\void}
409 Returns TRUE if the text has been modified.
411 \membersection{wxTextCtrl::LoadFile}\label{wxtextctrlloadfile}
413 \func{bool}{LoadFile}{\param{const wxString\& }{ filename}}
415 Loads and displays the named file, if it exists.
417 \wxheading{Parameters}
419 \docparam{filename}{The filename of the file to load.}
421 \wxheading{Return value}
423 TRUE if successful, FALSE otherwise.
425 \membersection{wxTextCtrl::OnChar}\label{wxtextctrlonchar}
427 \func{void}{OnChar}{\param{wxKeyEvent\& }{event}}
429 Default handler for character input.
433 It is possible to intercept character
434 input by overriding this member. Call this function
435 to let the default behaviour take place; not calling
436 it results in the character being ignored. You can
437 replace the {\it keyCode} member of {\it event} to
438 translate keystrokes.
440 Note that Windows and Motif have different ways
441 of implementing the default behaviour. In Windows,
442 calling wxTextCtrl::OnChar immediately
443 processes the character. In Motif,
444 calling this function simply sets a flag
445 to let default processing happen. This might affect
446 the way in which you write your OnChar function
447 on different platforms.
451 \helpref{wxKeyEvent}{wxkeyevent}
453 \membersection{wxTextCtrl::OnDropFiles}\label{wxtextctrlondropfiles}
455 \func{void}{OnDropFiles}{\param{wxDropFilesEvent\& }{event}}
457 This event handler function implements default drag and drop behaviour, which
458 is to load the first dropped file into the control.
460 \wxheading{Parameters}
462 \docparam{event}{The drop files event.}
466 This is not implemented on non-Windows platforms.
470 \helpref{wxDropFilesEvent}{wxdropfilesevent}
472 \membersection{wxTextCtrl::Paste}\label{wxtextctrlpaste}
474 \func{virtual void}{Paste}{\void}
476 Pastes text from the clipboard to the text item.
478 \membersection{wxTextCtrl::PositionToXY}\label{wxtextctrlpositiontoxy}
480 \constfunc{bool}{PositionToXY}{\param{long }{pos}, \param{long *}{x}, \param{long *}{y}}
482 Converts given position to a zero-based column, line number pair.
484 \wxheading{Parameters}
486 \docparam{pos}{Position.}
488 \docparam{x}{Receives zero based column number.}
490 \docparam{y}{Receives zero based line number.}
492 \wxheading{Return value}
494 TRUE on success, FALSE on failure (most likely due to a too large position
499 \helpref{wxTextCtrl::XYToPosition}{wxtextctrlxytoposition}
501 \pythonnote{In Python, PositionToXY() returns a tuple containing the x and
502 y values, so (x,y) = PositionToXY() is equivalent to the call described
505 \perlnote{In wxPerl this method only takes the {\tt pos} parameter, and
506 returns a 2-element list {\tt ( x, y )}.}
508 \membersection{wxTextCtrl::Redo}\label{wxtextctrlredo}
510 \func{virtual void}{Redo}{\void}
512 If there is a redo facility and the last operation can be redone, redoes the last operation. Does nothing
513 if there is no redo facility.
515 \membersection{wxTextCtrl::Remove}\label{wxtextctrlremove}
517 \func{virtual void}{Remove}{\param{long}{ from}, \param{long}{ to}}
519 Removes the text starting at the first given position up to (but not including)
520 the character at the last position.
522 \wxheading{Parameters}
524 \docparam{from}{The first position.}
526 \docparam{to}{The last position.}
528 \membersection{wxTextCtrl::Replace}\label{wxtextctrlreplace}
530 \func{virtual void}{Replace}{\param{long}{ from}, \param{long}{ to}, \param{const wxString\& }{value}}
532 Replaces the text starting at the first position up to (but not including)
533 the character at the last position with the given text.
535 \wxheading{Parameters}
537 \docparam{from}{The first position.}
539 \docparam{to}{The last position.}
541 \docparam{value}{The value to replace the existing text with.}
543 \membersection{wxTextCtrl::SaveFile}\label{wxtextctrlsavefile}
545 \func{bool}{SaveFile}{\param{const wxString\& }{ filename}}
547 Saves the contents of the control in a text file.
549 \wxheading{Parameters}
551 \docparam{filename}{The name of the file in which to save the text.}
553 \wxheading{Return value}
555 TRUE if the operation was successful, FALSE otherwise.
557 \membersection{wxTextCtrl::SetEditable}\label{wxtextctrlseteditable}
559 \func{virtual void}{SetEditable}{\param{const bool}{ editable}}
561 Makes the text item editable or read-only, overriding the {\bf wxTE\_READONLY} flag.
563 \wxheading{Parameters}
565 \docparam{editable}{If TRUE, the control is editable. If FALSE, the control is read-only.}
567 \membersection{wxTextCtrl::SetInsertionPoint}\label{wxtextctrlsetinsertionpoint}
569 \func{virtual void}{SetInsertionPoint}{\param{long}{ pos}}
571 Sets the insertion point at the given position.
573 \wxheading{Parameters}
575 \docparam{pos}{Position to set.}
577 \membersection{wxTextCtrl::SetInsertionPointEnd}\label{wxtextctrlsetinsertionpointend}
579 \func{virtual void}{SetInsertionPointEnd}{\void}
581 Sets the insertion point at the end of the text control. This is equivalent
582 to \helpref{SetInsertionPoint}{wxtextctrlsetinsertionpoint}(\helpref{GetLastPosition}{wxtextctrlgetlastposition}()).
584 \membersection{wxTextCtrl::SetMaxLength}\label{wxtextctrlsetmaxlength}
586 \func{virtual void}{SetMaxLength}{\param{unsigned long }{len}}
588 This function sets the maximum number of characters the user can enter into the
589 control. In other words, it allows to limit the text value length to {\it len}
590 not counting the terminating {\tt NUL} character.
592 If {\it len} is $0$, the previously set max length limi, if any, is discarded
593 and the user may enter as much text as the underlying native text control
594 widget supports (typically at least 32Kb).
596 If the user tries to enter more characters into the text control when it
597 already is filled up to the maximal length, a
598 {\tt wxEVT\_COMMAND\_TEXT\_MAXLEN} event is sent to notify the program about it
599 (giving it the possibility to show an explanatory message, for example) and the
600 extra input is discarded.
602 Note that this function may only be used with single line text controls.
604 \wxheading{Compatibility}
606 Only implemented in wxMSW/wxGTK starting with wxWindows 2.3.2.
608 \membersection{wxTextCtrl::SetSelection}\label{wxtextctrlsetselection}
610 \func{virtual void}{SetSelection}{\param{long}{ from}, \param{long}{ to}}
612 Selects the text starting at the first position up to (but not including) the character at the last position.
614 \wxheading{Parameters}
616 \docparam{from}{The first position.}
618 \docparam{to}{The last position.}
620 \membersection{wxTextCtrl::SetValue}\label{wxtextctrlsetvalue}
622 \func{virtual void}{SetValue}{\param{const wxString\& }{ value}}
624 Sets the text value and marks the control as not-modified.
626 \wxheading{Parameters}
628 \docparam{value}{The new value to set. It may contain newline characters if the text control is multi-line.}
630 \membersection{wxTextCtrl::ShowPosition}\label{wxtextctrlshowposition}
632 \func{void}{ShowPosition}{\param{long}{ pos}}
634 Makes the line containing the given position visible.
636 \wxheading{Parameters}
638 \docparam{pos}{The position that should be visible.}
640 \membersection{wxTextCtrl::Undo}\label{wxtextctrlundo}
642 \func{virtual void}{Undo}{\void}
644 If there is an undo facility and the last operation can be undone, undoes the last operation. Does nothing
645 if there is no undo facility.
647 \membersection{wxTextCtrl::WriteText}\label{wxtextctrlwritetext}
649 \func{void}{WriteText}{\param{const wxString\& }{ text}}
651 Writes the text into the text control at the current insertion position.
653 \wxheading{Parameters}
655 \docparam{text}{Text to write to the text control.}
659 Newlines in the text string
660 are the only control characters allowed, and they will cause appropriate
661 line breaks. See \helpref{wxTextCtrl::\cinsert}{wxtextctrlinsert} and \helpref{wxTextCtrl::AppendText}{wxtextctrlappendtext} for more convenient ways of writing to the window.
663 After the write operation, the insertion point will be at the end of the inserted text, so subsequent write operations will be appended. To append text after the user may have interacted with the control, call \helpref{wxTextCtrl::SetInsertionPointEnd}{wxtextctrlsetinsertionpointend} before writing.
665 \membersection{wxTextCtrl::XYToPosition}\label{wxtextctrlxytoposition}
667 \func{long}{XYToPosition}{\param{long}{ x}, \param{long}{ y}}
669 Converts the given zero based column and line number to a position.
671 \wxheading{Parameters}
673 \docparam{x}{The column number.}
675 \docparam{y}{The line number.}
677 \wxheading{Return value}
681 \membersection{wxTextCtrl::operator \cinsert}\label{wxtextctrlinsert}
683 \func{wxTextCtrl\&}{operator \cinsert}{\param{const wxString\& }{s}}
685 \func{wxTextCtrl\&}{operator \cinsert}{\param{int}{ i}}
687 \func{wxTextCtrl\&}{operator \cinsert}{\param{long}{ i}}
689 \func{wxTextCtrl\&}{operator \cinsert}{\param{float}{ f}}
691 \func{wxTextCtrl\&}{operator \cinsert}{\param{double}{ d}}
693 \func{wxTextCtrl\&}{operator \cinsert}{\param{char}{ c}}
695 Operator definitions for appending to a text control, for example:
698 wxTextCtrl *wnd = new wxTextCtrl(my_frame);
700 (*wnd) << "Welcome to text control number " << 1 << ".\n";