OpenTTD Source 20261123-master-g1c55c72fd0
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef TEXTBUF_TYPE_H
11#define TEXTBUF_TYPE_H
12
13#include "string_type.h"
14#include "string_base.h"
15
26
28struct Textbuf {
30 uint16_t max_bytes;
31 uint16_t max_chars;
32 uint16_t chars;
33 uint16_t pixels;
34 bool caret;
35 uint16_t caretpos;
36 uint16_t caretxoffs;
37 uint16_t markpos;
38 uint16_t markend;
39 uint16_t markxoffs;
40 uint16_t marklength;
41
42 explicit Textbuf(uint16_t max_bytes, uint16_t max_chars = UINT16_MAX);
43
44 void Assign(std::string_view text);
45
46 void DeleteAll();
47 bool InsertClipboard();
48
49 bool InsertChar(char32_t key);
50 bool InsertString(std::string_view str, bool marked,
51 std::optional<size_t> caret = std::nullopt,
52 std::optional<size_t> insert_location = std::nullopt, std::optional<size_t> replacement_end = std::nullopt);
53
54 bool DeleteChar(uint16_t keycode);
55 bool MovePos(uint16_t keycode);
56
57 HandleKeyPressResult HandleKeyPress(char32_t key, uint16_t keycode);
58
59 bool HandleCaret();
60 void UpdateSize();
61
62 void DiscardMarkedText(bool update = true);
63
64 std::string_view GetText() const;
65
66private:
67 std::string buf;
68 std::unique_ptr<StringIterator> char_iter;
69
70 bool CanDelChar(bool backspace);
71
74
75 void DeleteText(uint16_t from, uint16_t to, bool update);
76
77 void UpdateStringIter();
78 void UpdateWidth();
80 void UpdateMarkedText();
81};
82
83#endif /* TEXTBUF_TYPE_H */
IterType
Type of the iterator.
Definition string_base.h:17
Base types for interacting with strings.
Types for strings.
CharSetFilter
Valid filter types for IsValidChar.
Definition string_type.h:24
uint16_t pixels
the current size of the string in pixels
uint16_t caretpos
the current position of the caret in the buffer, in bytes
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition textbuf.cpp:444
bool MovePos(uint16_t keycode)
Handle text navigation with arrow keys left/right.
Definition textbuf.cpp:362
uint16_t markxoffs
the start position of the marked area in pixels
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition textbuf.cpp:310
void UpdateWidth()
Update pixel width of the text.
Definition textbuf.cpp:297
bool DeleteChar(uint16_t keycode)
Delete a character from a textbuffer, either with 'Delete' or 'Backspace' The character is delete fro...
Definition textbuf.cpp:50
void DeleteAll()
Delete every character in the textbuffer.
Definition textbuf.cpp:111
uint16_t max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
std::string_view GetText() const
Get the current text.
Definition textbuf.cpp:283
uint16_t chars
the current size of the string in characters (including terminating '\0')
bool MovePrev(StringIterator::IterType what)
Move to previous character position.
Definition textbuf.cpp:327
uint16_t max_chars
the maximum size of the buffer in characters (including terminating '\0')
uint16_t markend
the end position of the marked area in the buffer, in bytes
void DeleteText(uint16_t from, uint16_t to, bool update)
Delete a part of the text.
Definition textbuf.cpp:236
bool MoveNext(StringIterator::IterType what)
Move to next character position.
Definition textbuf.cpp:344
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition textbuf.cpp:222
bool InsertString(std::string_view str, bool marked, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Insert a string into the text buffer.
Definition textbuf.cpp:156
uint16_t caretxoffs
the current position of the caret in pixels
uint16_t marklength
the length of the marked area in pixels
uint16_t markpos
the start position of the marked area in the buffer, in bytes
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition textbuf.cpp:39
void Assign(std::string_view text)
Copy a string into the textbuffer.
Definition textbuf.cpp:419
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition textbuf.cpp:271
bool caret
is the caret ("_") visible or not
CharSetFilter afilter
Allowed characters.
bool InsertChar(char32_t key)
Insert a character to a textbuffer.
Definition textbuf.cpp:127
void UpdateStringIter()
Update the character iter after the text has changed.
Definition textbuf.cpp:289
bool HandleCaret()
Handle the flashing of the caret.
Definition textbuf.cpp:462
Textbuf(uint16_t max_bytes, uint16_t max_chars=UINT16_MAX)
Initialize the textbuffer by supplying it the buffer to write into and the maximum length of this buf...
Definition textbuf.cpp:402
std::string buf
buffer in which text is saved
void UpdateCaretPosition()
Update pixel position of the caret.
Definition textbuf.cpp:303
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
@ Confirm
Return or enter key pressed.
@ Cursor
Non-text change, e.g. cursor position.
@ Editing
Textbuf content changed.
@ NotHandled
Key does not affect editboxes.
@ Cancel
Escape key pressed.