OpenTTD Source  1.11.2
textbuf_type.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef TEXTBUF_TYPE_H
11 #define TEXTBUF_TYPE_H
12 
13 #include "string_type.h"
14 #include "strings_type.h"
15 #include "string_base.h"
16 
21 {
27 };
28 
30 struct Textbuf {
32  char * const buf;
33  uint16 max_bytes;
34  uint16 max_chars;
35  uint16 bytes;
36  uint16 chars;
37  uint16 pixels;
38  bool caret;
39  uint16 caretpos;
40  uint16 caretxoffs;
41  uint16 markpos;
42  uint16 markend;
43  uint16 markxoffs;
44  uint16 marklength;
45 
46  explicit Textbuf(uint16 max_bytes, uint16 max_chars = UINT16_MAX);
47  ~Textbuf();
48 
49  void Assign(StringID string);
50  void Assign(const char *text);
51  void CDECL Print(const char *format, ...) WARN_FORMAT(2, 3);
52 
53  void DeleteAll();
54  bool InsertClipboard();
55 
56  bool InsertChar(WChar key);
57  bool InsertString(const char *str, bool marked, const char *caret = nullptr, const char *insert_location = nullptr, const char *replacement_end = nullptr);
58 
59  bool DeleteChar(uint16 keycode);
60  bool MovePos(uint16 keycode);
61 
62  HandleKeyPressResult HandleKeyPress(WChar key, uint16 keycode);
63 
64  bool HandleCaret();
65  void UpdateSize();
66 
67  void DiscardMarkedText(bool update = true);
68 
69 private:
70  StringIterator *char_iter;
71 
72  bool CanDelChar(bool backspace);
73 
74  void DeleteText(uint16 from, uint16 to, bool update);
75 
76  void UpdateStringIter();
77  void UpdateWidth();
78  void UpdateCaretPosition();
79  void UpdateMarkedText();
80 };
81 
82 #endif /* TEXTBUF_TYPE_H */
Textbuf::DeleteText
void DeleteText(uint16 from, uint16 to, bool update)
Delete a part of the text.
Definition: textbuf.cpp:240
HKPR_EDITING
@ HKPR_EDITING
Textbuf content changed.
Definition: textbuf_type.h:22
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Textbuf::max_bytes
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
Definition: textbuf_type.h:33
Textbuf::Print
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
Definition: textbuf.cpp:415
HKPR_CONFIRM
@ HKPR_CONFIRM
Return or enter key pressed.
Definition: textbuf_type.h:24
Textbuf::Assign
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:396
Textbuf::MovePos
bool MovePos(uint16 keycode)
Handle text navigation with arrow keys left/right.
Definition: textbuf.cpp:319
Textbuf::caretpos
uint16 caretpos
the current position of the caret in the buffer, in bytes
Definition: textbuf_type.h:39
Textbuf::caretxoffs
uint16 caretxoffs
the current position of the caret in pixels
Definition: textbuf_type.h:40
Textbuf::markpos
uint16 markpos
the start position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:41
strings_type.h
Textbuf::InsertString
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.
Definition: textbuf.cpp:162
Textbuf::pixels
uint16 pixels
the current size of the string in pixels
Definition: textbuf_type.h:37
Textbuf::buf
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:32
Textbuf::HandleCaret
bool HandleCaret()
Handle the flashing of the caret.
Definition: textbuf.cpp:456
StringIterator
Class for iterating over different kind of parts of a string.
Definition: string_base.h:14
Textbuf::marklength
uint16 marklength
the length of the marked area in pixels
Definition: textbuf_type.h:44
HandleKeyPressResult
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition: textbuf_type.h:20
Textbuf::UpdateMarkedText
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition: textbuf.cpp:303
Textbuf::caret
bool caret
is the caret ("_") visible or not
Definition: textbuf_type.h:38
Textbuf::DeleteChar
bool DeleteChar(uint16 keycode)
Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete fro...
Definition: textbuf.cpp:53
Textbuf::UpdateCaretPosition
void UpdateCaretPosition()
Update pixel position of the caret.
Definition: textbuf.cpp:297
Textbuf::afilter
CharSetFilter afilter
Allowed characters.
Definition: textbuf_type.h:31
string_type.h
Textbuf::markend
uint16 markend
the end position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:42
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Textbuf::bytes
uint16 bytes
the current size of the string in bytes (including terminating '\0')
Definition: textbuf_type.h:35
Textbuf::InsertClipboard
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition: textbuf.cpp:225
Textbuf::Textbuf
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 buf...
Definition: textbuf.cpp:371
Textbuf::UpdateSize
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition: textbuf.cpp:430
HKPR_CANCEL
@ HKPR_CANCEL
Escape key pressed.
Definition: textbuf_type.h:25
Textbuf::UpdateStringIter
void UpdateStringIter()
Update the character iter after the text has changed.
Definition: textbuf.cpp:283
Textbuf::chars
uint16 chars
the current size of the string in characters (including terminating '\0')
Definition: textbuf_type.h:36
Textbuf::max_chars
uint16 max_chars
the maximum size of the buffer in characters (including terminating '\0')
Definition: textbuf_type.h:34
HKPR_NOT_HANDLED
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
Definition: textbuf_type.h:26
Textbuf::UpdateWidth
void UpdateWidth()
Update pixel width of the text.
Definition: textbuf.cpp:291
Textbuf::InsertChar
bool InsertChar(WChar key)
Insert a character to a textbuffer.
Definition: textbuf.cpp:132
Textbuf::CanDelChar
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition: textbuf.cpp:42
Textbuf::DeleteAll
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:116
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
HKPR_CURSOR
@ HKPR_CURSOR
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:23
Textbuf
Helper/buffer for input fields.
Definition: textbuf_type.h:30
Textbuf::DiscardMarkedText
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition: textbuf.cpp:274
Textbuf::markxoffs
uint16 markxoffs
the start position of the marked area in pixels
Definition: textbuf_type.h:43