OpenTTD
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(uint32 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 */
bool MovePos(uint16 keycode)
Handle text navigation with arrow keys left/right.
Definition: textbuf.cpp:319
uint16 markend
the end position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:42
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
uint16 chars
the current size of the string in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:36
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:116
Escape key pressed.
Definition: textbuf_type.h:25
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
Definition: textbuf.cpp:415
bool InsertChar(uint32 key)
Insert a character to a textbuffer.
Definition: textbuf.cpp:132
Helper/buffer for input fields.
Definition: textbuf_type.h:30
uint16 bytes
the current size of the string in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:35
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:23
void UpdateCaretPosition()
Update pixel position of the caret.
Definition: textbuf.cpp:297
void UpdateStringIter()
Update the character iter after the text has changed.
Definition: textbuf.cpp:283
void UpdateWidth()
Update pixel width of the text.
Definition: textbuf.cpp:291
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition: textbuf.cpp:303
bool caret
is the caret ("_") visible or not
Definition: textbuf_type.h:38
uint16 markxoffs
the start position of the marked area in pixels
Definition: textbuf_type.h:43
uint16 pixels
the current size of the string in pixels
Definition: textbuf_type.h:37
Key does not affect editboxes.
Definition: textbuf_type.h:26
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition: textbuf_type.h:20
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition: textbuf.cpp:225
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition: textbuf.cpp:42
void DeleteText(uint16 from, uint16 to, bool update)
Delete a part of the text.
Definition: textbuf.cpp:240
uint16 caretpos
the current position of the caret in the buffer, in bytes
Definition: textbuf_type.h:39
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:396
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition: textbuf.cpp:274
Return or enter key pressed.
Definition: textbuf_type.h:24
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:32
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:33
bool HandleCaret()
Handle the flashing of the caret.
Definition: textbuf.cpp:456
uint16 max_chars
the maximum size of the buffer in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:34
uint16 caretxoffs
the current position of the caret in pixels
Definition: textbuf_type.h:40
Class for iterating over different kind of parts of a string.
Definition: string_base.h:14
Types related to strings.
Types for strings.
bool DeleteChar(uint16 keycode)
Delete a character from a textbuffer, either with &#39;Delete&#39; or &#39;Backspace&#39; The character is delete fro...
Definition: textbuf.cpp:53
CharSetFilter afilter
Allowed characters.
Definition: textbuf_type.h:31
uint16 marklength
the length of the marked area in pixels
Definition: textbuf_type.h:44
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
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
uint16 markpos
the start position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:41
Textbuf content changed.
Definition: textbuf_type.h:22
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition: textbuf.cpp:430