## wxTextCtrl

A text control allows text to be displayed and edited. It may be single line or multi-line.

Derived from

streambuf
wxControl
wxWindow
wxEvtHandler
wxObject

Include files

<wx/textctrl.h>

Window styles

wxTE_PROCESS_ENTER | The control will generate the message wxEVENT_TYPE_TEXT_ENTER_COMMAND (otherwise pressing <Enter> is either processed internally by the control or used for navigation between dialog controls).
wxTE_PROCESS_TAB | The control will receieve EVT_CHAR messages for TAB pressed - normally, TAB is used for passing to the next control in a dialog instead. For the control created with this style, you can still use Ctrl-Enter to pass to the next control from the keyboard.
wxTE_MULTILINE | The text control allows multiple lines.
wxTE_PASSWORD | The text will be echoed as asterisks.
wxTE_READONLY | The text will not be user-editable.
wxHSCROLL | A horizontal scrollbar will be created. No effect under GTK+.

See also window styles overview and wxTextCtrl::wxTextCtrl.

Remarks

This class multiply-inherits from streambuf where compilers allow, allowing code such as the following:

      wxTextCtrl *control = new wxTextCtrl(...);

      ostream stream(control)

      stream << 123.456 << " some text\n";
      stream.flush();

If your compiler does not support derivation from streambuf and gives a compile error, define the symbol NO_TEXT_WINDOW_STREAM in the wxTextCtrl header file.

Note that any use of C++ iostreams (including this one) deprecated and might get completely removed in the future.

Event handling

The following commands are processed by default event handlers in wxTextCtrl: wxID_CUT, wxID_COPY, wxID_PASTE, wxID_UNDO, wxID_REDO. The associated UI update events are also processed automatically, when the control has the focus.

To process input from a text control, use these event handler macros to direct input to member functions that take a wxCommandEvent argument.

EVT_TEXT(id, func) | Respond to a wxEVT_COMMAND_TEXT_UPDATED event, generated when the text changes. Notice that this event will always be sent when the text controls contents changes - whether this is due to user input or comes from the program itself (for example, if SetValue() is called)
EVT_TEXT_ENTER(id, func) | Respond to a wxEVT_COMMAND_TEXT_ENTER event, generated when enter is pressed in a single-line text control.

Members

wxTextCtrl::wxTextCtrl
wxTextCtrl::~wxTextCtrl
wxTextCtrl::AppendText
wxTextCtrl::CanCopy
wxTextCtrl::CanCut
wxTextCtrl::CanPaste
wxTextCtrl::CanRedo
wxTextCtrl::CanUndo
wxTextCtrl::Clear
wxTextCtrl::Copy
wxTextCtrl::Create
wxTextCtrl::Cut
wxTextCtrl::DiscardEdits
wxTextCtrl::GetInsertionPoint
wxTextCtrl::GetLastPosition
wxTextCtrl::GetLineLength
wxTextCtrl::GetLineText
wxTextCtrl::GetNumberOfLines
wxTextCtrl::GetSelection
wxTextCtrl::GetValue
wxTextCtrl::IsModified
wxTextCtrl::LoadFile
wxTextCtrl::OnChar
wxTextCtrl::OnDropFiles
wxTextCtrl::Paste
wxTextCtrl::PositionToXY
wxTextCtrl::Redo
wxTextCtrl::Remove
wxTextCtrl::Replace
wxTextCtrl::SaveFile
wxTextCtrl::SetEditable
wxTextCtrl::SetInsertionPoint
wxTextCtrl::SetInsertionPointEnd
wxTextCtrl::SetSelection
wxTextCtrl::SetValue
wxTextCtrl::ShowPosition
wxTextCtrl::Undo
wxTextCtrl::WriteText
wxTextCtrl::XYToPosition
wxTextCtrl::operator <<

### wxTextCtrl::wxTextCtrl

wxTextCtrl()

Default constructor.

wxTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator, const wxString& name = "text")

Constructor, creating and showing a text control.

Parameters

parent

Parent window. Should not be NULL.

id

Control identifier. A value of -1 denotes a default value.

value

Default text value.

