]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/textentry.h
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)
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 wxTextPos is a position in the text
14 typedef long wxTextPos
;
20 Common base class for single line text entry fields.
22 This class is not a control itself, as it doesn't derive from wxWindow.
23 Instead it is used as a base class by other controls, notably wxTextCtrl
24 and wxComboBox and gathers the methods common to both of them.
29 @see wxTextCtrl, wxComboBox
37 Appends the text to the end of the text control.
40 Text to write to the text control.
43 After the text is appended, the insertion point will be at the
44 end of the text control. If this behaviour is not desired, the
45 programmer should use GetInsertionPoint() and SetInsertionPoint().
49 virtual void AppendText(const wxString
& text
);
52 Call this function to enable auto-completion of the text typed in a
53 single-line text control using the given @a choices.
55 Notice that currently this function is only implemented in wxGTK2,
56 wxMSW and wxOSX/Cocoa ports and does nothing under the other platforms.
61 @true if the auto-completion was enabled or @false if the operation
62 failed, typically because auto-completion is not supported by the
65 @see AutoCompleteFileNames()
67 bool AutoComplete(const wxArrayString
& choices
);
70 Enable auto-completion using the provided completer object.
72 This method should be used instead of AutoComplete() overload taking
73 the array of possible completions if the total number of strings is too
74 big as it allows to return the completions dynamically, depending on
75 the text already entered by user and so is more efficient.
77 The specified @a completer object will be used to retrieve the list of
78 possible completions for the already entered text and will be deleted
79 by wxTextEntry itself when it's not needed any longer.
81 Notice that you need to include @c wx/textcompleter.h in order to
82 define your class inheriting from wxTextCompleter.
84 Currently this method is only implemented in wxMSW and wxOSX/Cocoa.
89 The object to be used for generating completions if non-@NULL. If
90 it is @NULL, auto-completion is disabled. The wxTextEntry object
91 takes ownership of this pointer and will delete it in any case
92 (i.e. even if this method returns @false).
95 @true if the auto-completion was enabled or @false if the operation
96 failed, typically because auto-completion is not supported by the
101 bool AutoComplete(wxTextCompleter
*completer
);
104 Call this function to enable auto-completion of the text typed in a
105 single-line text control using all valid file system paths.
107 Notice that currently this function is only implemented in wxMSW port
108 and does nothing under the other platforms.
113 @true if the auto-completion was enabled or @false if the operation
114 failed, typically because auto-completion is not supported by the
119 bool AutoCompleteFileNames();
122 Call this function to enable auto-completion of the text using the file
125 Unlike AutoCompleteFileNames() which completes both file names and
126 directories, this function only completes the directory names.
128 Notice that currently this function is only implemented in wxMSW port
129 and does nothing under the other platforms.
134 @true if the auto-completion was enabled or @false if the operation
135 failed, typically because auto-completion is not supported by the
140 bool AutoCompleteDirectories();
143 Returns @true if the selection can be copied to the clipboard.
145 virtual bool CanCopy() const;
148 Returns @true if the selection can be cut to the clipboard.
150 virtual bool CanCut() const;
153 Returns @true if the contents of the clipboard can be pasted into the
156 On some platforms (Motif, GTK) this is an approximation and returns
157 @true if the control is editable, @false otherwise.
159 virtual bool CanPaste() const;
162 Returns @true if there is a redo facility available and the last
163 operation can be redone.
165 virtual bool CanRedo() const;
168 Returns @true if there is an undo facility available and the last
169 operation can be undone.
171 virtual bool CanUndo() const;
174 Sets the new text control value.
176 It also marks the control as not-modified which means that IsModified()
177 would return @false immediately after the call to ChangeValue().
179 The insertion point is set to the start of the control (i.e. position
182 This functions does not generate the @c wxEVT_TEXT
183 event but otherwise is identical to SetValue().
185 See @ref overview_events_prog for more information.
190 The new value to set. It may contain newline characters if the text
191 control is multi-line.
193 virtual void ChangeValue(const wxString
& value
);
196 Clears the text in the control.
198 Note that this function will generate a @c wxEVT_TEXT
199 event, i.e. its effect is identical to calling @c SetValue("").
201 virtual void Clear();
204 Copies the selected text to the clipboard.
209 Copies the selected text to the clipboard and removes it from the control.
214 Returns the insertion point, or cursor, position.
216 This is defined as the zero based index of the character position to
217 the right of the insertion point. For example, if the insertion point
218 is at the end of the single-line text control, it is equal to
221 Notice that insertion position is, in general, different from the index
222 of the character the cursor position at in the string returned by
223 GetValue(). While this is always the case for the single line controls,
224 multi-line controls can use two characters @c "\\r\\n" as line
225 separator (this is notably the case under MSW) meaning that indices in
226 the control and its string value are offset by 1 for every line.
228 Hence to correctly get the character at the current cursor position,
229 taking into account that there can be none if the cursor is at the end
230 of the string, you could do the following:
233 wxString GetCurrentChar(wxTextCtrl *tc)
235 long pos = tc->GetInsertionPoint();
236 if ( pos == tc->GetLastPosition() )
239 return tc->GetRange(pos, pos + 1);
243 virtual long GetInsertionPoint() const;
246 Returns the zero based index of the last position in the text control,
247 which is equal to the number of characters in the control.
249 virtual wxTextPos
GetLastPosition() const;
252 Returns the string containing the text starting in the positions
253 @a from and up to @a to in the control.
255 The positions must have been returned by another wxTextCtrl method.
256 Please note that the positions in a multiline wxTextCtrl do @b not
257 correspond to the indices in the string returned by GetValue() because
258 of the different new line representations (@c CR or @c CR LF) and so
259 this method should be used to obtain the correct results instead of
260 extracting parts of the entire value. It may also be more efficient,
261 especially if the control contains a lot of data.
263 virtual wxString
GetRange(long from
, long to
) const;
266 Gets the current selection span.
268 If the returned values are equal, there was no selection. Please note
269 that the indices returned may be used with the other wxTextCtrl methods
270 but don't necessarily represent the correct indices into the string
271 returned by GetValue() for multiline controls under Windows (at least,)
272 you should use GetStringSelection() to get the selected text.
275 The returned first position.
277 The returned last position.
280 In wxPerl this method takes no parameters and returns a
281 2-element list (from, to).
284 virtual void GetSelection(long* from
, long* to
) const;
287 Gets the text currently selected in the control.
289 If there is no selection, the returned string is empty.
291 virtual wxString
GetStringSelection() const;
294 Gets the contents of the control.
296 Notice that for a multiline text control, the lines will be separated
297 by (Unix-style) @c \\n characters, even under Windows where they are
298 separated by a @c \\r\\n sequence in the native control.
300 virtual wxString
GetValue() const;
303 Returns @true if the controls contents may be edited by user (note that
304 it always can be changed by the program).
306 In other words, this functions returns @true if the control hasn't been
307 put in read-only mode by a previous call to SetEditable().
309 virtual bool IsEditable() const;
312 Returns @true if the control is currently empty.
314 This is the same as @c GetValue().empty() but can be much more
315 efficient for the multiline controls containing big amounts of text.
319 virtual bool IsEmpty() const;
322 Pastes text from the clipboard to the text item.
324 virtual void Paste();
327 If there is a redo facility and the last operation can be redone,
328 redoes the last operation.
330 Does nothing if there is no redo facility.
335 Removes the text starting at the first given position up to
336 (but not including) the character at the last position.
338 This function puts the current insertion point position at @a to as a
346 virtual void Remove(long from
, long to
);
349 Replaces the text starting at the first position up to
350 (but not including) the character at the last position with the given text.
352 This function puts the current insertion point position at @a to as a
360 The value to replace the existing text with.
362 virtual void Replace(long from
, long to
, const wxString
& value
);
365 Makes the text item editable or read-only, overriding the
366 @b wxTE_READONLY flag.
369 If @true, the control is editable. If @false, the control is
374 virtual void SetEditable(bool editable
);
377 Sets the insertion point at the given position.
380 Position to set, in the range from 0 to GetLastPosition() inclusive.
382 virtual void SetInsertionPoint(long pos
);
385 Sets the insertion point at the end of the text control.
387 This is equivalent to calling wxTextCtrl::SetInsertionPoint() with
388 wxTextCtrl::GetLastPosition() argument.
390 virtual void SetInsertionPointEnd();
393 This function sets the maximum number of characters the user can enter
396 In other words, it allows to limit the text value length to @a len not
397 counting the terminating @c NUL character.
399 If @a len is 0, the previously set max length limit, if any, is discarded
400 and the user may enter as much text as the underlying native text control widget
401 supports (typically at least 32Kb).
402 If the user tries to enter more characters into the text control when it
403 already is filled up to the maximal length, a @c wxEVT_TEXT_MAXLEN
404 event is sent to notify the program about it (giving it the possibility
405 to show an explanatory message, for example) and the extra input is discarded.
407 Note that in wxGTK this function may only be used with single line text controls.
409 virtual void SetMaxLength(unsigned long len
);
412 Selects the text starting at the first position up to (but not
413 including) the character at the last position.
415 If both parameters are equal to -1 all text in the control is selected.
417 Notice that the insertion point will be moved to @a from by this
427 virtual void SetSelection(long from
, long to
);
430 Selects all text in the control.
434 virtual void SelectAll();
437 Deselects selected text in the control.
441 virtual void SelectNone();
444 Sets a hint shown in an empty unfocused text control.
446 The hints are usually used to indicate to the user what is supposed to
447 be entered into the given entry field, e.g. a common use of them is to
448 show an explanation of what can be entered in a wxSearchCtrl.
450 The hint is shown (usually greyed out) for an empty control until it
451 gets focus and is shown again if the control loses it and remains
452 empty. It won't be shown once the control has a non-empty value,
453 although it will be shown again if the control contents is cleared.
454 Because of this, it generally only makes sense to use hints with the
455 controls which are initially empty.
457 Notice that hints are known as <em>cue banners</em> under MSW or
458 <em>placeholder strings</em> under OS X.
460 @remarks For the platforms without native hints support (and currently
461 only the MSW port does have it and even there it is only used under
462 Windows Vista and later only), the implementation has several known
463 limitations. Notably, the hint display will not be properly updated
464 if you change wxTextEntry contents programmatically when the hint
465 is displayed using methods other than SetValue() or ChangeValue()
466 or others which use them internally (e.g. Clear()). In other words,
467 currently you should avoid calling methods such as WriteText() or
468 Replace() when using hints and the text control is empty.
470 @remarks Hints can only be used for single line text controls,
471 native multi-line text controls don't support hints under any
472 platform and hence wxWidgets doesn't provide them neither.
476 virtual bool SetHint(const wxString
& hint
);
479 Returns the current hint string.
481 See SetHint() for more information about hints.
485 virtual wxString
GetHint() const;
489 Attempts to set the control margins. When margins are given as wxPoint,
490 x indicates the left and y the top margin. Use -1 to indicate that
491 an existing value should be used.
494 @true if setting of all requested margins was successful.
498 bool SetMargins(const wxPoint
& pt
);
499 bool SetMargins(wxCoord left
, wxCoord top
= -1);
503 Returns the margins used by the control. The @c x field of the returned
504 point is the horizontal margin and the @c y field is the vertical one.
506 @remarks If given margin cannot be accurately determined, its value
507 will be set to -1. On some platforms you cannot obtain valid
508 margin values until you have called SetMargins().
514 wxPoint
GetMargins() const;
517 Sets the new text control value.
519 It also marks the control as not-modified which means that IsModified()
520 would return @false immediately after the call to SetValue().
522 The insertion point is set to the start of the control (i.e. position
525 Note that, unlike most other functions changing the controls values,
526 this function generates a @c wxEVT_TEXT event. To avoid
527 this you can use ChangeValue() instead.
530 The new value to set. It may contain newline characters if the text
531 control is multi-line.
533 virtual void SetValue(const wxString
& value
);
536 If there is an undo facility and the last operation can be undone,
537 undoes the last operation.
539 Does nothing if there is no undo facility.
544 Writes the text into the text control at the current insertion position.
547 Text to write to the text control.
550 Newlines in the text string are the only control characters
551 allowed, and they will cause appropriate line breaks.
552 See operator<<() and AppendText() for more convenient ways of
553 writing to the window.
554 After the write operation, the insertion point will be at the end
555 of the inserted text, so subsequent write operations will be appended.
556 To append text after the user may have interacted with the control,
557 call wxTextCtrl::SetInsertionPointEnd() before writing.
559 virtual void WriteText(const wxString
& text
);