wxTextCtrl(3erl)

πŸ¦‹ wxTextCtrl(3erl) β€” Erlang Module Definition, part 1 πŸ¦‹

🌸 NAME 🌸

wxTextCtrl β€” functions for the wxTextCtrl class. Periodt. πŸ’…

✨ DESCRIPTION ✨

okay so a text control lets text be displayed and edited. It can be single line or multi-line. Bestie, important detail: a lot of the methods of the text controls live in the base wxTextEntry (not implemented in wx) class, which is a common base class for wxTextCtrl and other controls using a single line text entry field (e.g. wxComboBox). 🫢

πŸŽ€ Styles πŸŽ€

This class supports the following styles:

πŸ¦‹ wxTextCtrl Text Format πŸ¦‹

The multiline text controls always store the text as a sequence of lines separated by '\n' characters, i.e. in the Unix text format even on non-Unix platforms. This lets your user code ignore the differences between platforms, but at a price: the indices in the control, such as those returned by getInsertionPoint/1 or getSelection/1, can not be used as indices into the string returned by getValue/1, because they’re going to be slightly off for platforms using "\\r\\n" as separator (as Windows does). 😭

Instead, if you need to obtain a substring between the 2 indices obtained from the control with the help of the functions mentioned above, you should use getRange/3. And the indices themselves can only be passed to other methods, for example setInsertionPoint/2 or setSelection/3. ✨

To summarize: never use the indices returned by (multiline) wxTextCtrl as indices into the string it contains, but only as arguments to be passed back to the other wxTextCtrl methods. This problem doesn’t arise for single-line platforms however, where the indices in the control do correspond to the positions in the value string. πŸŽ€

🌈 wxTextCtrl Positions and Coordinates 🌈

It is possible to use either linear positions, i.e. roughly (but not always exactly, as explained in the previous section) the index of the character in the text contained in the control, or X-Y coordinates, i.e. column and line of the character, when working with this class β€” and it provides the functions positionToXY/2 and xYToPosition/3 to convert between the two. πŸ¦‹

Additionally, a position in the control can be converted to its coordinates in pixels using PositionToCoords() (not implemented in wx), which can be useful to e.g. show a popup menu near the given character. And, in the other direction, HitTest() (not implemented in wx) can be used to find the character under, or near, the given pixel coordinates. πŸ‘‘

To be more precise, positions actually refer to the gaps between characters and not the characters themselves. Thus, position 0 is the one before the very first character in the control, and so is a valid position even when the control is empty. And if the control contains a single character, it has two valid positions: 0 before this character and 1 β€” after it. So when the documentation of various functions mentions "invalid position", it doesn’t consider the position just after the last character of the line to be invalid; only the positions beyond that one (e.g. 2 and greater in the single character example) are actually invalid. πŸ’€

πŸ’… wxTextCtrl Styles. πŸ’…

Multi-line text controls support styling, i.e. provide a possibility to set colours and font for individual characters in it (note that under Windows wxTE_RICH style is required for style support). To use the styles you can either call setDefaultStyle/2 before inserting the text, or call setStyle/4 later to change the style of the text already in the control (the first solution is much more efficient). πŸ”₯

In either case, if the style doesn’t specify some of the attributes (for example you only want to set the text colour but without changing the font nor the text background), the values of the default style will be used for them. If there is no default style, the attributes of the text control itself are used. 🌸

So the following code correctly describes what it does: the second call to setDefaultStyle/2 doesn’t change the text foreground colour (which stays red) while the last one doesn’t change the background colour (which stays grey):

🧚 wxTextCtrl and C++ Streams 🧚

This class multiply-inherits from std::streambuf (except for some really old compilers using non-standard iostream library), allowing code such as the following:

Note that even if your build of wxWidgets doesn’t support this (the symbol wxHAS_TEXT_WINDOW_STREAM has value of 0 then) you can still use wxTextCtrl itself in a stream-like manner:

However the possibility to create a std::ostream associated with wxTextCtrl may be useful if you need to redirect the output of a function taking a std::ostream as parameter to a text control. 🫢

Another commonly requested need is to redirect std::cout to the text control. This may be done in the following way:

But wxWidgets provides a convenient class to make it even simpler, so instead you may just do

See wxStreamToTextRedirector (not implemented in wx) for more details. ✨

🌟 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. πŸ‘‘

See: create/4, wxValidator (not implemented in wx)

This class is derived (and can use functions) from: wxControl wxWindow wxEvtHandler

wxWidgets docs: wxTextCtrl

πŸŽ‰ EVENTS πŸŽ‰

