blob: 84ee17b1685c6683af8ff39a86930496dce525a6 [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 Bocekede51c52014-02-07 23:58:09 +010050 : GUIObject(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());
Vojtech Bocek76ee9032014-09-07 15:01:56 +0200142 mFontHeight = gr_getMaxFontHeight(mFont ? mFont->GetResource() : NULL);
Dees_Troy51a0e822012-09-05 15:24:24 -0400143 }
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;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200219 if (mAction) delete mAction;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220}
221
222int GUIInput::HandleTextLocation(int x)
223{
224 int textWidth;
225 string displayValue, originalValue, insertChar;
226 void* fontResource = NULL;
227
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200228 if (mFont)
229 fontResource = mFont->GetResource();
Dees_Troy51a0e822012-09-05 15:24:24 -0400230
231 DataManager::GetValue(mVariable, originalValue);
232 displayValue = originalValue;
233 if (HasMask) {
234 int index, string_size = displayValue.size();
235 string maskedValue;
236 for (index=0; index<string_size; index++)
237 maskedValue += mMask;
238 displayValue = maskedValue;
239 }
240 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
241 if (textWidth <= mRenderW) {
242 lastX = x;
243 scrollingX = 0;
244 skipChars = 0;
245 mInputText->SkipCharCount(skipChars);
246 mRendered = false;
247 return 0;
248 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200249
250 if (skipChars && skipChars < displayValue.size())
Dees_Troy51a0e822012-09-05 15:24:24 -0400251 displayValue.erase(0, skipChars);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200252
Dees_Troy51a0e822012-09-05 15:24:24 -0400253 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
254 mRendered = false;
255
256 int deltaX, deltaText, newWidth;
257
258 if (x < -1000) {
259 // No change in scrolling
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 if (x == -1003)
Dees_Troy51a0e822012-09-05 15:24:24 -0400261 mCursorLocation = -1;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262
Dees_Troy51a0e822012-09-05 15:24:24 -0400263 if (mCursorLocation == -1) {
264 displayValue = originalValue;
265 skipChars = 0;
266 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
267 while (textWidth > mRenderW) {
268 displayValue.erase(0, 1);
269 skipChars++;
270 textWidth = gr_measureEx(displayValue.c_str(), fontResource);
271 }
272 scrollingX = mRenderW - textWidth;
273 mInputText->SkipCharCount(skipChars);
274 } else if (x == -1001) {
275 // Added a new character
276 int adjust_scrollingX = 0;
277 string cursorLocate;
278
279 cursorLocate = displayValue;
280 cursorLocate.resize(mCursorLocation);
281 textWidth = gr_measureEx(cursorLocate.c_str(), fontResource);
282 while (textWidth > mRenderW) {
283 skipChars++;
284 mCursorLocation--;
285 cursorLocate.erase(0, 1);
286 textWidth = gr_measureEx(cursorLocate.c_str(), fontResource);
287 adjust_scrollingX = -1;
288 }
289 if (adjust_scrollingX) {
290 scrollingX = mRenderW - textWidth;
291 if (scrollingX < 0)
292 scrollingX = 0;
293 }
294 mInputText->SkipCharCount(skipChars);
295 } else if (x == -1002) {
296 // Deleted a character
297 while (-1) {
298 if (skipChars == 0) {
299 scrollingX = 0;
300 mInputText->SkipCharCount(skipChars);
301 return 0;
302 }
303 insertChar = originalValue.substr(skipChars - 1, 1);
304 displayValue.insert(0, insertChar);
305 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
306 deltaText = newWidth - textWidth;
307 if (newWidth > mRenderW) {
308 scrollingX = mRenderW - textWidth;
309 if (scrollingX < 0)
310 scrollingX = 0;
311 mInputText->SkipCharCount(skipChars);
312 return 0;
313 } else {
314 textWidth = newWidth;
315 skipChars--;
316 mCursorLocation++;
317 }
318 }
319 } else
Dees_Troy2673cec2013-04-02 20:22:16 +0000320 LOGINFO("GUIInput::HandleTextLocation -> We really shouldn't ever get here...\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 } else if (x > lastX) {
322 // Dragging to right, scrolling left
323 while (-1) {
324 deltaX = x - lastX + scrollingX;
325 if (skipChars == 0 || deltaX == 0) {
326 scrollingX = 0;
327 lastX = x;
328 mInputText->SkipCharCount(skipChars);
329 return 0;
330 }
331 insertChar = originalValue.substr(skipChars - 1, 1);
332 displayValue.insert(0, insertChar);
333 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
334 deltaText = newWidth - textWidth;
335 if (deltaText < deltaX) {
336 lastX += deltaText;
337 textWidth = newWidth;
338 skipChars--;
339 } else {
340 scrollingX = deltaX;
341 lastX = x;
342 mInputText->SkipCharCount(skipChars);
343 return 0;
344 }
345 }
346 } else if (x < lastX) {
347 // Dragging to left, scrolling right
348 if (textWidth <= mRenderW) {
349 lastX = x;
350 scrollingX = mRenderW - textWidth;
351 return 0;
352 }
353 if (scrollingX) {
354 deltaX = lastX - x;
355 if (scrollingX > deltaX) {
356 scrollingX -= deltaX;
357 lastX = x;
358 return 0;
359 } else {
360 lastX -= deltaX;
361 scrollingX = 0;
362 }
363 }
364 while (-1) {
365 deltaX = lastX - x;
366 displayValue.erase(0, 1);
367 skipChars++;
368 newWidth = gr_measureEx(displayValue.c_str(), fontResource);
369 deltaText = textWidth - newWidth;
370 if (newWidth <= mRenderW) {
371 scrollingX = mRenderW - newWidth;
372 lastX = x;
373 mInputText->SkipCharCount(skipChars);
374 return 0;
375 }
376 if (deltaText < deltaX) {
377 lastX -= deltaText;
378 textWidth = newWidth;
379 } else {
380 scrollingX = deltaText - deltaX;
381 lastX = x;
382 mInputText->SkipCharCount(skipChars);
383 return 0;
384 }
385 }
386 }
387 return 0;
388}
389
390int GUIInput::Render(void)
391{
392 if (!isConditionTrue())
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200393 {
394 mRendered = false;
395 return 0;
396 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400397
398 void* fontResource = NULL;
399 if (mFont) fontResource = mFont->GetResource();
400
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200401 // First step, fill background
Dees_Troy51a0e822012-09-05 15:24:24 -0400402 gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
403 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
404
405 // Next, render the background resource (if it exists)
406 if (mBackground && mBackground->GetResource())
407 {
408 mBackgroundX = mRenderX + ((mRenderW - mBackgroundW) / 2);
409 mBackgroundY = mRenderY + ((mRenderH - mBackgroundH) / 2);
410 gr_blit(mBackground->GetResource(), 0, 0, mBackgroundW, mBackgroundH, mBackgroundX, mBackgroundY);
411 }
412
413 int ret = 0;
414
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200415 // Render the text
Dees_Troy51a0e822012-09-05 15:24:24 -0400416 mInputText->SetRenderPos(mRenderX + scrollingX, mFontY);
417 mInputText->SetMaxWidth(mRenderW - scrollingX);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200418 if (mInputText) ret = mInputText->Render();
419 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400420
421 if (HasInputFocus && DrawCursor) {
422 // Render the cursor
423 string displayValue;
424 int cursorX;
425 DataManager::GetValue(mVariable, displayValue);
426 if (HasMask) {
427 int index, string_size = displayValue.size();
428 string maskedValue;
429 for (index=0; index<string_size; index++)
430 maskedValue += mMask;
431 displayValue = maskedValue;
432 }
433 if (displayValue.size() == 0) {
434 skipChars = 0;
435 mCursorLocation = -1;
436 cursorX = mRenderX;
437 } else {
438 if (skipChars && skipChars < displayValue.size()) {
439 displayValue.erase(0, skipChars);
440 }
441 if (mCursorLocation == 0) {
442 // Cursor is at the beginning
443 cursorX = mRenderX;
444 } else if (mCursorLocation > 0) {
445 // Cursor is in the middle
446 if (displayValue.size() > (unsigned)mCursorLocation) {
447 string cursorDisplay;
448
449 cursorDisplay = displayValue;
450 cursorDisplay.resize(mCursorLocation);
451 cursorX = gr_measureEx(cursorDisplay.c_str(), fontResource) + mRenderX;
452 } else {
453 // Cursor location is after the end of the text - reset to -1
454 mCursorLocation = -1;
455 cursorX = gr_measureEx(displayValue.c_str(), fontResource) + mRenderX;
456 }
457 } else {
458 // Cursor is at the end (-1)
459 cursorX = gr_measureEx(displayValue.c_str(), fontResource) + mRenderX;
460 }
461 }
462 cursorX += scrollingX;
463 // Make sure that the cursor doesn't go past the boundaries of the box
464 if (cursorX + (int)CursorWidth > mRenderX + mRenderW)
465 cursorX = mRenderX + mRenderW - CursorWidth;
466
467 // Set the color for the cursor
468 gr_color(mCursorColor.red, mCursorColor.green, mCursorColor.blue, 255);
469 gr_fill(cursorX, mFontY, CursorWidth, mFontHeight);
470 }
471
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200472 mRendered = true;
473 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400474}
475
476int GUIInput::Update(void)
477{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200478 if (!isConditionTrue()) return (mRendered ? 2 : 0);
479 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400480
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200481 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200483 if (mInputText) ret = mInputText->Update();
484 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400485
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200486 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400487}
488
489int GUIInput::GetSelection(int x, int y)
490{
491 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) return -1;
492 return (x - mRenderX);
493}
494
495int GUIInput::NotifyTouch(TOUCH_STATE state, int x, int y)
496{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200497 static int startSelection = -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 int textWidth;
499 string displayValue, originalValue;
500 void* fontResource = NULL;
501
502 if (mFont) fontResource = mFont->GetResource();
503
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200504 if (!isConditionTrue())
505 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400506
507 if (!HasInputFocus) {
508 if (state != TOUCH_RELEASE)
509 return 0; // Only change focus if touch releases within the input box
510 if (GetSelection(x, y) >= 0) {
511 // When changing focus, we don't scroll or change the cursor location
512 PageManager::SetKeyBoardFocus(0);
513 PageManager::NotifyKeyboard(0);
514 SetInputFocus(1);
515 DrawCursor = true;
516 mRendered = false;
517 }
518 } else {
519 switch (state) {
520 case TOUCH_HOLD:
521 case TOUCH_REPEAT:
522 break;
523 case TOUCH_START:
524 startSelection = GetSelection(x,y);
525 lastX = x;
526 DrawCursor = false;
527 mRendered = false;
528 break;
529
530 case TOUCH_DRAG:
531 // Check if we dragged out of the selection window
532 if (GetSelection(x, y) == -1) {
533 lastX = 0;
534 break;
535 }
536
537 DrawCursor = false;
538
539 // Provide some debounce on initial touches
540 if (startSelection != -1 && abs(x - lastX) < 6) {
541 break;
542 }
543
544 startSelection = -1;
545 if (lastX != x)
546 HandleTextLocation(x);
547 break;
548
549 case TOUCH_RELEASE:
550 // We've moved the cursor location
551 int relativeX = x - mRenderX;
552
553 mRendered = false;
554 DrawCursor = true;
555 DataManager::GetValue(mVariable, displayValue);
556 if (HasMask) {
557 int index, string_size = displayValue.size();
558 string maskedValue;
559 for (index=0; index<string_size; index++)
560 maskedValue += mMask;
561 displayValue = maskedValue;
562 }
563 if (displayValue.size() == 0) {
564 skipChars = 0;
565 mCursorLocation = -1;
566 return 0;
567 } else if (skipChars && skipChars < displayValue.size()) {
568 displayValue.erase(0, skipChars);
569 }
570
571 string cursorString;
572 int cursorX = 0;
573 unsigned index = 0;
574
575 for(index=0; index<displayValue.size(); index++)
576 {
577 cursorString = displayValue.substr(0, index);
578 cursorX = gr_measureEx(cursorString.c_str(), fontResource) + mRenderX;
579 if (cursorX > x) {
580 if (index > 0)
581 mCursorLocation = index - 1;
582 else
583 mCursorLocation = index;
584 return 0;
585 }
586 }
587 mCursorLocation = -1;
588 break;
589 }
590 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200591 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400592}
593
Vojtech Bocek07220562014-02-08 02:05:33 +0100594int GUIInput::NotifyVarChange(const std::string& varName, const std::string& value)
Dees_Troy51a0e822012-09-05 15:24:24 -0400595{
Vojtech Bocek07220562014-02-08 02:05:33 +0100596 GUIObject::NotifyVarChange(varName, value);
597
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 if (varName == mVariable && !isLocalChange) {
599 HandleTextLocation(-1003);
600 return 0;
601 }
602 return 0;
603}
604
605int GUIInput::NotifyKeyboard(int key)
606{
607 string variableValue;
608
609 if (HasInputFocus) {
610 if (key == KEYBOARD_BACKSPACE) {
611 //Backspace
612 DataManager::GetValue(mVariable, variableValue);
613 if (variableValue.size() > 0 && (mCursorLocation + skipChars != 0 || mCursorLocation == -1)) {
614 if (mCursorLocation == -1) {
615 variableValue.resize(variableValue.size() - 1);
616 } else {
617 variableValue.erase(mCursorLocation + skipChars - 1, 1);
618 if (mCursorLocation > 0)
619 mCursorLocation--;
620 else if (skipChars > 0)
621 skipChars--;
622 }
623 isLocalChange = true;
624 DataManager::SetValue(mVariable, variableValue);
625 isLocalChange = false;
626
627 if (HasMask) {
628 int index, string_size = variableValue.size();
629 string maskedValue;
630 for (index=0; index<string_size; index++)
631 maskedValue += mMask;
632 DataManager::SetValue(mMaskVariable, maskedValue);
633 }
634 HandleTextLocation(-1002);
635 }
636 } else if (key == KEYBOARD_SWIPE_LEFT) {
637 // Delete all
638 isLocalChange = true;
639 if (mCursorLocation == -1) {
640 DataManager::SetValue (mVariable, "");
641 if (HasMask)
642 DataManager::SetValue(mMaskVariable, "");
643 mCursorLocation = -1;
644 } else {
645 DataManager::GetValue(mVariable, variableValue);
646 variableValue.erase(0, mCursorLocation + skipChars);
647 DataManager::SetValue(mVariable, variableValue);
648 if (HasMask) {
649 DataManager::GetValue(mMaskVariable, variableValue);
650 variableValue.erase(0, mCursorLocation + skipChars);
651 DataManager::SetValue(mMaskVariable, variableValue);
652 }
653 mCursorLocation = 0;
654 }
655 skipChars = 0;
656 scrollingX = 0;
657 mInputText->SkipCharCount(skipChars);
658 isLocalChange = false;
659 mRendered = false;
660 return 0;
661 } else if (key == KEYBOARD_ARROW_LEFT) {
662 if (mCursorLocation == 0 && skipChars == 0)
663 return 0; // we're already at the beginning
664 if (mCursorLocation == -1) {
665 DataManager::GetValue(mVariable, variableValue);
666 if (variableValue.size() == 0)
667 return 0;
668 mCursorLocation = variableValue.size() - skipChars - 1;
669 } else if (mCursorLocation == 0) {
670 skipChars--;
671 HandleTextLocation(-1002);
672 } else {
673 mCursorLocation--;
674 HandleTextLocation(-1002);
675 }
676 mRendered = false;
677 return 0;
678 } else if (key == KEYBOARD_ARROW_RIGHT) {
679 if (mCursorLocation == -1)
680 return 0; // we're already at the end
681 mCursorLocation++;
682 DataManager::GetValue(mVariable, variableValue);
683 if (variableValue.size() <= mCursorLocation + skipChars)
684 mCursorLocation = -1;
685 HandleTextLocation(-1001);
686 mRendered = false;
687 return 0;
688 } else if (key == KEYBOARD_HOME || key == KEYBOARD_ARROW_UP) {
689 DataManager::GetValue(mVariable, variableValue);
690 if (variableValue.size() == 0)
691 return 0;
692 mCursorLocation = 0;
693 skipChars = 0;
694 mRendered = false;
695 HandleTextLocation(-1002);
696 return 0;
697 } else if (key == KEYBOARD_END || key == KEYBOARD_ARROW_DOWN) {
698 mCursorLocation = -1;
699 mRendered = false;
700 HandleTextLocation(-1003);
701 return 0;
702 } else if (key < KEYBOARD_SPECIAL_KEYS && key > 0) {
703 // Regular key
704 if (HasAllowed && AllowedList.find((char)key) == string::npos) {
705 return 0;
706 }
707 if (HasDisabled && DisabledList.find((char)key) != string::npos) {
708 return 0;
709 }
710 DataManager::GetValue(mVariable, variableValue);
711 if (MaxLen != 0 && variableValue.size() >= MaxLen) {
712 return 0;
713 }
714 if (mCursorLocation == -1) {
715 variableValue += key;
716 } else {
717 const char newchar = (char)key;
718 const char* a = &newchar;
719 string newstring = a;
720 newstring.resize(1);
721 variableValue.insert(mCursorLocation + skipChars, newstring);
722 mCursorLocation++;
723 }
724 isLocalChange = true;
725 DataManager::SetValue(mVariable, variableValue);
726 HandleTextLocation(-1001);
727 isLocalChange = false;
728
729 if (HasMask) {
730 int index, string_size = variableValue.size();
731 string maskedValue;
732 for (index=0; index<string_size; index++)
733 maskedValue += mMask;
734 DataManager::SetValue(mMaskVariable, maskedValue);
735 }
736 } else if (key == KEYBOARD_ACTION) {
737 // Action
738 DataManager::GetValue(mVariable, variableValue);
739 if (mAction) {
740 unsigned inputLen = variableValue.length();
741 if (inputLen < MinLen)
742 return 0;
743 else if (MaxLen != 0 && inputLen > MaxLen)
744 return 0;
745 else
746 return (mAction ? mAction->NotifyTouch(TOUCH_RELEASE, mRenderX, mRenderY) : 1);
747 }
748 }
749 return 0;
750 } else {
751 if (key == 0) {
752 // Somewhat ugly hack-ish way to tell the box to redraw after losing focus to remove the cursor
753 mRendered = false;
754 return 1;
755 }
756 }
757 return 1;
758}