OpenTTD Source  20241108-master-g80f628063a
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. More...
 
void Assign (StringID string)
 Render a string into the textbuffer. More...
 
void Assign (const std::string_view text)
 Copy a string into the textbuffer. More...
 
void DeleteAll ()
 Delete every character in the textbuffer.
 
bool InsertClipboard ()
 Insert a chunk of text from the clipboard onto the textbuffer. More...
 
bool InsertChar (char32_t key)
 Insert a character to a textbuffer. More...
 
bool InsertString (const char *str, bool marked, const char *caret=nullptr, const char *insert_location=nullptr, const char *replacement_end=nullptr)
 Insert a string into the text buffer. More...
 
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. More...
 
bool MovePos (uint16_t keycode)
 Handle text navigation with arrow keys left/right. More...
 
HandleKeyPressResult HandleKeyPress (char32_t key, uint16_t keycode)
 
bool HandleCaret ()
 Handle the flashing of the caret. More...
 
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. More...
 
void DiscardMarkedText (bool update=true)
 Discard any marked text. More...
 
const char * GetText () const
 Get the current text. More...
 

Data Fields

CharSetFilter afilter
 Allowed characters.
 
char *const buf
 buffer in which text is saved
 
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 bytes
 the current size of the string in bytes (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. More...
 
bool MovePrev (StringIterator::IterType what)
 Move to previous character position. More...
 
bool MoveNext (StringIterator::IterType what)
 Move to next character position. More...
 
void DeleteText (uint16_t from, uint16_t to, bool update)
 Delete a part of the text. More...
 
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::unique_ptr< StringIteratorchar_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_bytesmaximum size in bytes, including terminating '\0'
max_charsmaximum size in chars, including terminating '\0'

Definition at line 409 of file textbuf.cpp.

Member Function Documentation

◆ Assign() [1/2]

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

Copy a string into the textbuffer.

Parameters
textSource.

Definition at line 440 of file textbuf.cpp.

References buf, bytes, max_bytes, max_chars, StrMakeValidInPlace(), SVS_NONE, UpdateSize(), Utf8PrevChar(), and Utf8StringLength().

◆ Assign() [2/2]

void Textbuf::Assign ( StringID  string)

Render a string into the textbuffer.

Parameters
stringString

Definition at line 431 of file textbuf.cpp.

References GetString().

Referenced by SaveLoadWindow::GenerateFileName(), SavePresetWindow::SavePresetWindow(), and ScriptDebugWindow::ScriptDebugWindow().

◆ CanDelChar()

bool Textbuf::CanDelChar ( bool  backspace)
private

Checks if it is possible to delete a character.

Parameters
backspaceif 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 bytes, and caretpos.

◆ 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
keycodeType 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.

◆ DeleteText()

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

Delete a part of the text.

Parameters
fromStart of the text to delete.
toEnd of the text to delete.
updateSet to true if the internal state should be updated.

Definition at line 237 of file textbuf.cpp.

Referenced by DiscardMarkedText().

◆ DiscardMarkedText()

void Textbuf::DiscardMarkedText ( bool  update = true)

Discard any marked text.

Parameters
updateSet to true if the internal state should be updated.

Definition at line 278 of file textbuf.cpp.

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

◆ GetText()

const char * Textbuf::GetText ( ) const

Get the current text.

Returns
Current text.

Definition at line 290 of file textbuf.cpp.

References buf.

◆ HandleCaret()

bool Textbuf::HandleCaret ( )

Handle the flashing of the caret.

Returns
True if the caret state changes.

Definition at line 489 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
keyCharacter to be inserted
Returns
Return true on successful change of Textbuf, or false otherwise

Definition at line 130 of file textbuf.cpp.

◆ 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 ( const char *  str,
bool  marked,
const char *  caret = nullptr,
const char *  insert_location = nullptr,
const char *  replacement_end = nullptr 
)

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
strString to insert.
markedReplace the currently marked text with the new text.
caretMove the caret to this point in the insertion string.
insert_locationPosition at which to insert the string.
replacement_endReplace 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 160 of file textbuf.cpp.

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

◆ MoveNext()

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

Move to next character position.

Parameters
whatMove ITER_CHARACTER or ITER_WORD.
Returns
true iff able to move.

Definition at line 351 of file textbuf.cpp.

References bytes, caretpos, and StringIterator::END.

◆ 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
keycodeDirection 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 369 of file textbuf.cpp.

◆ MovePrev()

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

Move to previous character position.

Parameters
whatMove ITER_CHARACTER or ITER_WORD.
Returns
true iff able to move.

Definition at line 334 of file textbuf.cpp.

References caretpos, and StringIterator::END.

◆ 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 463 of file textbuf.cpp.

References buf, bytes, and chars.

Referenced by Assign().


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