Event types emitted from this class: command_text_updated, command_text_enter, text_maxlen πŸ’–

πŸ“ DATA TYPES πŸ“

wxTextCtrl() = wx:wx_object()

πŸ‘‘ EXPORTS πŸ‘‘

new()

new() -> wxTextCtrl()

Default ctor. πŸ’…

new(Parent, Id)

new(Parent, Id) -> wxTextCtrl()

Types:

Parent = wxWindow:wxWindow()
Id = integer()

new(Parent, Id, Options :: [Option])

new(Parent, Id, Options :: [Option]) -> wxTextCtrl()

Types:

Parent = wxWindow:wxWindow()
Id = integer()
Option =
    {value, unicode:chardata()} |
    {pos, {X :: integer(), Y :: integer()}} |
    {size, {W :: integer(), H :: integer()}} |
    {style, integer()} |
    {validator, wx:wx_object()}

Constructor, creating and showing a text control. ✨

Remark: The horizontal scrollbar (wxHSCROLL 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. πŸ‘€

See: create/4, wxValidator (not implemented in wx)

destroy(This :: wxTextCtrl())

destroy(This :: wxTextCtrl()) -> ok

Destructor, destroying the text control. πŸ’€

appendText(This, Text)

appendText(This, Text) -> ok

Types:

This = wxTextCtrl()
Text = unicode:chardata()

Appends the text to the end of the text control. 🧚

Remark: 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/1 and setInsertionPoint/2. πŸŽ€

See: writeText/2

canCopy(This)

canCopy(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if the selection can be copied to the clipboard. πŸ’–

canCut(This)

canCut(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if the selection can be cut to the clipboard. βœ‚οΈ

canPaste(This)

canPaste(This) -> boolean()

Types:

This = wxTextCtrl()

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. πŸŽ€

canRedo(This)

canRedo(This) -> boolean()

Types:

This = wxTextCtrl()

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

canUndo(This)

canUndo(This) -> boolean()

Types:

This = wxTextCtrl()

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

clear(This)

clear(This) -> ok

Types:

This = wxTextCtrl()

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(""). πŸ’…

copy(This)

copy(This) -> ok

Types:

This = wxTextCtrl()

Copies the selected text to the clipboard. πŸ“‹

create(This, Parent, Id)

create(This, Parent, Id) -> boolean()

Types:

This = wxTextCtrl()
Parent = wxWindow:wxWindow()
Id = integer()

create(This, Parent, Id, Options :: [Option])

create(This, Parent, Id, Options :: [Option]) -> boolean()

Types:

This = wxTextCtrl()
Parent = wxWindow:wxWindow()
Id = integer()
Option =
    {value, unicode:chardata()} |
    {pos, {X :: integer(), Y :: integer()}} |
    {size, {W :: integer(), H :: integer()}} |
    {style, integer()} |
    {validator, wx:wx_object()}

Creates the text control for two-step construction. πŸ› οΈ

This method should be called if the default constructor was used for the control creation. Its parameters have the same meaning as for the non-default constructor. ✨

cut(This)

cut(This) -> ok

Types:

This = wxTextCtrl()

Copies the selected text to the clipboard and removes it from the control. βœ‚οΈπŸ“‹

discardEdits(This)

discardEdits(This) -> ok

Types:

This = wxTextCtrl()

Resets the internal modified flag as if the current changes had been saved. 🧘

changeValue(This, Value)

changeValue(This, Value) -> ok

Types:

This = wxTextCtrl()
Value = unicode:chardata()

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/2. πŸŽ€

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

This function does not generate the wxEVT_TEXT event but otherwise is identical to setValue/2. πŸ‘‘

See overview_events_prog for more information.

Since: 2.7.1

emulateKeyPress(This, Event)

emulateKeyPress(This, Event) -> boolean()

Types:

This = wxTextCtrl()
Event = wxKeyEvent:wxKeyEvent()

This function inserts into the control the character which would have been inserted if the given key event had occurred in the text control. ⌨️✨

Okay so about that event object β€” it should be literally the same one that wxWidgets handed to your EVT_KEY_DOWN handler earlier, no substitutes, no lookalikes bestie 🫢

Also flagging, not to be dramatic but: this function doesn't currently work correctly for all keys under any platform but MSW. πŸ’€ So like... MSW or bust, iykyk.

Return: true if the event resulted in a change to the control, false otherwise.

getDefaultStyle(This) -> wxTextAttr:wxTextAttr()

Types:

This = wxTextCtrl()

Returns the style that's currently being used for new text ✨

See: setDefaultStyle/2

getInsertionPoint(This) -> integer()

Types:

This = wxTextCtrl()

Returns the insertion point (a.k.a. the cursor) position, bestie πŸ‘‘

Here's the thing: it's defined as the zero based index of the character position to the right of the insertion point. So like, if your insertion point is chillin' at the end of a single-line text control, it equals getLastPosition/1. Slay.

Now the important lore: the insertion position is, in general, different from the index of the character the cursor sits at in the string you get back from getValue/1. For single line controls this is always true, but multi-line controls can use two characters "\r\n" as the line separator (this is notably the case under MSW 😭), which means indices in the control and its string value are offset by 1 for every line. Yeah. Every. Single. Line.

So to correctly get the character at the current cursor position β€” keeping in mind there might be none at all if the cursor is at the end of the string β€” you could do the following:

getLastPosition(This) -> integer()

Types:

This = wxTextCtrl()

Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control. That's it, that's the whole vibe ✨

getLineLength(This, LineNo) -> integer()

Types:

This = wxTextCtrl()
LineNo = integer()

Gets the length of the line you asked for, not including any trailing newline character(s) πŸŽ€

Return: the length of the line, or -1 if LineNo was invalid. (so if you get -1 back, that line does not exist, bestie, it's giving ghost town πŸ‘»)

getLineText(This, LineNo) -> unicode:charlist()

Types:

This = wxTextCtrl()
LineNo = integer()

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

Return: the contents of the line. Periodt.

getNumberOfLines(This) -> integer()

Types:

This = wxTextCtrl()

Returns the number of lines in the text control buffer πŸ’…

Okay so the returned number is the number of logical lines for the wxGTK and wxOSX/Cocoa ports β€” i.e. just the count of the number of newline characters in the control + 1 β€” while it's the number of physical lines for wxMSW, i.e. the count of lines actually shown in the control. Two completely different meanings depending on where you're running 😭 Because of this discrepancy, it is not recommended to use this function. Consider that your vibe check.

Remark: even empty text controls have one line (that's where the insertion point is hanging out), so getNumberOfLines/1 never returns 0. Never. Zero. Nope. 🚫

getRange(This, From, To) -> unicode:charlist()

Types:

This = wxTextCtrl()
From = To = integer()

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. And please note β€” the positions in a multiline wxTextCtrl do not correspond to the indices in the string returned by getValue/1, because of the different new line representations (CR or CR LF). So this method is the one to use to obtain correct results instead of extracting parts of the entire value. Bonus: it may also be more efficient, especially if the control contains a lot of data. Highkey the move πŸ”₯

getSelection(This) -> {From :: integer(), To :: integer()}

Types:

This = wxTextCtrl()

Gets the current selection span ✨

If the returned values are equal, there was no selection. And heads up: the indices returned may be used with the other wxTextCtrl methods, but they don't necessarily represent the correct indices into the string returned by getValue/1 for multiline controls under Windows (at least 😬) β€” for that, you should use getStringSelection/1 to get the selected text.

getStringSelection(This) -> unicode:charlist()

Types:

This = wxTextCtrl()

Gets the text currently selected in the control πŸ’–

If there is no selection, the returned string is empty. That's the whole function, no notes.

getStyle(This, Position, Style) -> boolean()

Types:

This = wxTextCtrl()
Position = integer()
Style = wxTextAttr:wxTextAttr()

Returns the style at this position in the text control πŸŽ€

Not all platforms support this function. (rude, honestly πŸ’€)

Return: true on success, false if an error occurred β€” and this may also mean that the styles are just not supported under this platform, so don't come for it.

See: setStyle/4, wxTextAttr

getValue(This) -> unicode:charlist()

Types:

This = wxTextCtrl()

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're separated by a \r\n sequence in the native control. So consistent. Iconic. That's the good stuff 🌈

isEditable(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if the control's contents may be edited by the user β€” note that it always can be changed by the program, though, so don't get it twisted 🫢

In other words: this function returns true if the control hasn't been put in read-only mode by a previous call to setEditable/2.

isModified(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if the text has been modified by the user πŸ’…

Note that calling setValue/2 doesn't make the control modified. Sneaky little detail, love that for us.

See: markDirty/1

isMultiLine(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if this is a multi line edit control and false otherwise ✨

See: isSingleLine/1

isSingleLine(This) -> boolean()

Types:

This = wxTextCtrl()

Returns true if this is a single line edit control and false otherwise 🌸

See: isSingleLine/1, isMultiLine/1 β€” and yes, the cross-reference genuinely says isSingleLine/1 from inside isSingleLine/1, that's what the source says, iykyk, I'm just the messenger πŸ˜­πŸ™ˆ

loadFile(This, Filename) -> boolean()

Types:

This = wxTextCtrl()
Filename = unicode:chardata()

loadFile(This, Filename, Options :: [Option]) -> boolean()

Types:

This = wxTextCtrl()
Filename = unicode:chardata()
Option = {fileType, integer()}

Loads and displays the named file, if it exists. πŸ“‚

Return: true if successful, false otherwise. No cap.

markDirty(This) -> ok

Types:

This = wxTextCtrl()

Mark text as modified (dirty) πŸ”₯ β€” go on, make it messy.

See: isModified/1

paste(This) -> ok

Types:

This = wxTextCtrl()

Pastes text from the clipboard to the text item. Ctrl+V energy, except you're the one doing it programmatically πŸ’…

positionToXY(This, Pos) -> Result

Types:

Result = {Res :: boolean(), X :: integer(), Y :: integer()}
This = wxTextCtrl()
Pos = integer()

Converts a given position to a zero-based column, line number pair πŸ¦‹

Return: true on success, false on failure (most likely due to a too-large Pos parameter β€” like, that position does not exist bestie 😭).

See: xYToPosition/3

redo(This) -> ok

Types:

This = wxTextCtrl()

If there's a redo facility and the last operation can be redone, redoes the last operation β™»οΈβœ¨

Does nothing if there's no redo facility. Just vibes, no action. πŸ’€

remove(This, From, To) -> ok

Types:

This = wxTextCtrl()
From = To = integer()

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. Sneaky but documented, so it's fine πŸŽ€

replace(This, From, To, Value) -> ok

Types:

This = wxTextCtrl()
From = To = integer()
Value = unicode:chardata()

Replaces the text starting at the first position up to β€” but not including β€” the character at the last position, with the given text πŸ”

Same heads-up as remove: this function puts the current insertion point position at To as a side effect. Yes, again. Yes, on purpose. πŸ’œ

saveFile(This) -> boolean()

Types:

This = wxTextCtrl()

No description, no prose, just pure mystery 😭 the arity-1 version simply exists.

saveFile(This, Options :: [Option]) -> boolean()

Types:

This = wxTextCtrl()
Option = {file, unicode:chardata()} | {fileType, integer()}

Saves the contents of the control in a text file πŸ’ΎπŸŒΈ

Return: true if the operation was successful, false otherwise.

setDefaultStyle(This, Style) -> boolean()

Types:

This = wxTextCtrl()
Style = wxTextAttr:wxTextAttr()

Changes the default style to use for the new text that's going to be added to the control πŸŽ€

This applies both to text added programmatically using writeText/2 or appendText/2 and to text entered by the user interactively. Everyone's included, we love inclusive defaults πŸ’–

If either of the font, foreground, or background colour isn't set in Style, the values of the previous default style are used for them. And if the previous default style didn't set them either, the global font or colours of the text control itself are used as fall back. It's giving inheritance chain, and honestly? It works.

However β€” big however β€” if the Style parameter is the default wxTextAttr, then the default style is just reset, instead of being combined with the new style (which wouldn't change it at all anyway). So don't be out here thinking it merged. It did not. πŸ™ˆ

Return: true on success, false if an error occurred β€” and again, that may also mean the styles are not supported under this platform. Not every platform is a styles girlie πŸ’…

See: getDefaultStyle/1

setEditable(This, Editable) -> ok

Types:

This = wxTextCtrl()
Editable = boolean()

Makes the text item editable or read-only, overriding the wxTE_READONLY flag πŸ‘‘

See: isEditable/1

setInsertionPoint(This, Pos) -> ok

Types:

This = wxTextCtrl()
Pos = integer()

Sets the insertion point at the given position. Drop the cursor right there, bestie πŸ¦‹

setInsertionPointEnd(This) -> ok

Types:

This = wxTextCtrl()

Sets the insertion point at the end of the text control 🏁✨ the classic "jump to the end" move.

...which is literally the same thing as calling setInsertionPoint/2 with the getLastPosition/1 argument. Cute, right? One function doing the glow-up for another πŸ’…


πŸŽ€ setMaxLength(This, Len) -> ok

Types:

This = wxTextCtrl()
Len = integer()

Okay so this one caps how many characters your user is allowed to type into the control. That's it, that's the whole vibe β€” it's the bouncer at the club door πŸšͺ✨

Basically it lets you limit the text value length to len, and bestie, the terminating NUL character does not count toward that. Not invited to the party.

If len is 0, then any previously set max length limit (if there even was one) gets yeeted into the void πŸ™ˆ and the user can enter as much text as the underlying native text control widget supports β€” which is typically at least 32Kb, so like, plenty.

But here's the tea β˜•: if the user tries to shove more characters in when the control is already filled up to the maximal length, a wxEVT_TEXT_MAXLEN event gets sent to notify your program about it (so your program can show an explanatory message, for example β€” very thoughtful of it) and the extra input is straight-up discarded. Sorry babes, no more room 🫢

Heads up: in wxGTK this function may only be used with single line text controls. Single line only, no exceptions, don't @ me.

✨ setSelection(This, From, To) -> ok

Types:

This = wxTextCtrl()
From = To = integer()

Selects the text starting at the first position up to β€” but not including β€” the character at the last position. Half-open range energy, very demure, very mindful πŸŽ€

If both parameters are equal to -1, then literally all the text in the control gets selected. Select-all slay πŸ’…

Notice that the insertion point will be moved to from by this function. So yeah, your cursor is getting relocated, that's part of the deal.

See: SelectAll() (not implemented in wx) β€” yeah, that one's not implemented, so don't come for it 😭


πŸ¦‹ setStyle(This, Start, End, Style) -> boolean()

Types:

This = wxTextCtrl()
Start = End = integer()
Style = wxTextAttr:wxTextAttr()

Changes the style of the given range. Glow-up time for your text ✨🌈

If any attribute within style is not set, then the corresponding attribute from getDefaultStyle/1 is used instead β€” the defaults are the fallback besties, always there for you 🫢

Return: true on success, false if an error occurred. And ngl, false may also mean that the styles are just not supported under this platform, so don't take it personally πŸ’€

See: getStyle/3, wxTextAttr


πŸ’– setValue(This, Value) -> ok

Types:

This = wxTextCtrl()
Value = unicode:chardata()

Sets the new text control value. Simple, iconic, does exactly what it says on the tin πŸ‘‘

It also marks the control as not-modified, which means IsModified() would return false immediately after the call to setValue/2. Clean slate behavior, we love to see it.

The insertion point gets set to the start of the control (i.e. position 0) by this function β€” unless the control value doesn't change at all, in which case the insertion point is left exactly where it originally was. Respectful of your cursor's personal space fr fr πŸ₯Ί

⚠️ Careful bestie: unlike most other functions changing the control's values, this function generates a wxEVT_TEXT event. If you want to avoid that, use changeValue/2 instead. The more you know πŸ’«

🌸 showPosition(This, Pos) -> ok

Types:

This = wxTextCtrl()
Pos = integer()

Makes the line containing the given position visible. Just scrolls your little line into the spotlight, main character energy πŸ‘‘βœ¨


🧚 undo(This) -> ok

Types:

This = wxTextCtrl()

If there is an undo facility and the last operation can be undone, then it undoes the last operation. Ctrl+Z but make it an Erlang function πŸ’«

Does nothing if there is no undo facility. Literally just vibes at that point, sitting there, existing πŸ’€


πŸ“ writeText(This, Text) -> ok

Types:

This = wxTextCtrl()
Text = unicode:chardata()

Writes the text into the text control at the current insertion position. Right where the cursor's at, no teleporting πŸŽ€

Remark: Newlines in the text string are the only control characters allowed, and they will cause appropriate line breaks. Everything else? Not allowed, babes. Those are the rules 🚫

See operator<<() and appendText/2 for more convenient ways of writing to the window β€” those are the shortcuts if you don't wanna do it the manual way πŸ‘€

After the write operation, the insertion point will be at the end of the inserted text, so subsequent write operations will be appended. Nice and tidy, queue up your writes πŸ’…

To append text after the user may have interacted with the control, call setInsertionPointEnd/1 before writing. Don't forget, that's the important part πŸ“Œ


πŸ”₯ xYToPosition(This, X, Y) -> integer()

Types:

This = wxTextCtrl()
X = Y = integer()

Converts the given zero based column and line number to a position. Coordinates to position, the classic glow-up πŸ’«

Return: The position value, or -1 if x or y was invalid. Yeah, invalid coords get a -1, that's the "nope, not today" number 😭


That's the whole page, bestie!! You made it, and honestly? You ate that πŸ’…βœ¨ Go forth and control some text β€” you're gonna slay.

Written by the wxWidgets team 🫢, this is wx 2.2.2.1, and the page is wxTextCtrl(3erl) β€” so go hit up your local erl man lookup when you need her again πŸ’–

πŸ“¦ where this page comes from