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 
23 {
29 };
30 
32 struct Textbuf {
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 */
bool MovePos(uint16 keycode)
Handle text navigation with arrow keys left/right.
Definition: textbuf.cpp:321
uint16 markend
the end position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:44
uint16 chars
the current size of the string in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:38
void CDECL void DeleteAll()
Delete every character in the textbuffer.
Definition: textbuf.cpp:118
Escape key pressed.
Definition: textbuf_type.h:27
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
Definition: textbuf.cpp:418
bool InsertChar(uint32 key)
Insert a character to a textbuffer.
Definition: textbuf.cpp:134
Helper/buffer for input fields.
Definition: textbuf_type.h:32
uint16 bytes
the current size of the string in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:37
Non-text change, e.g. cursor position.
Definition: textbuf_type.h:25
void UpdateCaretPosition()
Update pixel position of the caret.
Definition: textbuf.cpp:299
void UpdateStringIter()
Update the character iter after the text has changed.
Definition: textbuf.cpp:285
void UpdateWidth()
Update pixel width of the text.
Definition: textbuf.cpp:293
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition: textbuf.cpp:305
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
bool caret
is the caret ("_") visible or not
Definition: textbuf_type.h:40
uint16 markxoffs
the start position of the marked area in pixels
Definition: textbuf_type.h:45
uint16 pixels
the current size of the string in pixels
Definition: textbuf_type.h:39
Key does not affect editboxes.
Definition: textbuf_type.h:28
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition: textbuf_type.h:22
bool InsertClipboard()
Insert a chunk of text from the clipboard onto the textbuffer.
Definition: textbuf.cpp:227
bool CanDelChar(bool backspace)
Checks if it is possible to delete a character.
Definition: textbuf.cpp:44
void DeleteText(uint16 from, uint16 to, bool update)
Delete a part of the text.
Definition: textbuf.cpp:242
uint16 caretpos
the current position of the caret in the buffer, in bytes
Definition: textbuf_type.h:41
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
CharSetFilter
Valid filter types for IsValidChar.
Definition: string_type.h:26
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:399
void DiscardMarkedText(bool update=true)
Discard any marked text.
Definition: textbuf.cpp:276
Return or enter key pressed.
Definition: textbuf_type.h:26
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:34
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:35
bool HandleCaret()
Handle the flashing of the caret.
Definition: textbuf.cpp:459
uint16 max_chars
the maximum size of the buffer in characters (including terminating &#39;\0&#39;)
Definition: textbuf_type.h:36
uint16 caretxoffs
the current position of the caret in pixels
Definition: textbuf_type.h:42
Class for iterating over different kind of parts of a string.
Definition: string_base.h:16
Types related to strings.
Types for strings.
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
CharSetFilter afilter
Allowed characters.
Definition: textbuf_type.h:33
uint16 marklength
the length of the marked area in pixels
Definition: textbuf_type.h:46
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
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:35
uint16 markpos
the start position of the marked area in the buffer, in bytes
Definition: textbuf_type.h:43
Textbuf content changed.
Definition: textbuf_type.h:24
void UpdateSize()
Update Textbuf type with its actual physical character and screenlength Get the count of characters i...
Definition: textbuf.cpp:433