OpenTTD Source: Textbuf Struct Reference

OpenTTD Source 15.3

Public Member Functions | Data Fields | Private Member Functions | Private Attributes
Textbuf Struct Reference

Helper/buffer for input fields. More...

`#include <textbuf_type.h>`

## Public Member Functions
| Textbuf (uint16_t max_bytes, uint16_t max_chars=UINT16_MAX)
| Initialize the textbuffer by supplying it the buffer to write into and the maximum length of this buffer.
void | Assign (std::string_view text)
| Copy a string into the textbuffer.
void | DeleteAll ()
| Delete every character in the textbuffer.
bool | InsertClipboard ()
| Insert a chunk of text from the clipboard onto the textbuffer.
bool | InsertChar (char32_t key)
| Insert a character to a textbuffer.
bool | InsertString (std::string_view str, bool marked, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
| Insert a string into the text buffer.
bool | DeleteChar (uint16_t keycode)
| Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete from the position the caret is at.
bool | MovePos (uint16_t keycode)
| Handle text navigation with arrow keys left/right.
HandleKeyPressResult | HandleKeyPress (char32_t key, uint16_t keycode)
bool | HandleCaret ()
| Handle the flashing of the caret.
void | UpdateSize ()
| Update Textbuf type with its actual physical character and screenlength Get the count of characters in the string as well as the width in pixels.
void | DiscardMarkedText (bool update=true)
| Discard any marked text.
std::string_view | GetText () const
| Get the current text.

## Data Fields
CharSetFilter | afilter
| Allowed characters.
uint16_t | max_bytes
| the maximum size of the buffer in bytes (including terminating '\0')
uint16_t | max_chars
| the maximum size of the buffer in characters (including terminating '\0')
uint16_t | chars
| the current size of the string in characters (including terminating '\0')
uint16_t | pixels
| the current size of the string in pixels
bool | caret
| is the caret ("_") visible or not
uint16_t | caretpos
| the current position of the caret in the buffer, in bytes
uint16_t | caretxoffs
| the current position of the caret in pixels
uint16_t | markpos
| the start position of the marked area in the buffer, in bytes
uint16_t | markend
| the end position of the marked area in the buffer, in bytes
uint16_t | markxoffs
| the start position of the marked area in pixels
uint16_t | marklength
| the length of the marked area in pixels

## Private Member Functions
bool | CanDelChar (bool backspace)
| Checks if it is possible to delete a character.
bool | MovePrev (StringIterator::IterType what)
| Move to previous character position.
bool | MoveNext (StringIterator::IterType what)
| Move to next character position.
void | DeleteText (uint16_t from, uint16_t to, bool update)
| Delete a part of the text.
void | UpdateStringIter ()
| Update the character iter after the text has changed.
void | UpdateWidth ()
| Update pixel width of the text.
void | UpdateCaretPosition ()
| Update pixel position of the caret.
void | UpdateMarkedText ()
| Update pixel positions of the marked text area.

## Private Attributes
std::string | buf
| buffer in which text is saved
std::unique_ptr< StringIterator > | char_iter

## Detailed Description

Helper/buffer for input fields.

Definition at line 30 of file textbuf_type.h.

## Constructor & Destructor Documentation

## ◆ Textbuf()

Textbuf::Textbuf | ( | uint16_t | max_bytes,
|  | uint16_t | max_chars = `UINT16_MAX`
| ) |  |
| explicit

Initialize the textbuffer by supplying it the buffer to write into and the maximum length of this buffer.

Parameters

  max_bytes | maximum size in bytes, including terminating '\0'
  max_chars | maximum size in chars, including terminating '\0'

Definition at line 403 of file textbuf.cpp.

References afilter, caret, CS_ALPHANUMERAL, DeleteAll(), max_bytes, and max_chars.

## Member Function Documentation

## ◆ Assign()

void Textbuf::Assign | ( | std::string_view | text | ) |

Copy a string into the textbuffer.

Parameters

  text | Source.

Definition at line 420 of file textbuf.cpp.

References buf, max_bytes, max_chars, StrMakeValid(), and UpdateSize().

Referenced by AutoCompletion::AutoComplete(), SaveLoadWindow::GenerateFileName(), IConsoleHistoryNavigate(), SaveLoadWindow::OnClick(), SavePresetWindow::OnClick(), OskWindow::OnClick(), SavePresetWindow::SavePresetWindow(), and ScriptDebugWindow::ScriptDebugWindow().

## ◆ CanDelChar()

bool Textbuf::CanDelChar | ( | bool | backspace | ) |
| private

Checks if it is possible to delete a character.

Parameters

  backspace | if set, delete the character before the caret, otherwise, delete the character after it.

Returns
  true if a character can be deleted in the given direction.

Definition at line 40 of file textbuf.cpp.

References buf, and caretpos.

Referenced by DeleteChar().

## ◆ DeleteAll()

void Textbuf::DeleteAll | ( |  | ) |

Delete every character in the textbuffer.

Definition at line 112 of file textbuf.cpp.

References buf, caretpos, caretxoffs, chars, markend, marklength, markpos, markxoffs, pixels, and UpdateStringIter().

Referenced by Window::HandleEditBoxKey(), IConsoleHistoryNavigate(), QueryStringWindow::OnClick(), and Textbuf().

## ◆ DeleteChar()

bool Textbuf::DeleteChar | ( | uint16_t | keycode | ) |

Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete from the position the caret is at.

Parameters

  keycode | Type of deletion, either WKC_BACKSPACE or WKC_DELETE

Returns
  Return true on successful change of Textbuf, or false otherwise

Definition at line 51 of file textbuf.cpp.

References buf, CanDelChar(), caretpos, chars, Utf8View::GetIterAtByte(), StringIterator::ITER_CHARACTER, StringIterator::ITER_WORD, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), UpdateWidth(), and Utf8StringLength().

