1 /* $Id: textbuf.cpp 25708 2013-08-10 12:47:11Z fonsinchen $ */
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/>.
10 /** @file textbuf.cpp Textbuffer handling. */
15 #include "textbuf_type.h"
16 #include "string_func.h"
17 #include "strings_func.h"
20 #include "window_func.h"
21 #include "core/alloc_func.hpp"
23 #include "safeguards.h"
26 * Try to retrieve the current clipboard contents.
28 * @note OS-specific function.
29 * @param buffer Clipboard content.
30 * @param last The pointer to the last element of the destination buffer
31 * @return True if some text could be retrieved.
33 bool GetClipboardContents(char *buffer
, const char *last
);
39 * Checks if it is possible to delete a character.
40 * @param backspace if set, delete the character before the caret,
41 * otherwise, delete the character after it.
42 * @return true if a character can be deleted in the given direction.
44 bool Textbuf::CanDelChar(bool backspace
)
46 return backspace
? this->caretpos
!= 0 : this->caretpos
< this->bytes
- 1;
50 * Delete a character from a textbuffer, either with 'Delete' or 'Backspace'
51 * The character is delete from the position the caret is at
52 * @param keycode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
53 * @return Return true on successful change of Textbuf, or false otherwise
55 bool Textbuf::DeleteChar(uint16 keycode
)
57 bool word
= (keycode
& WKC_CTRL
) != 0;
59 keycode
&= ~WKC_SPECIAL_KEYS
;
60 if (keycode
!= WKC_BACKSPACE
&& keycode
!= WKC_DELETE
) return false;
62 bool backspace
= keycode
== WKC_BACKSPACE
;
64 if (!CanDelChar(backspace
)) return false;
66 char *s
= this->buf
+ this->caretpos
;
70 /* Delete a complete word. */
72 /* Delete whitespace and word in front of the caret. */
73 len
= this->caretpos
- (uint16
)this->char_iter
->Prev(StringIterator::ITER_WORD
);
76 /* Delete word and following whitespace following the caret. */
77 len
= (uint16
)this->char_iter
->Next(StringIterator::ITER_WORD
) - this->caretpos
;
79 /* Update character count. */
80 for (const char *ss
= s
; ss
< s
+ len
; Utf8Consume(&ss
)) {
84 /* Delete a single character. */
86 /* Delete the last code point in front of the caret. */
89 len
= (uint16
)Utf8Decode(&c
, s
);
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
)) {
101 /* Move the remaining characters over the marker */
102 memmove(s
, s
+ len
, this->bytes
- (s
- this->buf
) - len
);
105 if (backspace
) this->caretpos
-= len
;
107 this->UpdateStringIter();
109 this->UpdateCaretPosition();
110 this->UpdateMarkedText();
116 * Delete every character in the textbuffer
118 void Textbuf::DeleteAll()
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();
128 * Insert a character to a textbuffer. If maxwidth of the Textbuf is zero,
129 * we don't care about the visual-length but only about the physical
130 * length of the string
131 * @param key Character to be inserted
132 * @return Return true on successful change of Textbuf, or false otherwise
134 bool Textbuf::InsertChar(WChar key
)
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
);
142 this->caretpos
+= len
;
144 this->UpdateStringIter();
146 this->UpdateCaretPosition();
147 this->UpdateMarkedText();
154 * Insert a string into the text buffer. If maxwidth of the Textbuf is zero,
155 * we don't care about the visual-length but only about the physical
156 * length of the string.
157 * @param str String to insert.
158 * @param marked Replace the currently marked text with the new text.
159 * @param caret Move the caret to this point in the insertion string.
160 * @param insert_location Position at which to insert the string.
161 * @param replacement_end Replace all characters from #insert_location up to this location with the new string.
162 * @return True on successful change of Textbuf, or false otherwise.
164 bool Textbuf::InsertString(const char *str
, bool marked
, const char *caret
, const char *insert_location
, const char *replacement_end
)
166 uint16 insertpos
= (marked
&& this->marklength
!= 0) ? this->markpos
: this->caretpos
;
167 if (insert_location
!= nullptr) {
168 insertpos
= insert_location
- this->buf
;
169 if (insertpos
> this->bytes
) return false;
171 if (replacement_end
!= nullptr) {
172 this->DeleteText(insertpos
, replacement_end
- this->buf
, str
== nullptr);
175 if (marked
) this->DiscardMarkedText(str
== nullptr);
178 if (str
== nullptr) return false;
180 uint16 bytes
= 0, chars
= 0;
182 for (const char *ptr
= str
; (c
= Utf8Consume(&ptr
)) != '\0';) {
183 if (!IsValidChar(c
, this->afilter
)) break;
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;
192 /* Move caret if needed. */
193 if (ptr
== caret
) this->caretpos
= insertpos
+ bytes
;
196 if (bytes
== 0) return false;
199 this->markpos
= insertpos
;
200 this->markend
= insertpos
+ bytes
;
203 memmove(this->buf
+ insertpos
+ bytes
, this->buf
+ insertpos
, this->bytes
- insertpos
);
204 memcpy(this->buf
+ insertpos
, str
, bytes
);
206 this->bytes
+= bytes
;
207 this->chars
+= chars
;
208 if (!marked
&& caret
== nullptr) 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
213 this->UpdateStringIter();
215 this->UpdateCaretPosition();
216 this->UpdateMarkedText();
222 * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
223 * and append this up to the maximum length (either absolute or screenlength). If maxlength
224 * is zero, we don't care about the screenlength but only about the physical length of the string
225 * @return true on successful change of Textbuf, or false otherwise
227 bool Textbuf::InsertClipboard()
231 if (!GetClipboardContents(utf8_buf
, lastof(utf8_buf
))) return false;
233 return this->InsertString(utf8_buf
, false);
237 * Delete a part of the text.
238 * @param from Start of the text to delete.
239 * @param to End of the text to delete.
240 * @param update Set to true if the internal state should be updated.
242 void Textbuf::DeleteText(uint16 from
, uint16 to
, bool update
)
245 const char *s
= this->buf
+ from
;
246 while (s
< this->buf
+ to
) {
251 /* Strip marked characters from buffer. */
252 memmove(this->buf
+ from
, this->buf
+ to
, this->bytes
- to
);
253 this->bytes
-= to
- from
;
256 /* Fixup caret if needed. */
257 if (this->caretpos
> from
) {
258 if (this->caretpos
<= to
) {
259 this->caretpos
= from
;
261 this->caretpos
-= to
- from
;
266 this->UpdateStringIter();
267 this->UpdateCaretPosition();
268 this->UpdateMarkedText();
273 * Discard any marked text.
274 * @param update Set to true if the internal state should be updated.
276 void Textbuf::DiscardMarkedText(bool update
)
278 if (this->markend
== 0) return;
280 this->DeleteText(this->markpos
, this->markend
, update
);
281 this->markpos
= this->markend
= this->markxoffs
= this->marklength
= 0;
284 /** Update the character iter after the text has changed. */
285 void Textbuf::UpdateStringIter()
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
;
292 /** Update pixel width of the text. */
293 void Textbuf::UpdateWidth()
295 this->pixels
= GetStringBoundingBox(this->buf
, FS_NORMAL
).width
;
298 /** Update pixel position of the caret. */
299 void Textbuf::UpdateCaretPosition()
301 this->caretxoffs
= this->chars
> 1 ? GetCharPosInString(this->buf
, this->buf
+ this->caretpos
, FS_NORMAL
).x
: 0;
304 /** Update pixel positions of the marked text area. */
305 void Textbuf::UpdateMarkedText()
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
;
311 this->markxoffs
= this->marklength
= 0;
316 * Handle text navigation with arrow keys left/right.
317 * This defines where the caret will blink and the next character interaction will occur
318 * @param keycode Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME
319 * @return Return true on successful change of Textbuf, or false otherwise
321 bool Textbuf::MovePos(uint16 keycode
)
325 case WKC_CTRL
| WKC_LEFT
: {
326 if (this->caretpos
== 0) break;
328 size_t pos
= this->char_iter
->Prev(keycode
& WKC_CTRL
? StringIterator::ITER_WORD
: StringIterator::ITER_CHARACTER
);
329 if (pos
== StringIterator::END
) return true;
331 this->caretpos
= (uint16
)pos
;
332 this->UpdateCaretPosition();
337 case WKC_CTRL
| WKC_RIGHT
: {
338 if (this->caretpos
>= this->bytes
- 1) break;
340 size_t pos
= this->char_iter
->Next(keycode
& WKC_CTRL
? StringIterator::ITER_WORD
: StringIterator::ITER_CHARACTER
);
341 if (pos
== StringIterator::END
) return true;
343 this->caretpos
= (uint16
)pos
;
344 this->UpdateCaretPosition();
350 this->char_iter
->SetCurPosition(this->caretpos
);
351 this->UpdateCaretPosition();
355 this->caretpos
= this->bytes
- 1;
356 this->char_iter
->SetCurPosition(this->caretpos
);
357 this->UpdateCaretPosition();
368 * Initialize the textbuffer by supplying it the buffer to write into
369 * and the maximum length of this buffer
370 * @param buf the buffer that will be holding the data for input
371 * @param max_bytes maximum size in bytes, including terminating '\0'
372 * @param max_chars maximum size in chars, including terminating '\0'
374 Textbuf::Textbuf(uint16 max_bytes
, uint16 max_chars
)
375 : buf(MallocT
<char>(max_bytes
))
377 assert(max_bytes
!= 0);
378 assert(max_chars
!= 0);
380 this->char_iter
= StringIterator::Create();
382 this->afilter
= CS_ALPHANUMERAL
;
383 this->max_bytes
= max_bytes
;
384 this->max_chars
= max_chars
== UINT16_MAX
? max_bytes
: max_chars
;
391 delete this->char_iter
;
396 * Render a string into the textbuffer.
397 * @param string String
399 void Textbuf::Assign(StringID string
)
401 GetString(this->buf
, string
, &this->buf
[this->max_bytes
- 1]);
406 * Copy a string into the textbuffer.
407 * @param text Source.
409 void Textbuf::Assign(const char *text
)
411 strecpy(this->buf
, text
, &this->buf
[this->max_bytes
- 1]);
416 * Print a formatted string into the textbuffer.
418 void Textbuf::Print(const char *format
, ...)
421 va_start(va
, format
);
422 vseprintf(this->buf
, &this->buf
[this->max_bytes
- 1], format
, va
);
429 * Update Textbuf type with its actual physical character and screenlength
430 * Get the count of characters in the string as well as the width in pixels.
431 * Useful when copying in a larger amount of text at once
433 void Textbuf::UpdateSize()
435 const char *buf
= this->buf
;
437 this->chars
= this->bytes
= 1; // terminating zero
440 while ((c
= Utf8Consume(&buf
)) != '\0') {
441 this->bytes
+= Utf8CharLen(c
);
444 assert(this->bytes
<= this->max_bytes
);
445 assert(this->chars
<= this->max_chars
);
447 this->caretpos
= this->bytes
- 1;
448 this->UpdateStringIter();
450 this->UpdateMarkedText();
452 this->UpdateCaretPosition();
456 * Handle the flashing of the caret.
457 * @return True if the caret state changes.
459 bool Textbuf::HandleCaret()
462 bool b
= !!(_caret_timer
& 0x20);
464 if (b
!= this->caret
) {
471 HandleKeyPressResult
Textbuf::HandleKeyPress(WChar key
, uint16 keycode
)
476 case WKC_ESC
: return HKPR_CANCEL
;
478 case WKC_RETURN
: case WKC_NUM_ENTER
: return HKPR_CONFIRM
;
480 case (WKC_CTRL
| 'V'):
481 edited
= this->InsertClipboard();
484 case (WKC_CTRL
| 'U'):
489 case WKC_BACKSPACE
: case WKC_DELETE
:
490 case WKC_CTRL
| WKC_BACKSPACE
: case WKC_CTRL
| WKC_DELETE
:
491 edited
= this->DeleteChar(keycode
);
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
);
500 if (IsValidChar(key
, this->afilter
)) {
501 edited
= this->InsertChar(key
);
503 return HKPR_NOT_HANDLED
;
508 return edited
? HKPR_EDITING
: HKPR_CURSOR
;