Version: 3.1.0
wxTextEntry Class Reference

#include <wx/textentry.h>

+ Inheritance diagram for wxTextEntry:

Detailed Description

Common base class for single line text entry fields.

This class is not a control itself, as it doesn't derive from wxWindow. Instead it is used as a base class by other controls, notably wxTextCtrl and wxComboBox and gathers the methods common to both of them.

Library:  wxCore
Category:  Controls
See also
wxTextCtrl, wxComboBox
Since
2.9.0

Public Member Functions

virtual void AppendText (const wxString &text)
 Appends the text to the end of the text control. More...
 
bool AutoComplete (const wxArrayString &choices)
 Call this function to enable auto-completion of the text typed in a single-line text control using the given choices. More...
 
bool AutoComplete (wxTextCompleter *completer)
 Enable auto-completion using the provided completer object. More...
 
bool AutoCompleteFileNames ()
 Call this function to enable auto-completion of the text typed in a single-line text control using all valid file system paths. More...
 
bool AutoCompleteDirectories ()
 Call this function to enable auto-completion of the text using the file system directories. More...
 
virtual bool CanCopy () const
 Returns true if the selection can be copied to the clipboard. More...
 
virtual bool CanCut () const
 Returns true if the selection can be cut to the clipboard. More...
 
virtual bool CanPaste () const
 Returns true if the contents of the clipboard can be pasted into the text control. More...
 
virtual bool CanRedo () const
 Returns true if there is a redo facility available and the last operation can be redone. More...
 
virtual bool CanUndo () const
 Returns true if there is an undo facility available and the last operation can be undone. More...
 
virtual void ChangeValue (const wxString &value)
 Sets the new text control value. More...
 
virtual void Clear ()
 Clears the text in the control. More...
 
virtual void Copy ()
 Copies the selected text to the clipboard. More...
 
virtual void Cut ()
 Copies the selected text to the clipboard and removes it from the control. More...
 
void ForceUpper ()
 Convert all text entered into the control to upper case. More...
 
virtual long GetInsertionPoint () const
 Returns the insertion point, or cursor, position. More...
 
virtual wxTextPos GetLastPosition () const
 Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control. More...
 
virtual wxString GetRange (long from, long to) const
 Returns the string containing the text starting in the positions from and up to to in the control. More...
 
virtual void GetSelection (long *from, long *to) const
 Gets the current selection span. More...
 
virtual wxString GetStringSelection () const
 Gets the text currently selected in the control. More...
 
virtual wxString GetValue () const
 Gets the contents of the control. More...
 
virtual bool IsEditable () const
 Returns true if the controls contents may be edited by user (note that it always can be changed by the program). More...
 
virtual bool IsEmpty () const
 Returns true if the control is currently empty. More...
 
virtual void Paste ()
 Pastes text from the clipboard to the text item. More...
 
virtual void Redo ()
 If there is a redo facility and the last operation can be redone, redoes the last operation. More...
 
virtual void Remove (long from, long to)
 Removes the text starting at the first given position up to (but not including) the character at the last position. More...
 
virtual void Replace (long from, long to, const wxString &value)
 Replaces the text starting at the first position up to (but not including) the character at the last position with the given text. More...
 
virtual void SetEditable (bool editable)
 Makes the text item editable or read-only, overriding the wxTE_READONLY flag. More...
 
virtual void SetInsertionPoint (long pos)
 Sets the insertion point at the given position. More...
 
virtual void SetInsertionPointEnd ()
 Sets the insertion point at the end of the text control. More...
 
virtual void SetMaxLength (unsigned long len)
 This function sets the maximum number of characters the user can enter into the control. More...
 
virtual void SetSelection (long from, long to)
 Selects the text starting at the first position up to (but not including) the character at the last position. More...
 
virtual void SelectAll ()
 Selects all text in the control. More...
 
virtual void SelectNone ()
 Deselects selected text in the control. More...
 
