Code sync
[apps/native/starter.git] / lock-setting / lockscreen-options / src / lockscreen-options-locktitle.c
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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
18
19 #include <Ecore_X.h>
20
21 #include "lockscreen-options.h"
22 #include "lockscreen-options-debug.h"
23 #include "lockscreen-options-util.h"
24 #include "lockscreen-options-locktitle.h"
25
26 #define EDJE_DIR "/usr/ug/res/edje/ug-lockscreen-options-efl"
27
28 typedef struct {
29         unsigned char red;
30         unsigned char green;
31         unsigned char blue;
32         unsigned char reserved;
33 } options_locktitle_rgb_s;
34
35 typedef struct {
36         lockscreen_options_ug_data *ug_data;
37         Evas_Object *layout;
38         Evas_Object *toolbar;
39         Evas_Object *entry;
40         Ecore_IMF_Context *entry_ctx;
41         Evas_Object *colorselector;
42         Evas_Object *fontselector;
43         Evas_Object *font_radio;
44
45         options_locktitle_rgb_s rgb;
46 } options_locktile_view_s;
47
48 static options_locktile_view_s *options_locktile_view_data = NULL;
49
50 static const fonttype_size = 5;
51 static const char *fonttype_value[] = {
52         "Arial", "Courier New", "Roboto", "Tahoma", "Verdana"
53 };
54
55 static char *_fonttype_dropdown_text_get(void *data, Evas_Object * obj, const char *part);
56 static Evas_Object *_fonttype_dropdown_content_get(void *data, Evas_Object * obj, const char *part);
57 static Evas_Object *_editfield_content_get(void *data, Evas_Object * obj, const char *part);
58 static void _lockscreen_options_locktitle_font_selector_create();
59 static void _lockscreen_options_locktitle_color_selector_create();
60
61 static Elm_Genlist_Item_Class itc_fontlist_style = {
62         .item_style = "1text.1icon.2",
63         .func.text_get = _fonttype_dropdown_text_get,
64         .func.content_get = _fonttype_dropdown_content_get,
65 };
66
67 static Elm_Genlist_Item_Class itc_editfield_style = {
68         .item_style = "dialogue/1icon",
69         .func.text_get = NULL,
70         .func.content_get = _editfield_content_get,
71 };
72
73 static char *_fonttype_dropdown_text_get(void *data, Evas_Object * obj,
74                                              const char *part)
75 {
76         int index = (int)data;
77         if (strcmp(part, "elm.text") == 0) {
78                 return strdup(fonttype_value[index]);
79         }
80         return NULL;
81 }
82
83 static Evas_Object *_fonttype_dropdown_content_get(void *data,
84                                                        Evas_Object * obj,
85                                                        const char *part)
86 {
87         int index = (int)data;
88         if (!strcmp(part, "elm.icon")) {
89                 Evas_Object *radio = elm_radio_add(obj);
90                 elm_radio_state_value_set(radio, index);
91                 elm_radio_group_add(radio, options_locktile_view_data->font_radio);
92                 return radio;
93         }
94         return NULL;
95 }
96
97 static void *_fonttype_sel(void *data, Evas_Object * obj, void *event_info)
98 {
99         //select font type
100 }
101
102 static void _lockscreen_options_locktitle_delete_cb(void *data, Evas * e,
103                                                     Evas_Object * obj,
104                                                     void *event_info)
105 {
106         if (options_locktile_view_data) {
107                 free(options_locktile_view_data);
108                 options_locktile_view_data = NULL;
109         }
110 }
111
112 static void _lockscreen_options_locktitle_back_cb(void *data, Evas_Object * obj,
113                                                   void *event_info)
114 {
115         lockscreen_options_ug_data *ug_data =
116             (lockscreen_options_ug_data *) data;
117
118         if (ug_data == NULL) {
119                 LOCKOPTIONS_ERR("ug_data is null.");
120                 return;
121         }
122
123         Evas_Object *navi_bar = ug_data->navi_bar;
124
125         if (navi_bar == NULL) {
126                 LOCKOPTIONS_ERR("navi_bar is null.");
127                 return;
128         }
129         elm_naviframe_item_pop(navi_bar);
130 }
131
132 static void _send_int_message_to_edc(Evas_Object * obj, int msgID, int param)
133 {
134         Edje_Message_Int msg;
135         msg.val = param;
136         edje_object_message_send(elm_layout_edje_get(obj),
137                                  EDJE_MESSAGE_INT, msgID, &msg);
138 }
139
140 static void _send_float_message_to_edc(Evas_Object * obj, int msgID, float param)
141 {
142         Edje_Message_Float msg;
143         msg.val = param;
144         edje_object_message_send(elm_layout_edje_get(obj),
145                                  EDJE_MESSAGE_FLOAT, msgID, &msg);
146 }
147
148 static void _lockscreen_options_locktitle_entry_imf_state_cb(void *data,
149                                                              Ecore_IMF_Context *
150                                                              ctx, int value)
151 {
152         static float val = 1.0;
153         if (value == ECORE_IMF_INPUT_PANEL_STATE_SHOW) {
154                 Evas_Coord keypad_x = 0;
155                 Evas_Coord keypad_y = 0;
156                 Evas_Coord keypad_w = 0;
157                 Evas_Coord keypad_h = 0;
158                 Evas_Coord window_width = 0;
159                 Evas_Coord window_height = 0;
160
161                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
162                                         &window_width, &window_height);
163                 ecore_imf_context_input_panel_geometry_get(ctx, &keypad_x,
164                                                            &keypad_y, &keypad_w,
165                                                            &keypad_h);
166                 val = (float)(keypad_y) / (float)window_height;
167                 /*set the toolbar's location*/
168                 _send_float_message_to_edc(options_locktile_view_data->layout, 1, val);
169                 /*set toolbar visible*/
170                 _send_float_message_to_edc(options_locktile_view_data->layout, 2, 0.0);
171         } else if (value == ECORE_IMF_INPUT_PANEL_STATE_HIDE
172                                         &&options_locktile_view_data->colorselector == NULL
173                                         &&options_locktile_view_data->fontselector == NULL) {
174                 _send_float_message_to_edc(options_locktile_view_data->layout, 3, 0.0);
175         }else {
176                 /*set the toolbar's location*/
177                 _send_float_message_to_edc(options_locktile_view_data->layout, 1, val);
178         }
179 }
180
181 static void _lockscreen_options_locktitle_entry_imf_resize_cb(void *data,
182                                                               Ecore_IMF_Context
183                                                               * ctx, int value)
184 {
185         Evas_Coord keypad_x = 0;
186         Evas_Coord keypad_y = 0;
187         Evas_Coord keypad_w = 0;
188         Evas_Coord keypad_h = 0;
189         Evas_Coord window_width = 0;
190         Evas_Coord window_height = 0;
191
192         ecore_x_window_size_get(ecore_x_window_root_first_get(), &window_width,
193                                 &window_height);
194         ecore_imf_context_input_panel_geometry_get(ctx, &keypad_x, &keypad_y,
195                                            &keypad_w, &keypad_h);
196 }
197
198 /* This callback is for showing(hiding) X marked button.*/
199 static void _changed_cb(void *data, Evas_Object *obj, void *event_info)
200 {
201         if (elm_object_focus_get(data)) {
202                 if (elm_entry_is_empty(obj)) {
203                         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
204                 }else {
205                         elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
206                 }
207         }
208 }
209
210 /* Focused callback will show X marked button and hide guidetext.*/
211 static void _focused_cb(void *data, Evas_Object *obj, void *event_info)
212 {
213         if (!elm_entry_is_empty(obj)) {
214                 elm_object_signal_emit(data, "elm,state,eraser,show", "elm");
215         }
216         elm_object_signal_emit(data, "elm,state,guidetext,hide", "elm");
217 }
218
219 /*Unfocused callback will show guidetext and hide X marked button.*/
220 static void _unfocused_cb(void *data, Evas_Object *obj, void *event_info)
221 {
222         if (elm_entry_is_empty(obj)) {
223                 elm_object_signal_emit(data, "elm,state,guidetext,show", "elm");
224         }
225         elm_object_signal_emit(data, "elm,state,eraser,hide", "elm");
226 }
227
228 static void _eraser_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source) // When X marked button clicked, make string as empty.
229 {
230         elm_entry_entry_set(data, "");
231 }
232
233 static Evas_Object *_lockscreen_options_editfield_create(Evas_Object *parent)
234 {
235         LOCKOPTIONS_DBG("[ == %s == ]", __func__);
236         Evas_Object *layout = NULL;
237         Evas_Object *entry = NULL;
238
239         layout = elm_layout_add(parent);
240         if(layout == NULL)
241                 return NULL;
242         elm_layout_theme_set(layout, "layout", "editfield", "default");
243         evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
244
245         entry = elm_entry_add(parent);
246         if(entry == NULL)
247                 return NULL;
248         evas_object_smart_callback_add(entry, "changed", _changed_cb, layout);
249         evas_object_smart_callback_add(entry, "focused", _focused_cb, layout);
250         evas_object_smart_callback_add(entry, "unfocused", _unfocused_cb, layout);
251
252         elm_object_part_content_set(layout, "elm.swallow.content", entry);
253         elm_object_part_text_set(layout, "elm.guidetext", lockscreen_optoins_get_string(IDS_LOCKSCREEN_OPTIONS_LOCK_SCREEN_TITLE_GUIDE_TEXT));
254         elm_object_signal_callback_add(layout, "elm,eraser,clicked", "elm", _eraser_clicked_cb, entry);
255
256         options_locktile_view_data->entry = entry;
257
258         Ecore_IMF_Context *entry_ctx = elm_entry_imf_context_get(entry);
259         if(entry_ctx != NULL) {
260                 ecore_imf_context_input_panel_event_callback_add((Ecore_IMF_Context *)
261                                                         entry_ctx,
262                                                         ECORE_IMF_INPUT_PANEL_STATE_EVENT,
263                                                         _lockscreen_options_locktitle_entry_imf_state_cb,
264                                                         options_locktile_view_data);
265                 ecore_imf_context_input_panel_event_callback_add((Ecore_IMF_Context *)
266                                                         entry_ctx,
267                                                         ECORE_IMF_INPUT_PANEL_GEOMETRY_EVENT,
268                                                         _lockscreen_options_locktitle_entry_imf_resize_cb,
269                                                         options_locktile_view_data);
270                 options_locktile_view_data->entry_ctx = entry_ctx;
271         }
272
273         return layout;
274 }
275
276 static Evas_Object *_editfield_content_get(void *data,
277                                                        Evas_Object * obj,
278                                                        const char *part)
279 {
280         if (!strcmp(part, "elm.icon")) {
281                 LOCKOPTIONS_DBG("[ == %s == ]", __func__);
282                 Evas_Object *editfield = NULL;
283                 editfield = _lockscreen_options_editfield_create(obj);
284                 return editfield;
285         }
286         return NULL;
287 }
288
289 static void *_editfield_sel(void *data, Evas_Object * obj, void *event_info)
290 {
291         elm_genlist_item_selected_set((Elm_Object_Item *)event_info, EINA_FALSE);
292 }
293
294 static void _on_text_fonttype_btn_clicked_cb(void *data, Evas_Object * obj,
295                                                const char *emission,
296                                                const char *source)
297 {
298         if (options_locktile_view_data != NULL) {
299                 if(options_locktile_view_data->fontselector == NULL) {
300                         if(options_locktile_view_data->colorselector != NULL) {
301                                 evas_object_del(options_locktile_view_data->colorselector);
302                                 options_locktile_view_data->colorselector = NULL;
303                         }
304                         _lockscreen_options_locktitle_font_selector_create();
305                         /*if create fontselector successfully, hide IME*/
306                         if(options_locktile_view_data->fontselector != NULL) {
307                                 elm_object_focus_set(options_locktile_view_data->entry, EINA_FALSE);
308                         }
309                 }else {
310                         evas_object_del(options_locktile_view_data->fontselector);
311                         options_locktile_view_data->fontselector = NULL;
312                         elm_object_focus_set(options_locktile_view_data->entry, EINA_TRUE);
313                 }
314         }
315 }
316
317 static void _on_text_draw_btn_clicked_cb(void *data, Evas_Object * obj,
318                                                const char *emission,
319                                                const char *source)
320 {
321         if (options_locktile_view_data != NULL) {
322                 if(options_locktile_view_data->colorselector == NULL) {
323                         if(options_locktile_view_data->fontselector != NULL) {
324                                 evas_object_del(options_locktile_view_data->fontselector);
325                                 options_locktile_view_data->fontselector = NULL;
326                         }
327                         _lockscreen_options_locktitle_color_selector_create();
328                         /*if create colorselector successfully, hide IME*/
329                         if(options_locktile_view_data->colorselector != NULL) {
330                                 elm_object_focus_set(options_locktile_view_data->entry, EINA_FALSE);
331                         }
332                 }else {
333                         evas_object_del(options_locktile_view_data->colorselector);
334                         options_locktile_view_data->colorselector = NULL;
335                         elm_object_focus_set(options_locktile_view_data->entry, EINA_TRUE);
336                 }
337         }
338 }
339
340 static void _on_text_bold_btn_clicked_cb(void *data, Evas_Object * obj,
341                                           const char *emission,
342                                           const char *source)
343 {
344         if (options_locktile_view_data != NULL) {
345                 _send_int_message_to_edc(options_locktile_view_data->toolbar, 1, 1);
346         }
347 }
348
349 static void _on_text_italic_btn_clicked_cb(void *data, Evas_Object * obj,
350                                             const char *emission,
351                                             const char *source)
352 {
353         if (options_locktile_view_data != NULL) {
354                 _send_int_message_to_edc(options_locktile_view_data->toolbar, 1, 2);
355         }
356
357 }
358
359 static void _on_text_underline_btn_clicked_cb(void *data, Evas_Object * obj,
360                                                const char *emission,
361                                                const char *source)
362 {
363         if (options_locktile_view_data != NULL) {
364                 _send_int_message_to_edc(options_locktile_view_data->toolbar, 1, 3);
365         }
366 }
367
368 static void _lockscreen_options_locktitle_colorselector_changed_cb(void *data, Evas_Object *obj, void *event_info)
369 {
370         Evas_Object *rect = (Evas_Object *)data;
371         int r = 0, g = 0, b = 0, a = 0;
372         elm_colorselector_color_get(obj, &r, &g, &b, &a);
373         evas_object_color_set(rect, r, g, b, a);
374 }
375
376 static void _lockscreen_options_locktitle_color_selector_create()
377 {
378         if(options_locktile_view_data == NULL)
379                 return;
380         Evas_Object *colorselector = NULL;
381         Evas_Object *rect = NULL;
382         Evas_Object *color = NULL;
383
384         colorselector =
385                         lockscreen_options_util_create_layout(options_locktile_view_data->layout,
386                                                           EDJE_DIR
387                                                           "/lockscreen-options.edj",
388                                                           "lockscreen.options.locktitle.colorselector");
389         if(colorselector == NULL)
390                 return;
391
392         rect = evas_object_rectangle_add(evas_object_evas_get(options_locktile_view_data->layout));
393         elm_object_part_content_set(colorselector, "rect", rect);
394         evas_object_color_set(rect, options_locktile_view_data->rgb.red, options_locktile_view_data->rgb.green, options_locktile_view_data->rgb.blue, options_locktile_view_data->rgb.reserved);
395
396         color = elm_colorselector_add(options_locktile_view_data->layout);
397         elm_colorselector_mode_set(color, ELM_COLORSELECTOR_COMPONENTS);
398         elm_object_part_content_set(colorselector, "color", color);
399         evas_object_smart_callback_add(color, "changed", _lockscreen_options_locktitle_colorselector_changed_cb, rect);
400         evas_object_color_set(color, options_locktile_view_data->rgb.red, options_locktile_view_data->rgb.green, options_locktile_view_data->rgb.blue, options_locktile_view_data->rgb.reserved);
401
402         options_locktile_view_data->colorselector = colorselector;
403         elm_object_part_content_set(options_locktile_view_data->layout, "elm.swallow.selector", colorselector);
404         evas_object_show(colorselector);
405 }
406
407 static void _lockscreen_options_locktitle_font_selector_create()
408 {
409         if(options_locktile_view_data == NULL)
410                 return;
411         Evas_Object *fontselector = NULL;
412         Evas_Object *font_radio = NULL;
413
414         fontselector = elm_genlist_add(options_locktile_view_data->layout);
415         if(fontselector == NULL)
416                 return;
417
418         font_radio= elm_radio_add(fontselector);
419         if(font_radio == NULL)
420                 return;
421
422         int index = 0;
423         for (index = 0; index < fonttype_size; index++) {
424                 elm_genlist_item_append(fontselector, &itc_fontlist_style,
425                                 (void *)index, NULL,
426                                 ELM_GENLIST_ITEM_NONE, _fonttype_sel, NULL);
427         }
428
429         options_locktile_view_data->fontselector = fontselector;
430         options_locktile_view_data->font_radio = font_radio;
431         elm_object_part_content_set(options_locktile_view_data->layout, "elm.swallow.selector", fontselector);
432         evas_object_show(fontselector);
433 }
434
435 static void _lockscreen_options_locktitle_create_toolbar()
436 {
437         if(options_locktile_view_data == NULL)
438                 return;
439
440         Evas_Object *toolbar = NULL;
441
442         toolbar =
443                         lockscreen_options_util_create_layout(options_locktile_view_data->layout,
444                                                           EDJE_DIR
445                                                           "/lockscreen-options.edj",
446                                                           "lockscreen.options.locktitle.toolbar");
447         if(toolbar == NULL)
448                 return;
449
450         elm_object_part_content_set(options_locktile_view_data->layout, "elm.swallow.toolbar", toolbar);
451         evas_object_show(toolbar);
452         options_locktile_view_data->toolbar = toolbar;
453
454         edje_object_signal_callback_add(elm_layout_edje_get(toolbar),
455                                 "dropdown.fonttype.clicked", "*",
456                                 _on_text_fonttype_btn_clicked_cb,
457                                 NULL);
458         edje_object_signal_callback_add(elm_layout_edje_get(toolbar),
459                                 "draw.button.clicked", "*",
460                                 _on_text_draw_btn_clicked_cb, NULL);
461         edje_object_signal_callback_add(elm_layout_edje_get(toolbar),
462                                 "text.bold.clicked", "*",
463                                 _on_text_bold_btn_clicked_cb, NULL);
464         edje_object_signal_callback_add(elm_layout_edje_get(toolbar),
465                                 "text.italic.clicked", "*",
466                                 _on_text_italic_btn_clicked_cb, NULL);
467         edje_object_signal_callback_add(elm_layout_edje_get(toolbar),
468                                 "text.underline.clicked", "*",
469                                 _on_text_underline_btn_clicked_cb, NULL);
470 }
471
472 void lockscreen_options_locktitle_create_view(lockscreen_options_ug_data *
473                                               ug_data)
474 {
475         if (ug_data == NULL) {
476                 LOCKOPTIONS_ERR("ug is NULL");
477                 return;
478         }
479         LOCKOPTIONS_DBG("lockscreen_options_locktitle_create_view begin\n");
480         Evas_Object *navi_bar = ug_data->navi_bar;
481         if (navi_bar == NULL) {
482                 LOCKOPTIONS_ERR("navi_bar is null.");
483                 return;
484         }
485
486         /*initialization*/
487         options_locktile_view_data =
488             (options_locktile_view_s *) calloc(1,
489                                                sizeof(options_locktile_view_s));
490         options_locktile_view_data->ug_data = ug_data;
491         options_locktile_view_data->rgb.red = 255;
492         options_locktile_view_data->rgb.green = 255;
493         options_locktile_view_data->rgb.blue = 255;
494         options_locktile_view_data->rgb.reserved = 255;
495
496         Evas_Object *layout = NULL;
497         Evas_Object *back_button = NULL;
498         Evas_Object *genlist = NULL;
499
500         layout =
501             lockscreen_options_util_create_layout(navi_bar,
502                                                   EDJE_DIR
503                                                   "/lockscreen-options.edj",
504                                                   "lockscreen.options.locktitle.main");
505
506         if (layout == NULL) {
507                 LOCKOPTIONS_ERR("can't create locktitle layout.");
508                 return;
509         }
510
511         evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL,
512                                        _lockscreen_options_locktitle_delete_cb,
513                                        NULL);
514         options_locktile_view_data->layout = layout;
515
516         genlist = elm_genlist_add(layout);
517         if(genlist == NULL) {
518                 return;
519         }
520         elm_genlist_mode_set(genlist, ELM_LIST_COMPRESS);
521         elm_genlist_item_append(genlist, &itc_editfield_style, NULL, NULL, ELM_GENLIST_ITEM_NONE, _editfield_sel, NULL);
522         elm_object_part_content_set(layout, "elm.swallow.content", genlist);
523         evas_object_show(genlist);
524
525         _lockscreen_options_locktitle_create_toolbar();
526
527         back_button = elm_button_add(navi_bar);
528         elm_object_style_set(back_button, "naviframe/back_btn/default");
529         evas_object_smart_callback_add(back_button, "clicked",
530                                        _lockscreen_options_locktitle_back_cb,
531                                        ug_data);
532
533         elm_naviframe_item_push(navi_bar, "Lock screen title", back_button,
534                                 NULL, layout, NULL);
535 }