OpenTTD

textbuf_type.h
Go to the documentation of this file.
1 /* $Id: textbuf_type.h 25692 2013-08-05 20:37:57Z michi_cc $ */
2
3 /*
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/>.
8 */
9
12 #ifndef TEXTBUF_TYPE_H
13 #define TEXTBUF_TYPE_H
14
15 #include "string_type.h"
16 #include "strings_type.h"
17 #include "string_base.h"
18
22 enum HandleKeyPressResult
23 {
24 HKPR_EDITING,
25 HKPR_CURSOR,
26 HKPR_CONFIRM,
27 HKPR_CANCEL,
28 HKPR_NOT_HANDLED,
29 };
30
32 struct Textbuf {
33 CharSetFilter afilter;
34 char * const buf;
35 uint16 max_bytes;
36 uint16 max_chars;
37 uint16 bytes;
38 uint16 chars;
39 uint16 pixels;
40 bool caret;
41 uint16 caretpos;
42 uint16 caretxoffs;
43 uint16 markpos;
44 uint16 markend;
45 uint16 markxoffs;
46 uint16 marklength;
47
48 explicit Textbuf(uint16 max_bytes, uint16 max_chars = UINT16_MAX);
49 ~Textbuf();
50
51 void Assign(StringID string);
52 void Assign(const char *text);
53 void CDECL Print(const char *format, ...) WARN_FORMAT(2, 3);
54
55 void DeleteAll();
56 bool InsertClipboard();
57
58 bool InsertChar(uint32 key);
59 bool InsertString(const char *str, bool marked, const char *caret = NULL, const char *insert_location = NULL, const char *replacement_end = NULL);
60
61 bool DeleteChar(uint16 keycode);
62 bool MovePos(uint16 keycode);
63
64 HandleKeyPressResult HandleKeyPress(WChar key, uint16 keycode);
65
66 bool HandleCaret();
67 void UpdateSize();
68
69 void DiscardMarkedText(bool update = true);
70
71 private:
72 StringIterator *char_iter;
73
74 bool CanDelChar(bool backspace);
75
76 void DeleteText(uint16 from, uint16 to, bool update);
77
78 void UpdateStringIter();
79 void UpdateWidth();
80 void UpdateCaretPosition();
81 void UpdateMarkedText();
82 };
83
84 #endif /* TEXTBUF_TYPE_H */
Textbuf::MovePos
bool MovePos(uint16 keycode)
Handle text navigation with arrow keys left/right.
Definition: textbuf.cpp:321
Textbuf::markend
uint16 markend
the end position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:44
Textbuf::chars
uint16 chars
the current size of the string in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:38
Textbuf::DeleteAll
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:118
HKPR_CANCEL
Escape key pressed.
Definition: textbuf_type.h:27
Textbuf::Print
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
Definition: textbuf.cpp:418
Textbuf::InsertChar
bool InsertChar(uint32 key)
Insert a character to a textbuffer.
Definition: textbuf.cpp:134
Textbuf
Helper/buffer for input fields.
Definition: textbuf_type.h:32
Textbuf::bytes
uint16 bytes
the current size of the string in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:37
HKPR_CURSOR
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:25
Textbuf::UpdateCaretPosition
void UpdateCaretPosition()
Update pixel position of the caret.
Definition: textbuf.cpp:299
Textbuf::UpdateStringIter
void UpdateStringIter()
Update the character iter after the text has changed.
Definition: textbuf.cpp:285
Textbuf::UpdateWidth
void UpdateWidth()
Update pixel width of the text.
Definition: textbuf.cpp:293
Textbuf::UpdateMarkedText
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition: textbuf.cpp:305
Textbuf::InsertString
bool InsertString(const char *str, bool marked, const char *caret=NULL, const char *insert_location=NULL, const char *replacement_end=NULL)
Insert a string into the text buffer.
Definition: textbuf.cpp:164
Textbuf::caret
bool caret
is the caret ("_") visible or not
Definition: textbuf_type.h:40
Textbuf::markxoffs
uint16 markxoffs
the start position of the marked area in pixels
Definition: textbuf_type.h:45
Textbuf::pixels
uint16 pixels
the current size of the string in pixels
Definition: textbuf_type.h:39
HKPR_NOT_HANDLED
Key does not affect editboxes.
Definition: textbuf_type.h:28
HandleKeyPressResult
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition: textbuf_type.h:22
Textbuf::InsertClipboard
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition: textbuf.cpp:227
Textbuf::CanDelChar
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition: textbuf.cpp:44
Textbuf::DeleteText
void DeleteText(uint16 from, uint16 to, bool update)
Delete a part of the text.
Definition: textbuf.cpp:242
Textbuf::caretpos
uint16 caretpos
the current position of the caret in the buffer, in bytes
Definition: textbuf_type.h:41
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
CharSetFilter
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
Textbuf::Assign
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:399
Textbuf::DiscardMarkedText
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition: textbuf.cpp:276
HKPR_CONFIRM
Return or enter key pressed.
Definition: textbuf_type.h:26
Textbuf::buf
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:34
Textbuf::max_bytes
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:35
Textbuf::HandleCaret
bool HandleCaret()
Handle the flashing of the caret.
Definition: textbuf.cpp:459
Textbuf::max_chars
uint16 max_chars
the maximum size of the buffer in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:36
Textbuf::caretxoffs
uint16 caretxoffs
the current position of the caret in pixels
Definition: textbuf_type.h:42
StringIterator
Class for iterating over different kind of parts of a string.
Definition: string_base.h:16
strings_type.h
Types related to strings.
string_type.h
Types for strings.
Textbuf::DeleteChar
bool DeleteChar(uint16 keycode)
Delete a character from a textbuffer, either with &#39;Delete&#39; or &#39;Backspace&#39; The character is delete fro...
Definition: textbuf.cpp:55
Textbuf::afilter
CharSetFilter afilter
Allowed characters.
Definition: textbuf_type.h:33
Textbuf::marklength
uint16 marklength
the length of the marked area in pixels
Definition: textbuf_type.h:46
Textbuf::Textbuf
Textbuf(uint16 max_bytes, uint16 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:374
WChar
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
Textbuf::markpos
uint16 markpos
the start position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:43
HKPR_EDITING
Textbuf content changed.
Definition: textbuf_type.h:24
Textbuf::UpdateSize
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition: textbuf.cpp:433