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