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