annotate src/textbuf.cpp @ 20147:09c103c821b1 draft

(svn r25091) -Codechange: Call keycodes by their name.
author frosch <frosch@openttd.org>
date Sun, 17 Mar 2013 13:05:18 +0000
parents b39f0ea0d0cc
children 115d14632c22
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
1 /* $Id$ */
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
2
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
3 /*
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
4 * This file is part of OpenTTD.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
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.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
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.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
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/>.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
8 */
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
9
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
10 /** @file textbuf.cpp Textbuffer handling. */
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
11
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
12 #include "stdafx.h"
19804
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
13 #include <stdarg.h>
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
14
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
15 #include "textbuf_type.h"
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
16 #include "string_func.h"
19804
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
17 #include "strings_func.h"
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
18 #include "gfx_type.h"
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
19 #include "gfx_func.h"
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
20 #include "window_func.h"
19805
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
21 #include "core/alloc_func.hpp"
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
22
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
23 /**
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
24 * Try to retrieve the current clipboard contents.
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
25 *
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
26 * @note OS-specific function.
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
27 * @return True if some text could be retrieved.
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
28 */
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
29 bool GetClipboardContents(char *buffer, size_t buff_len);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
30
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
31 int _caret_timer;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
32
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
33
19610
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
34 /**
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
35 * Checks if it is possible to delete a character.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
36 * @param backspace if set, delete the character before the caret,
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
37 * otherwise, delete the character after it.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
38 * @return true if a character can be deleted in the given direction.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
39 */
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
40 bool Textbuf::CanDelChar(bool backspace)
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
41 {
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
42 return backspace ? this->caretpos != 0 : this->caretpos < this->bytes - 1;
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
43 }
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
44
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
45 /**
19612
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
46 * Get the next character that will be removed by DelChar.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
47 * @param backspace if set, delete the character before the caret,
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
48 * otherwise, delete the character after it.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
49 * @return the next character that will be removed by DelChar.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
50 * @warning You should ensure Textbuf::CanDelChar returns true before calling this function.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
51 */
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
52 WChar Textbuf::GetNextDelChar(bool backspace)
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
53 {
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
54 assert(this->CanDelChar(backspace));
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
55
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
56 const char *s;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
57 if (backspace) {
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
58 s = Utf8PrevChar(this->buf + this->caretpos);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
59 } else {
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
60 s = this->buf + this->caretpos;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
61 }
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
62
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
63 WChar c;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
64 Utf8Decode(&c, s);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
65 return c;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
66 }
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
67
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
68 /**
19610
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
69 * Delete a character at the caret position in a text buf.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
70 * @param backspace if set, delete the character before the caret,
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
71 * else delete the character after it.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
72 * @warning You should ensure Textbuf::CanDelChar returns true before calling this function.
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
73 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
74 void Textbuf::DelChar(bool backspace)
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
75 {
19610
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
76 assert(this->CanDelChar(backspace));
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
77
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
78 WChar c;
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
79 char *s = this->buf + this->caretpos;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
80
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
81 if (backspace) s = Utf8PrevChar(s);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
82
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
83 uint16 len = (uint16)Utf8Decode(&c, s);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
84 uint width = GetCharacterWidth(FS_NORMAL, c);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
85
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
86 this->pixels -= width;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
87 if (backspace) {
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
88 this->caretpos -= len;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
89 this->caretxoffs -= width;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
90 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
91
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
92 /* Move the remaining characters over the marker */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
93 memmove(s, s + len, this->bytes - (s - this->buf) - len);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
94 this->bytes -= len;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
95 this->chars--;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
96 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
97
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
98 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
99 * Delete a character from a textbuffer, either with 'Delete' or 'Backspace'
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
100 * The character is delete from the position the caret is at
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
101 * @param keycode Type of deletion, either WKC_BACKSPACE or WKC_DELETE
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
102 * @return Return true on successful change of Textbuf, or false otherwise
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
103 */
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
104 bool Textbuf::DeleteChar(uint16 keycode)
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
105 {
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
106 if (keycode == WKC_BACKSPACE || keycode == WKC_DELETE) {
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
107 bool backspace = keycode == WKC_BACKSPACE;
19610
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
108 if (CanDelChar(backspace)) {
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
109 this->DelChar(backspace);
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
110 return true;
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
111 }
55aadf549c63 (svn r24519) -Codechange [FS#5203]: Refactor character removal code of text edit
zuu <zuu@openttd.org>
parents: 19609
diff changeset
112 return false;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
113 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
114
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
115 if (keycode == (WKC_CTRL | WKC_BACKSPACE) || keycode == (WKC_CTRL | WKC_DELETE)) {
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
116 bool backspace = keycode == (WKC_CTRL | WKC_BACKSPACE);
19612
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
117
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
118 if (!CanDelChar(backspace)) return false;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
119 WChar c = this->GetNextDelChar(backspace);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
120
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
121 /* Backspace: Delete left whitespaces.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
122 * Delete: Delete right word.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
123 */
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
124 while (backspace ? IsWhitespace(c) : !IsWhitespace(c)) {
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
125 this->DelChar(backspace);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
126 if (!this->CanDelChar(backspace)) return true;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
127 c = this->GetNextDelChar(backspace);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
128 }
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
129 /* Backspace: Delete left word.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
130 * Delete: Delete right whitespaces.
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
131 */
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
132 while (backspace ? !IsWhitespace(c) : IsWhitespace(c)) {
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
133 this->DelChar(backspace);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
134 if (!this->CanDelChar(backspace)) return true;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
135 c = this->GetNextDelChar(backspace);
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
136 }
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
137 return true;
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
138 }
73e785234adc (svn r24521) -Feature [FS#5203]: Ctrl + Backspace/Delete to remove characters up to next word beginning in text edit boxes
zuu <zuu@openttd.org>
parents: 19611
diff changeset
139
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
140 return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
141 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
142
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
143 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
144 * Delete every character in the textbuffer
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
145 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
146 void Textbuf::DeleteAll()
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
147 {
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
148 memset(this->buf, 0, this->max_bytes);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
149 this->bytes = this->chars = 1;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
150 this->pixels = this->caretpos = this->caretxoffs = 0;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
151 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
152
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
153 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
154 * Insert a character to a textbuffer. If maxwidth of the Textbuf is zero,
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
155 * we don't care about the visual-length but only about the physical
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
156 * length of the string
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
157 * @param key Character to be inserted
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
158 * @return Return true on successful change of Textbuf, or false otherwise
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
159 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
160 bool Textbuf::InsertChar(WChar key)
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
161 {
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
162 const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
163 uint16 len = (uint16)Utf8CharLen(key);
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
164 if (this->bytes + len <= this->max_bytes && this->chars + 1 <= this->max_chars) {
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
165 memmove(this->buf + this->caretpos + len, this->buf + this->caretpos, this->bytes - this->caretpos);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
166 Utf8Encode(this->buf + this->caretpos, key);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
167 this->chars++;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
168 this->bytes += len;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
169 this->pixels += charwidth;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
170
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
171 this->caretpos += len;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
172 this->caretxoffs += charwidth;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
173 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
174 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
175 return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
176 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
177
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
178 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
179 * Insert a chunk of text from the clipboard onto the textbuffer. Get TEXT clipboard
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
180 * and append this up to the maximum length (either absolute or screenlength). If maxlength
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
181 * is zero, we don't care about the screenlength but only about the physical length of the string
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
182 * @return true on successful change of Textbuf, or false otherwise
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
183 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
184 bool Textbuf::InsertClipboard()
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
185 {
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
186 char utf8_buf[512];
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
187
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
188 if (!GetClipboardContents(utf8_buf, lengthof(utf8_buf))) return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
189
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
190 uint16 pixels = 0, bytes = 0, chars = 0;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
191 WChar c;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
192 for (const char *ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) {
20146
b39f0ea0d0cc (svn r25090) -Fix: Make editbox character filters also apply to pasted content from clipboard.
frosch <frosch@openttd.org>
parents: 20145
diff changeset
193 if (!IsValidChar(c, this->afilter)) break;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
194
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
195 byte len = Utf8CharLen(c);
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
196 if (this->bytes + bytes + len > this->max_bytes) break;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
197 if (this->chars + chars + 1 > this->max_chars) break;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
198
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
199 byte char_pixels = GetCharacterWidth(FS_NORMAL, c);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
200
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
201 pixels += char_pixels;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
202 bytes += len;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
203 chars++;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
204 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
205
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
206 if (bytes == 0) return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
207
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
208 memmove(this->buf + this->caretpos + bytes, this->buf + this->caretpos, this->bytes - this->caretpos);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
209 memcpy(this->buf + this->caretpos, utf8_buf, bytes);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
210 this->pixels += pixels;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
211 this->caretxoffs += pixels;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
212
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
213 this->bytes += bytes;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
214 this->chars += chars;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
215 this->caretpos += bytes;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
216 assert(this->bytes <= this->max_bytes);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
217 assert(this->chars <= this->max_chars);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
218 this->buf[this->bytes - 1] = '\0'; // terminating zero
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
219
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
220 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
221 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
222
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
223 /**
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
224 * Checks if it is possible to move caret to the left
19609
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
225 * @return true if the caret can be moved to the left, otherwise false.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
226 */
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
227 bool Textbuf::CanMoveCaretLeft()
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
228 {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
229 return this->caretpos != 0;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
230 }
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
231
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
232 /**
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
233 * Moves the caret to the left.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
234 * @pre Ensure that Textbuf::CanMoveCaretLeft returns true
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
235 * @return The character under the caret.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
236 */
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
237 WChar Textbuf::MoveCaretLeft()
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
238 {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
239 assert(this->CanMoveCaretLeft());
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
240
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
241 WChar c;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
242 const char *s = Utf8PrevChar(this->buf + this->caretpos);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
243 Utf8Decode(&c, s);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
244 this->caretpos = s - this->buf;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
245 this->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
246
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
247 return c;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
248 }
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
249
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
250 /**
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
251 * Checks if it is possible to move caret to the right
19609
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
252 * @return true if the caret can be moved to the right, otherwise false.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
253 */
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
254 bool Textbuf::CanMoveCaretRight()
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
255 {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
256 return this->caretpos < this->bytes - 1;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
257 }
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
258
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
259 /**
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
260 * Moves the caret to the right.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
261 * @pre Ensure that Textbuf::CanMoveCaretRight returns true
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
262 * @return The character under the caret.
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
263 */
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
264 WChar Textbuf::MoveCaretRight()
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
265 {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
266 assert(this->CanMoveCaretRight());
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
267
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
268 WChar c;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
269 this->caretpos += (uint16)Utf8Decode(&c, this->buf + this->caretpos);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
270 this->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
271
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
272 Utf8Decode(&c, this->buf + this->caretpos);
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
273 return c;
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
274 }
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
275
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
276 /**
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
277 * Handle text navigation with arrow keys left/right.
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
278 * This defines where the caret will blink and the next character interaction will occur
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
279 * @param keycode Direction in which navigation occurs (WKC_CTRL |) WKC_LEFT, (WKC_CTRL |) WKC_RIGHT, WKC_END, WKC_HOME
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
280 * @return Return true on successful change of Textbuf, or false otherwise
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
281 */
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
282 bool Textbuf::MovePos(uint16 keycode)
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
283 {
20147
09c103c821b1 (svn r25091) -Codechange: Call keycodes by their name.
frosch <frosch@openttd.org>
parents: 20146
diff changeset
284 switch (keycode) {
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
285 case WKC_LEFT:
19609
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
286 if (this->CanMoveCaretLeft()) {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
287 this->MoveCaretLeft();
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
288 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
289 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
290 break;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
291
19611
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
292 case WKC_CTRL | WKC_LEFT: {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
293 if (!this->CanMoveCaretLeft()) break;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
294
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
295 /* Unconditionally move one char to the left. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
296 WChar c = this->MoveCaretLeft();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
297 /* Consume left whitespaces. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
298 while (IsWhitespace(c)) {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
299 if (!this->CanMoveCaretLeft()) return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
300 c = this->MoveCaretLeft();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
301 }
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
302 /* Consume left word. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
303 while (!IsWhitespace(c)) {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
304 if (!this->CanMoveCaretLeft()) return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
305 c = this->MoveCaretLeft();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
306 }
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19805
diff changeset
307 /* Place caret at the beginning of the left word. */
19611
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
308 this->MoveCaretRight();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
309 return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
310 }
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
311
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
312 case WKC_RIGHT:
19609
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
313 if (this->CanMoveCaretRight()) {
9b71be2c6bfa (svn r24518) -Codechange [FS#5203]: Refactor arrow key text edit movement code (sbr)
zuu <zuu@openttd.org>
parents: 19421
diff changeset
314 this->MoveCaretRight();
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
315 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
316 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
317 break;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
318
19611
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
319 case WKC_CTRL | WKC_RIGHT: {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
320 if (!this->CanMoveCaretRight()) break;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
321
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
322 /* Unconditionally move one char to the right. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
323 WChar c = this->MoveCaretRight();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
324 /* Continue to consume current word. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
325 while (!IsWhitespace(c)) {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
326 if (!this->CanMoveCaretRight()) return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
327 c = this->MoveCaretRight();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
328 }
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
329 /* Consume right whitespaces. */
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
330 while (IsWhitespace(c)) {
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
331 if (!this->CanMoveCaretRight()) return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
332 c = this->MoveCaretRight();
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
333 }
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
334 return true;
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
335 }
e2e0bcaae55d (svn r24520) -Feature [FS#5203]: Ctrl + Arrow keys to move entire words in text edit boxes (sbr)
zuu <zuu@openttd.org>
parents: 19610
diff changeset
336
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
337 case WKC_HOME:
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
338 this->caretpos = 0;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
339 this->caretxoffs = 0;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
340 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
341
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
342 case WKC_END:
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
343 this->caretpos = this->bytes - 1;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
344 this->caretxoffs = this->pixels;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
345 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
346
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
347 default:
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
348 break;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
349 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
350
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
351 return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
352 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
353
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
354 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
355 * Initialize the textbuffer by supplying it the buffer to write into
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
356 * and the maximum length of this buffer
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
357 * @param buf the buffer that will be holding the data for input
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
358 * @param max_bytes maximum size in bytes, including terminating '\0'
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
359 * @param max_chars maximum size in chars, including terminating '\0'
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
360 */
19805
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
361 Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
362 : buf(MallocT<char>(max_bytes))
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
363 {
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
364 assert(max_bytes != 0);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
365 assert(max_chars != 0);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
366
20145
182822e16d5d (svn r25089) -Codechange: Move CharSetFilter from QueryString to Textbuf.
frosch <frosch@openttd.org>
parents: 19968
diff changeset
367 this->afilter = CS_ALPHANUMERAL;
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
368 this->max_bytes = max_bytes;
19805
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
369 this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars;
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
370 this->caret = true;
19805
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
371 this->DeleteAll();
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
372 }
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
373
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
374 Textbuf::~Textbuf()
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
375 {
3b005f175764 (svn r24738) -Codechange: Remove Textbuf::Initialize in favour of a constructor.
frosch <frosch@openttd.org>
parents: 19804
diff changeset
376 free(this->buf);
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
377 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
378
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
379 /**
19804
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
380 * Render a string into the textbuffer.
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
381 * @param string String
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
382 */
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
383 void Textbuf::Assign(StringID string)
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
384 {
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
385 GetString(this->buf, string, &this->buf[this->max_bytes - 1]);
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
386 this->UpdateSize();
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
387 }
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
388
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
389 /**
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
390 * Copy a string into the textbuffer.
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
391 * @param text Source.
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
392 */
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
393 void Textbuf::Assign(const char *text)
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
394 {
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
395 ttd_strlcpy(this->buf, text, this->max_bytes);
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
396 this->UpdateSize();
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
397 }
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
398
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
399 /**
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
400 * Print a formatted string into the textbuffer.
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
401 */
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
402 void Textbuf::Print(const char *format, ...)
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
403 {
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
404 va_list va;
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
405 va_start(va, format);
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
406 vsnprintf(this->buf, this->max_bytes, format, va);
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
407 va_end(va);
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
408 this->UpdateSize();
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
409 }
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
410
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
411
78a39d7d2217 (svn r24737) -Add: Textbuf::Assign and Textbuf::Print.
frosch <frosch@openttd.org>
parents: 19621
diff changeset
412 /**
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
413 * Update Textbuf type with its actual physical character and screenlength
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
414 * Get the count of characters in the string as well as the width in pixels.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
415 * Useful when copying in a larger amount of text at once
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
416 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
417 void Textbuf::UpdateSize()
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
418 {
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
419 const char *buf = this->buf;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
420
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
421 this->pixels = 0;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
422 this->chars = this->bytes = 1; // terminating zero
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
423
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
424 WChar c;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
425 while ((c = Utf8Consume(&buf)) != '\0') {
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
426 this->pixels += GetCharacterWidth(FS_NORMAL, c);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
427 this->bytes += Utf8CharLen(c);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
428 this->chars++;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
429 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
430
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
431 assert(this->bytes <= this->max_bytes);
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
432 assert(this->chars <= this->max_chars);
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
433
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
434 this->caretpos = this->bytes - 1;
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
435 this->caretxoffs = this->pixels;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
436 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
437
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
438 /**
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
439 * Handle the flashing of the caret.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
440 * @return True if the caret state changes.
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
441 */
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
442 bool Textbuf::HandleCaret()
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
443 {
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
444 /* caret changed? */
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
445 bool b = !!(_caret_timer & 0x20);
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
446
19421
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
447 if (b != this->caret) {
904203312f04 (svn r24324) -Codechange: Turn functions dealing with Textbufs into member functions.
frosch <frosch@openttd.org>
parents: 19420
diff changeset
448 this->caret = b;
19420
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
449 return true;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
450 }
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
451 return false;
a413e5168939 (svn r24323) -Codechange: Split Textbuf and associated functions to separate files.
frosch <frosch@openttd.org>
parents:
diff changeset
452 }