Root/
| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Massimiliano Torromeo * |
| 3 | * massimiliano.torromeo@gmail.com * |
| 4 | * * |
| 5 | * This program 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 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program 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 this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #include "inputdialog.h" |
| 22 | |
| 23 | #include "buttonbox.h" |
| 24 | #include "delegate.h" |
| 25 | #include "gmenu2x.h" |
| 26 | #include "iconbutton.h" |
| 27 | #include "surface.h" |
| 28 | #include "utilities.h" |
| 29 | |
| 30 | #include <SDL_gfxPrimitives.h> |
| 31 | |
| 32 | using namespace std; |
| 33 | |
| 34 | #define KEY_WIDTH 20 |
| 35 | #define KEY_HEIGHT 20 |
| 36 | #define KB_TOP 90 |
| 37 | |
| 38 | static bool utf8Code(unsigned char c) |
| 39 | { |
| 40 | return (c>=194 && c<=198) || c==208 || c==209; |
| 41 | } |
| 42 | |
| 43 | InputDialog::InputDialog(GMenu2X *gmenu2x, InputManager &inputMgr_, |
| 44 | Touchscreen &ts_, const string &text, |
| 45 | const string &startvalue, const string &title, const string &icon) |
| 46 | : Dialog(gmenu2x) |
| 47 | , inputMgr(inputMgr_) |
| 48 | , ts(ts_) |
| 49 | { |
| 50 | if (title.empty()) { |
| 51 | this->title = text; |
| 52 | this->text = ""; |
| 53 | } else { |
| 54 | this->title = title; |
| 55 | this->text = text; |
| 56 | } |
| 57 | this->icon = ""; |
| 58 | if (!icon.empty() && gmenu2x->sc[icon] != NULL) { |
| 59 | this->icon = icon; |
| 60 | } |
| 61 | |
| 62 | input = startvalue; |
| 63 | selCol = 0; |
| 64 | selRow = 0; |
| 65 | keyboard.resize(7); |
| 66 | |
| 67 | keyboard[0].push_back("abcdefghijklm"); |
| 68 | keyboard[0].push_back("nopqrstuvwxyz"); |
| 69 | keyboard[0].push_back("0123456789. "); |
| 70 | |
| 71 | keyboard[1].push_back("ABCDEFGHIJKLM"); |
| 72 | keyboard[1].push_back("NOPQRSTUVWXYZ"); |
| 73 | keyboard[1].push_back("_\"'`.,:;!? "); |
| 74 | |
| 75 | |
| 76 | keyboard[2].push_back("¡¿*+-/\\&<=>|"); |
| 77 | keyboard[2].push_back("()[]{}@#$%^~"); |
| 78 | keyboard[2].push_back("_\"'`.,:;!? "); |
| 79 | |
| 80 | |
| 81 | keyboard[3].push_back("àáèéìíòóùúýäõ"); |
| 82 | keyboard[3].push_back("ëïöüÿâêîôûåãñ"); |
| 83 | keyboard[3].push_back("čďěľĺňôřŕšťůž"); |
| 84 | keyboard[3].push_back("ąćęłńśżź "); |
| 85 | |
| 86 | keyboard[4].push_back("ÀÁÈÉÌÍÒÓÙÚÝÄÕ"); |
| 87 | keyboard[4].push_back("ËÏÖÜŸÂÊÎÔÛÅÃÑ"); |
| 88 | keyboard[4].push_back("ČĎĚĽĹŇÔŘŔŠŤŮŽ"); |
| 89 | keyboard[4].push_back("ĄĆĘŁŃŚŻŹ "); |
| 90 | |
| 91 | |
| 92 | keyboard[5].push_back("æçабвгдеёжзий "); |
| 93 | keyboard[5].push_back("клмнопрстуфхцч"); |
| 94 | keyboard[5].push_back("шщъыьэюяøðßÐÞþ"); |
| 95 | |
| 96 | keyboard[6].push_back("ÆÇАБВГДЕЁЖЗИЙ "); |
| 97 | keyboard[6].push_back("КЛМНОПРСТУФХЦЧ"); |
| 98 | keyboard[6].push_back("ШЩЪЫЬЭЮЯØðßÐÞþ"); |
| 99 | |
| 100 | setKeyboard(0); |
| 101 | |
| 102 | buttonbox = new ButtonBox(gmenu2x); |
| 103 | IconButton *btnBackspace = new IconButton(gmenu2x, ts, |
| 104 | "skin:imgs/buttons/l.png", gmenu2x->tr["Backspace"]); |
| 105 | btnBackspace->setAction(BIND(&InputDialog::backspace)); |
| 106 | buttonbox->add(btnBackspace); |
| 107 | |
| 108 | IconButton *btnSpace = new IconButton(gmenu2x, ts, |
| 109 | "skin:imgs/buttons/r.png", gmenu2x->tr["Space"]); |
| 110 | btnSpace->setAction(BIND(&InputDialog::space)); |
| 111 | buttonbox->add(btnSpace); |
| 112 | |
| 113 | IconButton *btnConfirm = new IconButton(gmenu2x, ts, |
| 114 | "skin:imgs/buttons/accept.png", gmenu2x->tr["Confirm"]); |
| 115 | btnConfirm->setAction(BIND(&InputDialog::confirm)); |
| 116 | buttonbox->add(btnConfirm); |
| 117 | |
| 118 | IconButton *btnChangeKeys = new IconButton(gmenu2x, ts, |
| 119 | "skin:imgs/buttons/cancel.png", gmenu2x->tr["Change keys"]); |
| 120 | btnChangeKeys->setAction(BIND(&InputDialog::changeKeys)); |
| 121 | buttonbox->add(btnChangeKeys); |
| 122 | } |
| 123 | |
| 124 | void InputDialog::setKeyboard(int kb) { |
| 125 | kb = constrain(kb, 0, keyboard.size() - 1); |
| 126 | curKeyboard = kb; |
| 127 | this->kb = &(keyboard[kb]); |
| 128 | kbLength = this->kb->at(0).length(); |
| 129 | for (int x = 0, l = kbLength; x < l; x++) { |
| 130 | if (utf8Code(this->kb->at(0)[x])) { |
| 131 | kbLength--; |
| 132 | x++; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | kbLeft = 160 - kbLength * KEY_WIDTH / 2; |
| 137 | kbWidth = kbLength * KEY_WIDTH + 3; |
| 138 | kbHeight = (this->kb->size() + 1) * KEY_HEIGHT + 3; |
| 139 | |
| 140 | kbRect.x = kbLeft - 3; |
| 141 | kbRect.y = KB_TOP - 2; |
| 142 | kbRect.w = kbWidth; |
| 143 | kbRect.h = kbHeight; |
| 144 | } |
| 145 | |
| 146 | bool InputDialog::exec() { |
| 147 | SDL_Rect box = { |
| 148 | 0, 60, 0, static_cast<Uint16>(gmenu2x->font->getHeight() + 4) |
| 149 | }; |
| 150 | |
| 151 | Uint32 caretTick = 0, curTick; |
| 152 | bool caretOn = true; |
| 153 | |
| 154 | close = false; |
| 155 | ok = true; |
| 156 | while (!close) { |
| 157 | gmenu2x->bg->blit(gmenu2x->s,0,0); |
| 158 | writeTitle(title); |
| 159 | writeSubTitle(text); |
| 160 | drawTitleIcon(icon); |
| 161 | |
| 162 | buttonbox->paint(5); |
| 163 | |
| 164 | box.w = gmenu2x->font->getTextWidth(input) + 18; |
| 165 | box.x = 160 - box.w / 2; |
| 166 | gmenu2x->s->box(box.x, box.y, box.w, box.h, |
| 167 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 168 | gmenu2x->s->rectangle(box.x, box.y, box.w, box.h, |
| 169 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 170 | |
| 171 | gmenu2x->s->write(gmenu2x->font, input, box.x + 5, box.y + box.h - 2, |
| 172 | Font::HAlignLeft, Font::VAlignBottom); |
| 173 | |
| 174 | curTick = SDL_GetTicks(); |
| 175 | if (curTick - caretTick >= 600) { |
| 176 | caretOn = !caretOn; |
| 177 | caretTick = curTick; |
| 178 | } |
| 179 | |
| 180 | if (caretOn) { |
| 181 | gmenu2x->s->box(box.x + box.w - 12, box.y + 3, 8, box.h - 6, |
| 182 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 183 | } |
| 184 | |
| 185 | if (ts.available()) ts.poll(); |
| 186 | drawVirtualKeyboard(); |
| 187 | gmenu2x->s->flip(); |
| 188 | |
| 189 | switch (inputMgr.waitForPressedButton()) { |
| 190 | case InputManager::SETTINGS: |
| 191 | ok = false; |
| 192 | close = true; |
| 193 | break; |
| 194 | case InputManager::UP: |
| 195 | selRow--; |
| 196 | break; |
| 197 | case InputManager::DOWN: |
| 198 | selRow++; |
| 199 | if (selRow == (int)kb->size()) selCol = selCol < 8 ? 0 : 1; |
| 200 | break; |
| 201 | case InputManager::LEFT: |
| 202 | selCol--; |
| 203 | break; |
| 204 | case InputManager::RIGHT: |
| 205 | selCol++; |
| 206 | break; |
| 207 | case InputManager::ACCEPT: |
| 208 | confirm(); |
| 209 | break; |
| 210 | case InputManager::CANCEL: |
| 211 | changeKeys(); |
| 212 | break; |
| 213 | case InputManager::ALTLEFT: |
| 214 | backspace(); |
| 215 | break; |
| 216 | case InputManager::ALTRIGHT: |
| 217 | space(); |
| 218 | break; |
| 219 | default: |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return ok; |
| 225 | } |
| 226 | |
| 227 | void InputDialog::backspace() { |
| 228 | // Check for UTF8 characters. |
| 229 | input = input.substr(0, input.length() |
| 230 | - (utf8Code(input[input.length() - 2]) ? 2 : 1)); |
| 231 | } |
| 232 | |
| 233 | void InputDialog::space() { |
| 234 | input += " "; |
| 235 | } |
| 236 | |
| 237 | void InputDialog::confirm() { |
| 238 | if (selRow == (int)kb->size()) { |
| 239 | if (selCol == 0) { |
| 240 | ok = false; |
| 241 | } |
| 242 | close = true; |
| 243 | } else { |
| 244 | int xc = 0; |
| 245 | for (uint x = 0; x < kb->at(selRow).length(); x++) { |
| 246 | bool utf8 = utf8Code(kb->at(selRow)[x]); |
| 247 | if (xc == selCol) input += kb->at(selRow).substr(x, utf8 ? 2 : 1); |
| 248 | if (utf8) x++; |
| 249 | xc++; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void InputDialog::changeKeys() { |
| 255 | if (curKeyboard == 6) { |
| 256 | setKeyboard(0); |
| 257 | } else { |
| 258 | setKeyboard(curKeyboard + 1); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void InputDialog::drawVirtualKeyboard() { |
| 263 | //keyboard border |
| 264 | gmenu2x->s->rectangle(kbRect, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 265 | |
| 266 | if (selCol<0) selCol = selRow==(int)kb->size() ? 1 : kbLength-1; |
| 267 | if (selCol>=(int)kbLength) selCol = 0; |
| 268 | if (selRow<0) selRow = kb->size()-1; |
| 269 | if (selRow>(int)kb->size()) selRow = 0; |
| 270 | |
| 271 | //selection |
| 272 | if (selRow<(int)kb->size()) |
| 273 | gmenu2x->s->box(kbLeft + selCol * KEY_WIDTH - 1, |
| 274 | KB_TOP + selRow * KEY_HEIGHT, KEY_WIDTH - 1, KEY_HEIGHT - 2, |
| 275 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 276 | else { |
| 277 | if (selCol > 1) selCol = 0; |
| 278 | if (selCol < 0) selCol = 1; |
| 279 | gmenu2x->s->box(kbLeft + selCol * kbLength * KEY_WIDTH / 2 - 1, |
| 280 | KB_TOP + kb->size() * KEY_HEIGHT, kbLength * KEY_WIDTH / 2 - 1, |
| 281 | KEY_HEIGHT - 1, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 282 | } |
| 283 | |
| 284 | //keys |
| 285 | for (uint l=0; l<kb->size(); l++) { |
| 286 | string line = kb->at(l); |
| 287 | for (uint x=0, xc=0; x<line.length(); x++) { |
| 288 | string charX; |
| 289 | //utf8 characters |
| 290 | if (utf8Code(line[x])) { |
| 291 | charX = line.substr(x,2); |
| 292 | x++; |
| 293 | } else |
| 294 | charX = line[x]; |
| 295 | |
| 296 | SDL_Rect re = { |
| 297 | static_cast<Sint16>(kbLeft + xc * KEY_WIDTH - 1), |
| 298 | static_cast<Sint16>(KB_TOP + l * KEY_HEIGHT), |
| 299 | KEY_WIDTH - 1, |
| 300 | KEY_HEIGHT - 2 |
| 301 | }; |
| 302 | |
| 303 | //if ts on rect, change selection |
| 304 | if (ts.available() && ts.pressed() && ts.inRect(re)) { |
| 305 | selCol = xc; |
| 306 | selRow = l; |
| 307 | } |
| 308 | |
| 309 | gmenu2x->s->rectangle(re, |
| 310 | gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 311 | gmenu2x->s->write(gmenu2x->font, charX, |
| 312 | kbLeft + xc * KEY_WIDTH + KEY_WIDTH / 2 - 1, |
| 313 | KB_TOP + l * KEY_HEIGHT + KEY_HEIGHT / 2, |
| 314 | Font::HAlignCenter, Font::VAlignMiddle); |
| 315 | xc++; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | //Ok/Cancel |
| 320 | SDL_Rect re = { |
| 321 | static_cast<Sint16>(kbLeft - 1), |
| 322 | static_cast<Sint16>(KB_TOP + kb->size() * KEY_HEIGHT), |
| 323 | static_cast<Uint16>(kbLength * KEY_WIDTH / 2 - 1), |
| 324 | KEY_HEIGHT - 1 |
| 325 | }; |
| 326 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 327 | if (ts.available() && ts.pressed() && ts.inRect(re)) { |
| 328 | selCol = 0; |
| 329 | selRow = kb->size(); |
| 330 | } |
| 331 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], |
| 332 | (int)(160 - kbLength * KEY_WIDTH / 4), |
| 333 | KB_TOP + kb->size() * KEY_HEIGHT + KEY_HEIGHT / 2, |
| 334 | Font::HAlignCenter, Font::VAlignMiddle); |
| 335 | |
| 336 | re.x = kbLeft + kbLength * KEY_WIDTH / 2 - 1; |
| 337 | gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); |
| 338 | if (ts.available() && ts.pressed() && ts.inRect(re)) { |
| 339 | selCol = 1; |
| 340 | selRow = kb->size(); |
| 341 | } |
| 342 | gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], |
| 343 | (int)(160 + kbLength * KEY_WIDTH / 4), |
| 344 | KB_TOP + kb->size() * KEY_HEIGHT + KEY_HEIGHT / 2, |
| 345 | Font::HAlignCenter, Font::VAlignMiddle); |
| 346 | } |
| 347 |
Branches:
install_locations
master
opkrun
packages
