| 1 | #include "stdlib.h" |
|---|
| 2 | #include "keyboard.h" |
|---|
| 3 | #include "platform.h" |
|---|
| 4 | #include "core.h" |
|---|
| 5 | #include "conf.h" |
|---|
| 6 | #include "lang.h" |
|---|
| 7 | #include "gui.h" |
|---|
| 8 | #include "gui_draw.h" |
|---|
| 9 | #include "gui_lang.h" |
|---|
| 10 | #include "gui_menu.h" |
|---|
| 11 | #include "gui_mbox.h" |
|---|
| 12 | #include "gui_tbox.h" |
|---|
| 13 | |
|---|
| 14 | #include "module_load.h" |
|---|
| 15 | |
|---|
| 16 | //------------------------------------------------------------------- |
|---|
| 17 | extern void gui_tbox_kbd_process(); |
|---|
| 18 | extern void gui_tbox_draw(int enforce_redraw); |
|---|
| 19 | |
|---|
| 20 | gui_handler GUI_MODE_TBOX = |
|---|
| 21 | /*GUI_MODE_TBOX*/ { GUI_MODE_MODULE, gui_tbox_draw, gui_tbox_kbd_process, gui_tbox_kbd_process, 0, GUI_MODE_MAGICNUM }; |
|---|
| 22 | |
|---|
| 23 | static gui_handler *gui_tbox_mode_old; // stored previous gui_mode |
|---|
| 24 | static int module_idx = -1; |
|---|
| 25 | |
|---|
| 26 | static int gui_tbox_redraw; |
|---|
| 27 | static char text_limit_reached; |
|---|
| 28 | static int vkbd_txtfield_width=200; |
|---|
| 29 | static int vkbd_txtfield_height=75; |
|---|
| 30 | static unsigned int tbox_width; //width of the 'window' |
|---|
| 31 | |
|---|
| 32 | static const char* tbox_title; |
|---|
| 33 | static const char* tbox_msg; |
|---|
| 34 | static char cursor_to_draw; |
|---|
| 35 | |
|---|
| 36 | // height of prompt |
|---|
| 37 | #define MAX_LINES 6 |
|---|
| 38 | #define MAX_WIDTH 40 |
|---|
| 39 | #define MIN_WIDTH 28 |
|---|
| 40 | #define SPACING_TITLE 4 |
|---|
| 41 | #define SPACING_BTN 4 |
|---|
| 42 | #define SPACING_BELOW_TEXT 10 |
|---|
| 43 | #define BUTTON_SEP 18 |
|---|
| 44 | #define BUTTON_SIZE 6 |
|---|
| 45 | |
|---|
| 46 | // max possible charsets |
|---|
| 47 | #define MAX_CHARSET 10 |
|---|
| 48 | |
|---|
| 49 | #define MAX_TEXT_WIDTH (MAX_WIDTH-2) |
|---|
| 50 | |
|---|
| 51 | #define RESET_CHAR lastKey = '\0'; curchar = -1; curgroup = -1; |
|---|
| 52 | |
|---|
| 53 | #define MAX_MSG_LENGH 20 //max length of hardcoded messages (>navigate cursor<, text limit reached) |
|---|
| 54 | |
|---|
| 55 | typedef void (*tbox_on_select_t)(const char* newstr); |
|---|
| 56 | tbox_on_select_t tbox_on_select = 0; |
|---|
| 57 | |
|---|
| 58 | static coord tbox_buttons_x, tbox_buttons_y; |
|---|
| 59 | |
|---|
| 60 | typedef char *cmap[][4]; |
|---|
| 61 | |
|---|
| 62 | // Char maps |
|---|
| 63 | cmap tbox_chars_default = |
|---|
| 64 | { |
|---|
| 65 | {"ABCDEF","GHIJKL","MNOPQRS","TUVWXYZ"}, |
|---|
| 66 | {"abcdef","ghijkl","mnopqrs","tuvwxyz"}, |
|---|
| 67 | {"123","456","789","0+-=/"}, |
|---|
| 68 | {".,:;?!","@#$%^&£","()[]{}","<>\"'`~_"}, |
|---|
| 69 | {0} |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | cmap tbox_chars_german = |
|---|
| 73 | { |
|---|
| 74 | {"ABCDEF","GHIJKL","MNOPQRS","TUVWXYZ"}, |
|---|
| 75 | {"abcdef","ghijkl","mnopqrs","tuvwxyz"}, |
|---|
| 76 | {"123","456","789","0+-=/"}, |
|---|
| 77 | {".,:;?!","@#$%^&£","()[]{}","<>\"'`~_"}, |
|---|
| 78 | {"ÄÖÜ","äöüß","§µ","°²³"}, |
|---|
| 79 | {0} |
|---|
| 80 | }; |
|---|
| 81 | |
|---|
| 82 | cmap tbox_chars_russian = |
|---|
| 83 | { |
|---|
| 84 | {"ABCDEF","GKLHIJ","MNOPQRS","TUVWXYZ"}, |
|---|
| 85 | {"abcdef","ghijkl","mnopqrs","tuvwxyz"}, |
|---|
| 86 | {"ÀÁÂÃÄÅÆ","ÇÈÉÊËÌÍ","ÎÏÐÑÒÓÔ","ÕÖרÙÛÜÝÞß"}, |
|---|
| 87 | {"àáâãäåæ","çèéêëìí","îïðñòóô","õö÷øùûüýþÿ"}, |
|---|
| 88 | {"123","456","789","0+-="}, |
|---|
| 89 | {" .:,",";/\\","'\"*","#%&"}, |
|---|
| 90 | {0} |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | cmap *charmaps[] = { &tbox_chars_default, &tbox_chars_german, &tbox_chars_russian }; |
|---|
| 94 | |
|---|
| 95 | int lines = 0; // num of valid lines in active charmap |
|---|
| 96 | |
|---|
| 97 | int tbox_button_active, line; |
|---|
| 98 | int curchar; // idx of current entered char in current tmap |
|---|
| 99 | int curgroup; |
|---|
| 100 | int cursor; |
|---|
| 101 | char lastKey; // Last pressed key (Left, Right, Up, Down) |
|---|
| 102 | char Mode; // K=keyboard, T=textnavigate, B=button |
|---|
| 103 | |
|---|
| 104 | char text_buf[MAX_TEXT_SIZE+1]; // Default buffer if not supplied by caller |
|---|
| 105 | char *text = 0; // Entered text |
|---|
| 106 | int maxlen, offset; |
|---|
| 107 | coord text_offset_x, text_offset_y, key_offset_x; |
|---|
| 108 | |
|---|
| 109 | //------------------------------------------------------- |
|---|
| 110 | static char *map_chars(int line, int group) |
|---|
| 111 | { |
|---|
| 112 | return (*charmaps[conf.tbox_char_map])[line][group]; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | //------------------------------------------------------- |
|---|
| 116 | int textbox_init(int title, int msg, const char* defaultstr, unsigned int maxsize, void (*on_select)(const char* newstr), char *input_buffer) |
|---|
| 117 | { |
|---|
| 118 | if (input_buffer) |
|---|
| 119 | text = input_buffer; |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | if (maxsize > MAX_TEXT_SIZE) maxsize = MAX_TEXT_SIZE; |
|---|
| 123 | text = text_buf; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | for (lines=0; map_chars(lines,0) && lines<MAX_CHARSET; lines++); |
|---|
| 127 | |
|---|
| 128 | memset(text, '\0', sizeof(char)*(maxsize+1)); |
|---|
| 129 | |
|---|
| 130 | if ( defaultstr ) |
|---|
| 131 | strncpy(text, defaultstr, maxsize); |
|---|
| 132 | |
|---|
| 133 | tbox_button_active = 0; |
|---|
| 134 | |
|---|
| 135 | tbox_title = lang_str(title); |
|---|
| 136 | tbox_msg = lang_str(msg); |
|---|
| 137 | tbox_on_select = on_select; |
|---|
| 138 | |
|---|
| 139 | Mode = 'K'; |
|---|
| 140 | line = 0; |
|---|
| 141 | RESET_CHAR |
|---|
| 142 | cursor = -1; |
|---|
| 143 | maxlen = maxsize; |
|---|
| 144 | offset = 0; |
|---|
| 145 | |
|---|
| 146 | gui_tbox_redraw = 2; |
|---|
| 147 | gui_tbox_mode_old = gui_set_mode( &GUI_MODE_TBOX ); |
|---|
| 148 | |
|---|
| 149 | return 1; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | static void gui_tbox_draw_buttons() { |
|---|
| 154 | coord x = tbox_buttons_x; |
|---|
| 155 | color cl; |
|---|
| 156 | |
|---|
| 157 | cl = MAKE_COLOR((tbox_button_active==0)?COLOR_RED:COLOR_BLACK, COLOR_WHITE); |
|---|
| 158 | draw_rect(x-1, tbox_buttons_y+FONT_HEIGHT-1, x+BUTTON_SIZE*FONT_WIDTH+3, tbox_buttons_y+2*FONT_HEIGHT+3, COLOR_BLACK); //shadow |
|---|
| 159 | draw_filled_rect(x-2, tbox_buttons_y-2+FONT_HEIGHT, x+BUTTON_SIZE*FONT_WIDTH+2, tbox_buttons_y+2*FONT_HEIGHT+2, cl); |
|---|
| 160 | draw_string(x+(((BUTTON_SIZE-strlen(lang_str(LANG_MBOX_BTN_OK)))*FONT_WIDTH)>>1), tbox_buttons_y+FONT_HEIGHT, lang_str(LANG_MBOX_BTN_OK), cl); |
|---|
| 161 | x+=BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP; |
|---|
| 162 | |
|---|
| 163 | cl = MAKE_COLOR((tbox_button_active==1)?COLOR_RED:COLOR_BLACK, COLOR_WHITE); |
|---|
| 164 | draw_rect(x-1, tbox_buttons_y+FONT_HEIGHT-1, x+BUTTON_SIZE*FONT_WIDTH+3, tbox_buttons_y+2*FONT_HEIGHT+3, COLOR_BLACK); //shadow |
|---|
| 165 | draw_filled_rect(x-2, tbox_buttons_y-2+FONT_HEIGHT, x+BUTTON_SIZE*FONT_WIDTH+2, tbox_buttons_y+2*FONT_HEIGHT+2, cl); |
|---|
| 166 | draw_string(x+(((BUTTON_SIZE-strlen(lang_str(LANG_MBOX_BTN_CANCEL)))*FONT_WIDTH)>>1), tbox_buttons_y+FONT_HEIGHT, lang_str(LANG_MBOX_BTN_CANCEL), cl); |
|---|
| 167 | x+=BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void gui_tbox_draw(int enforce_redraw) |
|---|
| 171 | { |
|---|
| 172 | if ((gui_tbox_redraw && !text_limit_reached) || gui_tbox_redraw == 2) { |
|---|
| 173 | if (gui_tbox_redraw==2) { |
|---|
| 174 | text_limit_reached = 0; |
|---|
| 175 | char c[MAX_LINES][MAX_WIDTH+1]; // PROMPT PARSED |
|---|
| 176 | const char *p=tbox_msg; |
|---|
| 177 | coord x=0, y=0; |
|---|
| 178 | unsigned int h=0, l=0, bw=(2*BUTTON_SIZE*FONT_WIDTH+BUTTON_SEP); |
|---|
| 179 | |
|---|
| 180 | memset(c,0,sizeof(c)); |
|---|
| 181 | |
|---|
| 182 | tbox_width =strlen(tbox_title); |
|---|
| 183 | if (tbox_width > MAX_WIDTH) tbox_width = MAX_WIDTH; |
|---|
| 184 | while (*p) { |
|---|
| 185 | if (*p == '\n') { |
|---|
| 186 | c[h++][l] = 0; |
|---|
| 187 | l=0; |
|---|
| 188 | if (h == MAX_LINES) break; |
|---|
| 189 | } else { |
|---|
| 190 | if (l < MAX_WIDTH) { |
|---|
| 191 | c[h][l++]=*p; |
|---|
| 192 | if (l > tbox_width) tbox_width = l; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | ++p; |
|---|
| 196 | } |
|---|
| 197 | tbox_width+=2; |
|---|
| 198 | if (h<MAX_LINES) |
|---|
| 199 | c[h++][l] = 0; |
|---|
| 200 | if (bw+BUTTON_SEP>tbox_width*FONT_WIDTH) |
|---|
| 201 | tbox_width=(bw+BUTTON_SEP)/FONT_WIDTH+1; |
|---|
| 202 | if (tbox_width<MIN_WIDTH) |
|---|
| 203 | tbox_width=MIN_WIDTH; // keyboard length |
|---|
| 204 | if (tbox_width<MAX_MSG_LENGH) |
|---|
| 205 | tbox_width=MAX_MSG_LENGH; // max message length |
|---|
| 206 | if (tbox_width<maxlen) { |
|---|
| 207 | if (maxlen < MAX_TEXT_WIDTH) |
|---|
| 208 | tbox_width=maxlen+2; // text length |
|---|
| 209 | else |
|---|
| 210 | tbox_width=((MAX_TEXT_WIDTH+2)>tbox_width)?MAX_TEXT_WIDTH+2:tbox_width; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | key_offset_x=(camera_screen.width - tbox_width*FONT_WIDTH)>>1; |
|---|
| 214 | |
|---|
| 215 | h += 2; |
|---|
| 216 | |
|---|
| 217 | x = (camera_screen.width - tbox_width * FONT_WIDTH) >> 1; |
|---|
| 218 | y = (camera_screen.height - (h+4) * FONT_HEIGHT-SPACING_BELOW_TEXT) >> 1; |
|---|
| 219 | draw_rect_shadow(x-3, y-3, x+tbox_width*FONT_WIDTH+5, y+(h+4)*FONT_HEIGHT+SPACING_BTN+2+SPACING_TITLE+12, COLOR_BLACK, 3); //shadow |
|---|
| 220 | draw_filled_rect_thick(x-4, y-4, x+tbox_width*FONT_WIDTH+4, y+(h+4)*FONT_HEIGHT+SPACING_BTN+2+SPACING_TITLE+11, MAKE_COLOR(COLOR_GREY, COLOR_WHITE), 3); // main box |
|---|
| 221 | draw_filled_rect(x-2, y-2, x+tbox_width*FONT_WIDTH+2, y+FONT_HEIGHT+2, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title |
|---|
| 222 | |
|---|
| 223 | l = strlen(tbox_title); |
|---|
| 224 | draw_string(x+((tbox_width-l)>>1)*FONT_WIDTH, y, tbox_title, MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); //title text |
|---|
| 225 | y+=FONT_HEIGHT+2+SPACING_TITLE; |
|---|
| 226 | |
|---|
| 227 | tbox_buttons_x = x+((tbox_width*FONT_WIDTH-bw)>>1); |
|---|
| 228 | |
|---|
| 229 | text_offset_x = x+((tbox_width-((maxlen<MAX_TEXT_WIDTH)?maxlen:MAX_TEXT_WIDTH))>>1)*FONT_WIDTH; |
|---|
| 230 | text_offset_y = y+(h-2)*FONT_HEIGHT+SPACING_BELOW_TEXT; |
|---|
| 231 | |
|---|
| 232 | tbox_buttons_y = text_offset_y+FONT_HEIGHT+SPACING_BELOW_TEXT; // on place of symbol line |
|---|
| 233 | |
|---|
| 234 | // draw prompt |
|---|
| 235 | while (h) { |
|---|
| 236 | l = strlen(c[--h]); |
|---|
| 237 | draw_string(x+(((tbox_width-l)>>1)*FONT_WIDTH), y+h*FONT_HEIGHT, c[h], MAKE_COLOR(COLOR_GREY, COLOR_WHITE)); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | if ( Mode == 'B' ) |
|---|
| 241 | gui_tbox_draw_buttons(); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | // draw edit field |
|---|
| 245 | int i; |
|---|
| 246 | for (i=offset; (i-offset)<((maxlen<MAX_TEXT_WIDTH)?maxlen:MAX_TEXT_WIDTH); i++) { |
|---|
| 247 | draw_char(text_offset_x+(i-offset)*FONT_WIDTH, text_offset_y, (i<strlen(text))?text[i]:' ', MAKE_COLOR(COLOR_BLACK, COLOR_WHITE)); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // draw long text marker |
|---|
| 251 | if ((strlen(text)-offset)>MAX_TEXT_WIDTH) draw_char(text_offset_x+MAX_TEXT_WIDTH*FONT_WIDTH, text_offset_y, '\20', MAKE_COLOR(COLOR_GREY, COLOR_RED)); |
|---|
| 252 | else draw_char(text_offset_x+MAX_TEXT_WIDTH*FONT_WIDTH, text_offset_y, ' ', MAKE_COLOR(COLOR_GREY, COLOR_GREY)); |
|---|
| 253 | if (offset>0) draw_char(text_offset_x-FONT_WIDTH, text_offset_y, '\21', MAKE_COLOR(COLOR_GREY, COLOR_RED)); |
|---|
| 254 | else draw_char(text_offset_x-FONT_WIDTH, text_offset_y, ' ', MAKE_COLOR(COLOR_GREY, COLOR_GREY)); |
|---|
| 255 | |
|---|
| 256 | // symbol line |
|---|
| 257 | if ( Mode== 'T' ) |
|---|
| 258 | { |
|---|
| 259 | //rect clears any previous message |
|---|
| 260 | draw_filled_rect(key_offset_x+((tbox_width-MAX_MSG_LENGH)>>1)*FONT_WIDTH, tbox_buttons_y, |
|---|
| 261 | key_offset_x+((tbox_width+MAX_MSG_LENGH)>>1)*FONT_WIDTH, tbox_buttons_y+3*FONT_HEIGHT, MAKE_COLOR(COLOR_GREY,COLOR_GREY)); |
|---|
| 262 | draw_string(key_offset_x+((tbox_width-17)>>1)*FONT_WIDTH, tbox_buttons_y+FONT_HEIGHT, ">navigate cursor<", MAKE_COLOR(COLOR_GREY, COLOR_WHITE)); |
|---|
| 263 | } |
|---|
| 264 | else if ( Mode == 'K' ) |
|---|
| 265 | { |
|---|
| 266 | // draw keyboard |
|---|
| 267 | char ch; |
|---|
| 268 | color cl; |
|---|
| 269 | |
|---|
| 270 | // clean previous symbols line |
|---|
| 271 | int pline = (line == 0)?lines:line-1; |
|---|
| 272 | draw_filled_rect(key_offset_x, tbox_buttons_y, key_offset_x+(tbox_width-1)*FONT_WIDTH, tbox_buttons_y+3*FONT_HEIGHT, MAKE_COLOR(COLOR_GREY, COLOR_GREY)); |
|---|
| 273 | |
|---|
| 274 | // draw current symbols line |
|---|
| 275 | int x, group; |
|---|
| 276 | |
|---|
| 277 | for (group = 0; group < 4; group++) |
|---|
| 278 | { |
|---|
| 279 | char *tstr = map_chars(line,group); |
|---|
| 280 | |
|---|
| 281 | int y = tbox_buttons_y; |
|---|
| 282 | int l = strlen(tstr); |
|---|
| 283 | |
|---|
| 284 | switch (group) { |
|---|
| 285 | case 0: |
|---|
| 286 | x=key_offset_x+4*FONT_WIDTH; |
|---|
| 287 | y=tbox_buttons_y+FONT_HEIGHT; |
|---|
| 288 | break; |
|---|
| 289 | case 1: |
|---|
| 290 | x=key_offset_x+(((tbox_width-l)*FONT_WIDTH)>>1); |
|---|
| 291 | y=tbox_buttons_y; |
|---|
| 292 | break; |
|---|
| 293 | case 2: |
|---|
| 294 | x=key_offset_x+(tbox_width-l-4)*FONT_WIDTH; |
|---|
| 295 | y=tbox_buttons_y+FONT_HEIGHT; |
|---|
| 296 | break; |
|---|
| 297 | case 3: |
|---|
| 298 | x=key_offset_x+(((tbox_width-l)*FONT_WIDTH)>>1); |
|---|
| 299 | y=tbox_buttons_y+2*FONT_HEIGHT; |
|---|
| 300 | break; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | for(i = 0; tstr[i] != '\0'; i++, x += FONT_WIDTH) { |
|---|
| 304 | ch = tstr[i]; |
|---|
| 305 | |
|---|
| 306 | if ( (i != curchar) || (group != curgroup) ) |
|---|
| 307 | cl = MAKE_COLOR(COLOR_GREY, COLOR_WHITE); |
|---|
| 308 | else if ( ch != ' ' ) |
|---|
| 309 | cl = MAKE_COLOR(COLOR_GREY, COLOR_RED); |
|---|
| 310 | else |
|---|
| 311 | cl = MAKE_COLOR(COLOR_RED,COLOR_GREY); // "space" is special color case (inverted) |
|---|
| 312 | |
|---|
| 313 | draw_char(x, y, ch, cl); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | gui_tbox_redraw = 0; |
|---|
| 318 | } |
|---|
| 319 | if (text_limit_reached) { |
|---|
| 320 | // clean max_keyboard_length chars long field |
|---|
| 321 | if (text_limit_reached%4 == 0) |
|---|
| 322 | draw_filled_rect(key_offset_x+3, tbox_buttons_y, |
|---|
| 323 | key_offset_x+tbox_width*FONT_WIDTH-3, tbox_buttons_y+3*FONT_HEIGHT, MAKE_COLOR(COLOR_GREY, COLOR_GREY)); |
|---|
| 324 | draw_string(key_offset_x+((tbox_width-18)>>1)*FONT_WIDTH, tbox_buttons_y+FONT_HEIGHT, "text limit reached", MAKE_COLOR(COLOR_GREY, COLOR_RED)); |
|---|
| 325 | text_limit_reached--; |
|---|
| 326 | } |
|---|
| 327 | if (cursor_to_draw) { |
|---|
| 328 | draw_line(text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+1, text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+FONT_HEIGHT-3, MAKE_COLOR(COLOR_GREY, COLOR_YELLOW)); |
|---|
| 329 | cursor_to_draw = 0; |
|---|
| 330 | } |
|---|
| 331 | else { |
|---|
| 332 | draw_line(text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+1, text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+FONT_HEIGHT-3, MAKE_COLOR(COLOR_GREY, COLOR_GREY)); |
|---|
| 333 | cursor_to_draw = 1; |
|---|
| 334 | } |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | static void tbox_move_cursor(int direction) |
|---|
| 338 | { |
|---|
| 339 | draw_line(text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+1, text_offset_x+(1+cursor-offset)*FONT_WIDTH, text_offset_y+FONT_HEIGHT-3, COLOR_BLACK); |
|---|
| 340 | if (direction<0) { |
|---|
| 341 | if (cursor >= 0) { |
|---|
| 342 | cursor--; |
|---|
| 343 | if (maxlen>MAX_TEXT_WIDTH && offset != 0 && cursor<offset) |
|---|
| 344 | offset--; |
|---|
| 345 | } |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | if (direction>0) { |
|---|
| 349 | if (cursor < (maxlen-1)) { |
|---|
| 350 | cursor++; |
|---|
| 351 | if (maxlen>MAX_TEXT_WIDTH && (cursor-offset)>=MAX_TEXT_WIDTH) |
|---|
| 352 | offset++; |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | gui_tbox_redraw = 1; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | static void tbox_move_text(int direction) { |
|---|
| 359 | int i; |
|---|
| 360 | if (direction<0) { |
|---|
| 361 | //This loop moves all characters on the right of the cursor one place left |
|---|
| 362 | for (i=cursor; i<strlen(text); i++) { |
|---|
| 363 | text[i]=text[i+1]; |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | if (direction>0) { |
|---|
| 368 | //This loop moves all characters on the right of the cursor one place right |
|---|
| 369 | for (i=(strlen(text) < maxlen-1)?strlen(text):maxlen-1; i>cursor; i--) { |
|---|
| 370 | text[i]=text[i-1]; |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | gui_tbox_redraw = 1; |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | static void tbox_keyboard_key(char curKey, int subgroup) |
|---|
| 377 | { |
|---|
| 378 | if (curKey == 'b') { |
|---|
| 379 | if (cursor >= 0) { |
|---|
| 380 | tbox_move_text(-1); |
|---|
| 381 | if (strlen(text)>=MAX_TEXT_WIDTH && (cursor-offset)>=MAX_TEXT_WIDTH-1) offset--; |
|---|
| 382 | tbox_move_cursor(-1); |
|---|
| 383 | RESET_CHAR |
|---|
| 384 | } |
|---|
| 385 | } |
|---|
| 386 | else if (lastKey == curKey) { |
|---|
| 387 | curchar++; |
|---|
| 388 | curgroup = subgroup; |
|---|
| 389 | char *tstr = map_chars(line,subgroup); |
|---|
| 390 | if (tstr[curchar] == 0) curchar = 0; |
|---|
| 391 | text[cursor] = tstr[curchar]; |
|---|
| 392 | } |
|---|
| 393 | else if (curKey == 's') { |
|---|
| 394 | if (strlen(text)<maxlen) { |
|---|
| 395 | if (text[cursor+1] != '\0') tbox_move_text(1); //check wheter cursor is at the end of the string |
|---|
| 396 | tbox_move_cursor(1); |
|---|
| 397 | text[cursor] = ' '; |
|---|
| 398 | RESET_CHAR |
|---|
| 399 | } |
|---|
| 400 | else { |
|---|
| 401 | text_limit_reached = 8; |
|---|
| 402 | RESET_CHAR |
|---|
| 403 | } |
|---|
| 404 | } |
|---|
| 405 | else if (curKey == 'O') { |
|---|
| 406 | RESET_CHAR |
|---|
| 407 | } |
|---|
| 408 | else { |
|---|
| 409 | if (strlen(text)<maxlen) { |
|---|
| 410 | curchar = 0; curgroup = subgroup; |
|---|
| 411 | if (cursor < (maxlen-1)) { |
|---|
| 412 | if (text[cursor+1] != '\0') tbox_move_text(1); //check wheter cursor is at the end of the string |
|---|
| 413 | tbox_move_cursor(1); |
|---|
| 414 | } |
|---|
| 415 | lastKey = curKey; |
|---|
| 416 | char *tstr = map_chars(line,subgroup); |
|---|
| 417 | text[cursor] = tstr[curchar]; |
|---|
| 418 | } |
|---|
| 419 | else { |
|---|
| 420 | text_limit_reached = 8; |
|---|
| 421 | RESET_CHAR |
|---|
| 422 | } |
|---|
| 423 | } |
|---|
| 424 | gui_tbox_redraw = 1; |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | void gui_tbox_kbd_process() |
|---|
| 428 | { |
|---|
| 429 | if (Mode == 'K') { |
|---|
| 430 | switch (kbd_get_autoclicked_key() | get_jogdial_direction()) { |
|---|
| 431 | case KEY_SHOOT_HALF: |
|---|
| 432 | line = (line+1)%lines; |
|---|
| 433 | RESET_CHAR |
|---|
| 434 | gui_tbox_redraw = 1; |
|---|
| 435 | break; |
|---|
| 436 | case KEY_UP: |
|---|
| 437 | tbox_keyboard_key('U',1); |
|---|
| 438 | break; |
|---|
| 439 | case KEY_DOWN: |
|---|
| 440 | tbox_keyboard_key('D',3); |
|---|
| 441 | break; |
|---|
| 442 | case KEY_LEFT: |
|---|
| 443 | tbox_keyboard_key('L',0); |
|---|
| 444 | break; |
|---|
| 445 | case KEY_RIGHT: |
|---|
| 446 | tbox_keyboard_key('R',2); |
|---|
| 447 | break; |
|---|
| 448 | case KEY_SET: |
|---|
| 449 | tbox_keyboard_key('O',0); |
|---|
| 450 | break; |
|---|
| 451 | case KEY_ZOOM_IN: |
|---|
| 452 | case KEY_DISPLAY: |
|---|
| 453 | tbox_keyboard_key('s',0); //space |
|---|
| 454 | break; |
|---|
| 455 | case KEY_ZOOM_OUT: |
|---|
| 456 | tbox_keyboard_key('b',0); //backspace |
|---|
| 457 | break; |
|---|
| 458 | case JOGDIAL_LEFT: |
|---|
| 459 | tbox_move_cursor(-1); |
|---|
| 460 | RESET_CHAR |
|---|
| 461 | break; |
|---|
| 462 | case JOGDIAL_RIGHT: |
|---|
| 463 | if (text[cursor+1] != '\0') tbox_move_cursor(1); |
|---|
| 464 | RESET_CHAR |
|---|
| 465 | break; |
|---|
| 466 | case KEY_MENU: |
|---|
| 467 | Mode = 'T'; |
|---|
| 468 | gui_tbox_redraw=2; |
|---|
| 469 | break; |
|---|
| 470 | } |
|---|
| 471 | } |
|---|
| 472 | else if (Mode == 'T') { |
|---|
| 473 | switch (kbd_get_autoclicked_key() | get_jogdial_direction()) { |
|---|
| 474 | case JOGDIAL_LEFT: |
|---|
| 475 | case KEY_LEFT: |
|---|
| 476 | tbox_move_cursor(-1); |
|---|
| 477 | RESET_CHAR |
|---|
| 478 | break; |
|---|
| 479 | case JOGDIAL_RIGHT: |
|---|
| 480 | case KEY_RIGHT: |
|---|
| 481 | if (text[cursor+1] != '\0') tbox_move_cursor(1); |
|---|
| 482 | RESET_CHAR |
|---|
| 483 | break; |
|---|
| 484 | case KEY_SHOOT_HALF: |
|---|
| 485 | tbox_keyboard_key('b',0); //backspace - for non-zoom cameras? |
|---|
| 486 | break; |
|---|
| 487 | case KEY_DISPLAY: |
|---|
| 488 | tbox_keyboard_key('s',0); //space -,,- |
|---|
| 489 | break; |
|---|
| 490 | case KEY_ZOOM_IN: |
|---|
| 491 | if (offset<(strlen(text)-MAX_TEXT_WIDTH)) { |
|---|
| 492 | offset++; |
|---|
| 493 | if ((cursor+1)<offset) cursor++; |
|---|
| 494 | gui_tbox_redraw = 1; |
|---|
| 495 | } |
|---|
| 496 | break; |
|---|
| 497 | case KEY_ZOOM_OUT: |
|---|
| 498 | if (offset > 0) { |
|---|
| 499 | offset--; |
|---|
| 500 | if ((cursor-offset)>=MAX_TEXT_WIDTH) cursor--; |
|---|
| 501 | gui_tbox_redraw = 1; |
|---|
| 502 | } |
|---|
| 503 | break; |
|---|
| 504 | case KEY_MENU: |
|---|
| 505 | Mode = 'B'; |
|---|
| 506 | gui_tbox_redraw=2; |
|---|
| 507 | break; |
|---|
| 508 | } |
|---|
| 509 | } |
|---|
| 510 | else { // Mode == 'B' |
|---|
| 511 | switch (kbd_get_autoclicked_key() | get_jogdial_direction()) { |
|---|
| 512 | case JOGDIAL_LEFT: |
|---|
| 513 | case KEY_LEFT: |
|---|
| 514 | if (tbox_button_active > 0) tbox_button_active = 0; |
|---|
| 515 | else tbox_button_active = 1; |
|---|
| 516 | gui_tbox_draw_buttons(); |
|---|
| 517 | break; |
|---|
| 518 | case JOGDIAL_RIGHT: |
|---|
| 519 | case KEY_RIGHT: |
|---|
| 520 | if (tbox_button_active <= 0) tbox_button_active = 1; |
|---|
| 521 | else tbox_button_active = 0; |
|---|
| 522 | gui_tbox_draw_buttons(); |
|---|
| 523 | break; |
|---|
| 524 | case KEY_SET: |
|---|
| 525 | kbd_reset_autoclicked_key(); |
|---|
| 526 | gui_set_mode(gui_tbox_mode_old); |
|---|
| 527 | draw_restore(); |
|---|
| 528 | if (tbox_on_select) { |
|---|
| 529 | if (tbox_button_active == 0) |
|---|
| 530 | tbox_on_select(text); // ok |
|---|
| 531 | else { |
|---|
| 532 | tbox_on_select(0); // cancel |
|---|
| 533 | } |
|---|
| 534 | tbox_on_select = 0; // Prevent unloader from calling this function again |
|---|
| 535 | } |
|---|
| 536 | module_async_unload(module_idx); |
|---|
| 537 | break; |
|---|
| 538 | case KEY_MENU: |
|---|
| 539 | Mode = 'K'; |
|---|
| 540 | gui_tbox_redraw=2; |
|---|
| 541 | break; |
|---|
| 542 | } |
|---|
| 543 | } |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | |
|---|
| 547 | //================================================== |
|---|
| 548 | |
|---|
| 549 | struct libtextbox_sym libtextbox = { |
|---|
| 550 | MAKE_API_VERSION(1,0), // apiver: increase major if incompatible changes made in module, |
|---|
| 551 | // increase minor if compatible changes made(including extending this struct) |
|---|
| 552 | textbox_init, |
|---|
| 553 | }; |
|---|
| 554 | |
|---|
| 555 | |
|---|
| 556 | void* MODULE_EXPORT_LIST[] = { |
|---|
| 557 | /* 0 */ (void*)EXPORTLIST_MAGIC_NUMBER, |
|---|
| 558 | /* 1 */ (void*)1, |
|---|
| 559 | |
|---|
| 560 | &libtextbox |
|---|
| 561 | }; |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | //--------------------------------------------------------- |
|---|
| 565 | // PURPOSE: Bind module symbols with chdk. |
|---|
| 566 | // Required function |
|---|
| 567 | // PARAMETERS: pointer to chdk list of export |
|---|
| 568 | // RETURN VALUE: 1 error, 0 ok |
|---|
| 569 | //--------------------------------------------------------- |
|---|
| 570 | int _module_loader( void** chdk_export_list ) |
|---|
| 571 | { |
|---|
| 572 | if ( (unsigned int)chdk_export_list[0] != EXPORTLIST_MAGIC_NUMBER ) |
|---|
| 573 | return 1; |
|---|
| 574 | |
|---|
| 575 | if ( !API_VERSION_MATCH_REQUIREMENT( gui_version.common_api, 1, 0 ) ) |
|---|
| 576 | return 1; |
|---|
| 577 | |
|---|
| 578 | return 0; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | //--------------------------------------------------------- |
|---|
| 583 | // PURPOSE: Finalize module operations (close allocs, etc) |
|---|
| 584 | // RETURN VALUE: 0-ok, 1-fail |
|---|
| 585 | //--------------------------------------------------------- |
|---|
| 586 | int _module_unloader() |
|---|
| 587 | { |
|---|
| 588 | // clean allocated resource |
|---|
| 589 | if (tbox_on_select) |
|---|
| 590 | { |
|---|
| 591 | tbox_on_select(0); // notify callback about exit as cancel |
|---|
| 592 | tbox_on_select = 0; // prevent calling twice in the (unlikely) event of the unload called twice |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | //sanity clean to prevent accidentaly assign/restore guimode to unloaded module |
|---|
| 596 | GUI_MODE_TBOX.magicnum = 0; |
|---|
| 597 | |
|---|
| 598 | return 0; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | |
|---|
| 602 | //--------------------------------------------------------- |
|---|
| 603 | // PURPOSE: Default action for simple modules (direct run) |
|---|
| 604 | // NOTE: Please comment this function if no default action and this library module |
|---|
| 605 | //--------------------------------------------------------- |
|---|
| 606 | int _module_run(int moduleidx, int argn, int* arguments) |
|---|
| 607 | { |
|---|
| 608 | module_idx=moduleidx; |
|---|
| 609 | |
|---|
| 610 | return 0; |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | |
|---|
| 614 | /******************** Module Information structure ******************/ |
|---|
| 615 | |
|---|
| 616 | struct ModuleInfo _module_info = { MODULEINFO_V1_MAGICNUM, |
|---|
| 617 | sizeof(struct ModuleInfo), |
|---|
| 618 | |
|---|
| 619 | ANY_CHDK_BRANCH, 0, // Requirements of CHDK version |
|---|
| 620 | ANY_PLATFORM_ALLOWED, // Specify platform dependency |
|---|
| 621 | MODULEINFO_FLAG_SYSTEM, // flag |
|---|
| 622 | (int32_t)"Virtual keyboard", // Module name |
|---|
| 623 | 1, 0, // Module version |
|---|
| 624 | 0 |
|---|
| 625 | }; |
|---|