comparison engine/core/gui/widgets/utf8textbox.cpp @ 228:756b895e1dab

Merged unicode-support back into trunk. Now all GUI/visible strings should be unicode. Internal strings unchanged. Remember to use a font that actually has the desired codepoints. Current default unicode policiy is 'ignore'.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 21 Mar 2009 10:38:11 +0000
parents
children 64738befdf3b
comparison
equal deleted inserted replaced
227:d642169490f7 228:756b895e1dab
1 /***************************************************************************
2 * Copyright (C) 2009 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Lesser General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2.1 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <cassert>
24
25 // 3rd party library includes
26
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "util/utf8/utf8.h"
32
33 #include "utf8textbox.h"
34
35 namespace gcn {
36
37 UTF8TextBox::UTF8TextBox(const std::string & text)
38 :TextBox(text)
39 {
40 mStringEditor = new UTF8StringEditor();
41 }
42
43 UTF8TextBox::~ UTF8TextBox()
44 {
45 delete mStringEditor;
46 }
47
48 void UTF8TextBox::keyPressed(KeyEvent& keyEvent)
49 {
50 Key key = keyEvent.getKey();
51
52 if (key.getValue() == Key::LEFT)
53 {
54 if (mCaretColumn == 0)
55 {
56 if (mCaretRow > 0)
57 {
58 mCaretRow--;
59 mCaretColumn = mTextRows[mCaretRow].size();
60 }
61 }
62 else
63 {
64 mCaretColumn = mStringEditor->prevChar(mTextRows[mCaretRow], mCaretColumn);
65 }
66 }
67 else if (key.getValue() == Key::RIGHT)
68 {
69 if (mCaretColumn < mTextRows[mCaretRow].size())
70 {
71 mCaretColumn = mStringEditor->nextChar(mTextRows[mCaretRow], mCaretColumn);
72 }
73 else
74 {
75 if (mCaretRow < mTextRows.size() - 1)
76 {
77 mCaretRow++;
78 mCaretColumn = 0;
79 }
80 }
81 }
82 else if (key.getValue() == Key::DOWN)
83 {
84 setCaretRowUTF8(mCaretRow + 1);
85 }
86
87 else if (key.getValue() == Key::UP)
88 {
89 setCaretRowUTF8(mCaretRow - 1);
90 }
91
92 else if (key.getValue() == Key::HOME)
93 {
94 mCaretColumn = 0;
95 }
96
97 else if (key.getValue() == Key::END)
98 {
99 mCaretColumn = mTextRows[mCaretRow].size();
100 }
101
102 else if (key.getValue() == Key::ENTER && mEditable)
103 {
104 mTextRows.insert(mTextRows.begin() + mCaretRow + 1,
105 mTextRows[mCaretRow].substr(mCaretColumn, mTextRows[mCaretRow].size() - mCaretColumn));
106 mTextRows[mCaretRow].resize(mCaretColumn);
107 ++mCaretRow;
108 mCaretColumn = 0;
109 }
110
111 else if (key.getValue() == Key::BACKSPACE
112 && mCaretColumn != 0
113 && mEditable)
114 {
115 mCaretColumn = mStringEditor->prevChar(mTextRows[mCaretRow], mCaretColumn);
116 mCaretColumn = mStringEditor->eraseChar(mTextRows[mCaretRow], mCaretColumn);
117 }
118
119 else if (key.getValue() == Key::BACKSPACE
120 && mCaretColumn == 0
121 && mCaretRow != 0
122 && mEditable)
123 {
124 mCaretColumn = mTextRows[mCaretRow - 1].size();
125 mTextRows[mCaretRow - 1] += mTextRows[mCaretRow];
126 mTextRows.erase(mTextRows.begin() + mCaretRow);
127 mCaretRow--;
128 }
129
130 else if (key.getValue() == Key::DELETE
131 && mCaretColumn < (int)mTextRows[mCaretRow].size()
132 && mEditable)
133 {
134 mCaretColumn = mStringEditor->eraseChar(mTextRows[mCaretRow], mCaretColumn);
135 }
136
137 else if (key.getValue() == Key::DELETE
138 && mCaretColumn == (int)mTextRows[mCaretRow].size()
139 && mCaretRow < ((int)mTextRows.size() - 1)
140 && mEditable)
141 {
142 mTextRows[mCaretRow] += mTextRows[mCaretRow + 1];
143 mTextRows.erase(mTextRows.begin() + mCaretRow + 1);
144 }
145
146 else if(key.getValue() == Key::PAGE_UP)
147 {
148 Widget* par = getParent();
149
150 if (par != NULL)
151 {
152 int rowsPerPage = par->getChildrenArea().height / getFont()->getHeight();
153 int chars = mStringEditor->countChars(mTextRows[mCaretRow], mCaretColumn);
154 mCaretRow -= rowsPerPage;
155
156 if (mCaretRow < 0)
157 {
158 mCaretRow = 0;
159 }
160 mCaretColumn = mStringEditor->getOffset(mTextRows[mCaretRow], chars);
161 }
162 }
163
164 else if(key.getValue() == Key::PAGE_DOWN)
165 {
166 Widget* par = getParent();
167
168 if (par != NULL)
169 {
170 int rowsPerPage = par->getChildrenArea().height / getFont()->getHeight();
171 int chars = mStringEditor->countChars(mTextRows[mCaretRow], mCaretColumn);
172 mCaretRow += rowsPerPage;
173
174 if (mCaretRow >= (int)mTextRows.size())
175 {
176 mCaretRow = mTextRows.size() - 1;
177 }
178
179 mCaretColumn = mStringEditor->getOffset(mTextRows[mCaretRow], chars);
180 }
181 }
182
183 else if(key.getValue() == Key::TAB
184 && mEditable)
185 {
186 // FIXME: jump X spaces, so mCaretColumn % TAB_SIZE = 0 and X <= TAB_SIZE
187 mTextRows[mCaretRow].insert(mCaretColumn,std::string(" "));
188 mCaretColumn += 4;
189 }
190
191 else if ((key.isCharacter() || key.getValue() > 255)
192 && mEditable)
193 {
194 mCaretColumn = mStringEditor->insertChar(mTextRows[mCaretRow], mCaretColumn, key.getValue());
195 }
196
197 adjustSize();
198 scrollToCaret();
199 assert( utf8::is_valid(mTextRows[mCaretRow].begin(),mTextRows[mCaretRow].end()) );
200 assert( utf8::is_valid(mTextRows[mCaretRow].begin(),mTextRows[mCaretRow].begin() + mCaretColumn) );
201 keyEvent.consume();
202 }
203
204
205 void UTF8TextBox::setCaretColumnUTF8(int column)
206 {
207 // no need to clip the column, mStringEditor handles it automaticly
208 mCaretColumn = mStringEditor->getOffset(mTextRows[mCaretRow], column);
209 }
210
211 void UTF8TextBox::setCaretRowUTF8(int row)
212 {
213 int chars = mStringEditor->countChars(mTextRows[mCaretRow], mCaretColumn);
214 if (row < 0) {
215 row = 0;
216 } else if (row >= mTextRows.size()) {
217 row = mTextRows.size() - 1;
218 }
219 mCaretRow = row;
220 mCaretColumn = mStringEditor->getOffset(mTextRows[mCaretRow], chars);
221 }
222
223 }
224
225