]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/text.tex
11ca136d6442593b4905b37c0a0574b888f501eb
[wxWidgets.git] / docs / latex / wx / text.tex
1 \section{\class{wxTextCtrl}}\label{wxtextctrl}
2
3 A text control allows text to be displayed and edited. It may be
4 single line or multi-line.
5
6 \wxheading{Derived from}
7
8 streambuf\\
9 \helpref{wxControl}{wxcontrol}\\
10 \helpref{wxWindow}{wxwindow}\\
11 \helpref{wxEvtHandler}{wxevthandler}\\
12 \helpref{wxObject}{wxobject}
13
14 \wxheading{Window styles}
15
16 \twocolwidtha{5cm}
17 \begin{twocollist}\itemsep=0pt
18 \twocolitem{\windowstyle{wxTE\_PROCESS\_ENTER}}{The callback function will
19 receive the message wxEVENT\_TYPE\_TEXT\_ENTER\_COMMAND. Note
20 that this will break tab traversal for this panel item under
21 Windows.}
22 \twocolitem{\windowstyle{wxTE\_MULTILINE}}{The text control allows multiple lines.}
23 \twocolitem{\windowstyle{wxTE\_PASSWORD}}{The text will be echoed as asterisks.}
24 \twocolitem{\windowstyle{wxTE\_READONLY}}{The text will not be user-editable.}
25 \twocolitem{\windowstyle{wxHSCROLL}}{A horizontal scrollbar will be created.}
26 \end{twocollist}
27
28 See also \helpref{window styles overview}{windowstyles} and
29 \helpref{wxTextCtrl::wxTextCtrl}{wxtextctrlconstr}.
30
31 \wxheading{Remarks}
32
33 This class multiply-inherits from {\bf streambuf} where compilers allow, allowing code such as the following:
34
35 {\small%
36 \begin{verbatim}
37 wxTextCtrl *control = new wxTextCtrl(...);
38
39 ostream stream(control)
40
41 stream << 123.456 << " some text\n";
42 stream.flush();
43 \end{verbatim}
44 }%
45
46 If your compiler does not support derivation from {\bf streambuf} and gives a compile error, define the symbol {\bf NO\_TEXT\_WINDOW\_STREAM} in the
47 wxTextCtrl header file.
48
49 \wxheading{Event handling}
50
51 To process input from a text control, use these event handler macros to direct input to member
52 functions that take a \helpref{wxCommandEvent}{wxcommandevent} argument.
53
54 \twocolwidtha{7cm}%
55 \begin{twocollist}\itemsep=0pt
56 \twocolitem{{\bf EVT\_TEXT(id, func)}}{Respond to a wxEVT\_COMMAND\_TEXT\_UPDATED event,
57 generated when the text changes.}
58 \twocolitem{{\bf EVT\_TEXT\_ENTER(id, func)}}{Respond to a wxEVT\_COMMAND\_TEXT\_ENTER event,
59 generated when enter is pressed in a single-line text control.}
60 \end{twocollist}%
61
62 %\wxheading{See also}
63 %
64 %\helpref{wxRichTextCtrl}{wxrichtextctrl}
65 %
66 \latexignore{\rtfignore{\wxheading{Members}}}
67
68 \membersection{wxTextCtrl::wxTextCtrl}\label{wxtextctrlconstr}
69
70 \func{}{wxTextCtrl}{\void}
71
72 Default constructor.
73
74 \func{}{wxTextCtrl}{\param{wxWindow* }{parent}, \param{wxWindowID}{ id},\rtfsp
75 \param{const wxString\& }{value = ``"}, \param{const wxPoint\& }{pos}, \param{const wxSize\& }{size = wxDefaultSize},\rtfsp
76 \param{long}{ style = 0}, \param{const wxValidator\& }{validator}, \param{const wxString\& }{name = ``text"}}
77
78 Constructor, creating and showing a text control.
79
80 \wxheading{Parameters}
81
82 \docparam{parent}{Parent window. Should not be NULL.}
83
84 \docparam{id}{Control identifier. A value of -1 denotes a default value.}
85
86 \docparam{value}{Default text value.}
87
88 \docparam{pos}{Text control position.}
89
90 \docparam{size}{Text control size.}
91
92 \docparam{style}{Window style. See \helpref{wxTextCtrl}{wxtextctrl}.}
93
94 \docparam{validator}{Window validator.}
95
96 \docparam{name}{Window name.}
97
98 \wxheading{Remarks}
99
100 The horizontal scrollbar ({\bf wxTE\_HSCROLL} style flag) will only be created for multi-line text controls.
101 Without a horizontal scrollbar, text lines that don't fit in the control's
102 size will be wrapped (but no newline character is inserted). Single line
103 controls don't have a horizontal scrollbar, the text is automatically scrolled
104 so that the \helpref{insertion point}{wxtextctrlgetinsertionpoint} is always
105 visible.
106
107 Under Windows, if the {\bf wxTE\_MULTILINE} style is used, the window is implemented
108 as a Windows rich text control with unlimited capacity. Otherwise, normal edit control limits
109 apply.
110
111 For the GTK, the single line text control is implemented as a gtk\_entry,
112 while the multi-line control is a gtk\_text.
113
114 \wxheading{See also}
115
116 \helpref{wxTextCtrl::Create}{wxtextctrlcreate}, \helpref{wxValidator}{wxvalidator}
117
118 \membersection{wxTextCtrl::\destruct{wxTextCtrl}}
119
120 \func{}{\destruct{wxTextCtrl}}{\void}
121
122 Destructor, destroying the text control.
123
124 \membersection{wxTextCtrl::Clear}\label{wxtextctrlclear}
125
126 \func{virtual void}{Clear}{\void}
127
128 Clears the text in the control.
129
130 \membersection{wxTextCtrl::Copy}\label{wxtextctrlcopy}
131
132 \func{virtual void}{Copy}{\void}
133
134 Copies the selected text to the clipboard under Motif and MS Windows.
135
136 \membersection{wxTextCtrl::Create}\label{wxtextctrlcreate}
137
138 \func{bool}{Create}{\param{wxWindow* }{parent}, \param{wxWindowID}{ id},\rtfsp
139 \param{const wxString\& }{value = ``"}, \param{const wxPoint\& }{pos}, \param{const wxSize\& }{size = wxDefaultSize},\rtfsp
140 \param{long}{ style = 0}, \param{const wxValidator\& }{validator}, \param{const wxString\& }{name = ``text"}}
141
142 Creates the text control for two-step construction. Derived classes
143 should call or replace this function. See \helpref{wxTextCtrl::wxTextCtrl}{wxtextctrlconstr}\rtfsp
144 for further details.
145
146 \membersection{wxTextCtrl::Cut}\label{wxtextctrlcut}
147
148 \func{virtual void}{Cut}{\void}
149
150 Copies the selected text to the clipboard and removes the selection.
151
152 \membersection{wxTextCtrl::DiscardEdits}
153
154 \func{void}{DiscardEdits}{\void}
155
156 Resets the internal `modified' flag as if the current edits had been saved.
157
158 \membersection{wxTextCtrl::GetInsertionPoint}\label{wxtextctrlgetinsertionpoint}
159
160 \constfunc{virtual long}{GetInsertionPoint}{\void}
161
162 Returns the insertion point. This is defined as the zero based index of the
163 character position to the right of the insertion point. For example, if
164 the insertion point is at the end of the text control, it is equal to
165 both \helpref{GetValue()}{wxtextctrlgetvalue}.Length() and
166 \helpref{GetLastPosition()}{wxtextctrlgetlastposition}.
167
168 The following code snippet safely returns the character at the insertion
169 point or the zero character if the point is at the end of the control.
170
171 {\small%
172 \begin{verbatim}
173 char GetCurrentChar(wxTextCtrl *tc) {
174 if (tc->GetInsertionPoint() == tc->GetLastPosition())
175 return '\0';
176 return tc->GetValue[tc->GetInsertionPoint()];
177 }
178 \end{verbatim}
179 }%
180
181 \membersection{wxTextCtrl::GetLastPosition}\label{wxtextctrlgetlastposition}
182
183 \constfunc{virtual long}{GetLastPosition}{\void}
184
185 Returns the zero based index of the last position in the text control,
186 which is equal to the number of characters in the control.
187
188 \membersection{wxTextCtrl::GetLineLength}\label{wxtextctrlgetlinelength}
189
190 \constfunc{int}{GetLineLength}{\param{long}{ lineNo}}
191
192 Gets the length of the specified line, not including any trailing newline
193 character(s).
194
195 \wxheading{Parameters}
196
197 \docparam{lineNo}{Line number (starting from zero).}
198
199 \wxheading{Return value}
200
201 The length of the line, or -1 if {\it lineNo} was invalid.
202
203 \membersection{wxTextCtrl::GetLineText}\label{wxtextctrlgetlinetext}
204
205 \constfunc{wxString}{GetLineText}{\param{long}{ lineNo}}
206
207 Returns the contents of a given line in the text control, not including
208 any trailing newline character(s).
209
210 \wxheading{Parameters}
211
212 \docparam{lineNo}{The line number, starting from zero.}
213
214 \wxheading{Return value}
215
216 The contents of the line.
217
218 \membersection{wxTextCtrl::GetNumberOfLines}\label{wxtextctrlgetnumberoflines}
219
220 \constfunc{int}{GetNumberOfLines}{\void}
221
222 Returns the number of lines in the text control buffer.
223
224 \wxheading{Remarks}
225
226 Note that even empty text controls have one line (where the insertion point
227 is), so GetNumberOfLines() never returns 0.
228
229 For gtk\_text (multi-line) controls, the number of lines is
230 calculated by actually counting newline characters in the buffer. You
231 may wish to avoid using functions that work with line numbers if you are
232 working with controls that contain large amounts of text.
233
234 \membersection{wxTextCtrl::GetValue}\label{wxtextctrlgetvalue}
235
236 \constfunc{wxString}{GetValue}{\void}
237
238 Gets the contents of the control.
239
240 \membersection{wxTextCtrl::IsModified}\label{wxtextctrlismodified}
241
242 \constfunc{bool}{IsModified}{\void}
243
244 Returns TRUE if the text has been modified.
245
246 \membersection{wxTextCtrl::LoadFile}\label{wxtextctrlloadfile}
247
248 \func{bool}{LoadFile}{\param{const wxString\& }{ filename}}
249
250 Loads and displays the named file, if it exists.
251
252 \wxheading{Parameters}
253
254 \docparam{filename}{The filename of the file to load.}
255
256 \wxheading{Return value}
257
258 TRUE if successful, FALSE otherwise.
259
260 \membersection{wxTextCtrl::OnChar}\label{wxtextctrlonchar}
261
262 \func{void}{OnChar}{\param{wxKeyEvent\& }{event}}
263
264 Default handler for character input.
265
266 \wxheading{Remarks}
267
268 It is possible to intercept character
269 input by overriding this member. Call this function
270 to let the default behaviour take place; not calling
271 it results in the character being ignored. You can
272 replace the {\it keyCode} member of {\it event} to
273 translate keystrokes.
274
275 Note that Windows and Motif have different ways
276 of implementing the default behaviour. In Windows,
277 calling wxTextCtrl::OnChar immediately
278 processes the character. In Motif,
279 calling this function simply sets a flag
280 to let default processing happen. This might affect
281 the way in which you write your OnChar function
282 on different platforms.
283
284 \wxheading{See also}
285
286 \helpref{wxKeyEvent}{wxkeyevent}
287
288 \membersection{wxTextCtrl::OnDropFiles}\label{wxtextctrlondropfiles}
289
290 \func{void}{OnDropFiles}{\param{wxDropFilesEvent\& }{event}}
291
292 This event handler function implements default drag and drop behaviour, which
293 is to load the first dropped file into the control.
294
295 \wxheading{Parameters}
296
297 \docparam{event}{The drop files event.}
298
299 \wxheading{Remarks}
300
301 This is not yet implemented for the GTK.
302
303 \wxheading{See also}
304
305 \helpref{wxDropFilesEvent}{wxdropfilesevent}
306
307 \membersection{wxTextCtrl::Paste}\label{wxtextctrlpaste}
308
309 \func{virtual void}{Paste}{\void}
310
311 Pastes text from the clipboard to the text item.
312
313 \membersection{wxTextCtrl::PositionToXY}\label{wxtextctrlpositiontoxy}
314
315 \constfunc{long}{PositionToXY}{\param{long }{pos}, \param{long *}{x}, \param{long *}{y}}
316
317 Converts given position to a zero-based column, line number pair.
318
319 \wxheading{Parameters}
320
321 \docparam{pos}{Position.}
322
323 \docparam{x}{Receives zero based column number.}
324
325 \docparam{y}{Receives zero based line number.}
326
327 \wxheading{Return value}
328
329 Non-zero on success, zero on failure (most likely due to a too large position
330 parameter).
331
332 \wxheading{See also}
333
334 \helpref{wxTextCtrl::XYToPosition}{wxtextctrlxytoposition}
335
336 \pythonnote{In Python, PositionToXY() returns a tuple containing the x and
337 y values, so (x,y) = PositionToXY() is equivalent to the call described
338 above.}
339
340 \membersection{wxTextCtrl::Remove}\label{wxtextctrlremove}
341
342 \func{virtual void}{Remove}{\param{long}{ from}, \param{long}{ to}}
343
344 Removes the text starting at the first given position up to (but not including)
345 the character at the last position.
346
347 \wxheading{Parameters}
348
349 \docparam{from}{The first position.}
350
351 \docparam{to}{The last position.}
352
353 \membersection{wxTextCtrl::Replace}\label{wxtextctrlreplace}
354
355 \func{virtual void}{Replace}{\param{long}{ from}, \param{long}{ to}, \param{const wxString\& }{value}}
356
357 Replaces the text starting at the first position up to (but not including)
358 the character at the last position with the given text.
359
360 \wxheading{Parameters}
361
362 \docparam{from}{The first position.}
363
364 \docparam{to}{The last position.}
365
366 \docparam{value}{The value to replace the existing text with.}
367
368 \membersection{wxTextCtrl::SaveFile}\label{wxtextctrlsavefile}
369
370 \func{bool}{SaveFile}{\param{const wxString\& }{ filename}}
371
372 Saves the contents of the control in a text file.
373
374 \wxheading{Parameters}
375
376 \docparam{filename}{The name of the file in which to save the text.}
377
378 \wxheading{Return value}
379
380 TRUE if the operation was successful, FALSE otherwise.
381
382 \membersection{wxTextCtrl::SetEditable}\label{wxtextctrlseteditable}
383
384 \func{virtual void}{SetEditable}{\param{const bool}{ editable}}
385
386 Makes the text item editable or read-only, overriding the {\bf wxTE\_READONLY}
387 flag.
388
389 \wxheading{Parameters}
390
391 \docparam{editable}{If TRUE, the control is editable. If FALSE, the control is read-only.}
392
393 \membersection{wxTextCtrl::SetInsertionPoint}\label{wxtextctrlsetinsertionpoint}
394
395 \func{virtual void}{SetInsertionPoint}{\param{long}{ pos}}
396
397 Sets the insertion point at the given position.
398
399 \wxheading{Parameters}
400
401 \docparam{pos}{Position to set.}
402
403 \membersection{wxTextCtrl::SetInsertionPointEnd}\label{wxtextctrlsetinsertionpointend}
404
405 \func{virtual void}{SetInsertionPointEnd}{\void}
406
407 Sets the insertion point at the end of the text control. This is equivalent
408 to \helpref{SetInsertionPoint}{wxtextctrlsetinsertionpoint}(\helpref{GetLastPosition}{wxtextctrlgetlastposition}()).
409
410 \membersection{wxTextCtrl::SetSelection}\label{wxtextctrlsetselection}
411
412 \func{virtual void}{SetSelection}{\param{long}{ from}, \param{long}{ to}}
413
414 Selects the text starting at the first position up to (but not including) the character at the last position.
415
416 \wxheading{Parameters}
417
418 \docparam{from}{The first position.}
419
420 \docparam{to}{The last position.}
421
422 \membersection{wxTextCtrl::SetValue}\label{wxtextctrlsetvalue}
423
424 \func{virtual void}{SetValue}{\param{const wxString\& }{ value}}
425
426 Sets the text value.
427
428 \wxheading{Parameters}
429
430 \docparam{value}{The new value to set. It may contain newline characters if the text control is multi-line.}
431
432 \membersection{wxTextCtrl::ShowPosition}\label{wxtextctrlshowposition}
433
434 \func{void}{ShowPosition}{\param{long}{ pos}}
435
436 Makes the line containing the given position visible.
437
438 \wxheading{Parameters}
439
440 \docparam{pos}{The position that should be visible.}
441
442 \membersection{wxTextCtrl::WriteText}\label{wxtextctrlwritetext}
443
444 \func{void}{WriteText}{\param{const wxString\& }{ text}}
445
446 Writes the text into the text control at the current insertion position.
447
448 \wxheading{Parameters}
449
450 \docparam{text}{Text to write to the text control.}
451
452 \wxheading{Remarks}
453
454 Newlines in the text string
455 are the only control characters allowed, and they will cause appropriate
456 line breaks. See \helpref{wxTextCtrl::\cinsert}{wxtextctrlinsert} and \helpref{wxTextCtrl::AppendText}{wxtextctrlappendtext} for more convenient ways of writing to the window.
457
458 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.
459
460 \membersection{wxTextCtrl::AppendText}\label{wxtextctrlappendtext}
461
462 \func{void}{AppendText}{\param{const wxString\& }{ text}}
463
464 Appends the text to the end of the text control.
465
466 \wxheading{Parameters}
467
468 \docparam{text}{Text to write to the text control.}
469
470 \wxheading{Remarks}
471
472 After the text is appended, the insertion point will be at the end of the text control. If this behaviour is not desired,
473 the programmer should use \helpref{GetInsertionPoint}{wxtextctrlgetinsertionpoint} and \helpref{SetInsertionPoint}{wxtextctrlsetinsertionpoint}.
474
475 \wxheading{See also}
476
477 \helpref{wxTextCtrl::WriteText}{wxtextctrlwritetext}
478
479 \membersection{wxTextCtrl::XYToPosition}\label{wxtextctrlxytoposition}
480
481 \func{long}{XYToPosition}{\param{long}{ x}, \param{long}{ y}}
482
483 Converts the given zero based column and line number to a position.
484
485 \wxheading{Parameters}
486
487 \docparam{x}{The column number.}
488
489 \docparam{y}{The line number.}
490
491 \wxheading{Return value}
492
493 The position value.
494
495 \membersection{wxTextCtrl::operator \cinsert}\label{wxtextctrlinsert}
496
497 \func{wxTextCtrl\&}{operator \cinsert}{\param{const wxString\& }{s}}
498
499 \func{wxTextCtrl\&}{operator \cinsert}{\param{int}{ i}}
500
501 \func{wxTextCtrl\&}{operator \cinsert}{\param{long}{ i}}
502
503 \func{wxTextCtrl\&}{operator \cinsert}{\param{float}{ f}}
504
505 \func{wxTextCtrl\&}{operator \cinsert}{\param{double}{ d}}
506
507 \func{wxTextCtrl\&}{operator \cinsert}{\param{char}{ c}}
508
509 Operator definitions for appending to a text control, for example:
510
511 \begin{verbatim}
512 wxTextCtrl *wnd = new wxTextCtrl(my_frame);
513
514 (*wnd) << "Welcome to text control number " << 1 << ".\n";
515 \end{verbatim}
516
517