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 
374 Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
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 }