Referenced by OskWindow::OnClick().

## ◆ DeleteText()

void Textbuf::DeleteText | ( | uint16_t | from,
|  | uint16_t | to,
|  | bool | update
| ) |  |
| private

Delete a part of the text.

Parameters

  from | Start of the text to delete.
  to | End of the text to delete.
  update | Set to true if the internal state should be updated.

Definition at line 237 of file textbuf.cpp.

References buf, caretpos, chars, markend, markpos, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), and Utf8StringLength().

Referenced by DiscardMarkedText(), and InsertString().

## ◆ DiscardMarkedText()

void Textbuf::DiscardMarkedText | ( | bool | update = `true` | ) |

Discard any marked text.

Parameters

  update | Set to true if the internal state should be updated.

Definition at line 272 of file textbuf.cpp.

References DeleteText(), markend, marklength, markpos, and markxoffs.

Referenced by InsertString().

## ◆ GetText()

std::string_view Textbuf::GetText | ( |  | ) | const

Get the current text.

Returns
  Current text.

Definition at line 284 of file textbuf.cpp.

References buf.

Referenced by AutoCompletion::AutoComplete(), NetworkGameWindow::BuildGUINetworkGameList(), QueryString::GetBoundingRect(), QueryString::GetCharAtPosition(), IConsoleWindow::GetTextBoundingRect(), IConsoleWindow::GetTextCharacterAtPosition(), Window::HandleEditBoxKey(), NetworkChatWindow::OnClick(), SavePresetWindow::OnClick(), SignWindow::OnClick(), BuildVehicleWindow::OnEditboxChanged(), SaveLoadWindow::OnEditboxChanged(), IndustryDirectoryWindow::OnEditboxChanged(), NetworkContentListWindow::OnEditboxChanged(), NetworkGameWindow::OnEditboxChanged(), PickerWindow::OnEditboxChanged(), ScriptDebugWindow::OnEditboxChanged(), GameOptionsWindow::OnEditboxChanged(), TownDirectoryWindow::OnEditboxChanged(), NewGRFWindow::OnEditboxChanged(), SignListWindow::OnEditboxChanged(), IConsoleWindow::OnKeyPress(), IConsoleWindow::OnPaint(), SignWindow::OnPlaceObject(), SaveLoadWindow::OnTimeout(), NetworkContentListWindow::OpenExternalSearch(), and UpdateOSKOriginalText().

## ◆ HandleCaret()

bool Textbuf::HandleCaret | ( |  | ) |

Handle the flashing of the caret.

Returns
  True if the caret state changes.

Definition at line 463 of file textbuf.cpp.

References caret.

Referenced by IConsoleWindow::OnMouseLoop().

## ◆ HandleKeyPress()

HandleKeyPressResult Textbuf::HandleKeyPress | ( | char32_t | key,
|  | uint16_t | keycode
| ) |  |

Definition at line 475 of file textbuf.cpp.

## ◆ InsertChar()

bool Textbuf::InsertChar | ( | char32_t | key | ) |

Insert a character to a textbuffer.

If maxwidth of the Textbuf is zero, we don't care about the visual-length but only about the physical length of the string

Parameters

  key | Character to be inserted

Returns
  Return true on successful change of Textbuf, or false otherwise

Definition at line 128 of file textbuf.cpp.

References buf, caretpos, chars, EncodeUtf8(), max_bytes, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), and UpdateWidth().

Referenced by OskWindow::OnClick().

## ◆ InsertClipboard()

