blob: 4fd1d0ecf47187076443d2312c731b21a7cfe107 [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
2 Copyright 2012 bigbiff/Dees_Troy TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
Dees_Troy51a0e822012-09-05 15:24:24 -040019// input.cpp - GUIInput object
20
21#include <linux/input.h>
22#include <pthread.h>
23#include <stdarg.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <sys/reboot.h>
29#include <sys/stat.h>
30#include <sys/time.h>
31#include <sys/mman.h>
32#include <sys/types.h>
33#include <sys/ioctl.h>
34#include <time.h>
35#include <unistd.h>
36#include <stdlib.h>
37
38#include <string>
39
40extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040043}
44
45#include "rapidxml.hpp"
46#include "objects.hpp"
47#include "../data.hpp"
48
49GUIInput::GUIInput(xml_node<>* node)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 : Conditional(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040051{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 xml_attribute<>* attr;
53 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 mInputText = NULL;
Dees_Troy51a0e822012-09-05 15:24:24 -040056 mAction = NULL;
57 mBackground = NULL;
58 mCursor = NULL;
59 mFont = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 mRendered = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040061 HasMask = false;
62 DrawCursor = false;
63 isLocalChange = true;
64 HasAllowed = false;
65 HasDisabled = false;
66 skipChars = scrollingX = mFontHeight = mFontY = lastX = 0;
67 mBackgroundX = mBackgroundY = mBackgroundW = mBackgroundH = MinLen = MaxLen = 0;
68 mCursorLocation = -1; // -1 is always the end of the string
69 CursorWidth = 3;
70 ConvertStrToColor("black", &mBackgroundColor);
71 ConvertStrToColor("white", &mCursorColor);
72
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 if (!node)
74 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 // Load text directly from the node
77 mInputText = new GUIText(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040078 // Load action directly from the node
79 mAction = new GUIAction(node);
80
81 if (mInputText->Render() < 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020082 {
83 delete mInputText;
84 mInputText = NULL;
85 }
Dees_Troy51a0e822012-09-05 15:24:24 -040086
87 // Load the background
88 child = node->first_node("background");
89 if (child)
90 {
91 attr = child->first_attribute("resource");
92 if (attr)
93 mBackground = PageManager::FindResource(attr->value());
94 attr = child->first_attribute("color");
95 if (attr)
96 {
97 std::string color = attr->value();
98 ConvertStrToColor(color, &mBackgroundColor);
99 }
100 }
101 if (mBackground && mBackground->GetResource())
102 {
103 mBackgroundW = gr_get_width(mBackground->GetResource());
104 mBackgroundH = gr_get_height(mBackground->GetResource());
105 }
106
107 // Load the cursor color
108 child = node->first_node("cursor");
109 if (child)
110 {
111 attr = child->first_attribute("resource");
112 if (attr)
113 mCursor = PageManager::FindResource(attr->value());
114 attr = child->first_attribute("color");
115 if (attr)
116 {
117 std::string color = attr->value();
118 ConvertStrToColor(color, &mCursorColor);
119 }
120 attr = child->first_attribute("hasfocus");
121 if (attr)
122 {
123 std::string color = attr->value();
124 SetInputFocus(atoi(color.c_str()));
125 }
126 attr = child->first_attribute("width");
127 if (attr)
128 {
129 std::string cwidth = gui_parse_text(attr->value());
130 CursorWidth = atoi(cwidth.c_str());
131 }
132 }
133 DrawCursor = HasInputFocus;
134
135 // Load the font, and possibly override the color
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 child = node->first_node("font");
137 if (child)
138 {
139 attr = child->first_attribute("resource");
140 if (attr) {
141 mFont = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -0400142 gr_getFontDetails(mFont ? mFont->GetResource() : NULL, &mFontHeight, NULL);
143 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200144 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
146 child = node->first_node("text");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 if (child) mText = child->value();
Dees_Troy51a0e822012-09-05 15:24:24 -0400148 mLastValue = gui_parse_text(mText);
149
150 child = node->first_node("data");
151 if (child)
152 {
153 attr = child->first_attribute("name");
154 if (attr)
155 mVariable = attr->value();
156 attr = child->first_attribute("default");
157 if (attr)
158 DataManager::SetValue(mVariable, attr->value());
159 attr = child->first_attribute("mask");
160 if (attr) {
161 mMask = attr->value();
162 HasMask = true;
163 }
164 attr = child->first_attribute("maskvariable");
165 if (attr)
166 mMaskVariable = attr->value();
167 else
168 mMaskVariable = mVariable;
169 }
170
171 // Load input restrictions
172 child = node->first_node("restrict");
173 if (child)
174 {
175 attr = child->first_attribute("minlen");
176 if (attr) {
177 std::string attrib = attr->value();
178 MinLen = atoi(attrib.c_str());
179 }
180 attr = child->first_attribute("maxlen");
181 if (attr) {
182 std::string attrib = attr->value();
183 MaxLen = atoi(attrib.c_str());
184 }
185 attr = child->first_attribute("allow");
186 if (attr) {
187 HasAllowed = true;
188 AllowedList = attr->value();
189 }
190 attr = child->first_attribute("disable");
191 if (attr) {
192 HasDisabled = true;
193 DisabledList = attr->value();
194 }
195 }
196
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 // Load the placement
Dees_Troy51a0e822012-09-05 15:24:24 -0400198 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH);
199 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
200
201 if (mInputText && mFontHeight && mFontHeight < (unsigned)mRenderH) {
202 mFontY = ((mRenderH - mFontHeight) / 2) + mRenderY;
203 mInputText->SetRenderPos(mRenderX, mFontY);
204 } else
205 mFontY = mRenderY;
206
207 if (mInputText)
208 mInputText->SetMaxWidth(mRenderW);
209
210 isLocalChange = false;
211 HandleTextLocation(-3);
Dees_Troy51a0e822012-09-05 15:24:24 -0400212}
213
214GUIInput::~GUIInput()
215{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200216 if (mInputText) delete mInputText;
217 if (mBackground) delete mBackground;
218 if (mCursor) delete mCursor;
219 if (mFont) delete mFont;
220 if (mAction) delete mAction;
Dees_Troy51a0e822012-09-05 15:24:24 -0400221}
222
223int GUIInput::HandleTextLocation(int x)
224{
225 int textWidth;
226 string displayValue, originalValue, insertChar;
227 void* fontResource = NULL;
228
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200229 if (mFont)
230 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400231
232 DataManager::GetValue(mVariable, originalValue);
233 displayValue = originalValue;
234 if (HasMask) {
235 int index, string_size = displayValue.size();
236 string maskedValue;
237 for (index=0; index<string_size; index++)
238 maskedValue += mMask;
239 displayValue = maskedValue;
240 }
241 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
242 if (textWidth <= mRenderW) {
243 lastX = x;
244 scrollingX = 0;
245 skipChars = 0;
246 mInputText->SkipCharCount(skipChars);
247 mRendered = false;
248 return 0;
249 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200250
251 if (skipChars && skipChars < displayValue.size())
Dees_Troy51a0e822012-09-05 15:24:24 -0400252 displayValue.erase(0, skipChars);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200253
Dees_Troy51a0e822012-09-05 15:24:24 -0400254 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
255 mRendered = false;
256
257 int deltaX, deltaText, newWidth;
258
259 if (x < -1000) {
260 // No change in scrolling
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200261 if (x == -1003)
Dees_Troy51a0e822012-09-05 15:24:24 -0400262 mCursorLocation = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200263
Dees_Troy51a0e822012-09-05 15:24:24 -0400264 if (mCursorLocation == -1) {
265 displayValue = originalValue;
266 skipChars = 0;
267 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
268 while (textWidth > mRenderW) {
269 displayValue.erase(0, 1);
270 skipChars++;
271 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
272 }
273 scrollingX = mRenderW - textWidth;
274 mInputText->SkipCharCount(skipChars);
275 } else if (x == -1001) {
276 // Added a new character
277 int adjust_scrollingX = 0;
278 string cursorLocate;
279
280 cursorLocate = displayValue;
281 cursorLocate.resize(mCursorLocation);
282 textWidth = gr_measureEx(cursorLocate.c_str(), fontResource);
283 while (textWidth > mRenderW) {
284 skipChars++;
285 mCursorLocation--;
286 cursorLocate.erase(0, 1);
287 textWidth = gr_measureEx(cursorLocate.c_str(), fontResource);
288 adjust_scrollingX = -1;
289 }
290 if (adjust_scrollingX) {
291 scrollingX = mRenderW - textWidth;
292 if (scrollingX < 0)
293 scrollingX = 0;
294 }
295 mInputText->SkipCharCount(skipChars);
296 } else if (x == -1002) {
297 // Deleted a character
298 while (-1) {
299 if (skipChars == 0) {
300 scrollingX = 0;
301 mInputText->SkipCharCount(skipChars);
302 return 0;
303 }
304 insertChar = originalValue.substr(skipChars - 1, 1);
305 displayValue.insert(0, insertChar);
306 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
307 deltaText = newWidth - textWidth;
308 if (newWidth > mRenderW) {
309 scrollingX = mRenderW - textWidth;
310 if (scrollingX < 0)
311 scrollingX = 0;
312 mInputText->SkipCharCount(skipChars);
313 return 0;
314 } else {
315 textWidth = newWidth;
316 skipChars--;
317 mCursorLocation++;
318 }
319 }
320 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000321 LOGINFO("GUIInput::HandleTextLocation -> We really shouldn't ever get here...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400322 } else if (x > lastX) {
323 // Dragging to right, scrolling left
324 while (-1) {
325 deltaX = x - lastX + scrollingX;
326 if (skipChars == 0 || deltaX == 0) {
327 scrollingX = 0;
328 lastX = x;
329 mInputText->SkipCharCount(skipChars);
330 return 0;
331 }
332 insertChar = originalValue.substr(skipChars - 1, 1);
333 displayValue.insert(0, insertChar);
334 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
335 deltaText = newWidth - textWidth;
336 if (deltaText < deltaX) {
337 lastX += deltaText;
338 textWidth = newWidth;
339 skipChars--;
340 } else {
341 scrollingX = deltaX;
342 lastX = x;
343 mInputText->SkipCharCount(skipChars);
344 return 0;
345 }
346 }
347 } else if (x < lastX) {
348 // Dragging to left, scrolling right
349 if (textWidth <= mRenderW) {
350 lastX = x;
351 scrollingX = mRenderW - textWidth;
352 return 0;
353 }
354 if (scrollingX) {
355 deltaX = lastX - x;
356 if (scrollingX > deltaX) {
357 scrollingX -= deltaX;
358 lastX = x;
359 return 0;
360 } else {
361 lastX -= deltaX;
362 scrollingX = 0;
363 }
364 }
365 while (-1) {
366 deltaX = lastX - x;
367 displayValue.erase(0, 1);
368 skipChars++;
369 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
370 deltaText = textWidth - newWidth;
371 if (newWidth <= mRenderW) {
372 scrollingX = mRenderW - newWidth;
373 lastX = x;
374 mInputText->SkipCharCount(skipChars);
375 return 0;
376 }
377 if (deltaText < deltaX) {
378 lastX -= deltaText;
379 textWidth = newWidth;
380 } else {
381 scrollingX = deltaText - deltaX;
382 lastX = x;
383 mInputText->SkipCharCount(skipChars);
384 return 0;
385 }
386 }
387 }
388 return 0;
389}
390
391int GUIInput::Render(void)
392{
393 if (!isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200394 {
395 mRendered = false;
396 return 0;
397 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400398
399 void* fontResource = NULL;
400 if (mFont) fontResource = mFont->GetResource();
401
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200402 // First step, fill background
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
404 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
405
406 // Next, render the background resource (if it exists)
407 if (mBackground && mBackground->GetResource())
408 {
409 mBackgroundX = mRenderX + ((mRenderW - mBackgroundW) / 2);
410 mBackgroundY = mRenderY + ((mRenderH - mBackgroundH) / 2);
411 gr_blit(mBackground->GetResource(), 0, 0, mBackgroundW, mBackgroundH, mBackgroundX, mBackgroundY);
412 }
413
414 int ret = 0;
415
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200416 // Render the text
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 mInputText->SetRenderPos(mRenderX + scrollingX, mFontY);
418 mInputText->SetMaxWidth(mRenderW - scrollingX);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200419 if (mInputText) ret = mInputText->Render();
420 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400421
422 if (HasInputFocus && DrawCursor) {
423 // Render the cursor
424 string displayValue;
425 int cursorX;
426 DataManager::GetValue(mVariable, displayValue);
427 if (HasMask) {
428 int index, string_size = displayValue.size();
429 string maskedValue;
430 for (index=0; index<string_size; index++)
431 maskedValue += mMask;
432 displayValue = maskedValue;
433 }
434 if (displayValue.size() == 0) {
435 skipChars = 0;
436 mCursorLocation = -1;
437 cursorX = mRenderX;
438 } else {
439 if (skipChars && skipChars < displayValue.size()) {
440 displayValue.erase(0, skipChars);
441 }
442 if (mCursorLocation == 0) {
443 // Cursor is at the beginning
444 cursorX = mRenderX;
445 } else if (mCursorLocation > 0) {
446 // Cursor is in the middle
447 if (displayValue.size() > (unsigned)mCursorLocation) {
448 string cursorDisplay;
449
450 cursorDisplay = displayValue;
451 cursorDisplay.resize(mCursorLocation);
452 cursorX = gr_measureEx(cursorDisplay.c_str(), fontResource) + mRenderX;
453 } else {
454 // Cursor location is after the end of the text - reset to -1
455 mCursorLocation = -1;
456 cursorX = gr_measureEx(displayValue.c_str(), fontResource) + mRenderX;
457 }
458 } else {
459 // Cursor is at the end (-1)
460 cursorX = gr_measureEx(displayValue.c_str(), fontResource) + mRenderX;
461 }
462 }
463 cursorX += scrollingX;
464 // Make sure that the cursor doesn't go past the boundaries of the box
465 if (cursorX + (int)CursorWidth > mRenderX + mRenderW)
466 cursorX = mRenderX + mRenderW - CursorWidth;
467
468 // Set the color for the cursor
469 gr_color(mCursorColor.red, mCursorColor.green, mCursorColor.blue, 255);
470 gr_fill(cursorX, mFontY, CursorWidth, mFontHeight);
471 }
472
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200473 mRendered = true;
474 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400475}
476
477int GUIInput::Update(void)
478{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200479 if (!isConditionTrue()) return (mRendered ? 2 : 0);
480 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200482 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400483
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200484 if (mInputText) ret = mInputText->Update();
485 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400486
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200487 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400488}
489
490int GUIInput::GetSelection(int x, int y)
491{
492 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) return -1;
493 return (x - mRenderX);
494}
495
496int GUIInput::NotifyTouch(TOUCH_STATE state, int x, int y)
497{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200498 static int startSelection = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 int textWidth;
500 string displayValue, originalValue;
501 void* fontResource = NULL;
502
503 if (mFont) fontResource = mFont->GetResource();
504
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200505 if (!isConditionTrue())
506 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400507
508 if (!HasInputFocus) {
509 if (state != TOUCH_RELEASE)
510 return 0; // Only change focus if touch releases within the input box
511 if (GetSelection(x, y) >= 0) {
512 // When changing focus, we don't scroll or change the cursor location
513 PageManager::SetKeyBoardFocus(0);
514 PageManager::NotifyKeyboard(0);
515 SetInputFocus(1);
516 DrawCursor = true;
517 mRendered = false;
518 }
519 } else {
520 switch (state) {
521 case TOUCH_HOLD:
522 case TOUCH_REPEAT:
523 break;
524 case TOUCH_START:
525 startSelection = GetSelection(x,y);
526 lastX = x;
527 DrawCursor = false;
528 mRendered = false;
529 break;
530
531 case TOUCH_DRAG:
532 // Check if we dragged out of the selection window
533 if (GetSelection(x, y) == -1) {
534 lastX = 0;
535 break;
536 }
537
538 DrawCursor = false;
539
540 // Provide some debounce on initial touches
541 if (startSelection != -1 && abs(x - lastX) < 6) {
542 break;
543 }
544
545 startSelection = -1;
546 if (lastX != x)
547 HandleTextLocation(x);
548 break;
549
550 case TOUCH_RELEASE:
551 // We've moved the cursor location
552 int relativeX = x - mRenderX;
553
554 mRendered = false;
555 DrawCursor = true;
556 DataManager::GetValue(mVariable, displayValue);
557 if (HasMask) {
558 int index, string_size = displayValue.size();
559 string maskedValue;
560 for (index=0; index<string_size; index++)
561 maskedValue += mMask;
562 displayValue = maskedValue;
563 }
564 if (displayValue.size() == 0) {
565 skipChars = 0;
566 mCursorLocation = -1;
567 return 0;
568 } else if (skipChars && skipChars < displayValue.size()) {
569 displayValue.erase(0, skipChars);
570 }
571
572 string cursorString;
573 int cursorX = 0;
574 unsigned index = 0;
575
576 for(index=0; index<displayValue.size(); index++)
577 {
578 cursorString = displayValue.substr(0, index);
579 cursorX = gr_measureEx(cursorString.c_str(), fontResource) + mRenderX;
580 if (cursorX > x) {
581 if (index > 0)
582 mCursorLocation = index - 1;
583 else
584 mCursorLocation = index;
585 return 0;
586 }
587 }
588 mCursorLocation = -1;
589 break;
590 }
591 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200592 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400593}
594
595int GUIInput::NotifyVarChange(std::string varName, std::string value)
596{
597 if (varName == mVariable && !isLocalChange) {
598 HandleTextLocation(-1003);
599 return 0;
600 }
601 return 0;
602}
603
604int GUIInput::NotifyKeyboard(int key)
605{
606 string variableValue;
607
608 if (HasInputFocus) {
609 if (key == KEYBOARD_BACKSPACE) {
610 //Backspace
611 DataManager::GetValue(mVariable, variableValue);
612 if (variableValue.size() > 0 && (mCursorLocation + skipChars != 0 || mCursorLocation == -1)) {
613 if (mCursorLocation == -1) {
614 variableValue.resize(variableValue.size() - 1);
615 } else {
616 variableValue.erase(mCursorLocation + skipChars - 1, 1);
617 if (mCursorLocation > 0)
618 mCursorLocation--;
619 else if (skipChars > 0)
620 skipChars--;
621 }
622 isLocalChange = true;
623 DataManager::SetValue(mVariable, variableValue);
624 isLocalChange = false;
625
626 if (HasMask) {
627 int index, string_size = variableValue.size();
628 string maskedValue;
629 for (index=0; index<string_size; index++)
630 maskedValue += mMask;
631 DataManager::SetValue(mMaskVariable, maskedValue);
632 }
633 HandleTextLocation(-1002);
634 }
635 } else if (key == KEYBOARD_SWIPE_LEFT) {
636 // Delete all
637 isLocalChange = true;
638 if (mCursorLocation == -1) {
639 DataManager::SetValue (mVariable, "");
640 if (HasMask)
641 DataManager::SetValue(mMaskVariable, "");
642 mCursorLocation = -1;
643 } else {
644 DataManager::GetValue(mVariable, variableValue);
645 variableValue.erase(0, mCursorLocation + skipChars);
646 DataManager::SetValue(mVariable, variableValue);
647 if (HasMask) {
648 DataManager::GetValue(mMaskVariable, variableValue);
649 variableValue.erase(0, mCursorLocation + skipChars);
650 DataManager::SetValue(mMaskVariable, variableValue);
651 }
652 mCursorLocation = 0;
653 }
654 skipChars = 0;
655 scrollingX = 0;
656 mInputText->SkipCharCount(skipChars);
657 isLocalChange = false;
658 mRendered = false;
659 return 0;
660 } else if (key == KEYBOARD_ARROW_LEFT) {
661 if (mCursorLocation == 0 && skipChars == 0)
662 return 0; // we're already at the beginning
663 if (mCursorLocation == -1) {
664 DataManager::GetValue(mVariable, variableValue);
665 if (variableValue.size() == 0)
666 return 0;
667 mCursorLocation = variableValue.size() - skipChars - 1;
668 } else if (mCursorLocation == 0) {
669 skipChars--;
670 HandleTextLocation(-1002);
671 } else {
672 mCursorLocation--;
673 HandleTextLocation(-1002);
674 }
675 mRendered = false;
676 return 0;
677 } else if (key == KEYBOARD_ARROW_RIGHT) {
678 if (mCursorLocation == -1)
679 return 0; // we're already at the end
680 mCursorLocation++;
681 DataManager::GetValue(mVariable, variableValue);
682 if (variableValue.size() <= mCursorLocation + skipChars)
683 mCursorLocation = -1;
684 HandleTextLocation(-1001);
685 mRendered = false;
686 return 0;
687 } else if (key == KEYBOARD_HOME || key == KEYBOARD_ARROW_UP) {
688 DataManager::GetValue(mVariable, variableValue);
689 if (variableValue.size() == 0)
690 return 0;
691 mCursorLocation = 0;
692 skipChars = 0;
693 mRendered = false;
694 HandleTextLocation(-1002);
695 return 0;
696 } else if (key == KEYBOARD_END || key == KEYBOARD_ARROW_DOWN) {
697 mCursorLocation = -1;
698 mRendered = false;
699 HandleTextLocation(-1003);
700 return 0;
701 } else if (key < KEYBOARD_SPECIAL_KEYS && key > 0) {
702 // Regular key
703 if (HasAllowed && AllowedList.find((char)key) == string::npos) {
704 return 0;
705 }
706 if (HasDisabled && DisabledList.find((char)key) != string::npos) {
707 return 0;
708 }
709 DataManager::GetValue(mVariable, variableValue);
710 if (MaxLen != 0 && variableValue.size() >= MaxLen) {
711 return 0;
712 }
713 if (mCursorLocation == -1) {
714 variableValue += key;
715 } else {
716 const char newchar = (char)key;
717 const char* a = &newchar;
718 string newstring = a;
719 newstring.resize(1);
720 variableValue.insert(mCursorLocation + skipChars, newstring);
721 mCursorLocation++;
722 }
723 isLocalChange = true;
724 DataManager::SetValue(mVariable, variableValue);
725 HandleTextLocation(-1001);
726 isLocalChange = false;
727
728 if (HasMask) {
729 int index, string_size = variableValue.size();
730 string maskedValue;
731 for (index=0; index<string_size; index++)
732 maskedValue += mMask;
733 DataManager::SetValue(mMaskVariable, maskedValue);
734 }
735 } else if (key == KEYBOARD_ACTION) {
736 // Action
737 DataManager::GetValue(mVariable, variableValue);
738 if (mAction) {
739 unsigned inputLen = variableValue.length();
740 if (inputLen < MinLen)
741 return 0;
742 else if (MaxLen != 0 && inputLen > MaxLen)
743 return 0;
744 else
745 return (mAction ? mAction->NotifyTouch(TOUCH_RELEASE, mRenderX, mRenderY) : 1);
746 }
747 }
748 return 0;
749 } else {
750 if (key == 0) {
751 // Somewhat ugly hack-ish way to tell the box to redraw after losing focus to remove the cursor
752 mRendered = false;
753 return 1;
754 }
755 }
756 return 1;
757}