Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / textbuf_type.h
blob2b6b5c8e421999bbe0926572d62b5de82a1b9c0a
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 */
8 /** @file textbuf_type.h Stuff related to text buffers. */
10 #ifndef TEXTBUF_TYPE_H
11 #define TEXTBUF_TYPE_H
13 #include "string_type.h"
14 #include "strings_type.h"
15 #include "string_base.h"
17 /**
18 * Return values for Textbuf::HandleKeypress
20 enum HandleKeyPressResult
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.
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_t max_bytes; ///< the maximum size of the buffer in bytes (including terminating '\0')
34 uint16_t max_chars; ///< the maximum size of the buffer in characters (including terminating '\0')
35 uint16_t bytes; ///< the current size of the string in bytes (including terminating '\0')
36 uint16_t chars; ///< the current size of the string in characters (including terminating '\0')
37 uint16_t pixels; ///< the current size of the string in pixels
38 bool caret; ///< is the caret ("_") visible or not
39 uint16_t caretpos; ///< the current position of the caret in the buffer, in bytes
40 uint16_t caretxoffs; ///< the current position of the caret in pixels
41 uint16_t markpos; ///< the start position of the marked area in the buffer, in bytes
42 uint16_t markend; ///< the end position of the marked area in the buffer, in bytes
43 uint16_t markxoffs; ///< the start position of the marked area in pixels
44 uint16_t marklength; ///< the length of the marked area in pixels
46 explicit Textbuf(uint16_t max_bytes, uint16_t max_chars = UINT16_MAX);
47 ~Textbuf();
49 void Assign(StringID string);
50 void Assign(const std::string_view text);
52 void DeleteAll();
53 bool InsertClipboard();
55 bool InsertChar(char32_t key);
56 bool InsertString(const char *str, bool marked, const char *caret = nullptr, const char *insert_location = nullptr, const char *replacement_end = nullptr);
58 bool DeleteChar(uint16_t keycode);
59 bool MovePos(uint16_t keycode);
61 HandleKeyPressResult HandleKeyPress(char32_t key, uint16_t keycode);
63 bool HandleCaret();
64 void UpdateSize();
66 void DiscardMarkedText(bool update = true);
68 const char *GetText() const;
70 private:
71 std::unique_ptr<StringIterator> char_iter;
73 bool CanDelChar(bool backspace);
75 bool MovePrev(StringIterator::IterType what);
76 bool MoveNext(StringIterator::IterType what);
78 void DeleteText(uint16_t from, uint16_t to, bool update);
80 void UpdateStringIter();
81 void UpdateWidth();
82 void UpdateCaretPosition();
83 void UpdateMarkedText();
86 #endif /* TEXTBUF_TYPE_H */