pos

Text control position.

size

Text control size.

style

Window style. See wxTextCtrl.

validator

Window validator.

name

Window name.

Remarks

The horizontal scrollbar (wxTE_HSCROLL style flag) will only be created for multi-line text controls. Without a horizontal scrollbar, text lines that don't fit in the control's size will be wrapped (but no newline character is inserted). Single line controls don't have a horizontal scrollbar, the text is automatically scrolled so that the insertion point is always visible.

Under Windows, if the wxTE_MULTILINE style is used, the window is implemented as a Windows rich text control with unlimited capacity. Otherwise, normal edit control limits apply.

See also

wxTextCtrl::Create, wxValidator

### wxTextCtrl::~wxTextCtrl

~wxTextCtrl()

Destructor, destroying the text control.

### wxTextCtrl::AppendText

void AppendText(const wxString& text)

Appends the text to the end of the text control.

Parameters

text

Text 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

wxTextCtrl::WriteText

### wxTextCtrl::CanCopy

virtual bool CanCopy()

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

### wxTextCtrl::CanCut

virtual bool CanCut()

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

### wxTextCtrl::CanPaste

virtual bool CanPaste()

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.

### wxTextCtrl::CanRedo

virtual bool CanRedo()

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

### wxTextCtrl::CanUndo

virtual bool CanUndo()

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

### wxTextCtrl::Clear

virtual void Clear()

Clears the text in the control.

### wxTextCtrl::Copy

virtual void Copy()

Copies the selected text to the clipboard under Motif and MS Windows.

### wxTextCtrl::Create

bool Create(wxWindow* parent, wxWindowID id, const wxString& value = "", const wxPoint& pos, const wxSize& size = wxDefaultSize, long style = 0, const wxValidator& validator, const wxString& name = "text")

Creates the text control for two-step construction. Derived classes should call or replace this function. See wxTextCtrl::wxTextCtrl for further details.

### wxTextCtrl::Cut

virtual void Cut()

Copies the selected text to the clipboard and removes the selection.

### wxTextCtrl::DiscardEdits

void DiscardEdits()

Resets the internal 'modified' flag as if the current edits had been saved.

### wxTextCtrl::GetInsertionPoint

virtual long GetInsertionPoint() const

Returns the insertion point. 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 text control, it is equal to both GetValue().Length() and GetLastPosition().

The following code snippet safely returns the character at the insertion point or the zero character if the point is at the end of the control.

      char GetCurrentChar(wxTextCtrl *tc) {
        if (tc->GetInsertionPoint() == tc->GetLastPosition())
          return '\0';
        return tc->GetValue[tc->GetInsertionPoint()];
      }

### wxTextCtrl::GetLastPosition

virtual long 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.

### wxTextCtrl::GetLineLength

int GetLineLength(long lineNo) const

Gets the length of the specified line, not including any trailing newline character(s).

Parameters

lineNo

Line number (starting from zero).

Return value

The length of the line, or -1 if lineNo was invalid.

### wxTextCtrl::GetLineText

wxString GetLineText(long lineNo) const

Returns the contents of a given line in the text control, not including any trailing newline character(s).

Parameters

lineNo

The line number, starting from zero.

Return value

The contents of the line.

### wxTextCtrl::GetNumberOfLines

int GetNumberOfLines() const

Returns the number of lines in the text control buffer.

Remarks

Note that even empty text controls have one line (where the insertion point is), so GetNumberOfLines() never returns 0.

For gtk_text (multi-line) controls, the number of lines is calculated by actually counting newline characters in the buffer. You may wish to avoid using functions that work with line numbers if you are working with controls that contain large amounts of text.

### wxTextCtrl::GetSelection

virtual void GetSelection(long* from, long* to)

Gets the current selection span. If the returned values are equal, there was no selection.

Parameters

from

The returned first position.

to

The returned last position.

wxPython note: The wxPython version of this method returns a tuple consisting of the from and to values.

### wxTextCtrl::GetValue

wxString GetValue() const

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.

### wxTextCtrl::IsModified

bool IsModified() const

Returns TRUE if the text has been modified.