virtual bool SetHint (const wxString &hint)
 Sets a hint shown in an empty unfocused text control. More...
 
virtual wxString GetHint () const
 Returns the current hint string. More...
 
wxPoint GetMargins () const
 Returns the margins used by the control. More...
 
virtual void SetValue (const wxString &value)
 Sets the new text control value. More...
 
virtual void Undo ()
 If there is an undo facility and the last operation can be undone, undoes the last operation. More...
 
virtual void WriteText (const wxString &text)
 Writes the text into the text control at the current insertion position. More...
 
bool SetMargins (const wxPoint &pt)
 Attempts to set the control margins. More...
 
bool SetMargins (wxCoord left, wxCoord top=-1)
 Attempts to set the control margins. More...
 

Member Function Documentation

virtual void wxTextEntry::AppendText ( const wxString text)
virtual

Appends the text to the end of the text control.

Parameters
textText to write to the text control.
Remarks
After the text is appended, the insertion point will be at the end of the text control. If this behaviour is not desired, the programmer should use GetInsertionPoint() and SetInsertionPoint().
See also
WriteText()

Reimplemented in wxStyledTextCtrl.

bool wxTextEntry::AutoComplete ( const wxArrayString choices)

Call this function to enable auto-completion of the text typed in a single-line text control using the given choices.

Notice that currently this function is only implemented in wxGTK2, wxMSW and wxOSX/Cocoa (for wxTextCtrl only, but not for wxComboBox) ports and does nothing under the other platforms.

Since
2.9.0
Returns
true if the auto-completion was enabled or false if the operation failed, typically because auto-completion is not supported by the current platform.
See also
AutoCompleteFileNames()
bool wxTextEntry::AutoComplete ( wxTextCompleter completer)

Enable auto-completion using the provided completer object.

This method should be used instead of AutoComplete() overload taking the array of possible completions if the total number of strings is too big as it allows to return the completions dynamically, depending on the text already entered by user and so is more efficient.

The specified completer object will be used to retrieve the list of possible completions for the already entered text and will be deleted by wxTextEntry itself when it's not needed any longer.

Notice that you need to include wx/textcompleter.h in order to define your class inheriting from wxTextCompleter.

Currently this method is only implemented in wxMSW and wxOSX/Cocoa (for wxTextCtrl only, but not for wxComboBox).

Since
2.9.2
Parameters
completerThe object to be used for generating completions if non-NULL. If it is NULL, auto-completion is disabled. The wxTextEntry object takes ownership of this pointer and will delete it in any case (i.e. even if this method returns false).
Returns
true if the auto-completion was enabled or false if the operation failed, typically because auto-completion is not supported by the current platform.
See also
wxTextCompleter
bool wxTextEntry::AutoCompleteDirectories ( )

Call this function to enable auto-completion of the text using the file system directories.

Unlike AutoCompleteFileNames() which completes both file names and directories, this function only completes the directory names.

Notice that currently this function is only implemented in wxMSW port and does nothing under the other platforms.

Since
2.9.3
Returns
true if the auto-completion was enabled or false if the operation failed, typically because auto-completion is not supported by the current platform.
See also
AutoComplete()
bool wxTextEntry::AutoCompleteFileNames ( )

Call this function to enable auto-completion of the text typed in a single-line text control using all valid file system paths.

Notice that currently this function is only implemented in wxMSW port and does nothing under the other platforms.

Since
2.9.0
Returns
true if the auto-completion was enabled or false if the operation failed, typically because auto-completion is not supported by the current platform.
See also
AutoComplete()
virtual bool wxTextEntry::CanCopy ( ) const
virtual

Returns true if the selection can be copied to the clipboard.

virtual bool wxTextEntry::CanCut ( ) const
virtual

Returns true if the selection can be cut to the clipboard.

virtual bool wxTextEntry::CanPaste ( ) const
virtual

Returns true if the contents of the clipboard can be pasted into the text control.

