#include #include #include #include #include #include "vkbd.h" static Evas_Smart *get_smart(); static void _kbd_object_add(Evas_Object *o); static void _kbd_object_del(Evas_Object *o); static void _kbd_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y); static void _kbd_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h); static void _kbd_object_show(Evas_Object *o); static void _kbd_object_hide(Evas_Object *o); static void _kbd_object_color_set(Evas_Object *o, int r, int g, int b, int a); static void _kbd_object_clip_set(Evas_Object *o, Evas_Object *clip); static void _kbd_object_clip_unset(Evas_Object *o); #define SMART_KBD_NAME "e_smart_kbd" #define INPUT_MAX 512 struct smart_data { Evas_Object *obj; Evas_Coord x, y, w, h; Evas_Object *clip; Evas_Object *edje; char input[INPUT_MAX]; int input_len; void (*kbd_cb)(char *); }; static void mouse_down(void *data, Evas_Object *o, const char *emission, const char *source) { int i; const char *key; struct smart_data *smart = (struct smart_data *)data; if(!smart || smart->input_len >= INPUT_MAX - 1) return; int len = strlen(source); for(i = len - 2; i >= 0; i--) { if(source[i] == '-') break; } key = source + i+1; if(strlen(key) == 1) { smart->input[smart->input_len++] = key[0]; smart->input[smart->input_len] = '\0'; } else if (strlen(key) == 2 && (key[0] & 0xff) == 0xc3) { smart->input[smart->input_len++] = key[0]; smart->input[smart->input_len++] = key[1]; smart->input[smart->input_len] = '\0'; } else { int len; char *val = edje_edit_data_value_get (o, key); if(!val) return; len = INPUT_MAX - smart->input_len - 2; len = (strlen(val) > len) ? len : strlen(val); strncat(smart->input + smart->input_len, val, len); smart->input_len += len; } edje_object_part_text_set(o, "field", smart->input); } static void kbd_cb(void *data, Evas_Object *o, const char *emission, const char *source) { struct smart_data *smart; Evas_Object *kbd = (Evas_Object *)data; if(!kbd) return; smart = evas_object_smart_data_get(kbd); if(strcmp(source, "hide") == 0) { if(smart->kbd_cb) smart->kbd_cb(strdup(smart->input)); smart->input[0] = 0; smart->input_len = 0; smart->kbd_cb = NULL; } printf("kbd_cb source: %s, emission %s\n", source, emission); evas_object_hide(kbd); } Evas_Object *vkbd_add(Evas *e) { Evas_Object *o; struct smart_data *smart; Evas_Object *kbd; o = evas_object_smart_add(e, get_smart()); smart = evas_object_smart_data_get(o); if(smart == NULL) { evas_object_del(o); return NULL; } kbd = edje_object_add(e); if(!edje_object_file_set(kbd, "vkbd/vkbd.edj", "main")) { printf("failed loading kbd\n"); evas_object_del(kbd); evas_object_del(o); return NULL; } evas_object_clip_set(kbd, smart->clip); edje_object_signal_callback_add(kbd, "mouse,down,1", "*key-bg*", mouse_down, smart); edje_object_signal_callback_add(kbd, "kbd", "*", kbd_cb, o); evas_object_show(kbd); smart->input[0] = '\0'; smart->input_len = 0; smart->kbd_cb = NULL; smart->edje = kbd; return o; } void vkbd_show(Evas_Object *o, Evas_Coord w, Evas_Coord h, void (*cb)(char *)) { struct smart_data *smart; smart = evas_object_smart_data_get(o); smart->kbd_cb = cb; evas_object_resize(o, w, h); evas_object_show(o); } static void _kbd_object_add(Evas_Object *o) { struct smart_data *smart; smart = calloc(1, sizeof(struct smart_data)); if (smart == NULL) return; smart->obj = o; smart->x = 0; smart->y = 0; smart->w = 0; smart->h = 0; smart->clip = evas_object_rectangle_add(evas_object_evas_get(o)); evas_object_smart_member_add(smart->clip, o); evas_object_color_set(smart->clip, 255, 255, 255, 255); evas_object_move(smart->clip, smart->x, smart->y); evas_object_resize(smart->clip, smart->w, smart->h); evas_object_smart_data_set(o, smart); } static void _kbd_object_del(Evas_Object *o) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_del(smart->clip); evas_object_del(smart->edje); free(smart); } static void _kbd_object_move(Evas_Object *o, Evas_Coord x, Evas_Coord y) { struct smart_data *smart; smart = evas_object_smart_data_get(o); smart->x = x; smart->y = y; evas_object_move(smart->clip, x, y); evas_object_move(smart->edje, x, y); } static void _kbd_object_resize(Evas_Object *o, Evas_Coord w, Evas_Coord h) { struct smart_data *smart; smart = evas_object_smart_data_get(o); smart->w = w; smart->h = h; evas_object_resize(smart->clip, w, h); evas_object_resize(smart->edje, w, h); } static void _kbd_object_show(Evas_Object *o) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_show(smart->clip); } static void _kbd_object_hide(Evas_Object *o) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_hide(smart->clip); } static void _kbd_object_color_set(Evas_Object *o, int r, int g, int b, int a) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_color_set(smart->clip, r, g, b, a); } static void _kbd_object_clip_set(Evas_Object *o, Evas_Object *clip) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_clip_set(smart->clip, clip); } static void _kbd_object_clip_unset(Evas_Object *o) { struct smart_data *smart; smart = evas_object_smart_data_get(o); evas_object_clip_unset(smart->clip); } static Evas_Smart *get_smart() { static Evas_Smart *e_smart_kbd = NULL; if(e_smart_kbd != NULL) return e_smart_kbd; e_smart_kbd = evas_smart_new(SMART_KBD_NAME, _kbd_object_add, _kbd_object_del, NULL, NULL, NULL, NULL, NULL, _kbd_object_move, _kbd_object_resize, _kbd_object_show, _kbd_object_hide, _kbd_object_color_set, _kbd_object_clip_set, _kbd_object_clip_unset, NULL); return e_smart_kbd; }