Public Member Functions | Data Fields | Private Member Functions

Textbuf Struct Reference

Helper/buffer for input fields. More...

#include <textbuf_type.h>

Public Member Functions

 Textbuf (uint16 max_bytes, uint16 max_chars=UINT16_MAX)
 Initialize the textbuffer by supplying it the buffer to write into and the maximum length of this buffer.
void Assign (StringID string)
 Render a string into the textbuffer.
void Assign (const char *text)
 Copy a string into the textbuffer.
void CDECL Print (const char *format,...) WARN_FORMAT(2
 Print a formatted string into the textbuffer.
void CDECL void DeleteAll ()
 Delete every character in the textbuffer.
bool DeleteChar (int delmode)
 Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete from the position the caret is at.
bool InsertChar (uint32 key)
bool InsertClipboard ()
 Insert a chunk of text from the clipboard onto the textbuffer.
bool MovePos (int navmode)
 Handle text navigation with arrow keys left/right.
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.

Data Fields

CharSetFilter afilter
 Allowed characters.
char *const buf
 buffer in which text is saved
uint16 max_bytes
 the maximum size of the buffer in bytes (including terminating '')
uint16 max_chars
 the maximum size of the buffer in characters (including terminating '')
uint16 bytes
 the current size of the string in bytes (including terminating '')
uint16 chars
 the current size of the string in characters (including terminating '')
uint16 pixels
 the current size of the string in pixels
bool caret
 is the caret ("_") visible or not
uint16 caretpos
 the current position of the caret in the buffer, in bytes
uint16 caretxoffs
 the current position of the caret in pixels

Private Member Functions

bool CanDelChar (bool backspace)
 Checks if it is possible to delete a character.
WChar GetNextDelChar (bool backspace)
 Get the next character that will be removed by DelChar.
void DelChar (bool backspace)
 Delete a character at the caret position in a text buf.
bool CanMoveCaretLeft ()
 Checks if it is possible to move caret to the left.
WChar MoveCaretLeft ()
 Moves the caret to the left.
bool CanMoveCaretRight ()
 Checks if it is possible to move caret to the right.
WChar MoveCaretRight ()
 Moves the caret to the right.

Detailed Description

Helper/buffer for input fields.

Definition at line 19 of file textbuf_type.h.


Constructor & Destructor Documentation

Textbuf::Textbuf ( uint16  max_bytes,
uint16  max_chars = UINT16_MAX 
) [explicit]

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

Parameters:
buf the buffer that will be holding the data for input
max_bytes maximum size in bytes, including terminating ''
max_chars maximum size in chars, including terminating ''

Definition at line 361 of file textbuf.cpp.

References afilter, caret, and DeleteAll().


Member Function Documentation

void Textbuf::Assign ( StringID  string  ) 

Render a string into the textbuffer.

Parameters:
string String

Definition at line 383 of file textbuf.cpp.

References buf, max_bytes, and UpdateSize().

Referenced by AIDebugWindow::AIDebugWindow(), NetworkChatWindow::ChatTabCompletion(), IConsoleHistoryNavigate(), OskWindow::OnClick(), and SaveLoadWindow::OnClick().

void Textbuf::Assign ( const char *  text  ) 

Copy a string into the textbuffer.

Parameters:
text Source.

Definition at line 393 of file textbuf.cpp.

References buf, max_bytes, ttd_strlcpy(), and UpdateSize().

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

Referenced by DelChar(), DeleteChar(), and GetNextDelChar().

bool Textbuf::CanMoveCaretLeft (  )  [private]

Checks if it is possible to move caret to the left.

Returns:
true if the caret can be moved to the left, otherwise false.

Definition at line 227 of file textbuf.cpp.

References caretpos.

Referenced by MoveCaretLeft(), and MovePos().

bool Textbuf::CanMoveCaretRight (  )  [private]

Checks if it is possible to move caret to the right.

Returns:
true if the caret can be moved to the right, otherwise false.

Definition at line 254 of file textbuf.cpp.

References bytes, and caretpos.

Referenced by MoveCaretRight(), and MovePos().

void Textbuf::DelChar ( bool  backspace  )  [private]

Delete a character at the caret position in a text buf.

Parameters:
backspace if set, delete the character before the caret, else delete the character after it.
Warning:
You should ensure Textbuf::CanDelChar returns true before calling this function.

Definition at line 74 of file textbuf.cpp.

References buf, bytes, CanDelChar(), caretpos, caretxoffs, chars, FS_NORMAL, GetCharacterWidth(), pixels, Utf8Decode(), and Utf8PrevChar().

Referenced by DeleteChar().

bool Textbuf::DeleteChar ( int  delmode  ) 

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

Parameters:
delmode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
Returns:
Return true on successful change of Textbuf, or false otherwise

Definition at line 104 of file textbuf.cpp.

References CanDelChar(), DelChar(), GetNextDelChar(), and IsWhitespace().

Referenced by OskWindow::OnClick(), and IConsoleWindow::OnKeyPress().

WChar Textbuf::GetNextDelChar ( bool  backspace  )  [private]

Get the next character that will be removed by DelChar.

Parameters:
backspace if set, delete the character before the caret, otherwise, delete the character after it.
Returns:
the next character that will be removed by DelChar.
Warning:
You should ensure Textbuf::CanDelChar returns true before calling this function.

Definition at line 52 of file textbuf.cpp.

References buf, CanDelChar(), caretpos, Utf8Decode(), and Utf8PrevChar().

Referenced by DeleteChar().

bool Textbuf::HandleCaret (  ) 

Handle the flashing of the caret.

Returns:
True if the caret state changes.

Definition at line 442 of file textbuf.cpp.

References caret.

Referenced by IConsoleWindow::OnMouseLoop().

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

References afilter, buf, bytes, caretpos, caretxoffs, chars, FS_NORMAL, GetCharacterWidth(), GetClipboardContents(), IsValidChar(), lengthof, max_bytes, max_chars, pixels, and Utf8CharLen().

Referenced by IConsoleWindow::OnKeyPress().

WChar Textbuf::MoveCaretLeft (  )  [private]

Moves the caret to the left.

Precondition:
Ensure that Textbuf::CanMoveCaretLeft returns true
Returns:
The character under the caret.

Definition at line 237 of file textbuf.cpp.

References buf, CanMoveCaretLeft(), caretpos, caretxoffs, FS_NORMAL, GetCharacterWidth(), Utf8Decode(), and Utf8PrevChar().

Referenced by MovePos().

WChar Textbuf::MoveCaretRight (  )  [private]

Moves the caret to the right.

Precondition:
Ensure that Textbuf::CanMoveCaretRight returns true
Returns:
The character under the caret.

Definition at line 264 of file textbuf.cpp.

References buf, CanMoveCaretRight(), caretpos, caretxoffs, FS_NORMAL, GetCharacterWidth(), and Utf8Decode().

Referenced by MovePos().

bool Textbuf::MovePos ( int  navmode  ) 

Handle text navigation with arrow keys left/right.

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

Parameters:
navmode 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 282 of file textbuf.cpp.

References bytes, CanMoveCaretLeft(), CanMoveCaretRight(), caretpos, caretxoffs, IsWhitespace(), MoveCaretLeft(), MoveCaretRight(), and pixels.

Referenced by OskWindow::OnClick(), and IConsoleWindow::OnKeyPress().

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

References buf, bytes, caretpos, caretxoffs, chars, FS_NORMAL, GetCharacterWidth(), max_bytes, max_chars, pixels, and Utf8CharLen().

Referenced by Assign(), SaveLoadWindow::GenerateFileName(), and Print().


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