On some platforms (Motif, GTK) this is an approximation and returns true if the control is editable, false otherwise.

Reimplemented in wxStyledTextCtrl.

virtual bool wxTextEntry::CanRedo ( ) const
virtual

Returns true if there is a redo facility available and the last operation can be redone.

Reimplemented in wxStyledTextCtrl.

virtual bool wxTextEntry::CanUndo ( ) const
virtual

Returns true if there is an undo facility available and the last operation can be undone.

Reimplemented in wxStyledTextCtrl.

virtual void wxTextEntry::ChangeValue ( const wxString value)
virtual

Sets the new text control value.

It also marks the control as not-modified which means that IsModified() would return false immediately after the call to ChangeValue().

The insertion point is set to the start of the control (i.e. position 0) by this function.

This functions does not generate the wxEVT_TEXT event but otherwise is identical to SetValue().

See User Generated Events vs Programmatically Generated Events for more information.

Since
2.7.1
Parameters
valueThe new value to set. It may contain newline characters if the text control is multi-line.
virtual void wxTextEntry::Clear ( )
virtual

Clears the text in the control.

Note that this function will generate a wxEVT_TEXT event, i.e. its effect is identical to calling SetValue("").

Reimplemented in wxStyledTextCtrl.

virtual void wxTextEntry::Copy ( )
virtual

Copies the selected text to the clipboard.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::Cut ( )
virtual

Copies the selected text to the clipboard and removes it from the control.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

void wxTextEntry::ForceUpper ( )

Convert all text entered into the control to upper case.

Call this method to ensure that all text entered into the control is converted on the fly to upper case. If the control is not empty, its existing contents is also converted to upper case.

Since
3.1.0
virtual wxString wxTextEntry::GetHint ( ) const
virtual

Returns the current hint string.

See SetHint() for more information about hints.

Since
2.9.0

Reimplemented in wxComboCtrl.

virtual long wxTextEntry::GetInsertionPoint ( ) const
virtual

Returns the insertion point, or cursor, position.

This is defined as the zero based index of the character position to the right of the insertion point. For example, if the insertion point is at the end of the single-line text control, it is equal to GetLastPosition().

Notice that insertion position is, in general, different from the index of the character the cursor position at in the string returned by GetValue(). While this is always the case for the single line controls, multi-line controls can use two characters "\\r\\n" as line separator (this is notably the case under MSW) meaning that indices in the control and its string value are offset by 1 for every line.

Hence to correctly get the character at the current cursor position, taking into account that there can be none if the cursor is at the end of the string, you could do the following:

wxString GetCurrentChar(wxTextCtrl *tc)
{
long pos = tc->GetInsertionPoint();
if ( pos == tc->GetLastPosition() )
return wxString();
return tc->GetRange(pos, pos + 1);
}

Reimplemented in wxStyledTextCtrl, wxComboCtrl, and wxComboBox.

virtual wxTextPos wxTextEntry::GetLastPosition ( ) const
virtual

Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

wxPoint wxTextEntry::GetMargins ( ) const

Returns the margins used by the control.

The x field of the returned point is the horizontal margin and the y field is the vertical one.

Remarks
If given margin cannot be accurately determined, its value will be set to -1. On some platforms you cannot obtain valid margin values until you have called SetMargins().
See also
SetMargins()
Since
2.9.1
virtual wxString wxTextEntry::GetRange ( long  from,
long  to 
) const
virtual

Returns the string containing the text starting in the positions from and up to to in the control.

The positions must have been returned by another wxTextCtrl method. Please note that the positions in a multiline wxTextCtrl do not correspond to the indices in the string returned by GetValue() because of the different new line representations (CR or CR LF) and so this method should be used to obtain the correct results instead of extracting parts of the entire value. It may also be more efficient, especially if the control contains a lot of data.

virtual void wxTextEntry::GetSelection ( long *  from,
long *  to 
) const
virtual