bool Textbuf::InsertClipboard | ( |  | ) |

Insert a chunk of text from the clipboard onto the textbuffer.

Get TEXT clipboard and append this up to the maximum length (either absolute or screenlength). If maxlength is zero, we don't care about the screenlength but only about the physical length of the string

Returns
  true on successful change of Textbuf, or false otherwise

Definition at line 223 of file textbuf.cpp.

References GetClipboardContents(), and InsertString().

## ◆ InsertString()

bool Textbuf::InsertString | ( | std::string_view | str,
|  | bool | marked,
|  | std::optional< size_t > | caret = `std::nullopt`,
|  | std::optional< size_t > | insert_location = `std::nullopt`,
|  | std::optional< size_t > | replacement_end = `std::nullopt`
| ) |  |

Insert a string into the text buffer.

If maxwidth of the Textbuf is zero, we don't care about the visual-length but only about the physical length of the string.

Parameters

  str | String to insert.
  marked | Replace the currently marked text with the new text.
  caret | Move the caret to this point in the insertion string.
  insert_location | Position at which to insert the string.
  replacement_end | Replace all characters from #insert_location up to this location with the new string.

Returns
  True on successful change of Textbuf, or false otherwise.

Definition at line 157 of file textbuf.cpp.

References afilter, buf, caret, caretpos, chars, DeleteText(), DiscardMarkedText(), IsValidChar(), markend, marklength, markpos, max_chars, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), and UpdateWidth().

Referenced by InsertClipboard(), Window::InsertTextString(), and IConsoleWindow::InsertTextString().

## ◆ MoveNext()

bool Textbuf::MoveNext | ( | StringIterator::IterType | what | ) |
| private

Move to next character position.

Parameters

  what | Move ITER_CHARACTER or ITER_WORD.

Returns
  true iff able to move.

Definition at line 345 of file textbuf.cpp.

References buf, caretpos, StringIterator::END, and UpdateCaretPosition().

Referenced by MovePos().

## ◆ MovePos()

bool Textbuf::MovePos | ( | uint16_t | keycode | ) |

Handle text navigation with arrow keys left/right.

This defines where the caret will blink and the next character interaction will occur

Parameters

  keycode | Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME

Returns
  Return true on successful change of Textbuf, or false otherwise

Definition at line 363 of file textbuf.cpp.

References _current_text_dir, buf, caretpos, StringIterator::ITER_CHARACTER, StringIterator::ITER_WORD, MoveNext(), MovePrev(), TD_LTR, and UpdateCaretPosition().

Referenced by OskWindow::OnClick().

## ◆ MovePrev()

bool Textbuf::MovePrev | ( | StringIterator::IterType | what | ) |
| private

Move to previous character position.

Parameters

  what | Move ITER_CHARACTER or ITER_WORD.

Returns
  true iff able to move.

Definition at line 328 of file textbuf.cpp.

References caretpos, StringIterator::END, and UpdateCaretPosition().

Referenced by MovePos().

## ◆ UpdateCaretPosition()

void Textbuf::UpdateCaretPosition | ( |  | ) |
| private

Update pixel position of the caret.

Definition at line 304 of file textbuf.cpp.

References _current_text_dir, buf, caretpos, caretxoffs, FS_NORMAL, GetCharPosInString(), and TD_LTR.

Referenced by DeleteChar(), DeleteText(), InsertChar(), InsertString(), MoveNext(), MovePos(), MovePrev(), and UpdateSize().

## ◆ UpdateMarkedText()

void Textbuf::UpdateMarkedText | ( |  | ) |
| private

Update pixel positions of the marked text area.

Definition at line 311 of file textbuf.cpp.

References buf, FS_NORMAL, GetCharPosInString(), markend, marklength, markpos, and markxoffs.

Referenced by DeleteChar(), DeleteText(), InsertChar(), InsertString(), and UpdateSize().

## ◆ UpdateSize()

void Textbuf::UpdateSize | ( |  | ) |

Update Textbuf type with its actual physical character and screenlength Get the count of characters in the string as well as the width in pixels.

Useful when copying in a larger amount of text at once

Definition at line 445 of file textbuf.cpp.

References buf, caretpos, chars, max_chars, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), UpdateWidth(), and Utf8StringLength().

Referenced by Assign(), and Window::UpdateQueryStringSize().

## ◆ UpdateStringIter()

void Textbuf::UpdateStringIter | ( |  | ) |
| private

Update the character iter after the text has changed.

Definition at line 290 of file textbuf.cpp.

References buf, caretpos, and StringIterator::END.

Referenced by DeleteAll(), DeleteChar(), DeleteText(), InsertChar(), InsertString(), and UpdateSize().

