OpenTTD Source: src/textbuf_type.h Source File

OpenTTD Source 15.0-beta1

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
20 enum HandleKeyPressResult
21{
22 HKPR_EDITING,
23 HKPR_CURSOR,
24 HKPR_CONFIRM,
25 HKPR_CANCEL,
26 HKPR_NOT_HANDLED,
27};
28
30 struct Textbuf {
31 CharSetFilter afilter;
32 char * const buf;
33 uint16_t max_bytes;
34 uint16_t max_chars;
35 uint16_t bytes;
36 uint16_t chars;
37 uint16_t pixels;
38 bool caret;
39 uint16_t caretpos;
40 uint16_t caretxoffs;
41 uint16_t markpos;
42 uint16_t markend;
43 uint16_t markxoffs;
44 uint16_t marklength;
45
46 explicit Textbuf(uint16_t max_bytes, uint16_t max_chars = UINT16_MAX);
47 ~Textbuf();
48
49 void Assign(StringID string);
50 void Assign(const std::string_view text);
51
52 void DeleteAll();
53 bool InsertClipboard();
54
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);
57
58 bool DeleteChar(uint16_t keycode);
59 bool MovePos(uint16_t keycode);
60
61 HandleKeyPressResult HandleKeyPress(char32_t key, uint16_t keycode);
62
63 bool HandleCaret();
64 void UpdateSize();
65
66 void DiscardMarkedText(bool update = true);
67
68 const char *GetText() const;
69
70private:
71 std::unique_ptr<StringIterator> char_iter;
72
73 bool CanDelChar(bool backspace);
74
75 bool MovePrev(StringIterator::IterType what);
76 bool MoveNext(StringIterator::IterType what);
77
78 void DeleteText(uint16_t from, uint16_t to, bool update);
79
80 void UpdateStringIter();
81 void UpdateWidth();
82 void UpdateCaretPosition();
83 void UpdateMarkedText();
84};
85
86#endif /* TEXTBUF_TYPE_H */
StringIterator::IterType
IterType
Type of the iterator.
Definition string_base.h:17
string_type.h
Types for strings.
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition string_type.h:24
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
Textbuf
Helper/buffer for input fields.
Definition textbuf_type.h:30
Textbuf::pixels
uint16_t pixels
the current size of the string in pixels
Definition textbuf_type.h:37
Textbuf::caretpos
uint16_t caretpos
the current position of the caret in the buffer, in bytes
Definition textbuf_type.h:39
Textbuf::UpdateSize
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition textbuf.cpp:463
Textbuf::MovePos
bool MovePos(uint16_t keycode)
Handle text navigation with arrow keys left/right.
Definition textbuf.cpp:369
Textbuf::markxoffs
uint16_t markxoffs
the start position of the marked area in pixels
Definition textbuf_type.h:43
Textbuf::UpdateMarkedText
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition textbuf.cpp:317
Textbuf::UpdateWidth
void UpdateWidth()
Update pixel width of the text.
Definition textbuf.cpp:304
Textbuf::DeleteChar
bool DeleteChar(uint16_t keycode)
Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete fro...
Definition textbuf.cpp:51
Textbuf::DeleteAll
void DeleteAll()
Delete every character in the textbuffer.
Definition textbuf.cpp:114
Textbuf::max_bytes
uint16_t max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
Definition textbuf_type.h:33
Textbuf::chars
uint16_t chars
the current size of the string in characters (including terminating '\0')
Definition textbuf_type.h:36
Textbuf::InsertString
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:160
Textbuf::MovePrev
bool MovePrev(StringIterator::IterType what)
Move to previous character position.
Definition textbuf.cpp:334
Textbuf::max_chars
uint16_t max_chars
the maximum size of the buffer in characters (including terminating '\0')
Definition textbuf_type.h:34
Textbuf::Assign
void Assign(StringID string)
Render a string into the textbuffer.
Definition textbuf.cpp:431
Textbuf::markend
uint16_t markend
the end position of the marked area in the buffer, in bytes
Definition textbuf_type.h:42
Textbuf::DeleteText
void DeleteText(uint16_t from, uint16_t to, bool update)
Delete a part of the text.
Definition textbuf.cpp:237
Textbuf::MoveNext
bool MoveNext(StringIterator::IterType what)
Move to next character position.
Definition textbuf.cpp:351
Textbuf::InsertClipboard
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition textbuf.cpp:223
Textbuf::bytes
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
Definition textbuf_type.h:35
Textbuf::GetText
const char * GetText() const
Get the current text.
Definition textbuf.cpp:290
Textbuf::caretxoffs
uint16_t caretxoffs
the current position of the caret in pixels
Definition textbuf_type.h:40
Textbuf::marklength
uint16_t marklength
the length of the marked area in pixels
Definition textbuf_type.h:44
Textbuf::markpos
uint16_t markpos
the start position of the marked area in the buffer, in bytes
Definition textbuf_type.h:41
Textbuf::CanDelChar
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition textbuf.cpp:40
Textbuf::DiscardMarkedText
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition textbuf.cpp:278
Textbuf::caret
bool caret
is the caret ("_") visible or not
Definition textbuf_type.h:38
Textbuf::afilter
CharSetFilter afilter
Allowed characters.
Definition textbuf_type.h:31
Textbuf::InsertChar
bool InsertChar(char32_t key)
Insert a character to a textbuffer.
Definition textbuf.cpp:130
Textbuf::UpdateStringIter
void UpdateStringIter()
Update the character iter after the text has changed.
Definition textbuf.cpp:296
Textbuf::buf
char *const buf
buffer in which text is saved
Definition textbuf_type.h:32
Textbuf::HandleCaret
bool HandleCaret()
Handle the flashing of the caret.
Definition textbuf.cpp:489
Textbuf::UpdateCaretPosition
void UpdateCaretPosition()
Update pixel position of the caret.
Definition textbuf.cpp:310
HandleKeyPressResult
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition textbuf_type.h:21
HKPR_NOT_HANDLED
@ HKPR_NOT_HANDLED
Key does not affect editboxes.
Definition textbuf_type.h:26
HKPR_CANCEL
@ HKPR_CANCEL
Escape key pressed.
Definition textbuf_type.h:25
HKPR_EDITING
@ HKPR_EDITING
Textbuf content changed.
Definition textbuf_type.h:22
HKPR_CONFIRM
@ HKPR_CONFIRM
Return or enter key pressed.
Definition textbuf_type.h:24
HKPR_CURSOR
@ HKPR_CURSOR
Non-text change, e.g. cursor position.
Definition textbuf_type.h:23