Gets the current selection span.

If the returned values are equal, there was no selection. Please note that the indices returned may be used with the other wxTextCtrl methods but don't necessarily represent the correct indices into the string returned by GetValue() for multiline controls under Windows (at least,) you should use GetStringSelection() to get the selected text.

Parameters
fromThe returned first position.
toThe returned last position.

wxPerl Note: In wxPerl this method takes no parameters and returns a 2-element list (from, to).

Reimplemented in wxStyledTextCtrl, and wxComboBox.

virtual wxString wxTextEntry::GetStringSelection ( ) const
virtual

Gets the text currently selected in the control.

If there is no selection, the returned string is empty.

Reimplemented in wxComboBox.

virtual wxString wxTextEntry::GetValue ( ) const
virtual

Gets the contents of the control.

Notice that for a multiline text control, the lines will be separated by (Unix-style) \n characters, even under Windows where they are separated by a \r\n sequence in the native control.

Reimplemented in wxComboCtrl.

virtual bool wxTextEntry::IsEditable ( ) const
virtual

Returns true if the controls contents may be edited by user (note that it always can be changed by the program).

In other words, this functions returns true if the control hasn't been put in read-only mode by a previous call to SetEditable().

Reimplemented in wxStyledTextCtrl.

virtual bool wxTextEntry::IsEmpty ( ) const
virtual

Returns true if the control is currently empty.

This is the same as GetValue().empty() but can be much more efficient for the multiline controls containing big amounts of text.

Since
2.7.1

Reimplemented in wxComboBox, and wxOwnerDrawnComboBox.

virtual void wxTextEntry::Paste ( )
virtual

Pastes text from the clipboard to the text item.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::Redo ( )
virtual

If there is a redo facility and the last operation can be redone, redoes the last operation.

Does nothing if there is no redo facility.

Reimplemented in wxStyledTextCtrl.

virtual void wxTextEntry::Remove ( long  from,
long  to 
)
virtual

Removes the text starting at the first given position up to (but not including) the character at the last position.

This function puts the current insertion point position at to as a side effect.

Parameters
fromThe first position.
toThe last position.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::Replace ( long  from,
long  to,
const wxString value 
)
virtual

Replaces the text starting at the first position up to (but not including) the character at the last position with the given text.

This function puts the current insertion point position at to as a side effect.

Parameters
fromThe first position.
toThe last position.
valueThe value to replace the existing text with.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::SelectAll ( )
virtual

Selects all text in the control.

See also
SetSelection()

Reimplemented in wxStyledTextCtrl.

virtual void wxTextEntry::SelectNone ( )
virtual

Deselects selected text in the control.

Since
2.9.5

Reimplemented in wxStyledTextCtrl.

virtual void wxTextEntry::SetEditable ( bool  editable)
virtual

Makes the text item editable or read-only, overriding the wxTE_READONLY flag.

Parameters
editableIf true, the control is editable. If false, the control is read-only.
See also
IsEditable()

Reimplemented in wxStyledTextCtrl.

virtual bool wxTextEntry::SetHint ( const wxString hint)
virtual

Sets a hint shown in an empty unfocused text control.

The hints are usually used to indicate to the user what is supposed to be entered into the given entry field, e.g. a common use of them is to show an explanation of what can be entered in a wxSearchCtrl.

The hint is shown (usually greyed out) for an empty control until it gets focus and is shown again if the control loses it and remains empty. It won't be shown once the control has a non-empty value, although it will be shown again if the control contents is cleared. Because of this, it generally only makes sense to use hints with the controls which are initially empty.

Notice that hints are known as cue banners under MSW or placeholder strings under OS X.

Remarks
Currently implemented natively on Windows (Vista and later only), OS X and GTK+ (3.2 and later).

