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 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 InsertClipboard ()
 Insert a chunk of text from the clipboard onto the textbuffer.
bool InsertChar (uint32 key)
bool InsertString (const char *str, bool marked, const char *caret=NULL, const char *insert_location=NULL, const char *replacement_end=NULL)
 Insert a string into the text buffer.
bool DeleteChar (uint16 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 keycode)
 Handle text navigation with arrow keys left/right.
HandleKeyPressResult HandleKeyPress (WChar key, uint16 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.

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
uint16 markpos
 the start position of the marked area in the buffer, in bytes
uint16 markend
 the end position of the marked area in the buffer, in bytes
uint16 markxoffs
 the start position of the marked area in pixels
uint16 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.
void DeleteText (uint16 from, uint16 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

StringIteratorchar_iter

Detailed Description

Helper/buffer for input fields.

Definition at line 32 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 370 of file textbuf.cpp.

References afilter, caret, StringIterator::Create(), and DeleteAll().


Member Function Documentation

void Textbuf::Assign ( StringID  string  ) 

Render a string into the textbuffer.

Parameters:
string String

Definition at line 395 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 405 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 DeleteChar().

bool Textbuf::DeleteChar ( uint16  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, bytes, CanDelChar(), caretpos, chars, StringIterator::ITER_CHARACTER, StringIterator::ITER_WORD, StringIterator::Next(), StringIterator::Prev(), UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), UpdateWidth(), Utf8Decode(), and Utf8PrevChar().

Referenced by OskWindow::OnClick().

void Textbuf::DeleteText ( uint16  from,
uint16  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 238 of file textbuf.cpp.

References buf, bytes, caretpos, chars, UpdateCaretPosition(), UpdateMarkedText(), and UpdateStringIter().

Referenced by DiscardMarkedText(), and InsertString().

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().

bool Textbuf::HandleCaret (  ) 

Handle the flashing of the caret.

Returns:
True if the caret state changes.

Definition at line 455 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 223 of file textbuf.cpp.

References GetClipboardContents(), InsertString(), and lengthof.

bool Textbuf::InsertString ( const char *  str,
bool  marked,
const char *  caret = NULL,
const char *  insert_location = NULL,
const char *  replacement_end = NULL 
)

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

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

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

bool Textbuf::MovePos ( uint16  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 317 of file textbuf.cpp.

References bytes, caretpos, StringIterator::END, StringIterator::ITER_CHARACTER, StringIterator::ITER_WORD, StringIterator::Next(), StringIterator::Prev(), StringIterator::SetCurPosition(), and UpdateCaretPosition().

Referenced by OskWindow::OnClick().

void Textbuf::UpdateCaretPosition (  )  [private]

Update pixel position of the caret.

Definition at line 295 of file textbuf.cpp.

References buf, caretpos, caretxoffs, chars, FS_NORMAL, and GetCharPosInString().

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

void Textbuf::UpdateMarkedText (  )  [private]

Update pixel positions of the marked text area.

Definition at line 301 of file textbuf.cpp.

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

Referenced by DeleteChar(), DeleteText(), InsertString(), and 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 429 of file textbuf.cpp.

References buf, bytes, caretpos, chars, max_bytes, max_chars, UpdateCaretPosition(), UpdateMarkedText(), UpdateStringIter(), UpdateWidth(), and Utf8CharLen().

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

void Textbuf::UpdateStringIter (  )  [private]

Update the character iter after the text has changed.

Definition at line 281 of file textbuf.cpp.

References buf, caretpos, StringIterator::END, StringIterator::SetCurPosition(), and StringIterator::SetString().

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

void Textbuf::UpdateWidth (  )  [private]

Update pixel width of the text.

Definition at line 289 of file textbuf.cpp.

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

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


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