4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file textbuf_type.h Stuff related to text buffers. */
12 #ifndef TEXTBUF_TYPE_H
13 #define TEXTBUF_TYPE_H
16 #include "strings_type.h"
17 #include "core/pointer.h"
18 #include "core/smallvec_type.hpp"
21 #include <unicode/utext.h>
22 #include <unicode/brkiter.h>
26 * Valid filter types for Textbuf::IsValidChar.
29 CS_ALPHANUMERAL
, ///< Both numeric and alphabetic and spaces and stuff
30 CS_NUMERAL
, ///< Only numeric ones
31 CS_HEXADECIMAL
, ///< Only hexadecimal characters
35 * Return values for Textbuf::HandleKeypress
37 enum HandleKeyPressResult
39 HKPR_EDITING
, ///< Textbuf content changed.
40 HKPR_CURSOR
, ///< Non-text change, e.g. cursor position.
41 HKPR_CONFIRM
, ///< Return or enter key pressed.
42 HKPR_CANCEL
, ///< Escape key pressed.
43 HKPR_NOT_HANDLED
, ///< Key does not affect editboxes.
46 /** Helper/buffer for input fields. */
47 struct Textbuf
: stringb
{
48 CharSetFilter afilter
; ///< Allowed characters
49 const uint16 max_chars
; ///< the maximum size of the buffer in characters (including terminating '\0')
50 uint16 chars
; ///< the current size of the string in characters (including terminating '\0')
51 uint16 pixels
; ///< the current size of the string in pixels
52 bool caret
; ///< is the caret ("_") visible or not
53 uint16 caretpos
; ///< the current position of the caret in the buffer, in bytes
54 uint16 caretxoffs
; ///< the current position of the caret in pixels
55 uint16 markpos
; ///< the start position of the marked area in the buffer, in bytes
56 uint16 markend
; ///< the end position of the marked area in the buffer, in bytes
57 uint16 markxoffs
; ///< the start position of the marked area in pixels
58 uint16 marklength
; ///< the length of the marked area in pixels
60 explicit Textbuf (uint16 max_bytes
, char *buf
, uint16 max_chars
= UINT16_MAX
);
62 bool IsValidChar (WChar key
) const;
64 void Assign(StringID string
);
65 void Assign(const char *text
);
66 void CDECL
Print(const char *format
, ...) WARN_FORMAT(2, 3);
69 bool InsertClipboard();
71 bool InsertChar(uint32 key
);
72 bool InsertString(const char *str
, bool marked
, const char *caret
= NULL
, const char *insert_location
= NULL
, const char *replacement_end
= NULL
);
74 bool DeleteChar (bool backspace
= true, bool word
= false);
75 bool MoveLeft (bool word
= false);
76 bool MoveRight (bool word
= false);
79 HandleKeyPressResult
HandleKeyPress(WChar key
, uint16 keycode
);
85 * Get the current text.
86 * @return Current text.
88 const char *GetText() const
94 * Get the position of the caret in the text buffer.
95 * @return Pointer to the caret in the text buffer.
97 const char *GetCaret() const
99 return this->c_str() + this->caretpos
;
103 * Get the currently marked text.
104 * @param[out] length Length of the marked text.
105 * @return Begining of the marked area or NULL if no text is marked.
107 const char *GetMarkedText (size_t *length
) const
109 if (this->markend
== 0) return NULL
;
111 *length
= this->markend
- this->markpos
;
112 return this->c_str() + this->markpos
;
115 void GetCharPositions (const char *c1
, int *x1
, const char *c2
, int *x2
) const;
117 const char *GetCharAtPosition (int x
) const;
121 ttd_unique_ptr
<icu::BreakIterator
> char_itr
; ///< ICU iterator for characters.
122 ttd_unique_ptr
<icu::BreakIterator
> word_itr
; ///< ICU iterator for words.
123 SmallVector
<UChar
, 32> utf16_str
; ///< UTF-16 copy of the string.
124 SmallVector
<size_t, 32> utf16_to_utf8
; ///< Mapping from UTF-16 code point position to index in the UTF-8 source string.
126 size_t cur_pos
; ///< Current iteration position.
129 void SetString (const char *s
);
130 size_t SetCurPosition (size_t pos
);
131 size_t Next (bool word
);
132 size_t Prev (bool word
);
134 void DeleteText(uint16 from
, uint16 to
, bool update
);
136 void UpdateStringIter();
138 void UpdateCaretPosition();
139 void UpdateMarkedText();
142 #endif /* TEXTBUF_TYPE_H */