Support API for set/get cursor position
[platform/core/uifw/inputdelegator.git] / src / w-input-emoticon.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <app.h>
18 #include <app_preference.h>
19 #include <Elementary.h>
20 #include <string>
21 #include <vector>
22 #include <stdint.h>
23
24 #include <vconf.h>
25 #include <vconf-keys.h>
26
27 #include "Debug.h"
28 #include "w-input-selector.h"
29
30 #define RECENT_EMOJI_LIST "recent_emoji_list"
31
32 #define EMOTICON_CNT 180
33 #define RECENT_CNT 9
34
35 extern App_Data* app_data;
36
37 static int is_content_reuse_on = 0;
38
39
40 using namespace std;
41
42 vector <int> recent_emoji_list;
43
44 typedef struct {
45     int code;
46     const char* name;
47 }Emoticon;
48
49 static Elm_Object_Item *it_emoticon_empty = NULL;
50 static Elm_Object_Item *it_emoticon_recent_group = NULL;
51 static Elm_Object_Item *it_emoticon_emoji_group = NULL;
52 static Elm_Object_Item* it_drawing = NULL;
53 static Elm_Object_Item *it_last = NULL;
54
55 static Elm_Genlist_Item_Class *itc_emoticon = NULL;
56
57 #define INITAL_ITEM_UNIT 24
58 #define LOADING_ITEM_UNIT 27
59 static int loading_done_for_item = 0;
60 Ecore_Timer *lazy_loading_timer_for_items = NULL;
61
62 #define INITAL_CONTENT_UNIT 51
63 #define LOADING_CONTENT_UNIT 9
64 static int loading_done_for_contents = 0;
65 Ecore_Timer *lazy_loading_timer_for_contents = NULL;
66
67
68 typedef struct emoticon_content
69 {
70     int index;
71     Evas_Object *content;
72     int used;
73 } emoticon_content_s;
74
75 static emoticon_content_s emoticon_contents_pool[EMOTICON_CNT] = { 0, };
76 static emoticon_content_s emoticon_drawing_pool = { 0, };
77 static emoticon_content_s emoticon_recents_pool[RECENT_CNT] = { 0, };
78
79
80 Emoticon emoticon_info[EMOTICON_CNT] = {
81     {0x1f600, ""},
82     {0x1f601, ""},
83     {0x1f602, ""},
84     {0x1f603, ""},
85     {0x1f604, ""},
86     {0x1f605, ""},
87     {0x1f606, ""},
88     {0x1f609, ""},
89     {0x1f60a, ""},
90     {0x1f60b, ""},
91     {0x1f60e, ""},
92     {0x1f60d, ""},
93     {0x1f618, ""},
94     {0x1f617, ""},
95     {0x1f619, ""},
96     {0x1f61a, ""},
97     {0x263a, ""},
98     {0x1f642, ""},
99     {0x1f917, ""},
100     {0x1f607, ""},
101     {0x1f914, ""},
102     {0x1f610, ""},
103     {0x1f611, ""},
104     {0x1f636, ""},
105     {0x1f644, ""},
106     {0x1f60f, ""},
107     {0x1f623, ""},
108     {0x1f625, ""},
109     {0x1f62e, ""},
110     {0x1f910, ""},
111     {0x1f62f, ""},
112     {0x1f634, ""},
113     {0x1f62a, ""},
114     {0x1f62b, ""},
115     {0x1f60c, ""},
116     {0x1f913, ""},
117     {0x1f61b, ""},
118     {0x1f61c, ""},
119     {0x1f61d, ""},
120     {0x1f641, ""},
121     {0x1f612, ""},
122     {0x1f613, ""},
123     {0x1f614, ""},
124     {0x1f615, ""},
125     {0x1f616, ""},
126     {0x1f643, ""},
127     {0x1f637, ""},
128     {0x1f912, ""},
129     {0x1f915, ""},
130     {0x1f911, ""},
131     {0x1f632, ""},
132     {0x1f61e, ""},
133     {0x1f61f, ""},
134     {0x1f624, ""},
135     {0x1f622, ""},
136     {0x1f62d, ""},
137     {0x1f626, ""},
138     {0x1f627, ""},
139     {0x1f628, ""},
140     {0x1f629, ""},
141     {0x1f62c, ""},
142     {0x1f630, ""},
143     {0x1f631, ""},
144     {0x1f633, ""},
145     {0x1f635, ""},
146     {0x1f621, ""},
147     {0x1f620, ""},
148     {0x1f608, ""},
149     {0x1f648, ""},
150     {0x1f649, ""},
151     {0x1f64a, ""},
152     {0x1f448, ""},
153     {0x1f449, ""},
154     {0x261d, ""},
155     {0x1f446, ""},
156     {0x1f595, ""},
157     {0x1f447, ""},
158     {0x270c, ""},
159     {0x1f596, ""},
160     {0x1f918, ""},
161     {0x1f591, ""},
162     {0x1f590, ""},
163     {0x270a, ""},
164     {0x270b, ""},
165     {0x1f44a, ""},
166     {0x1f44c, ""},
167     {0x1f44d, ""},
168     {0x1f44e, ""},
169     {0x1f592, ""},
170     {0x1f593, ""},
171     {0x1f44b, ""},
172     {0x1f44f, ""},
173     {0x1f450, ""},
174     {0x1f493, ""},
175     {0x1f494, ""},
176     {0x1f495, ""},
177     {0x1f496, ""},
178     {0x1f497, ""},
179     {0x1f49d, ""},
180     {0x1f49e, ""},
181     {0x1f49f, ""},
182     {0x2763, ""},
183     {0x1f35e, ""},
184     {0x1f9c0, ""},
185     {0x1f356, ""},
186     {0x1f357, ""},
187     {0x1f354, ""},
188     {0x1f35f, ""},
189     {0x1f355, ""},
190     {0x1f32d, ""},
191     {0x1f32e, ""},
192     {0x1f32f, ""},
193     {0x1f37f, ""},
194     {0x1f372, ""},
195     {0x1f371, ""},
196     {0x1f358, ""},
197     {0x1f359, ""},
198     {0x1f35a, ""},
199     {0x1f35c, ""},
200     {0x1f35b, ""},
201     {0x1f35d, ""},
202     {0x1f360, ""},
203     {0x1f362, ""},
204     {0x1f363, ""},
205     {0x1f364, ""},
206     {0x1f365, ""},
207     {0x1f361, ""},
208     {0x1f366, ""},
209     {0x1f368, ""},
210     {0x1f367, ""},
211     {0x1f369, ""},
212     {0x1f36a, ""},
213     {0x1f382, ""},
214     {0x1f370, ""},
215     {0x1f36b, ""},
216     {0x1f36c, ""},
217     {0x1f36d, ""},
218     {0x1f36e, ""},
219     {0x1f36f, ""},
220     {0x1f37c, ""},
221     {0x2615, ""},
222     {0x1f375, ""},
223     {0x1f376, ""},
224     {0x1f37e, ""},
225     {0x1f377, ""},
226     {0x1f378, ""},
227     {0x1f379, ""},
228     {0x1f37a, ""},
229     {0x1f37b, ""},
230     {0x1f383, ""},
231     {0x1f384, ""},
232     {0x1f388, ""},
233     {0x1f389, ""},
234     {0x1f38a, ""},
235     {0x26bd, ""},
236     {0x26be, ""},
237     {0x1f3c0, ""},
238     {0x1f3c8, ""},
239     {0x1f3c9, ""},
240     {0x1f3be, ""},
241     {0x1f3b1, ""},
242     {0x1f3b3, ""},
243     {0x26f3, ""},
244     {0x26f8, ""},
245     {0x1f3a3, ""},
246     {0x1f3bf, ""},
247     {0x1f3cf, ""},
248     {0x1f3d0, ""},
249     {0x1f3d1, ""},
250     {0x1f3d2, ""},
251     {0x1f3d3, ""},
252     {0x1f3f8, ""},
253     {0x1f3af, ""},
254     {0x1f3b2, ""},
255     {0x1f3df, ""},
256     {0x1f3db, ""},
257     {0x1f3e0, ""},
258     {0x1f3e2, ""},
259     {0x1f3e5, ""},
260     {0x1f3eb, ""},
261 };
262
263
264 //---------------------------------------------------------------------------------------//
265
266 static Eina_Bool _custom_back_cb2(void *data, Elm_Object_Item *it)
267 {
268     PRINTFUNC(DLOG_DEBUG, "");
269
270     if (is_content_reuse_on) {
271         int i;
272
273         if (lazy_loading_timer_for_items != NULL) {
274             ecore_timer_del(lazy_loading_timer_for_items);
275             lazy_loading_timer_for_items = NULL;
276         }
277
278         if (lazy_loading_timer_for_contents != NULL) {
279             ecore_timer_del(lazy_loading_timer_for_contents);
280             lazy_loading_timer_for_contents = NULL;
281         }
282
283         if(emoticon_drawing_pool.used == 0){
284             evas_object_del(emoticon_drawing_pool.content);
285         }
286         emoticon_drawing_pool.content = NULL;
287
288         //Recent EMOTICONS : the recent emoiton need to be updated whenever emoticon view is generated, so deleted here.
289         for (i=0;i< RECENT_CNT;i++)
290         {
291             if (emoticon_recents_pool[i].used == 0 && emoticon_recents_pool[i].content) {
292                 evas_object_del(emoticon_recents_pool[i].content);
293             }
294             emoticon_recents_pool[i].content = NULL;
295         }
296     }
297
298     _back_to_genlist_for_selector();
299     return EINA_TRUE;
300 }
301
302
303 void get_recent_emoticons(vector <int> &emoticon_list)
304 {
305     int ret = PREFERENCE_ERROR_NONE;
306     char *str = NULL;
307
308     ret = preference_get_string(RECENT_EMOJI_LIST, &str);
309     if (PREFERENCE_ERROR_NONE != ret) {
310         PRINTFUNC(DLOG_ERROR, "preference_get_string error!(%d)", ret);
311     }
312
313     emoticon_list.clear();
314
315     PRINTFUNC(DLOG_DEBUG, "str = %s", str);
316
317     if (str != NULL) {
318         char *tok, *ptr;
319         tok = strtok_r(str, ",", &ptr);
320         while (tok != NULL) {
321             PRINTFUNC(DLOG_DEBUG, "tok = %s", tok);
322             emoticon_list.push_back(strtol(tok, (char **)NULL, 10));
323             tok = strtok_r(NULL, ",", &ptr);
324         }
325     }
326
327     if (str)
328         free(str);
329
330     return;
331 }
332
333 void set_recent_emoticons(vector <int> &emoticon_list, int val)
334 {
335     unsigned int i;
336     int ret = PREFERENCE_ERROR_NONE;
337
338     if (emoticon_list.size() > 0) {
339         for (i = 0; i < emoticon_list.size(); i++) {
340             PRINTFUNC(DLOG_DEBUG, "%d == %d", emoticon_list.at(i), val);
341
342             if (emoticon_list.at(i) == val) {
343                 emoticon_list.erase(emoticon_list.begin()+i);
344                 break;
345             }
346         }
347
348         if (emoticon_list.size() >= RECENT_CNT) {
349             emoticon_list.erase(emoticon_list.end());
350         }
351     }
352
353     emoticon_list.insert(emoticon_list.begin(), val);
354
355     string stored;
356     char str[10] = {0, };
357
358     for (i = 0; i < emoticon_list.size(); i++) {
359         snprintf(str, sizeof(str), "%d", emoticon_list.at(i));
360         stored += str;
361         if (i+1 != emoticon_list.size())
362             stored += ",";
363     }
364
365     PRINTFUNC(DLOG_DEBUG, "%s", stored.c_str());
366
367     ret = preference_set_string(RECENT_EMOJI_LIST, stored.c_str());
368     if (PREFERENCE_ERROR_NONE != ret) {
369         PRINTFUNC(DLOG_ERROR, "preference_set_string error!(%d)", ret);
370     }
371
372 }
373
374 static void _drawing_app_control_reply_cb(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
375 {
376     char* path = NULL;
377     app_control_get_extra_data(reply, APP_CONTROL_DATA_PATH, &path);
378
379     if(path){
380         PRINTFUNC(DLOG_DEBUG, "path=%s", path);
381         char *pathes[] = {path, };
382         reply_to_sender_by_callback(NULL, "image", (const char **)pathes, NULL);
383         free(path);
384     }
385     ui_app_exit();
386 }
387
388 static void _drawing_item_clicked_cb(void *data, Evas_Object * obj, void *event_info)
389 {
390     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
391     App_Data* ad = (App_Data*) data;
392     if (!ad)
393         return;
394
395     Elm_Object_Item *item = (Elm_Object_Item *) event_info;
396     if (item)
397         elm_genlist_item_selected_set(item, EINA_FALSE);
398
399     /* launch */
400     app_control_h request = NULL;
401     app_control_create(&request);
402     app_control_set_app_id(request, "org.tizen.sketch");
403
404     int ret = app_control_send_launch_request(request, _drawing_app_control_reply_cb, NULL);
405     if (ret != APP_CONTROL_ERROR_NONE) {
406         PRINTFUNC(DLOG_DEBUG, "error code = 0x%x", ret);
407     }
408     app_control_destroy(request);
409 }
410
411 static void _emoticon_item_clicked_cb(void *data, Evas_Object * obj, void *event_info)
412 {
413     int index = (uintptr_t)data;
414
415     PRINTFUNC(DLOG_DEBUG, "index = %d", index);
416
417     // store in recents list
418     set_recent_emoticons(recent_emoji_list, index);
419
420     int length;
421     const Eina_Unicode unicode_event[2] = { (Eina_Unicode)emoticon_info[index].code, 0 };
422     char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
423
424     reply_to_sender_by_callback((const char*)utf_8, "emoticon", NULL, NULL);
425
426     PRINTFUNC(SECURE_DEBUG, "[%d]%s", index, utf_8);
427     if (utf_8)
428         free(utf_8);
429
430     ui_app_exit();
431 }
432
433 Evas_Object* get_emoticon_button(Evas_Object* parent, int index){
434     if (parent == NULL)
435         return NULL;
436
437      Evas_Object* btn = elm_button_add(parent);
438      elm_object_style_set(btn, "emoticon");
439      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
440      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
441
442      int length;
443      const Eina_Unicode unicode_event[2] = { (Eina_Unicode)emoticon_info[index].code, 0 };
444      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
445      elm_object_part_text_set(btn, "elm.text", utf_8);
446
447      if (utf_8)
448          free(utf_8);
449
450      evas_object_layer_set(btn, 32000);
451
452      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void *)(uintptr_t)index);
453
454      return btn;
455 }
456
457 Evas_Object* get_recent_emoticon_button(Evas_Object* parent, int index){
458     if (parent == NULL)
459         return NULL;
460
461      Evas_Object* btn = elm_button_add(parent);
462      elm_object_style_set(btn, "emoticon");
463      evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
464      evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
465
466      int length;
467      const Eina_Unicode unicode_event[2] = { (Eina_Unicode)emoticon_info[recent_emoji_list.at(index)].code, 0 };
468      char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
469      elm_object_part_text_set(btn, "elm.text", utf_8);
470
471      evas_object_data_set(btn, "index", (void*)(uintptr_t) recent_emoji_list.at(index));
472
473      if (utf_8)
474          free(utf_8);
475
476      evas_object_layer_set(btn, 32000);
477      evas_object_smart_callback_add(btn, "clicked", _emoticon_item_clicked_cb, (void*)(uintptr_t) recent_emoji_list.at(index));
478
479      return btn;
480 }
481
482 static void _emoticon_gl_lang_changed(void *data, Evas_Object *obj, void *event_info)
483 {
484     elm_genlist_realized_items_update(obj);
485 }
486
487 static char * __emoticon_gl_text_get(void *data, Evas_Object *obj, const char *part)
488 {
489     //PRINTFUNC(DLOG_DEBUG,"part = %s", part);
490     const char* str = (const char*) data;
491     if (!str)
492         return NULL;
493
494     if (!strcmp(part, "elm.text")) {
495         //PRINTFUNC(DLOG_DEBUG,"str = %s", str);
496         return strdup(gettext(str));
497     }
498     return NULL;
499 }
500
501 static void _emoticon_gl_content_unswallowed_cb(void *data, Evas_Object *obj, void *event_info)
502 {
503     Elm_Object_Item *it = (Elm_Object_Item *)event_info;
504
505     const Elm_Genlist_Item_Class *itc = elm_genlist_item_item_class_get(it);
506
507     //PRINTFUNC(DLOG_DEBUG,"%s - stype[%s]", __func__, itc->item_style);
508     if (!strcmp(itc->item_style, "3button_flat")) {
509         int index = (uintptr_t)elm_object_item_data_get(it);
510         //PRINTFUNC(DLOG_DEBUG,"it = %p", it);
511         PRINTFUNC(DLOG_DEBUG, "index = %d %d %d", index, index+1, index+2);
512
513         if (index < EMOTICON_CNT) {
514             emoticon_contents_pool[index].used = 0;
515         }
516         if (index + 1 < EMOTICON_CNT) {
517             emoticon_contents_pool[index+1].used = 0;
518         }
519         if (index + 2 < EMOTICON_CNT) {
520             emoticon_contents_pool[index+2].used = 0;
521         }
522     } else if (!strcmp(itc->item_style, "3button_flat_recent")) {
523         unsigned int index = (uintptr_t)elm_object_item_data_get(it);
524         //PRINTFUNC(DLOG_DEBUG,"index = %d",index);
525
526         if (index < recent_emoji_list.size()) {
527             emoticon_recents_pool[index].used = 0;
528         }
529         if (index + 1 < recent_emoji_list.size()) {
530             emoticon_recents_pool[index+1].used = 0;
531         }
532         if (index + 2 < recent_emoji_list.size()) {
533             emoticon_recents_pool[index+2].used = 0;
534         }
535     } else if (it_drawing == it) {
536         //PRINTFUNC(DLOG_DEBUG,"it_drawing = %p", it_drawing);
537         emoticon_drawing_pool.used = 0;
538     }
539 }
540
541 static Evas_Object * __emoticon_gl_1_content_get(void *data, Evas_Object *obj, const char *part)
542 {
543     if (!strcmp(part, "elm.icon")) {
544         Evas_Object* btn = elm_button_add(obj);
545         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
546         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
547         Evas_Object* ic = elm_image_add(btn);
548         elm_image_resizable_set(ic, EINA_TRUE, EINA_TRUE);
549
550         string path = get_resource_path();
551         if (_WEARABLE)
552             path = path + "wearable/";
553         else if (_TV)
554             path = path + "tv/";
555         else
556             path = path + "mobile/";
557
558         elm_object_style_set(btn, "ime_button_drawing");
559         string path_ic = path + "images/wi_drawing_icon.png";
560         PRINTFUNC(DLOG_DEBUG, "path_ic = %s", path_ic.c_str());
561         elm_image_file_set(ic, path_ic.c_str(), NULL);
562         elm_object_content_set(btn, ic);
563         evas_object_layer_set(btn, 32000);
564
565         emoticon_drawing_pool.index = 0;
566         emoticon_drawing_pool.content = btn;
567         emoticon_drawing_pool.used = 0;
568         return btn;
569     } else if(!strcmp(part, "base")){
570         Evas_Object* btn = elm_button_add(obj);
571         elm_object_style_set(btn, "ime/transparent");
572         return btn;
573     }
574     return NULL;
575 }
576
577 static Evas_Object * __emoticon_gl_recent_content_get(void *data, Evas_Object *obj, const char *part)
578 {
579     if (is_content_reuse_on) {
580         unsigned int index = (uintptr_t)data;
581         int new_index = 0;
582
583         //PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
584         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
585             if (!strcmp(part, "elm.icon.1")) {
586                 if (index >= recent_emoji_list.size()) return NULL;
587                 new_index = index;
588             } else if (!strcmp(part, "elm.icon.2")) {
589                 if (index + 1 >= recent_emoji_list.size()) return NULL;
590                 new_index = index + 1;
591             } else if (!strcmp(part, "elm.icon.3")) {
592                 if (index + 2 >= recent_emoji_list.size()) return NULL;
593                 new_index = index + 2;
594             }
595
596             Evas_Object* btn = NULL;
597             btn = (Evas_Object*)emoticon_recents_pool[new_index].content;
598             emoticon_recents_pool[new_index].used = 1;
599             return btn;
600         } else if (!strcmp(part, "base")) {
601             Evas_Object* btn = elm_button_add(obj);
602             elm_object_style_set(btn, "ime/transparent");
603             return btn;
604         }
605     } else {
606         unsigned int index = (uintptr_t)data;
607         int new_index = 0;
608
609         //PRINTFUNC(DLOG_DEBUG,"%s %d", part, index);
610         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
611             if (!strcmp(part, "elm.icon.1")) {
612                 if (index >= recent_emoji_list.size()) return NULL;
613                 new_index = index;
614             } else if (!strcmp(part, "elm.icon.2")) {
615                 if (index + 1 >= recent_emoji_list.size()) return NULL;
616                 new_index = index + 1;
617             } else if (!strcmp(part, "elm.icon.3")) {
618                 if (index + 2 >= recent_emoji_list.size()) return NULL;
619                 new_index = index + 2;
620             }
621             return get_recent_emoticon_button(obj, new_index);
622         } else if (!strcmp(part, "base")) {
623             Evas_Object* btn = elm_button_add(obj);
624             elm_object_style_set(btn, "ime/transparent");
625             return btn;
626         }
627     }
628     return NULL;
629 }
630
631 static Evas_Object * __emoticon_gl_emoticon_content_get(void *data, Evas_Object *obj, const char *part)
632 {
633     //PRINTFUNC(DLOG_DEBUG,"%s", __func__);
634     if (is_content_reuse_on) {
635         int index = (uintptr_t)data;
636         int new_index = 0;
637
638         if (!strcmp(part, "elm.icon.1") || (!strcmp(part, "elm.icon.2")) || (!strcmp(part, "elm.icon.3"))) {
639             if (!strcmp(part, "elm.icon.1")) {
640                 if (index >= EMOTICON_CNT) return NULL;
641                 new_index = index;
642             } else if (!strcmp(part, "elm.icon.2")) {
643                 if (index + 1 >= EMOTICON_CNT) return NULL;
644                 new_index = index + 1;
645             } else if (!strcmp(part, "elm.icon.3")) {
646                 if (index + 2 >= EMOTICON_CNT) return NULL;
647                 new_index = index + 2;
648             }
649
650             Evas_Object* btn = NULL;
651             btn = (Evas_Object*)emoticon_contents_pool[new_index].content;
652             emoticon_contents_pool[new_index].used = 1;
653 #if 0
654             char utf_8[10] = {0, };
655             snprintf(utf_8, sizeof(utf_8), "%d", new_index);
656             elm_object_part_text_set(btn, "elm.text", strdup(utf_8));
657 #else
658             int length;
659             const Eina_Unicode unicode_event[2] = { (Eina_Unicode)emoticon_info[new_index].code, 0 };
660             char* utf_8 = eina_unicode_unicode_to_utf8(unicode_event, &length);
661             elm_object_part_text_set(btn, "elm.text", utf_8);
662             evas_object_data_set(btn, "index", (void*)(uintptr_t)new_index);
663
664             if (utf_8)
665                 free(utf_8);
666 #endif
667             return btn;
668
669         } else if (!strcmp(part, "base")) {
670             Evas_Object* btn = elm_button_add(obj);
671             elm_object_style_set(btn, "ime/transparent");
672             return btn;
673         }
674     } else {
675         int index = (uintptr_t)data;
676         int new_index = 0;
677
678         if (!strcmp(part, "elm.icon.1") ||  (!strcmp(part, "elm.icon.2")) ||  (!strcmp(part, "elm.icon.3"))) {
679             if (!strcmp(part, "elm.icon.1")) {
680                 if (index >= EMOTICON_CNT) return NULL;
681                 new_index = index;
682             } else if (!strcmp(part, "elm.icon.2")) {
683                 if (index + 1 >= EMOTICON_CNT) return NULL;
684                 new_index = index + 1;
685             } else if (!strcmp(part, "elm.icon.3")) {
686                 if (index + 2 >= EMOTICON_CNT) return NULL;
687                 new_index = index + 2;
688             }
689             return get_emoticon_button(obj, new_index);
690         } else if (!strcmp(part, "base")) {
691             Evas_Object* btn = elm_button_add(obj);
692             elm_object_style_set(btn, "ime/transparent");
693             return btn;
694         }
695     }
696     return NULL;
697 }
698
699 void _create_reusable_recents(Evas_Object *parent)
700 {
701     if (!parent) {
702         PRINTFUNC(DLOG_ERROR, "parent is null");
703         return;
704     }
705
706     unsigned int i;
707     for (i = 0; i < recent_emoji_list.size(); i++ ) {
708         emoticon_recents_pool[i].index = i;
709         emoticon_recents_pool[i].content = get_recent_emoticon_button(parent, i);
710         emoticon_recents_pool[i].used = 0;
711     }
712 }
713
714 static Eina_Bool _lazy_loader_cb_for_contents(void *data)
715 {
716     Evas_Object *gl = (Evas_Object *)data;
717     if (!gl) {
718         PRINTFUNC(DLOG_ERROR, "gl is null");
719         lazy_loading_timer_for_contents = NULL;
720         return ECORE_CALLBACK_CANCEL;
721     }
722
723     if (loading_done_for_contents == EMOTICON_CNT) {
724         PRINTFUNC(DLOG_DEBUG, "lazy loading contents done");
725         lazy_loading_timer_for_contents = NULL;
726         return ECORE_CALLBACK_CANCEL;
727     }
728
729     int loading_top = 0;
730     if (loading_done_for_contents + LOADING_CONTENT_UNIT > EMOTICON_CNT) {
731         loading_top = EMOTICON_CNT;
732     } else {
733         loading_top = loading_done_for_contents + LOADING_CONTENT_UNIT;
734     }
735
736     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_contents loading_done = %d", loading_done_for_contents);
737
738     int i;
739     for (i = loading_done_for_contents; i < loading_top; i++) {
740         emoticon_contents_pool[i].index = i;
741         emoticon_contents_pool[i].used = 0;
742
743         if (emoticon_contents_pool[i].content == NULL) { // reusable
744             emoticon_contents_pool[i].content = get_emoticon_button(gl, i);
745         }
746     }
747
748     loading_done_for_contents = loading_top;
749
750     return ECORE_CALLBACK_RENEW;
751 }
752
753 void create_reusable_button(Evas_Object *parent)
754 {
755     if (!parent) {
756         PRINTFUNC(DLOG_ERROR, "parent is null");
757         return;
758     }
759     loading_done_for_contents = INITAL_CONTENT_UNIT;
760
761     int i;
762     for (i = 0; i < INITAL_CONTENT_UNIT; i++ ) {
763         emoticon_contents_pool[i].index = i;
764         emoticon_contents_pool[i].used = 0;
765
766         if (emoticon_contents_pool[i].content == NULL) { // reusable
767             emoticon_contents_pool[i].content = get_emoticon_button(parent, i);
768         }
769     }
770
771     lazy_loading_timer_for_contents = ecore_timer_add(0.25, _lazy_loader_cb_for_contents, (void *)parent);
772 }
773
774 static Eina_Bool _lazy_loader_cb_for_items(void *data)
775 {
776     Evas_Object *gl = (Evas_Object *)data;
777     if (!gl) {
778         PRINTFUNC(DLOG_ERROR, "gl is null");
779         lazy_loading_timer_for_items = NULL;
780         return ECORE_CALLBACK_CANCEL;
781     }
782
783     if (loading_done_for_item == EMOTICON_CNT) {
784         PRINTFUNC(DLOG_DEBUG, "lazy loading item done");
785         //elm_genlist_realized_items_update(gl);
786         elm_genlist_item_class_free(itc_emoticon);
787         lazy_loading_timer_for_items = NULL;
788
789         return ECORE_CALLBACK_CANCEL;
790     }
791
792     int loading_top = 0;
793     if (loading_done_for_item + LOADING_ITEM_UNIT > EMOTICON_CNT) {
794         loading_top = EMOTICON_CNT;
795     } else {
796         loading_top = loading_done_for_item + LOADING_ITEM_UNIT;
797     }
798
799     if (loading_top > loading_done_for_contents) {
800         PRINTFUNC(DLOG_DEBUG, "Wait for content loading");
801         return ECORE_CALLBACK_RENEW;
802     }
803     PRINTFUNC(DLOG_DEBUG, "_lazy_loader_cb_for_items loading_done_for_item = %d", loading_done_for_item);
804
805     int i;
806     for (i = loading_done_for_item; i < loading_top; i++ ) {
807         if (i%3 == 0)
808             elm_genlist_item_append(gl, itc_emoticon, (void*)(uintptr_t)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)(uintptr_t)i);
809     }
810
811     loading_done_for_item = loading_top;
812     return ECORE_CALLBACK_RENEW;
813 }
814
815 void _create_reusable_contents(Evas_Object *gl){
816     if (!gl) {
817         PRINTFUNC(DLOG_ERROR, "gl is null");
818         return;
819     }
820     int i;
821     loading_done_for_item = INITAL_ITEM_UNIT;
822
823     for (i = 0; i < INITAL_ITEM_UNIT; i++ ) {
824         if (i%3 == 0)
825             elm_genlist_item_append(gl, itc_emoticon, (void*)(uintptr_t)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)(uintptr_t)i);
826     }
827
828     lazy_loading_timer_for_items = ecore_timer_add(0.1, _lazy_loader_cb_for_items, (void *)gl);
829 }
830
831 Evas_Object* _create_emoticon_genlist(void* data)
832 {
833     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
834     App_Data* ad = (App_Data*) data;
835     if (!ad)
836         return NULL;
837
838     Evas_Object* genlist = elm_genlist_add(ad->naviframe);
839     if (NULL == genlist)
840         return NULL;
841
842     if (_WEARABLE) {
843         Evas_Object* circle_object_genlist = eext_circle_object_genlist_add(genlist, ad->circle_surface);
844         eext_circle_object_genlist_scroller_policy_set(circle_object_genlist, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
845         evas_object_data_set(genlist, "circle", (void *) circle_object_genlist);
846         eext_rotary_object_event_activated_set(circle_object_genlist, EINA_TRUE);
847     }
848     evas_object_size_hint_weight_set(genlist, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
849     evas_object_size_hint_align_set(genlist, EVAS_HINT_FILL, EVAS_HINT_FILL);
850     evas_object_show(genlist);
851
852     const char *item_style = NULL;
853     if (_WEARABLE)
854         item_style = "empty";
855     Elm_Object_Item *nf_emoticon_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, genlist, item_style);
856
857     elm_naviframe_item_pop_cb_set(nf_emoticon_item, _custom_back_cb2, ad);
858
859     evas_object_smart_callback_add(genlist, "language,changed", _emoticon_gl_lang_changed, genlist);
860
861     if (is_content_reuse_on) {
862         evas_object_smart_callback_add(genlist, "content,unswallowed", _emoticon_gl_content_unswallowed_cb, NULL);
863     }
864
865     return genlist;
866 }
867
868 void _update_emoticon_items(void *data)
869 {
870     PRINTFUNC(DLOG_DEBUG, "%s", __func__);
871     Evas_Object* gl = (Evas_Object*) data;
872     if (!gl)
873         return;
874
875     unsigned int i;
876     Elm_Object_Item *it = NULL;
877     Elm_Object_Item *first_it = NULL;
878
879     Elm_Genlist_Item_Class * itc_dummy = elm_genlist_item_class_new();
880     itc_dummy->item_style = "title";
881     itc_dummy->func.text_get = NULL;
882     itc_dummy->func.content_get = NULL;
883     itc_dummy->func.state_get = NULL;
884     itc_dummy->func.del = NULL;
885
886     Elm_Genlist_Item_Class *itc_1text_1icon = NULL;
887     if (app_data->mime_type == MIME_TYPE_ALL) {
888         itc_1text_1icon = elm_genlist_item_class_new();
889         itc_1text_1icon->item_style = "drawing";
890         itc_1text_1icon->func.text_get = __emoticon_gl_text_get;
891         itc_1text_1icon->func.content_get = __emoticon_gl_1_content_get;
892         itc_1text_1icon->func.state_get = NULL;
893         itc_1text_1icon->func.del = NULL;
894     }
895
896     Elm_Genlist_Item_Class *itc_group = elm_genlist_item_class_new();
897     itc_group->item_style = "groupindex";
898     itc_group->func.text_get = __emoticon_gl_text_get;
899     itc_group->func.content_get = NULL;
900     itc_group->func.state_get = NULL;
901     itc_group->func.del = NULL;
902
903     Elm_Genlist_Item_Class *itc_recent = elm_genlist_item_class_new();
904     itc_recent->item_style = "3button_flat_recent";
905     itc_recent->func.text_get = NULL;
906     itc_recent->func.content_get = __emoticon_gl_recent_content_get;
907     itc_recent->func.state_get = NULL;
908     itc_recent->func.del = NULL;
909
910     itc_emoticon = elm_genlist_item_class_new();
911     itc_emoticon->item_style = "3button_flat";
912     itc_emoticon->func.text_get = NULL;
913     itc_emoticon->func.content_get = __emoticon_gl_emoticon_content_get;
914     itc_emoticon->func.state_get = NULL;
915     itc_emoticon->func.del = NULL;
916
917     // dummy title for empty space
918     it_emoticon_empty = elm_genlist_item_append(gl, itc_dummy, NULL, NULL, ELM_GENLIST_ITEM_NONE, NULL, NULL);
919
920     // Drawing
921     if (app_data->mime_type == MIME_TYPE_ALL) {
922         it_drawing = elm_genlist_item_append(gl, itc_1text_1icon, "Doodle", NULL, ELM_GENLIST_ITEM_NONE, _drawing_item_clicked_cb, (void *)app_data);
923         first_it = it_drawing;
924     }
925
926     if (recent_emoji_list.size() > 0) {
927         if (is_content_reuse_on) {
928             _create_reusable_recents(gl);
929         }
930
931         // Group Recents
932         it_emoticon_recent_group = elm_genlist_item_append(gl, itc_group, (void*)"IDS_IME_HEADER_RECENT_M_RECETLY_SENT_EMOJIS_ABB", NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)2);
933         elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
934         if (first_it == NULL)
935             first_it = it_emoticon_recent_group;
936
937         PRINTFUNC(DLOG_DEBUG, "size = %d", recent_emoji_list.size());
938
939         for (i=0;i < recent_emoji_list.size();i=i+3)
940         {
941             it = elm_genlist_item_append(gl, itc_recent, (void*)(uintptr_t)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)(uintptr_t)i);
942         }
943     }
944
945     // Group Emoticons
946     it_emoticon_emoji_group = elm_genlist_item_append(gl, itc_group, (void*)"IDS_IME_HEADER_EMOJIS_ABB", NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)2);
947     elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
948     if (first_it == NULL)
949         first_it = it_emoticon_emoji_group;
950
951     if (is_content_reuse_on) {
952         _create_reusable_contents(gl);
953     } else {
954         // Emoticons
955         for (i=0;i< EMOTICON_CNT;i=i+3) {
956             it = elm_genlist_item_append(gl, itc_emoticon, (void*)(uintptr_t)i, NULL, ELM_GENLIST_ITEM_NONE, NULL, (void *)(uintptr_t)i);
957             it_last = it;
958         }
959         elm_genlist_item_class_free(itc_emoticon);
960     }
961     it = elm_genlist_item_next_get(first_it);
962     const Elm_Genlist_Item_Class *itc_temp = elm_genlist_item_item_class_get(it);
963     if (itc_temp == itc_group) {
964         it = elm_genlist_item_next_get(it);
965     }
966     elm_genlist_item_show(it, ELM_GENLIST_ITEM_SCROLLTO_MIDDLE);
967
968     elm_genlist_item_class_free(itc_recent);
969     elm_genlist_item_class_free(itc_group);
970     elm_genlist_item_class_free(itc_1text_1icon);
971     elm_genlist_item_class_free(itc_dummy);
972 }
973
974 void ise_show_emoticon_list(void *data)
975 {
976     App_Data* ad = (App_Data*) data;
977     if (!ad)
978         return;
979
980     it_emoticon_empty = NULL;
981     it_emoticon_recent_group = NULL;
982     it_emoticon_emoji_group = NULL;
983     it_drawing = NULL;
984     it_last = NULL;
985
986     get_recent_emoticons(recent_emoji_list);
987
988     Evas_Object* emoticon_list = NULL;
989
990     emoticon_list = _create_emoticon_genlist(ad);
991
992     if (is_content_reuse_on) {
993         create_reusable_button(ad->naviframe); // button object need to survive even if genlist is deleted.
994     }
995     _update_emoticon_items(emoticon_list);
996 }
997
998 void launch_drawing_app(void *data)
999 {
1000     App_Data* ad = (App_Data*) data;
1001     if (!ad)
1002         return;
1003
1004     _drawing_item_clicked_cb(ad, NULL, NULL);
1005 }