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 * Return values for Textbuf::HandleKeypress
28 enum HandleKeyPressResult
30 HKPR_EDITING
, ///< Textbuf content changed.
31 HKPR_CURSOR
, ///< Non-text change, e.g. cursor position.
32 HKPR_CONFIRM
, ///< Return or enter key pressed.
33 HKPR_CANCEL
, ///< Escape key pressed.
34 HKPR_NOT_HANDLED
, ///< Key does not affect editboxes.
37 /** Helper/buffer for input fields. */
38 struct Textbuf
: stringb
{
39 CharSetFilter afilter
; ///< Allowed characters
40 const uint16 max_chars
; ///< the maximum size of the buffer in characters (including terminating '\0')
41 uint16 chars
; ///< the current size of the string in characters (including terminating '\0')
42 uint16 pixels
; ///< the current size of the string in pixels
43 bool caret
; ///< is the caret ("_") visible or not
44 uint16 caretpos
; ///< the current position of the caret in the buffer, in bytes
45 uint16 caretxoffs
; ///< the current position of the caret in pixels
46 uint16 markpos
; ///< the start position of the marked area in the buffer, in bytes
47 uint16 markend
; ///< the end position of the marked area in the buffer, in bytes
48 uint16 markxoffs
; ///< the start position of the marked area in pixels
49 uint16 marklength
; ///< the length of the marked area in pixels
51 explicit Textbuf (uint16 max_bytes
, char *buf
, uint16 max_chars
= UINT16_MAX
);
53 void Assign(StringID string
);
54 void Assign(const char *text
);
55 void CDECL
Print(const char *format
, ...) WARN_FORMAT(2, 3);
58 bool InsertClipboard();
60 bool InsertChar(uint32 key
);
61 bool InsertString(const char *str
, bool marked
, const char *caret
= NULL
, const char *insert_location
= NULL
, const char *replacement_end
= NULL
);
63 bool DeleteChar (bool backspace
= true, bool word
= false);
64 bool MoveLeft (bool word
= false);
65 bool MoveRight (bool word
= false);
68 HandleKeyPressResult
HandleKeyPress(WChar key
, uint16 keycode
);
74 * Get the current text.
75 * @return Current text.
77 const char *GetText() const
83 * Get the position of the caret in the text buffer.
84 * @return Pointer to the caret in the text buffer.
86 const char *GetCaret() const
88 return this->c_str() + this->caretpos
;
92 * Get the currently marked text.
93 * @param[out] length Length of the marked text.
94 * @return Begining of the marked area or NULL if no text is marked.
96 const char *GetMarkedText (size_t *length
) const
98 if (this->markend
== 0) return NULL
;
100 *length
= this->markend
- this->markpos
;
101 return this->c_str() + this->markpos
;
104 void GetCharPositions (const char *c1
, int *x1
, const char *c2
, int *x2
) const;
106 const char *GetCharAtPosition (int x
) const;
110 ttd_unique_ptr
<icu::BreakIterator
> char_itr
; ///< ICU iterator for characters.
111 ttd_unique_ptr
<icu::BreakIterator
> word_itr
; ///< ICU iterator for words.
112 SmallVector
<UChar
, 32> utf16_str
; ///< UTF-16 copy of the string.
113 SmallVector
<size_t, 32> utf16_to_utf8
; ///< Mapping from UTF-16 code point position to index in the UTF-8 source string.
115 size_t cur_pos
; ///< Current iteration position.
118 void SetString (const char *s
);
119 size_t SetCurPosition (size_t pos
);
120 size_t Next (bool word
);
121 size_t Prev (bool word
);
123 void DeleteText(uint16 from
, uint16 to
, bool update
);
125 void UpdateStringIter();
127 void UpdateCaretPosition();
128 void UpdateMarkedText();
131 #endif /* TEXTBUF_TYPE_H */