### wxTextCtrl::LoadFile

bool LoadFile(const wxString& filename)

Loads and displays the named file, if it exists.

Parameters

filename

The filename of the file to load.

Return value

TRUE if successful, FALSE otherwise.

### wxTextCtrl::OnChar

void OnChar(wxKeyEvent& event)

Default handler for character input.

Remarks

It is possible to intercept character input by overriding this member. Call this function to let the default behaviour take place; not calling it results in the character being ignored. You can replace the keyCode member of event to translate keystrokes.

Note that Windows and Motif have different ways of implementing the default behaviour. In Windows, calling wxTextCtrl::OnChar immediately processes the character. In Motif, calling this function simply sets a flag to let default processing happen. This might affect the way in which you write your OnChar function on different platforms.

See also

wxKeyEvent

### wxTextCtrl::OnDropFiles

void OnDropFiles(wxDropFilesEvent& event)

This event handler function implements default drag and drop behaviour, which is to load the first dropped file into the control.

Parameters

event

The drop files event.

Remarks

This is not implemented on non-Windows platforms.

See also

wxDropFilesEvent

### wxTextCtrl::Paste

virtual void Paste()

Pastes text from the clipboard to the text item.

### wxTextCtrl::PositionToXY

bool PositionToXY(long pos, long *x, long *y) const

Converts given position to a zero-based column, line number pair.

Parameters

pos

Position.

x

Receives zero based column number.

y

Receives zero based line number.

Return value

TRUE on success, FALSE on failure (most likely due to a too large position parameter).

See also

wxTextCtrl::XYToPosition

wxPython note: In Python, PositionToXY() returns a tuple containing the x and y values, so (x,y) = PositionToXY() is equivalent to the call described above.

### wxTextCtrl::Redo

virtual void Redo()

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.

### wxTextCtrl::Remove

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.

Parameters

from

The first position.

to

The last position.

### wxTextCtrl::Replace

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.

Parameters

from

The first position.

to

The last position.

value

The value to replace the existing text with.

### wxTextCtrl::SaveFile

bool SaveFile(const wxString& filename)

Saves the contents of the control in a text file.

Parameters

filename

The name of the file in which to save the text.

Return value

TRUE if the operation was successful, FALSE otherwise.

### wxTextCtrl::SetEditable

virtual void SetEditable(const bool editable)

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

Parameters

editable

If TRUE, the control is editable. If FALSE, the control is read-only.

### wxTextCtrl::SetInsertionPoint

virtual void SetInsertionPoint(long pos)

Sets the insertion point at the given position.

Parameters

pos

Position to set.

### wxTextCtrl::SetInsertionPointEnd

virtual void SetInsertionPointEnd()

Sets the insertion point at the end of the text control. This is equivalent to SetInsertionPoint(GetLastPosition()).

### wxTextCtrl::SetSelection

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.

Parameters

from

The first position.

to

The last position.

### wxTextCtrl::SetValue

virtual void SetValue(const wxString& value)

Sets the text value and marks the control as not-modified.

Parameters

value

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

### wxTextCtrl::ShowPosition

void ShowPosition(long pos)

Makes the line containing the given position visible.

Parameters

pos

The position that should be visible.

### wxTextCtrl::Undo

virtual void Undo()

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.

### wxTextCtrl::WriteText

void WriteText(const wxString& text)

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

Parameters

text

Text 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 wxTextCtrl::<< and wxTextCtrl::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.

### wxTextCtrl::XYToPosition

long XYToPosition(long x, long y)

Converts the given zero based column and line number to a position.

Parameters

x

The column number.

y

The line number.

Return value

The position value.

### wxTextCtrl::operator <<

wxTextCtrl& operator <<(const wxString& s)

wxTextCtrl& operator <<(int i)

wxTextCtrl& operator <<(long i)

wxTextCtrl& operator <<(float f)

wxTextCtrl& operator <<(double d)

wxTextCtrl& operator <<(char c)

Operator definitions for appending to a text control, for example:

      wxTextCtrl *wnd = new wxTextCtrl(my_frame);

      (*wnd) << "Welcome to text control number " << 1 << ".\n";