upload isf tizen beta package
[framework/uifw/isf.git] / ism / demos / isf_demo_efl.cpp
1 /*
2  * ISF(Input Service Framework)
3  *
4  * ISF is based on SCIM 1.4.7 and extended for supporting more mobile fitable.
5  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6  *
7  * Contact: Shuo Liu <shuo0805.liu@samsung.com>, Jihoon Kim <jihoon48.kim@samsung.com>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License as published by the
11  * Free Software Foundation; either version 2.1 of the License, or (at your option)
12  * any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
15  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation, Inc., 51
21  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <Elementary.h>
28 #include <ui-gadget.h>
29 #include <Ecore_X.h>
30 #include "isf_demo_efl.h"
31 #include "isf_imcontrol_efl.h"
32 #include "isf_layout_efl.h"
33 #include "isf_event_efl.h"
34 #include "isf_autocapital_efl.h"
35 #include "isf_prediction_efl.h"
36
37 #define BASE_THEME_WIDTH 720.0f
38
39 static void _quit_cb (void *data, Evas_Object *obj, void *event_info)
40 {
41     elm_exit ();
42 }
43
44 static void _list_click (void *data, Evas_Object *obj, void *event_info)
45 {
46     struct appdata *ad = (struct appdata *)data;
47     if (ad == NULL) return;
48
49     Evas_Object *li = ad->li;
50     if (li == NULL) return;
51
52     Elm_List_Item *it = (Elm_List_Item *)elm_list_selected_item_get (li);
53
54     if (it != NULL)
55         elm_list_item_selected_set (it, EINA_FALSE);
56 }
57
58 static void layout_cb (struct ui_gadget *ug, enum ug_mode mode, void *priv)
59 {
60     struct appdata *ad = NULL;
61     Evas_Object *base = NULL;
62
63     if ( ug == NULL || priv == NULL)
64         return;
65
66     ad = (appdata *)priv;
67
68     base = (Evas_Object *)ug_get_layout (ug);
69     if (base == NULL)
70         return;
71
72     switch (mode) {
73     case UG_MODE_FULLVIEW:
74         evas_object_size_hint_weight_set (base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
75         elm_win_resize_object_add (ad->win_main, base);
76         evas_object_show (base);
77         break;
78     case UG_MODE_FRAMEVIEW:
79         printf("please set ug mode to UG_MODE_FULLVIEW!\n");
80         break;
81     default:
82         break;
83     }
84 }
85
86 static void result_cb (struct ui_gadget *ug, bundle *result, void *priv)
87 {
88     if (!result) return;
89
90     const char *name = bundle_get_val(result, "name");
91     printf("get key [ %s ]\n",name);
92
93     if (strcmp(name, "keyboard-setting-wizard-efl") == 0) {
94         const char *desp = bundle_get_val(result,"description");
95         printf("====================\nresult:%s\n====================\n",desp);
96     }
97 }
98
99 static void destroy_cb (struct ui_gadget *ug, void *priv)
100 {
101     if (ug == NULL)
102         return;
103
104     ug_destroy (ug);
105 }
106
107 static void isfsetting_bt (void *data, Evas_Object *obj, void *event_info)
108 {
109     struct appdata *ad = (struct appdata *)data;
110     struct ug_cbs cbs = {0, };
111
112     UG_INIT_EFL (ad->win_main, UG_OPT_INDICATOR_ENABLE);
113
114     cbs.layout_cb  = layout_cb;
115     cbs.result_cb  = result_cb;
116     cbs.destroy_cb = destroy_cb;
117     cbs.priv       = ad;
118     ad->ug = ug_create (NULL, "isfsetting-efl",
119                         UG_MODE_FULLVIEW,
120                         ad->data, &cbs);
121     bundle_free (ad->data);
122     ad->data = NULL;
123 }
124
125 static void keyboard_setting_wizard_bt (void *data, Evas_Object *obj, void *event_info)
126 {
127     struct appdata *ad = (struct appdata *)data;
128     struct ug_cbs cbs = {0, };
129
130     UG_INIT_EFL (ad->win_main, UG_OPT_INDICATOR_ENABLE);
131
132     cbs.layout_cb  = layout_cb;
133     cbs.result_cb  = result_cb;
134     cbs.destroy_cb = destroy_cb;
135     cbs.priv       = ad;
136     ad->data = bundle_create();
137     bundle_add(ad->data, "navi_btn_left", _("Previous"));
138     //bundle_add(ad->data, "navi_btn_left", NULL);
139     bundle_add(ad->data, "navi_btn_right", _("Next"));
140     ad->ug = ug_create (NULL, "keyboard-setting-wizard-efl",
141                         UG_MODE_FULLVIEW,
142                         ad->data, &cbs);
143     bundle_free (ad->data);
144     ad->data = NULL;
145 }
146
147 static int create_demo_view (struct appdata *ad)
148 {
149     Evas_Object *li = NULL;
150
151     Evas_Object *l_button = elm_button_add (ad->naviframe);
152     elm_object_style_set (l_button, "naviframe/end_btn/default");
153     evas_object_smart_callback_add (l_button, "clicked", _quit_cb, NULL);
154
155     // Create list
156     ad->li = li = elm_list_add (ad->naviframe);
157     elm_list_mode_set (li, ELM_LIST_COMPRESS);
158     evas_object_smart_callback_add (ad->li, "selected", _list_click, ad);
159
160     // Test ISF imcontrol API
161     elm_list_item_append (li, "ISF imcontrol API", NULL, NULL, imcontrolapi_bt, ad);
162
163     // test ISF layout
164     elm_list_item_append (li, "ISF Layout", NULL, NULL, ise_layout_bt, ad);
165
166     // Test autocapital type
167     elm_list_item_append (li, "ISF Autocapital", NULL, NULL, ise_autocapital_bt, ad);
168
169     // Test prediction allow
170     elm_list_item_append (li, "ISF Prediction allow", NULL, NULL, ise_prediction_bt, ad);
171
172     elm_list_item_append (li, "ISF Event Demo", NULL, NULL, isf_event_demo_bt, ad);
173
174     /*
175     ISF language selection
176     ISE selection
177     */
178     elm_list_item_append (li, "ISF Setting", NULL, NULL, isfsetting_bt, ad);
179     elm_list_item_append (li, "keyboard Setting wizard", NULL, NULL, keyboard_setting_wizard_bt, ad);
180     // ISF preedit string and commit string on Label and Entry
181
182     elm_list_go (li);
183
184     elm_naviframe_item_push (ad->naviframe, _("isf demo"), l_button, NULL, li, NULL);
185
186     return 0;
187 }
188
189 static int lang_changed (void *data)
190 {
191     struct appdata *ad = (appdata *)data;
192
193     if (ad->layout_main == NULL)
194         return 0;
195
196     ug_send_event (UG_EVENT_LANG_CHANGE);
197     return 0;
198 }
199
200 static int _rotate_cb (enum appcore_rm m, void *data)
201 {
202     struct appdata *ad = (struct appdata *)data;
203     if (ad == NULL || ad->win_main == NULL)
204         return 0;
205
206     int r;
207     switch (m) {
208     case APPCORE_RM_PORTRAIT_NORMAL:
209         ug_send_event (UG_EVENT_ROTATE_PORTRAIT);
210         r = 0;
211         break;
212     case APPCORE_RM_PORTRAIT_REVERSE:
213         ug_send_event (UG_EVENT_ROTATE_PORTRAIT_UPSIDEDOWN);
214         r = 180;
215         break;
216     case APPCORE_RM_LANDSCAPE_NORMAL:
217         ug_send_event (UG_EVENT_ROTATE_LANDSCAPE);
218         r = 270;
219         break;
220     case APPCORE_RM_LANDSCAPE_REVERSE:
221         ug_send_event (UG_EVENT_ROTATE_LANDSCAPE_UPSIDEDOWN);
222         r = 90;
223         break;
224     default:
225         r = -1;
226         break;
227     }
228
229     if (r >= 0)
230         elm_win_rotation_with_resize_set (ad->win_main, r);
231
232     return 0;
233 }
234
235 static void win_del (void *data, Evas_Object *obj, void *event)
236 {
237     elm_exit ();
238 }
239
240 static Evas_Object* create_win (const char *name)
241 {
242     Evas_Object *eo = NULL;
243     int w, h;
244
245     eo = elm_win_add (NULL, name, ELM_WIN_BASIC);
246     if (eo != NULL) {
247         elm_win_title_set (eo, name);
248         elm_win_borderless_set (eo, EINA_TRUE);
249         evas_object_smart_callback_add (eo, "delete,request",
250                                         win_del, NULL);
251         ecore_x_window_size_get (ecore_x_window_root_first_get (), &w, &h);
252         evas_object_resize (eo, w, h);
253     }
254
255     return eo;
256 }
257
258 static Evas_Object* create_layout_main (Evas_Object* parent)
259 {
260     Evas_Object *layout = elm_layout_add (parent);
261     elm_layout_theme_set (layout, "layout", "application", "default");
262     evas_object_size_hint_weight_set (layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
263     elm_win_resize_object_add (parent, layout);
264
265     evas_object_show (layout);
266
267     return layout;
268 }
269
270 static Evas_Object* _create_naviframe_layout (Evas_Object* parent)
271 {
272     Evas_Object *naviframe = elm_naviframe_add (parent);
273     elm_layout_content_set (parent, "elm.swallow.content", naviframe);
274
275     evas_object_show (naviframe);
276
277     return naviframe;
278 }
279
280 static Evas_Object* create_bg(Evas_Object *win)
281 {
282     Evas_Object *bg = elm_bg_add(win);
283     evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
284     elm_win_resize_object_add(win, bg);
285     evas_object_show(bg);
286
287     return bg;
288 }
289
290 static Eina_Bool _keydown_event(void *data, int type, void *event)
291 {
292     Ecore_Event_Key *ev = (Ecore_Event_Key *)event;
293     struct appdata *ad = (struct appdata *)data;
294     Elm_Object_Item *top_it, *bottom_it;
295     if (ad == NULL || ev == NULL) return ECORE_CALLBACK_RENEW;
296
297     if (strcmp(ev->keyname, "XF86Stop") == 0) {
298         if (ug_send_key_event(UG_KEY_EVENT_END) == -1) {
299             top_it = elm_naviframe_top_item_get(ad->naviframe);
300             bottom_it = elm_naviframe_top_item_get(ad->naviframe);
301             if (top_it && bottom_it && (elm_object_item_content_get(top_it) == elm_object_item_content_get(bottom_it))) {
302                 elm_exit();
303             }
304             else {
305                 elm_naviframe_item_pop(ad->naviframe);
306             }
307         }
308     }
309     return ECORE_CALLBACK_RENEW;
310 }
311
312 static int app_create (void *data)
313 {
314     struct appdata *ad = (struct appdata *)data;
315
316     appcore_measure_start ();
317
318     ad->win_main = create_win ("isf-demo-efl");
319     evas_object_show (ad->win_main);
320
321     ad->evas = evas_object_evas_get (ad->win_main);
322     /* get width and height of main window */
323     evas_object_geometry_get (ad->win_main, NULL, NULL, &ad->root_w, &ad->root_h);
324
325     if (ad->root_w >= 0) {
326         elm_scale_set(ad->root_w / BASE_THEME_WIDTH );
327     }
328
329     ad->bg = create_bg(ad->win_main);
330
331     ad->layout_main = create_layout_main (ad->win_main);
332
333     // Indicator
334     elm_win_indicator_state_set (ad->win_main, EINA_TRUE);
335
336     // Navigation Bar
337     ad->naviframe = _create_naviframe_layout (ad->layout_main);
338
339     //init the content in layout_main.
340     create_demo_view (ad);
341
342     lang_changed (ad);
343
344     evas_object_show (ad->win_main);
345
346     /* add system event callback */
347     appcore_set_event_callback (APPCORE_EVENT_LANG_CHANGE,
348                                 lang_changed, ad);
349
350     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _keydown_event, ad);
351
352     appcore_set_rotation_cb (_rotate_cb, ad);
353
354     appcore_measure_time ();
355
356     return 0;
357 }
358
359 static int app_exit (void *data)
360 {
361     struct appdata *ad = (struct appdata *)data;
362     if (ad->li != NULL)
363         evas_object_del (ad->li);
364
365     if (ad->ev_li != NULL)
366         evas_object_del (ad->ev_li);
367
368     if (ad->layout_main != NULL)
369         evas_object_del (ad->layout_main);
370
371     if (ad->win_main != NULL)
372         evas_object_del (ad->win_main);
373
374     return 0;
375 }
376
377 static int app_pause(void *data)
378 {
379     return 0;
380 }
381
382 static int app_resume (void *data)
383 {
384     return 0;
385 }
386
387 static int app_reset (bundle *b, void *data)
388 {
389     struct appdata *ad = (struct appdata *)data;
390
391     if ( (ad != NULL) && (ad->win_main != NULL))
392         elm_win_activate (ad->win_main);
393
394     return 0;
395 }
396
397 int main (int argc, char *argv[])
398 {
399     struct appdata ad;
400     struct appcore_ops ops;
401
402     ops.create    = app_create;
403     ops.terminate = app_exit;
404     ops.pause     = app_pause;
405     ops.resume    = app_resume;
406     ops.reset     = app_reset;
407
408     memset (&ad, 0x0, sizeof (struct appdata));
409     ops.data = &ad;
410     return appcore_efl_main ("isf-demo-efl", &argc, &argv, &ops);
411 }
412
413 /*
414 vi:ts=4:ai:nowrap:expandtab
415 */