OpenTTD
textbuf.cpp
Go to the documentation of this file.
1 /* $Id: textbuf.cpp 26758 2014-08-24 10:34:43Z 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 #include "stdafx.h"
13 #include <stdarg.h>
14 
15 #include "textbuf_type.h"
16 #include "string_func.h"
17 #include "strings_func.h"
18 #include "gfx_type.h"
19 #include "gfx_func.h"
20 #include "window_func.h"
21 #include "core/alloc_func.hpp"
22 
23 #include "safeguards.h"
24 
33 bool GetClipboardContents(char *buffer, const char *last);
34 
35 int _caret_timer;
36 
37 
44 bool Textbuf::CanDelChar(bool backspace)
45 {
46  return backspace ? this->caretpos != 0 : this->caretpos < this->bytes - 1;
47 }
48 
55 bool Textbuf::DeleteChar(uint16 keycode)
56 {
57  bool word = (keycode & WKC_CTRL) != 0;
58 
59  keycode &= ~WKC_SPECIAL_KEYS;
60  if (keycode != WKC_BACKSPACE && keycode != WKC_DELETE) return false;
61 
62  bool backspace = keycode == WKC_BACKSPACE;
63 
64  if (!CanDelChar(backspace)) return false;
65 
66  char *s = this->buf + this->caretpos;
67  uint16 len = 0;
68 
69  if (word) {
70  /* Delete a complete word. */
71  if (backspace) {
72  /* Delete whitespace and word in front of the caret. */
73  len = this->caretpos - (uint16)this->char_iter->Prev(StringIterator::ITER_WORD);
74  s -= len;
75  } else {
76  /* Delete word and following whitespace following the caret. */
77  len = (uint16)this->char_iter->Next(StringIterator::ITER_WORD) - this->caretpos;
78  }
79  /* Update character count. */
80  for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
81  this->chars--;
82  }
83  } else {
84  /* Delete a single character. */
85  if (backspace) {
86  /* Delete the last code point in front of the caret. */
87  s = Utf8PrevChar(s);
88  WChar c;
89  len = (uint16)Utf8Decode(&c, s);
90  this->chars--;
91  } else {
92  /* Delete the complete character following the caret. */
93  len = (uint16)this->char_iter->Next(StringIterator::ITER_CHARACTER) - this->caretpos;
94  /* Update character count. */
95  for (const char *ss = s; ss < s + len; Utf8Consume(&ss)) {
96  this->chars--;
97  }
98  }
99  }
100 
101  /* Move the remaining characters over the marker */
102  memmove(s, s + len, this->bytes - (s - this->buf) - len);
103  this->bytes -= len;
104 
105  if (backspace) this->caretpos -= len;
106 
107  this->UpdateStringIter();
108  this->UpdateWidth();
109  this->UpdateCaretPosition();
110  this->UpdateMarkedText();
111 
112  return true;
113 }
114 
119 {
120  memset(this->buf, 0, this->max_bytes);
121  this->bytes = this->chars = 1;
122  this->pixels = this->caretpos = this->caretxoffs = 0;
123  this->markpos = this->markend = this->markxoffs = this->marklength = 0;
124  this->UpdateStringIter();
125 }
126 
135 {
136  uint16 len = (uint16)Utf8CharLen(key);
137  if (this->bytes + len <= this->max_bytes && this->chars + 1 <= this->max_chars) {
138  memmove(this->buf + this->caretpos + len, this->buf + this->caretpos, this->bytes - this->caretpos);
139  Utf8Encode(this->buf + this->caretpos, key);
140  this->chars++;
141  this->bytes += len;
142  this->caretpos += len;
143 
144  this->UpdateStringIter();
145  this->UpdateWidth();
146  this->UpdateCaretPosition();
147  this->UpdateMarkedText();
148  return true;
149  }
150  return false;
151 }
152 
164 bool Textbuf::InsertString(const char *str, bool marked, const char *caret, const char *insert_location, const char *replacement_end)
165 {
166  uint16 insertpos = (marked && this->marklength != 0) ? this->markpos : this->caretpos;
167  if (insert_location != NULL) {
168  insertpos = insert_location - this->buf;
169  if (insertpos > this->bytes) return false;
170 
171  if (replacement_end != NULL) {
172  this->DeleteText(insertpos, replacement_end - this->buf, str == NULL);
173  }
174  } else {
175  if (marked) this->DiscardMarkedText(str == NULL);
176  }
177 
178  if (str == NULL) return false;
179 
180  uint16 bytes = 0, chars = 0;
181  WChar c;
182  for (const char *ptr = str; (c = Utf8Consume(&ptr)) != '\0';) {
183  if (!IsValidChar(c, this->afilter)) break;
184 
185  byte len = Utf8CharLen(c);
186  if (this->bytes + bytes + len > this->max_bytes) break;
187  if (this->chars + chars + 1 > this->max_chars) break;
188 
189  bytes += len;
190  chars++;
191 
192  /* Move caret if needed. */
193  if (ptr == caret) this->caretpos = insertpos + bytes;
194  }
195 
196  if (bytes == 0) return false;
197 
198  if (marked) {
199  this->markpos = insertpos;
200  this->markend = insertpos + bytes;
201  }
202 
203  memmove(this->buf + insertpos + bytes, this->buf + insertpos, this->bytes - insertpos);
204  memcpy(this->buf + insertpos, str, bytes);
205 
206  this->bytes += bytes;
207  this->chars += chars;
208  if (!marked && caret == NULL) this->caretpos += bytes;
209  assert(this->bytes <= this->max_bytes);
210  assert(this->chars <= this->max_chars);
211  this->buf[this->bytes - 1] = '\0'; // terminating zero
212 
213  this->UpdateStringIter();
214  this->UpdateWidth();
215  this->UpdateCaretPosition();
216  this->UpdateMarkedText();
217 
218  return true;
219 }
220 
228 {
229  char utf8_buf[512];
230 
231  if (!GetClipboardContents(utf8_buf, lastof(utf8_buf))) return false;
232 
233  return this->InsertString(utf8_buf, false);
234 }
235 
242 void Textbuf::DeleteText(uint16 from, uint16 to, bool update)
243 {
244  uint c = 0;
245  const char *s = this->buf + from;
246  while (s < this->buf + to) {
247  Utf8Consume(&s);
248  c++;
249  }
250 
251  /* Strip marked characters from buffer. */
252  memmove(this->buf + from, this->buf + to, this->bytes - to);
253  this->bytes -= to - from;
254  this->chars -= c;
255 
256  /* Fixup caret if needed. */
257  if (this->caretpos > from) {
258  if (this->caretpos <= to) {
259  this->caretpos = from;
260  } else {
261  this->caretpos -= to - from;
262  }
263  }
264 
265  if (update) {
266  this->UpdateStringIter();
267  this->UpdateCaretPosition();
268  this->UpdateMarkedText();
269  }
270 }
271 
276 void Textbuf::DiscardMarkedText(bool update)
277 {
278  if (this->markend == 0) return;
279 
280  this->DeleteText(this->markpos, this->markend, update);
281  this->markpos = this->markend = this->markxoffs = this->marklength = 0;
282 }
283 
286 {
287  this->char_iter->SetString(this->buf);
288  size_t pos = this->char_iter->SetCurPosition(this->caretpos);
289  this->caretpos = pos == StringIterator::END ? 0 : (uint16)pos;
290 }
291 
294 {
295  this->pixels = GetStringBoundingBox(this->buf, FS_NORMAL).width;
296 }
297 
300 {
301  this->caretxoffs = this->chars > 1 ? GetCharPosInString(this->buf, this->buf + this->caretpos, FS_NORMAL).x : 0;
302 }
303 
306 {
307  if (this->markend != 0) {
308  this->markxoffs = GetCharPosInString(this->buf, this->buf + this->markpos, FS_NORMAL).x;
309  this->marklength = GetCharPosInString(this->buf, this->buf + this->markend, FS_NORMAL).x - this->markxoffs;
310  } else {
311  this->markxoffs = this->marklength = 0;
312  }
313 }
314 
321 bool Textbuf::MovePos(uint16 keycode)
322 {
323  switch (keycode) {
324  case WKC_LEFT:
325  case WKC_CTRL | WKC_LEFT: {
326  if (this->caretpos == 0) break;
327 
328  size_t pos = this->char_iter->Prev(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
329  if (pos == StringIterator::END) return true;
330 
331  this->caretpos = (uint16)pos;
332  this->UpdateCaretPosition();
333  return true;
334  }
335 
336  case WKC_RIGHT:
337  case WKC_CTRL | WKC_RIGHT: {
338  if (this->caretpos >= this->bytes - 1) break;
339 
340  size_t pos = this->char_iter->Next(keycode & WKC_CTRL ? StringIterator::ITER_WORD : StringIterator::ITER_CHARACTER);
341  if (pos == StringIterator::END) return true;
342 
343  this->caretpos = (uint16)pos;
344  this->UpdateCaretPosition();
345  return true;
346  }
347 
348  case WKC_HOME:
349  this->caretpos = 0;
350  this->char_iter->SetCurPosition(this->caretpos);
351  this->UpdateCaretPosition();
352  return true;
353 
354  case WKC_END:
355  this->caretpos = this->bytes - 1;
356  this->char_iter->SetCurPosition(this->caretpos);
357  this->UpdateCaretPosition();
358  return true;
359 
360  default:
361  break;
362  }
363 
364  return false;
365 }
366 
375  : buf(MallocT<char>(max_bytes))
376 {
377  assert(max_bytes != 0);
378  assert(max_chars != 0);
379 
380  this->char_iter = StringIterator::Create();
381 
382  this->afilter = CS_ALPHANUMERAL;
383  this->max_bytes = max_bytes;
384  this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars;
385  this->caret = true;
386  this->DeleteAll();
387 }
388 
389 Textbuf::~Textbuf()
390 {
391  delete this->char_iter;
392  free(this->buf);
393 }
394 
400 {
401  GetString(this->buf, string, &this->buf[this->max_bytes - 1]);
402  this->UpdateSize();
403 }
404 
409 void Textbuf::Assign(const char *text)
410 {
411  strecpy(this->buf, text, &this->buf[this->max_bytes - 1]);
412  this->UpdateSize();
413 }
414 
418 void Textbuf::Print(const char *format, ...)
419 {
420  va_list va;
421  va_start(va, format);
422  vseprintf(this->buf, &this->buf[this->max_bytes - 1], format, va);
423  va_end(va);
424  this->UpdateSize();
425 }
426 
427 
434 {
435  const char *buf = this->buf;
436 
437  this->chars = this->bytes = 1; // terminating zero
438 
439  WChar c;
440  while ((c = Utf8Consume(&buf)) != '\0') {
441  this->bytes += Utf8CharLen(c);
442  this->chars++;
443  }
444  assert(this->bytes <= this->max_bytes);
445  assert(this->chars <= this->max_chars);
446 
447  this->caretpos = this->bytes - 1;
448  this->UpdateStringIter();
449  this->UpdateWidth();
450  this->UpdateMarkedText();
451 
452  this->UpdateCaretPosition();
453 }
454 
460 {
461  /* caret changed? */
462  bool b = !!(_caret_timer & 0x20);
463 
464  if (b != this->caret) {
465  this->caret = b;
466  return true;
467  }
468  return false;
469 }
470 
471 HandleKeyPressResult Textbuf::HandleKeyPress(WChar key, uint16 keycode)
472 {
473  bool edited = false;
474 
475  switch (keycode) {
476  case WKC_ESC: return HKPR_CANCEL;
477 
478  case WKC_RETURN: case WKC_NUM_ENTER: return HKPR_CONFIRM;
479 
480  case (WKC_CTRL | 'V'):
481  edited = this->InsertClipboard();
482  break;
483 
484  case (WKC_CTRL | 'U'):
485  this->DeleteAll();
486  edited = true;
487  break;
488 
489  case WKC_BACKSPACE: case WKC_DELETE:
490  case WKC_CTRL | WKC_BACKSPACE: case WKC_CTRL | WKC_DELETE:
491  edited = this->DeleteChar(keycode);
492  break;
493 
494  case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
495  case WKC_CTRL | WKC_LEFT: case WKC_CTRL | WKC_RIGHT:
496  this->MovePos(keycode);
497  break;
498 
499  default:
500  if (IsValidChar(key, this->afilter)) {
501  edited = this->InsertChar(key);
502  } else {
503  return HKPR_NOT_HANDLED;
504  }
505  break;
506  }
507 
508  return edited ? HKPR_EDITING : HKPR_CURSOR;
509 }
Functions related to OTTD&#39;s strings.
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
virtual size_t Next(IterType what=ITER_CHARACTER)=0
Advance the cursor by one iteration unit.
static const size_t END
Sentinel to indicate end-of-iteration.
Definition: string_base.h:25
static StringIterator * Create()
Create a new iterator instance.
Definition: string.cpp:739
Stuff related to text buffers.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:50
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
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
Definition: string.cpp:437
bool InsertChar(uint32 key)
Insert a character to a textbuffer.
Definition: textbuf.cpp:134
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
virtual void SetString(const char *s)=0
Set a new iteration string.
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
virtual size_t Prev(IterType what=ITER_CHARACTER)=0
Move the cursor back by one iteration unit.
virtual size_t SetCurPosition(size_t pos)=0
Change the current string cursor.
void UpdateMarkedText()
Update pixel positions of the marked text area.
Definition: textbuf.cpp:305
Iterate over characters (or more exactly grapheme clusters).
Definition: string_base.h:20
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
Functions related to low-level strings.
bool caret
is the caret ("_") visible or not
Definition: textbuf_type.h:40
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:338
bool GetClipboardContents(char *buffer, const char *last)
Try to retrieve the current clipboard contents.
Definition: os2.cpp:183
uint16 markxoffs
the start position of the marked area in pixels
Definition: textbuf_type.h:45
Functions related to the allocation of memory.
static T * MallocT(size_t num_elements)
Simplified allocation function that allocates the specified number of elements of the given type...
Definition: alloc_func.hpp:59
uint16 pixels
the current size of the string in pixels
Definition: textbuf_type.h:39
Functions related to the gfx engine.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
Key does not affect editboxes.
Definition: textbuf_type.h:28
HandleKeyPressResult
Return values for Textbuf::HandleKeypress.
Definition: textbuf_type.h:22
static int8 Utf8CharLen(WChar c)
Return the length of a UTF-8 encoded character.
Definition: string_func.h:99
Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize)
Get the leading corner of a character in a single-line string relative to the start of the string...
Definition: gfx.cpp:727
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
Iterate over words.
Definition: string_base.h:21
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:399
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:699
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
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:27
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
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
static char * Utf8PrevChar(char *s)
Retrieve the previous UNICODE character in an UTF-8 encoded string.
Definition: string_func.h:143
Index of the normal font in the font tables.
Definition: gfx_type.h:204
size_t Utf8Encode(char *buf, WChar c)
Encode a unicode character and place it in the buffer.
Definition: string.cpp:477
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
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
Window functions not directly related to making/drawing windows.
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
Types related to the graphics and/or input devices.
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