3 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <Elementary.h>
19 #include <app_debug.h>
28 #define KEY_ICON "icon"
29 #define KEY_FOCUS_ICON "focusicon"
31 #define CTXPOPUP_X 294
32 #define CTXPOPUP_Y 310
35 enum input_handler_type {
36 INPUT_HANDLER_TYPE_PHOTO,
37 INPUT_HANDLER_TYPE_DELETE,
38 INPUT_HANDLER_TYPE_EO,
39 INPUT_HANDLER_TYPE_BTN_DONE,
40 INPUT_HANDLER_TYPE_BTN_CANCEL,
41 INPUT_HANDLER_TYPE_POPUP_BTN_DISCARD,
42 INPUT_HANDLER_TYPE_POPUP_BTN_CANCEL,
43 INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_DELETE,
44 INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_CANCEL,
45 INPUT_HANDLER_TYPE_ENTRY
49 USER_ITEM_NONE = 0x00,
51 USER_ITEM_ADMIN = 0x02,
52 USER_ITEM_NORMAL = 0x08
57 const char *focus_file;
59 const char *focus_message;
69 Evas_Object *focus_icon;
72 Evas_Object *ctxpopup;
79 struct datamgr_item *di;
81 enum user_item_type user_type;
84 static void _focused(int id, void *data, Evas_Object *obj,
85 Elm_Object_Item *item)
90 case INPUT_HANDLER_TYPE_PHOTO:
91 case INPUT_HANDLER_TYPE_DELETE:
92 case INPUT_HANDLER_TYPE_EO:
93 elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
95 case INPUT_HANDLER_TYPE_ENTRY:
97 elm_object_signal_emit(obj, SIG_SELECT, SRC_ELM);
98 elm_object_signal_emit(priv->ly, SIG_FOCUS, SRC_PROG);
105 static void _unfocused(int id, void *data, Evas_Object *obj,
106 Elm_Object_Item *item)
111 case INPUT_HANDLER_TYPE_PHOTO:
112 case INPUT_HANDLER_TYPE_DELETE:
113 case INPUT_HANDLER_TYPE_EO:
114 elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
116 case INPUT_HANDLER_TYPE_ENTRY:
118 elm_object_signal_emit(obj, SIG_UNSELECT, SRC_ELM);
119 elm_object_signal_emit(priv->ly, SIG_UNFOCUS, SRC_PROG);
126 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
127 Evas_Event_Key_Down *ev);
129 static void _mouse_up(int id, void *data, Evas *e, Evas_Object *obj,
130 Evas_Event_Mouse_Up *ev);
132 static void _mouse_move(int id, void *data, Evas *e, Evas_Object *obj,
133 Evas_Event_Mouse_Move *ev)
135 if (!elm_object_focus_get(obj))
136 elm_object_focus_set(obj, EINA_TRUE);
139 static input_handler handler = {
141 .unfocused = _unfocused,
142 .key_down = _key_down,
143 .mouse_up = _mouse_up,
144 .mouse_move = _mouse_move
147 static void _get_profile(struct _priv *priv, const char **name,
150 *name = elm_entry_entry_get(priv->name);
151 elm_image_file_get(priv->icon, photo, NULL);
152 *photo = utils_get_icon_from_photo(*photo);
155 static void _delete_popup(struct _priv *priv, Evas_Object *obj)
157 inputmgr_remove_callback(obj, &handler);
158 evas_object_del(priv->popup);
162 static void _add_discard_popup(struct _priv *priv)
164 Evas_Object *popup, *discard_btn, *cancel_btn;
166 popup = utils_add_popup(priv->base, MESSAGE_DISCARD,
167 MESSAGE_DISCARD_CONTENTS);
169 _ERR("failed to add popup");
174 discard_btn = utils_add_button(popup, MESSAGE_DISCARD,
175 PART_POPUP_BUTTON_1);
177 _ERR("failed to add delete btn");
178 evas_object_del(popup);
181 inputmgr_add_callback(discard_btn, INPUT_HANDLER_TYPE_POPUP_BTN_DISCARD,
184 cancel_btn = utils_add_button(popup, MESSAGE_CANCEL,
185 PART_POPUP_BUTTON_2);
187 _ERR("failed to add cancel btn");
188 evas_object_del(popup);
191 inputmgr_add_callback(cancel_btn, INPUT_HANDLER_TYPE_POPUP_BTN_CANCEL,
193 elm_object_focus_set(cancel_btn, EINA_TRUE);
196 static bool _check_changed_input(struct _priv *priv)
198 const char *photo, *name;
200 _get_profile(priv, &name, &photo);
203 if (!strcmp(photo, priv->di->icon))
206 if (!strcmp(name, "") && !strcmp(photo, IMAGE_USER_DEFAULT))
213 static bool _add_btns(struct _priv *priv)
215 Evas_Object *done, *cancel;
217 done = utils_add_button(priv->base, MESSAGE_DONE,
218 PART_USER_EDIT_BTN_DONE);
220 _ERR("failed to add done btn");
223 inputmgr_add_callback(done, INPUT_HANDLER_TYPE_BTN_DONE, &handler,
226 cancel = utils_add_button(priv->base, MESSAGE_CANCEL,
227 PART_USER_EDIT_BTN_CANCEL);
229 _ERR("failed to add cancel btn");
230 evas_object_del(done);
233 inputmgr_add_callback(cancel, INPUT_HANDLER_TYPE_BTN_CANCEL, &handler,
236 priv->cancel = cancel;
242 static void _load_user_icon(struct _priv *priv, const char *file,
243 const char *focus_file)
245 Evas_Object *icon, *focus_icon;
247 icon = utils_add_icon(priv->photo, file, PART_USER_EDIT_PHOTO);
249 _ERR("failed to add icon");
251 focus_icon = utils_add_icon(priv->photo, focus_file,
252 PART_USER_EDIT_PHOTO_FOCUS);
254 _ERR("failed to add focus icon");
257 priv->focus_icon = focus_icon;
260 static Evas_Object *_pack_icon(Evas_Object *table, struct icon_info *_icon_info,
263 Evas_Object *eo, *ic, *focus_ic;
265 eo = utils_add_layout(table, GRP_USER_EDIT_ICON_LIST_ITEM, true, NULL);
267 _ERR("failed to add layout");
270 evas_object_show(eo);
271 elm_table_pack(table, eo, i % 4, i / 4, 1, 1);
273 ic = utils_add_icon(eo, _icon_info->icon_file,
274 PART_USER_EDIT_ICON_LIST_ITEM);
276 _ERR("failed to add icon");
281 focus_ic = utils_add_icon(eo, _icon_info->focus_icon_file,
282 PART_USER_EDIT_ICON_LIST_ITEM_FOCUS);
284 _ERR("failed to add focus icon");
288 evas_object_data_set(eo, KEY_ICON, _icon_info->photo_file);
293 static void _dismissed(void *data, Evas_Object *obj, void *ei)
295 struct _priv *priv = data;
297 elm_object_focus_allow_set(priv->photo, EINA_TRUE);
300 static void _add_icon_list(struct _priv *priv)
302 Evas_Coord x, y, w, h;
303 Evas_Object *ctxpopup, *ly, *table, *eo;
305 struct icon_info *_icon_info;
307 ctxpopup = elm_ctxpopup_add(priv->base);
309 _ERR("failed to add ctxpopup");
312 elm_ctxpopup_horizontal_set(ctxpopup, EINA_TRUE);
313 elm_ctxpopup_auto_hide_disabled_set(ctxpopup, EINA_TRUE);
314 evas_object_smart_callback_add(ctxpopup, "dismissed", _dismissed, priv);
316 ly = utils_add_layout(ctxpopup, GRP_USER_EDIT_ICON_LIST, false, NULL);
318 _ERR("failed to layout");
319 evas_object_del(ctxpopup);
322 elm_object_content_set(ctxpopup, ly);
323 evas_object_show(ly);
324 evas_object_show(ctxpopup);
326 evas_object_geometry_get(priv->photo, &x, &y, &w, &h);
327 evas_object_move(ctxpopup,
328 (x + w + CTXPOPUP_X) * elm_config_scale_get(),
329 (y + CTXPOPUP_Y) * elm_config_scale_get());
331 table = utils_add_table(ly, true, PART_USER_EDIT_ICON_LIST);
333 _ERR("failed to add table");
334 evas_object_del(ctxpopup);
337 elm_table_padding_set(table, elm_config_scale_get() * 1,
338 elm_config_scale_get() * 1);
339 evas_object_size_hint_weight_set(table, EVAS_HINT_EXPAND,
342 _icon_info = utils_get_icon_info();
344 for (i = 0; i < MAX_ITEM_COUNT; i++) {
345 eo = _pack_icon(table, &_icon_info[i], i);
349 inputmgr_add_callback(eo, INPUT_HANDLER_TYPE_EO, &handler,
352 elm_object_focus_set(eo, EINA_TRUE);
355 elm_object_focus_allow_set(priv->photo, EINA_FALSE);
356 priv->ctxpopup = ctxpopup;
359 static Evas_Object *_add_layout(struct _priv *priv)
361 Evas_Object *ly, *photo, *name, *lock;
363 ly = utils_add_layout(priv->base, GRP_USER_EDIT_CONTENTS, false,
364 PART_USER_EDIT_CONTENTS);
366 _ERR("failed to add layout");
370 photo = utils_add_layout(ly, GRP_USER_EDIT_PHOTO, true,
371 PART_USER_EDIT_CONTENTS_PHOTO);
375 name = utils_add_entry(ly, "", false, PART_USER_EDIT_CONTENTS_NAME);
379 lock = utils_add_layout(ly, GRP_USER_EDIT_SWITCH, false,
380 PART_USER_EDIT_CONTENTS_LOCK);
384 utils_add_icon(lock, IMAGE_SWITCH_OFF, PART_USER_EDIT_SWITCH);
386 inputmgr_add_callback(photo, INPUT_HANDLER_TYPE_PHOTO, &handler, priv);
387 inputmgr_add_callback(name, INPUT_HANDLER_TYPE_ENTRY, &handler, priv);
394 _ERR("failed to add layout");
399 static bool _add_user_edit(struct _priv *priv)
403 ly = _add_layout(priv);
405 _ERR("failed to add edit layout");
409 if (!_add_btns(priv)) {
410 _ERR("failed to add buttons");
420 static Evas_Object *_create(Evas_Object *win, void *data)
426 _ERR("Invalid argument");
430 priv = calloc(1, sizeof(*priv));
432 _ERR("failed to calloc priv");
436 base = utils_add_layout(win, GRP_USER_EDIT, false, NULL);
438 _ERR("failed to create base");
442 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
444 elm_win_resize_object_add(win, base);
450 if (!_add_user_edit(priv)) {
451 _ERR("failed to add user edit layout");
452 evas_object_del(base);
457 viewmgr_set_view_data(VIEW_USER_EDIT, priv);
458 viewmgr_add_view(view_photo_get_vclass(), NULL);
463 static void _add_delete_popup(struct _priv *priv)
465 Evas_Object *popup, *delete_btn, *cancel_btn;
468 snprintf(buf, sizeof(buf), "%s %s?<br> %s", MESSAGE_DELETE,
469 priv->di->title, MESSAGE_DELETE_CONTENTS);
471 popup = utils_add_popup(priv->base, MESSAGE_DELETE_USER, buf);
473 _ERR("failed to add popup");
478 delete_btn = utils_add_button(popup, MESSAGE_DELETE,
479 PART_POPUP_BUTTON_1);
481 _ERR("failed to add delete btn");
482 evas_object_del(popup);
485 inputmgr_add_callback(delete_btn,
486 INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_DELETE,
489 cancel_btn = utils_add_button(popup, MESSAGE_CANCEL,
490 PART_POPUP_BUTTON_2);
492 _ERR("failed to add cancel btn");
493 evas_object_del(popup);
496 inputmgr_add_callback(cancel_btn,
497 INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_CANCEL,
500 elm_object_focus_allow_set(priv->del, EINA_FALSE);
501 elm_object_focus_set(cancel_btn, EINA_TRUE);
504 static void _select_eo(struct _priv *priv, Evas_Object *obj)
506 const char *file, *focus_file;
508 evas_object_del(priv->ctxpopup);
509 priv->ctxpopup = NULL;
510 elm_object_focus_allow_set(priv->photo, EINA_TRUE);
512 file = evas_object_data_get(obj, KEY_ICON);
513 focus_file = utils_get_focus_photo_from_photo(file);
515 if (!strcmp(file, ICON_ADD) || !strcmp(focus_file, ICON_ADD)) {
516 viewmgr_push_view(VIEW_PHOTO);
517 elm_object_focus_allow_set(obj, EINA_FALSE);
519 _load_user_icon(priv, file, focus_file);
522 static void _select_btn_done(struct _priv *priv)
524 const char *name, *photo;
526 _get_profile(priv, &name, &photo);
528 priv->di->action = ITEM_SELECT_ACTION_UPDATE;
529 priv->di->title = strdup(name ? name : "");
530 priv->di->icon = strdup(photo ? photo :
532 datamgr_select_item(priv->dm, priv->di);
534 if (!name || !strcmp(name, "")) {
535 elm_object_focus_set(priv->name, EINA_TRUE);
538 datamgr_add_item(priv->dm, name, photo, "");
545 static void _select(struct _priv *priv, int id, Evas_Object *obj)
548 case INPUT_HANDLER_TYPE_PHOTO:
549 _add_icon_list(priv);
551 case INPUT_HANDLER_TYPE_DELETE:
552 _add_delete_popup(priv);
554 case INPUT_HANDLER_TYPE_EO:
555 _select_eo(priv, obj);
557 case INPUT_HANDLER_TYPE_ENTRY:
558 case INPUT_HANDLER_TYPE_BTN_DONE:
559 _select_btn_done(priv);
561 case INPUT_HANDLER_TYPE_BTN_CANCEL:
562 if (_check_changed_input(priv)) {
563 _add_discard_popup(priv);
568 case INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_DELETE:
569 _delete_popup(priv, obj);
570 elm_object_focus_allow_set(priv->del, EINA_TRUE);
571 priv->di->action = ITEM_SELECT_ACTION_DELETE;
572 datamgr_select_item(priv->dm, priv->di);
575 case INPUT_HANDLER_TYPE_POPUP_BTN_DISCARD:
576 _delete_popup(priv, obj);
579 case INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_CANCEL:
580 elm_object_focus_allow_set(priv->del, EINA_TRUE);
581 case INPUT_HANDLER_TYPE_POPUP_BTN_CANCEL:
582 _delete_popup(priv, obj);
589 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
590 Evas_Event_Key_Down *ev)
595 _ERR("Invalid argument");
600 if (!strcmp(ev->keyname, KEY_BACK) ||
601 !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
603 case INPUT_HANDLER_TYPE_PHOTO:
604 case INPUT_HANDLER_TYPE_BTN_DONE:
605 case INPUT_HANDLER_TYPE_BTN_CANCEL:
606 case INPUT_HANDLER_TYPE_DELETE:
607 case INPUT_HANDLER_TYPE_ENTRY:
608 if (_check_changed_input(priv)) {
609 _add_discard_popup(priv);
614 case INPUT_HANDLER_TYPE_EO:
615 evas_object_del(priv->ctxpopup);
616 priv->ctxpopup = NULL;
617 elm_object_focus_allow_set(priv->photo, EINA_TRUE);
619 case INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_DELETE:
620 _delete_popup(priv, obj);
621 elm_object_focus_allow_set(priv->del, EINA_TRUE);
623 case INPUT_HANDLER_TYPE_POPUP_BTN_DISCARD:
624 _delete_popup(priv, obj);
626 case INPUT_HANDLER_TYPE_DELETE_POPUP_BTN_CANCEL:
627 elm_object_focus_allow_set(priv->del, EINA_TRUE);
628 case INPUT_HANDLER_TYPE_POPUP_BTN_CANCEL:
629 _delete_popup(priv, obj);
634 } else if (!strcmp(ev->keyname, KEY_ENTER) ||
635 !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
636 _select(priv, id, obj);
637 } else if (!strcmp(ev->keyname, KEY_RIGHT)) {
638 if (id == INPUT_HANDLER_TYPE_PHOTO && priv->del)
639 elm_object_focus_set(priv->del, EINA_TRUE);
643 static void _mouse_up(int id, void *data, Evas *e, Evas_Object *obj,
644 Evas_Event_Mouse_Up *ev)
649 _ERR("Invalid argument");
654 _select(priv, id, obj);
657 static void _load_del_btn(struct _priv *priv)
661 btn = utils_add_layout(priv->ly, GRP_USER_EDIT_DELETE, true,
662 PART_USER_EDIT_CONTENTS_DELETE);
664 _ERR("failed to add delete button");
667 inputmgr_add_callback(btn, INPUT_HANDLER_TYPE_DELETE, &handler, priv);
672 static void _change_entry(Evas_Object *name, Evas_Object *ly, Eina_Bool disable,
673 const char *text, const char *signal)
675 elm_entry_entry_set(name, text);
676 elm_object_focus_allow_set(name, disable);
677 elm_entry_editable_set(name, disable);
678 elm_object_signal_emit(ly, signal, SRC_PROG);
681 static void _show(void *data)
684 const char *photo, *focus_photo;
687 _ERR("Invalid argument");
694 elm_object_part_text_set(priv->base, PART_USER_EDIT_TITLE,
696 _change_entry(priv->name, priv->ly, EINA_FALSE, priv->di->title,
698 if (priv->selected_photo) {
699 _load_user_icon(priv, priv->selected_photo,
700 priv->selected_photo);
702 photo = utils_get_photo_from_icon(priv->di->icon);
703 focus_photo = utils_get_focus_photo_from_photo(photo);
704 _load_user_icon(priv, photo, focus_photo);
706 if (priv->user_type == USER_ITEM_ADMIN &&
707 priv->di->type != USER_ITEM_ADMIN)
710 elm_object_part_text_set(priv->base, PART_USER_EDIT_TITLE,
712 _change_entry(priv->name, priv->ly, EINA_TRUE, "", SIG_ENABLE);
713 if (priv->selected_photo)
714 _load_user_icon(priv, priv->selected_photo,
715 priv->selected_photo);
717 _load_user_icon(priv, IMAGE_USER_CURRENT_DEFAULT,
718 IMAGE_USER_CURRENT_DEFAULT_FOCUS);
721 evas_object_show(priv->base);
722 elm_object_focus_set(priv->cancel, EINA_TRUE);
725 static void _hide(void *data)
730 _ERR("Invalid argument");
736 inputmgr_remove_callback(priv->del, &handler);
737 evas_object_del(priv->del);
741 priv->selected_photo = NULL;
742 evas_object_hide(priv->base);
745 static void _destroy(void *data)
750 _ERR("Invalid argument");
756 inputmgr_remove_callback(priv->photo, &handler);
757 inputmgr_remove_callback(priv->done, &handler);
758 inputmgr_remove_callback(priv->cancel, &handler);
759 viewmgr_remove_view(VIEW_PHOTO);
760 evas_object_del(priv->base);
764 static void _update(void *view_data, int update_type, void *data)
769 _ERR("Invalid argument");
775 switch (update_type) {
776 case UPDATE_TYPE_EDIT:
779 case UPDATE_TYPE_PHOTO:
780 priv->selected_photo = data;
782 case UPDATE_TYPE_USER:
783 priv->user_type = (enum user_item_type) data;
786 _ERR("Invalid type");
791 static view_class vclass = {
792 .view_id = VIEW_USER_EDIT,
800 view_class *view_user_edit_get_vclass(void)