For the platforms without native hints support, the implementation has several known limitations. Notably, the hint display will not be properly updated if you change wxTextEntry contents programmatically when the hint is displayed using methods other than SetValue() or ChangeValue() or others which use them internally (e.g. Clear()). In other words, currently you should avoid calling methods such as WriteText() or Replace() when using hints and the text control is empty. If you bind to the control's focus and wxEVT_TEXT events, you must call wxEvent::Skip() on them so that the generic implementation works correctly.

Remarks
Hints can be used for single line text controls under all platforms, but only MSW and GTK+ 2 support them for multi-line text controls, they are ignored for them under the other platforms.
Since
2.9.0

Reimplemented in wxComboCtrl.

virtual void wxTextEntry::SetInsertionPoint ( long  pos)
virtual

Sets the insertion point at the given position.

Parameters
posPosition to set, in the range from 0 to GetLastPosition() inclusive.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::SetInsertionPointEnd ( )
virtual

Sets the insertion point at the end of the text control.

This is equivalent to calling wxTextCtrl::SetInsertionPoint() with wxTextCtrl::GetLastPosition() argument.

Reimplemented in wxComboCtrl.

bool wxTextEntry::SetMargins ( const wxPoint pt)

Attempts to set the control margins.

When margins are given as wxPoint, x indicates the left and y the top margin. Use -1 to indicate that an existing value should be used.

Returns
true if setting of all requested margins was successful.
Since
2.9.1
bool wxTextEntry::SetMargins ( wxCoord  left,
wxCoord  top = -1 
)

Attempts to set the control margins.

When margins are given as wxPoint, x indicates the left and y the top margin. Use -1 to indicate that an existing value should be used.

Returns
true if setting of all requested margins was successful.
Since
2.9.1
virtual void wxTextEntry::SetMaxLength ( unsigned long  len)
virtual

This function sets the maximum number of characters the user can enter into the control.

In other words, it allows to limit the text value length to len not counting the terminating NUL character.

If len is 0, the previously set max length limit, if any, is discarded and the user may enter as much text as the underlying native text control widget supports (typically at least 32Kb). If the user tries to enter more characters into the text control when it already is filled up to the maximal length, a wxEVT_TEXT_MAXLEN event is sent to notify the program about it (giving it the possibility to show an explanatory message, for example) and the extra input is discarded.

Note that in wxGTK this function may only be used with single line text controls.

virtual void wxTextEntry::SetSelection ( long  from,
long  to 
)
virtual

Selects the text starting at the first position up to (but not including) the character at the last position.

If both parameters are equal to -1 all text in the control is selected.

Notice that the insertion point will be moved to from by this function.

Parameters
fromThe first position.
toThe last position.
See also
SelectAll()

Reimplemented in wxStyledTextCtrl, wxComboCtrl, and wxComboBox.

virtual void wxTextEntry::SetValue ( const wxString value)
virtual

Sets the new text control value.

It also marks the control as not-modified which means that IsModified() would return false immediately after the call to SetValue().

The insertion point is set to the start of the control (i.e. position 0) by this function.

Note that, unlike most other functions changing the controls values, this function generates a wxEVT_TEXT event. To avoid this you can use ChangeValue() instead.

Parameters
valueThe new value to set. It may contain newline characters if the text control is multi-line.

Reimplemented in wxComboCtrl, and wxComboBox.

virtual void wxTextEntry::Undo ( )
virtual

If there is an undo facility and the last operation can be undone, undoes the last operation.

Does nothing if there is no undo facility.

Reimplemented in wxStyledTextCtrl, and wxComboCtrl.

virtual void wxTextEntry::WriteText ( const wxString text)
virtual

Writes the text into the text control at the current insertion position.

Parameters
textText to write to the text control.
Remarks
Newlines in the text string are the only control characters allowed, and they will cause appropriate line breaks. See operator<<() and AppendText() for more convenient ways of writing to the window. 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 wxTextCtrl::SetInsertionPointEnd() before writing.

Reimplemented in wxStyledTextCtrl.