## ◆ UpdateWidth()

void Textbuf::UpdateWidth | ( |  | ) |
| private

Update pixel width of the text.

Definition at line 298 of file textbuf.cpp.

References buf, FS_NORMAL, GetStringBoundingBox(), and pixels.

Referenced by DeleteChar(), InsertChar(), InsertString(), and UpdateSize().

## Field Documentation

## ◆ afilter

CharSetFilter Textbuf::afilter

Allowed characters.

Definition at line 31 of file textbuf_type.h.

Referenced by InsertString(), OskWindow::OnClick(), Textbuf(), and OskWindow::UpdateOskState().

## ◆ buf

std::string Textbuf::buf
| private

buffer in which text is saved

Definition at line 69 of file textbuf_type.h.

Referenced by Assign(), CanDelChar(), DeleteAll(), DeleteChar(), DeleteText(), GetText(), InsertChar(), InsertString(), MoveNext(), MovePos(), UpdateCaretPosition(), UpdateMarkedText(), UpdateSize(), UpdateStringIter(), and UpdateWidth().

## ◆ caret

bool Textbuf::caret

is the caret ("_") visible or not

Definition at line 36 of file textbuf_type.h.

Referenced by HandleCaret(), InsertString(), IConsoleWindow::OnPaint(), and Textbuf().

## ◆ caretpos

uint16_t Textbuf::caretpos

the current position of the caret in the buffer, in bytes

Definition at line 37 of file textbuf_type.h.

Referenced by CanDelChar(), DeleteAll(), DeleteChar(), DeleteText(), InsertChar(), InsertString(), MoveNext(), MovePos(), MovePrev(), UpdateCaretPosition(), UpdateSize(), and UpdateStringIter().

## ◆ caretxoffs

uint16_t Textbuf::caretxoffs

the current position of the caret in pixels

Definition at line 38 of file textbuf_type.h.

Referenced by DeleteAll(), IConsoleWindow::GetCaretPosition(), QueryString::GetCaretPosition(), IConsoleWindow::OnPaint(), ScrollEditBoxTextRect(), and UpdateCaretPosition().

## ◆ char_iter

std::unique_ptr<StringIterator> Textbuf::char_iter
| private

Definition at line 70 of file textbuf_type.h.

## ◆ chars

uint16_t Textbuf::chars

the current size of the string in characters (including terminating '\0')

Definition at line 34 of file textbuf_type.h.

Referenced by DeleteAll(), DeleteChar(), DeleteText(), InsertChar(), InsertString(), and UpdateSize().

## ◆ markend

uint16_t Textbuf::markend

the end position of the marked area in the buffer, in bytes

Definition at line 40 of file textbuf_type.h.

Referenced by DeleteAll(), DeleteText(), DiscardMarkedText(), InsertString(), and UpdateMarkedText().

## ◆ marklength

uint16_t Textbuf::marklength

the length of the marked area in pixels

Definition at line 42 of file textbuf_type.h.

Referenced by DeleteAll(), DiscardMarkedText(), InsertString(), IConsoleWindow::OnPaint(), and UpdateMarkedText().

## ◆ markpos

uint16_t Textbuf::markpos

the start position of the marked area in the buffer, in bytes

Definition at line 39 of file textbuf_type.h.

Referenced by DeleteAll(), DeleteText(), DiscardMarkedText(), InsertString(), and UpdateMarkedText().

## ◆ markxoffs

uint16_t Textbuf::markxoffs

the start position of the marked area in pixels

Definition at line 41 of file textbuf_type.h.

Referenced by DeleteAll(), DiscardMarkedText(), IConsoleWindow::OnPaint(), and UpdateMarkedText().

## ◆ max_bytes

uint16_t Textbuf::max_bytes

the maximum size of the buffer in bytes (including terminating '\0')

Definition at line 32 of file textbuf_type.h.

Referenced by Assign(), InsertChar(), and Textbuf().

## ◆ max_chars

uint16_t Textbuf::max_chars

the maximum size of the buffer in characters (including terminating '\0')

Definition at line 33 of file textbuf_type.h.

Referenced by Assign(), InsertString(), Textbuf(), and UpdateSize().

## ◆ pixels

uint16_t Textbuf::pixels

the current size of the string in pixels

Definition at line 35 of file textbuf_type.h.

Referenced by DeleteAll(), IConsoleWindow::GetCaretPosition(), IConsoleWindow::GetTextBoundingRect(), IConsoleWindow::GetTextCharacterAtPosition(), IConsoleWindow::OnPaint(), ScrollEditBoxTextRect(), and UpdateWidth().

The documentation for this struct was generated from the following files:

- src/textbuf_type.h
- src/textbuf.cpp