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 
8 /** @file textbuf_type.h Stuff related to text buffers. */
9 
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 
17 /**
18  * Return values for Textbuf::HandleKeypress
19  */
20 enum HandleKeyPressResult
21 {
22 	HKPR_EDITING,     ///< Textbuf content changed.
23 	HKPR_CURSOR,      ///< Non-text change, e.g. cursor position.
24 	HKPR_CONFIRM,     ///< Return or enter key pressed.
25 	HKPR_CANCEL,      ///< Escape key pressed.
26 	HKPR_NOT_HANDLED, ///< Key does not affect editboxes.
27 };
28 
29 /** Helper/buffer for input fields. */
30 struct Textbuf {
31 	CharSetFilter afilter;    ///< Allowed characters
32 	char * const buf;         ///< buffer in which text is saved
33 	uint16 max_bytes;         ///< the maximum size of the buffer in bytes (including terminating '\0')
34 	uint16 max_chars;         ///< the maximum size of the buffer in characters (including terminating '\0')
35 	uint16 bytes;             ///< the current size of the string in bytes (including terminating '\0')
36 	uint16 chars;             ///< the current size of the string in characters (including terminating '\0')
37 	uint16 pixels;            ///< the current size of the string in pixels
38 	bool caret;               ///< is the caret ("_") visible or not
39 	uint16 caretpos;          ///< the current position of the caret in the buffer, in bytes
40 	uint16 caretxoffs;        ///< the current position of the caret in pixels
41 	uint16 markpos;           ///< the start position of the marked area in the buffer, in bytes
42 	uint16 markend;           ///< the end position of the marked area in the buffer, in bytes
43 	uint16 markxoffs;         ///< the start position of the marked area in pixels
44 	uint16 marklength;        ///< the length of the marked area in pixels
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 */
83