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