]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/textentry.h
193e47ef5100d0b0c83a70c29c16a3eb14e52985
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/textentry.h
3 // Purpose: interface of wxTextEntry
4 // Author: Vadim Zeitlin
5 // Created: 2009-03-01 (extracted from wx/textctrl.h)
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 wxTextPos is a position in the text
15 typedef long wxTextPos
;
21 Common base class for single line text entry fields.
23 This class is not a control itself, as it doesn't derive from wxWindow.
24 Instead it is used as a base class by other controls, notably wxTextCtrl
25 and wxComboBox and gathers the methods common to both of them.
30 @see wxTextCtrl, wxComboBox
38 Appends the text to the end of the text control.
41 Text to write to the text control.
44 After the text is appended, the insertion point will be at the
45 end of the text control. If this behaviour is not desired, the
46 programmer should use GetInsertionPoint() and SetInsertionPoint().
50 virtual void AppendText(const wxString
& text
);
53 Call this function to enable auto-completion of the text typed in a
54 single-line text control using the given @a choices.
56 Notice that currently this function is only implemented in wxGTK2,
57 wxMSW and wxOSX/Cocoa ports and does nothing under the other platforms.
62 @true if the auto-completion was enabled or @false if the operation
63 failed, typically because auto-completion is not supported by the
66 @see AutoCompleteFileNames()
68 bool AutoComplete(const wxArrayString
& choices
);
71 Enable auto-completion using the provided completer object.
73 This method should be used instead of AutoComplete() overload taking
74 the array of possible completions if the total number of strings is too
75 big as it allows to return the completions dynamically, depending on
76 the text already entered by user and so is more efficient.
78 The specified @a completer object will be used to retrieve the list of
79 possible completions for the already entered text and will be deleted
80 by wxTextEntry itself when it's not needed any longer.
82 Notice that you need to include @c wx/textcompleter.h in order to
83 define your class inheriting from wxTextCompleter.
85 Currently this method is only implemented in wxMSW and wxOSX/Cocoa.
90 The object to be used for generating completions if non-@NULL. If
91 it is @NULL, auto-completion is disabled. The wxTextEntry object
92 takes ownership of this pointer and will delete it in any case
93 (i.e. even if this method returns @false).
96 @true if the auto-completion was enabled or @false if the operation
97 failed, typically because auto-completion is not supported by the
102 bool AutoComplete(wxTextCompleter
*completer
);
105 Call this function to enable auto-completion of the text typed in a
106 single-line text control using all valid file system paths.
108 Notice that currently this function is only implemented in wxMSW port
109 and does nothing under the other platforms.
114 @true if the auto-completion was enabled or @false if the operation
115 failed, typically because auto-completion is not supported by the
120 bool AutoCompleteFileNames();
123 Call this function to enable auto-completion of the text using the file
126 Unlike AutoCompleteFileNames() which completes both file names and
127 directories, this function only completes the directory names.
129 Notice that currently this function is only implemented in wxMSW port
130 and does nothing under the other platforms.
135 @true if the auto-completion was enabled or @false if the operation
136 failed, typically because auto-completion is not supported by the
141 bool AutoCompleteDirectories();
144 Returns @true if the selection can be copied to the clipboard.
146 virtual bool CanCopy() const;
149 Returns @true if the selection can be cut to the clipboard.
151 virtual bool CanCut() const;
154 Returns @true if the contents of the clipboard can be pasted into the
157 On some platforms (Motif, GTK) this is an approximation and returns
158 @true if the control is editable, @false otherwise.
160 virtual bool CanPaste() const;
163 Returns @true if there is a redo facility available and the last
164 operation can be redone.
166 virtual bool CanRedo() const;
169 Returns @true if there is an undo facility available and the last
170 operation can be undone.
172 virtual bool CanUndo() const;
175 Sets the new text control value.
177 It also marks the control as not-modified which means that IsModified()
178 would return @false immediately after the call to ChangeValue().
180 The insertion point is set to the start of the control (i.e. position
183 This functions does not generate the @c wxEVT_TEXT
184 event but otherwise is identical to SetValue().
186 See @ref overview_events_prog for more information.
191 The new value to set. It may contain newline characters if the text
192 control is multi-line.
194 virtual void ChangeValue(const wxString
& value
);
197 Clears the text in the control.
199 Note that this function will generate a @c wxEVT_TEXT
200 event, i.e. its effect is identical to calling @c SetValue("").
202 virtual void Clear();
205 Copies the selected text to the clipboard.
210 Copies the selected text to the clipboard and removes it from the control.
215 Returns the insertion point, or cursor, position.
217 This is defined as the zero based index of the character position to
218 the right of the insertion point. For example, if the insertion point
219 is at the end of the single-line text control, it is equal to
222 Notice that insertion position is, in general, different from the index
223 of the character the cursor position at in the string returned by
224 GetValue(). While this is always the case for the single line controls,
225 multi-line controls can use two characters @c "\\r\\n" as line
226 separator (this is notably the case under MSW) meaning that indices in
227 the control and its string value are offset by 1 for every line.
229 Hence to correctly get the character at the current cursor position,
230 taking into account that there can be none if the cursor is at the end
231 of the string, you could do the following:
234 wxString GetCurrentChar(wxTextCtrl *tc)
236 long pos = tc->GetInsertionPoint();
237 if ( pos == tc->GetLastPosition() )
240 return tc->GetRange(pos, pos + 1);
244 virtual long GetInsertionPoint() const;
247 Returns the zero based index of the last position in the text control,
248 which is equal to the number of characters in the control.
250 virtual wxTextPos
GetLastPosition() const;
253 Returns the string containing the text starting in the positions
254 @a from and up to @a to in the control.
256 The positions must have been returned by another wxTextCtrl method.
257 Please note that the positions in a multiline wxTextCtrl do @b not
258 correspond to the indices in the string returned by GetValue() because
259 of the different new line representations (@c CR or @c CR LF) and so
260 this method should be used to obtain the correct results instead of
261 extracting parts of the entire value. It may also be more efficient,
262 especially if the control contains a lot of data.
264 virtual wxString
GetRange(long from
, long to
) const;
267 Gets the current selection span.
269 If the returned values are equal, there was no selection. Please note
270 that the indices returned may be used with the other wxTextCtrl methods
271 but don't necessarily represent the correct indices into the string
272 returned by GetValue() for multiline controls under Windows (at least,)
273 you should use GetStringSelection() to get the selected text.
276 The returned first position.
278 The returned last position.
281 In wxPerl this method takes no parameters and returns a
282 2-element list (from, to).
285 virtual void GetSelection(long* from
, long* to
) const;
288 Gets the text currently selected in the control.
290 If there is no selection, the returned string is empty.
292 virtual wxString
GetStringSelection() const;
295 Gets the contents of the control.
297 Notice that for a multiline text control, the lines will be separated
298 by (Unix-style) @c \\n characters, even under Windows where they are
299 separated by a @c \\r\\n sequence in the native control.
301 virtual wxString
GetValue() const;
304 Returns @true if the controls contents may be edited by user (note that
305 it always can be changed by the program).
307 In other words, this functions returns @true if the control hasn't been
308 put in read-only mode by a previous call to SetEditable().
310 virtual bool IsEditable() const;
313 Returns @true if the control is currently empty.
315 This is the same as @c GetValue().empty() but can be much more
316 efficient for the multiline controls containing big amounts of text.
320 virtual bool IsEmpty() const;
323 Pastes text from the clipboard to the text item.
325 virtual void Paste();
328 If there is a redo facility and the last operation can be redone,
329 redoes the last operation.
331 Does nothing if there is no redo facility.
336 Removes the text starting at the first given position up to
337 (but not including) the character at the last position.
339 This function puts the current insertion point position at @a to as a
347 virtual void Remove(long from
, long to
);
350 Replaces the text starting at the first position up to
351 (but not including) the character at the last position with the given text.
353 This function puts the current insertion point position at @a to as a
361 The value to replace the existing text with.
363 virtual void Replace(long from
, long to
, const wxString
& value
);
366 Makes the text item editable or read-only, overriding the
367 @b wxTE_READONLY flag.
370 If @true, the control is editable. If @false, the control is
375 virtual void SetEditable(bool editable
);
378 Sets the insertion point at the given position.
381 Position to set, in the range from 0 to GetLastPosition() inclusive.
383 virtual void SetInsertionPoint(long pos
);
386 Sets the insertion point at the end of the text control.
388 This is equivalent to calling wxTextCtrl::SetInsertionPoint() with
389 wxTextCtrl::GetLastPosition() argument.
391 virtual void SetInsertionPointEnd();
394 This function sets the maximum number of characters the user can enter
397 In other words, it allows to limit the text value length to @a len not
398 counting the terminating @c NUL character.
400 If @a len is 0, the previously set max length limit, if any, is discarded
401 and the user may enter as much text as the underlying native text control widget
402 supports (typically at least 32Kb).
403 If the user tries to enter more characters into the text control when it
404 already is filled up to the maximal length, a @c wxEVT_TEXT_MAXLEN
405 event is sent to notify the program about it (giving it the possibility
406 to show an explanatory message, for example) and the extra input is discarded.
408 Note that in wxGTK this function may only be used with single line text controls.
410 virtual void SetMaxLength(unsigned long len
);
413 Selects the text starting at the first position up to (but not
414 including) the character at the last position.
416 If both parameters are equal to -1 all text in the control is selected.
418 Notice that the insertion point will be moved to @a from by this
428 virtual void SetSelection(long from
, long to
);
431 Selects all text in the control.
435 virtual void SelectAll();
438 Deselects selected text in the control.
442 virtual void SelectNone();
445 Sets a hint shown in an empty unfocused text control.
447 The hints are usually used to indicate to the user what is supposed to
448 be entered into the given entry field, e.g. a common use of them is to
449 show an explanation of what can be entered in a wxSearchCtrl.
451 The hint is shown (usually greyed out) for an empty control until it
452 gets focus and is shown again if the control loses it and remains
453 empty. It won't be shown once the control has a non-empty value,
454 although it will be shown again if the control contents is cleared.
455 Because of this, it generally only makes sense to use hints with the
456 controls which are initially empty.
458 Notice that hints are known as <em>cue banners</em> under MSW or
459 <em>placeholder strings</em> under OS X.
461 @remarks For the platforms without native hints support (and currently
462 only the MSW port does have it and even there it is only used under
463 Windows Vista and later only), the implementation has several known
464 limitations. Notably, the hint display will not be properly updated
465 if you change wxTextEntry contents programmatically when the hint
466 is displayed using methods other than SetValue() or ChangeValue()
467 or others which use them internally (e.g. Clear()). In other words,
468 currently you should avoid calling methods such as WriteText() or
469 Replace() when using hints and the text control is empty.
471 @remarks Hints can only be used for single line text controls,
472 native multi-line text controls don't support hints under any
473 platform and hence wxWidgets doesn't provide them neither.
477 virtual bool SetHint(const wxString
& hint
);
480 Returns the current hint string.
482 See SetHint() for more information about hints.
486 virtual wxString
GetHint() const;
490 Attempts to set the control margins. When margins are given as wxPoint,
491 x indicates the left and y the top margin. Use -1 to indicate that
492 an existing value should be used.
495 @true if setting of all requested margins was successful.
499 bool SetMargins(const wxPoint
& pt
);
500 bool SetMargins(wxCoord left
, wxCoord top
= -1);
504 Returns the margins used by the control. The @c x field of the returned
505 point is the horizontal margin and the @c y field is the vertical one.
507 @remarks If given margin cannot be accurately determined, its value
508 will be set to -1. On some platforms you cannot obtain valid
509 margin values until you have called SetMargins().
515 wxPoint
GetMargins() const;
518 Sets the new text control value.
520 It also marks the control as not-modified which means that IsModified()
521 would return @false immediately after the call to SetValue().
523 The insertion point is set to the start of the control (i.e. position
526 Note that, unlike most other functions changing the controls values,
527 this function generates a @c wxEVT_TEXT event. To avoid
528 this you can use ChangeValue() instead.
531 The new value to set. It may contain newline characters if the text
532 control is multi-line.
534 virtual void SetValue(const wxString
& value
);
537 If there is an undo facility and the last operation can be undone,
538 undoes the last operation.
540 Does nothing if there is no undo facility.
545 Writes the text into the text control at the current insertion position.
548 Text to write to the text control.
551 Newlines in the text string are the only control characters
552 allowed, and they will cause appropriate line breaks.
553 See operator<<() and AppendText() for more convenient ways of
554 writing to the window.
555 After the write operation, the insertion point will be at the end
556 of the inserted text, so subsequent write operations will be appended.
557 To append text after the user may have interacted with the control,
558 call wxTextCtrl::SetInsertionPointEnd() before writing.
560 virtual void WriteText(const wxString
& text
);