set disable action menu item when there is no changed item
[profile/tv/apps/native/air_home.git] / src / view / view_action_menu.c
index b392b13..6e4232b 100644 (file)
 #include "utils.h"
 
 #define ACTION_MENU_TITLE_RESET "Reset"
+#define COUNT_MENU_ITEM 4
 
 enum input_handler_type {
-       INPUT_HANDLER_BTN,
-       INPUT_HANDLER_ITEM
+       INPUT_HANDLER_TYPE_BTN,
+       INPUT_HANDLER_TYPE_ITEM_RESET,
+       INPUT_HANDLER_TYPE_RESET,
+       INPUT_HANDLER_TYPE_DISCARD,
+       INPUT_HANDLER_TYPE_CANCEL
 };
 
 struct _priv {
        Evas_Object *win;
        Evas_Object *base;
        Evas_Object *btn;
-       Evas_Object *item;
+       Evas_Object *popup;
+       Evas_Object *box;
+       Eina_List *list;
+
+       bool edited;
+};
+
+struct _btn_info {
+       char *icon;
+       char *focus_icon;
+       char *dis_icon;
+       char *title;
+       int handler_id;
 };
 
 static void _focused(int id, void *data, Evas_Object *obj,
@@ -49,14 +65,101 @@ static void _unfocused(int id, void *data, Evas_Object *obj,
        elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
 }
 
+static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
+               Evas_Event_Key_Down *ev);
+
+static void _mouse_down(int id, void *data, Evas *e, Evas_Object *obj,
+               Evas_Event_Mouse_Down *ev);
+
+static void _mouse_move(int id, void *data, Evas *e, Evas_Object *obj,
+               Evas_Event_Mouse_Move *ev)
+{
+       if (!elm_object_focus_get(obj))
+               elm_object_focus_set(obj, EINA_TRUE);
+}
+
+static input_handler handler = {
+       .focused = _focused,
+       .unfocused = _unfocused,
+       .key_down = _key_down,
+       .mouse_down = _mouse_down,
+       .mouse_move = _mouse_move
+};
+
+static void _add_popup(struct _priv *priv, char *title, char *message,
+               char *first_btn_message, int first_btn_id,
+               char *second_btn_message, int second_btn_id)
+{
+       Evas_Object *popup, *first_btn, *second_btn;
+
+       popup = utils_add_popup(priv->base, title, message);
+       if (!popup) {
+               _ERR("failed to add popup");
+               return;
+       }
+
+       first_btn = utils_add_button(popup, first_btn_message,
+                       PART_POPUP_BUTTON_1);
+       if (!first_btn) {
+               _ERR("failed to add delete btn");
+               evas_object_del(popup);
+               return;
+       }
+       inputmgr_add_callback(first_btn, first_btn_id, &handler, priv);
+
+       second_btn = utils_add_button(popup, second_btn_message,
+                       PART_POPUP_BUTTON_2);
+       if (!second_btn) {
+               _ERR("failed to add cancel btn");
+               evas_object_del(popup);
+               return;
+       }
+       inputmgr_add_callback(second_btn, second_btn_id, &handler, priv);
+       elm_object_focus_set(second_btn, EINA_TRUE);
+
+       priv->popup = popup;
+}
+
+static void _delete_popup(struct _priv *priv, Evas_Object *obj)
+{
+       inputmgr_remove_callback(obj, &handler);
+       evas_object_del(priv->popup);
+       priv->popup = NULL;
+}
+
 static void _select(int id, void *data, Evas_Object *obj)
 {
-       /* It should be implemented later */
+       struct _priv *priv;
+
+       priv = data;
 
        switch (id) {
-       case INPUT_HANDLER_BTN:
+       case INPUT_HANDLER_TYPE_BTN:
+               if (priv->edited) {
+                       _add_popup(priv, MESSAGE_DISCARD,
+                                       MESSAGE_DISCARD_CONTENTS,
+                                       MESSAGE_DISCARD,
+                                       INPUT_HANDLER_TYPE_DISCARD,
+                                       MESSAGE_CANCEL,
+                                       INPUT_HANDLER_TYPE_CANCEL);
+                       return;
+               }
+               viewmgr_update_view(VIEW_USER_EDIT, UPDATE_TYPE_RESET, NULL);
+               viewmgr_hide_view(VIEW_ACTION_MENU);
+               break;
+       case INPUT_HANDLER_TYPE_ITEM_RESET:
+               _add_popup(priv, MESSAGE_RESET, MESSAGE_RESET_CONTENTS,
+                               MESSAGE_RESET, INPUT_HANDLER_TYPE_RESET,
+                               MESSAGE_CANCEL, INPUT_HANDLER_TYPE_CANCEL);
                break;
-       case INPUT_HANDLER_ITEM:
+       case INPUT_HANDLER_TYPE_RESET:
+       case INPUT_HANDLER_TYPE_DISCARD:
+               viewmgr_update_view(VIEW_USER_EDIT, UPDATE_TYPE_RESET, NULL);
+               viewmgr_hide_view(VIEW_ACTION_MENU);
+               _delete_popup(priv, obj);
+               break;
+       case INPUT_HANDLER_TYPE_CANCEL:
+               _delete_popup(priv, obj);
                break;
        default:
                return;
@@ -66,9 +169,30 @@ static void _select(int id, void *data, Evas_Object *obj)
 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
                Evas_Event_Key_Down *ev)
 {
+       struct _priv *priv;
+
+       if (!data) {
+               _ERR("Invalid argument");
+               return;
+       }
+
+       priv = data;
        if (!strcmp(ev->keyname, KEY_BACK) ||
                        !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
-               viewmgr_hide_view(VIEW_ACTION_MENU);
+               switch (id) {
+               case INPUT_HANDLER_TYPE_BTN:
+               case INPUT_HANDLER_TYPE_ITEM_RESET:
+                       viewmgr_hide_view(VIEW_ACTION_MENU);
+                       break;
+               case INPUT_HANDLER_TYPE_RESET:
+               case INPUT_HANDLER_TYPE_DISCARD:
+               case INPUT_HANDLER_TYPE_CANCEL:
+                       if (priv->popup)
+                               _delete_popup(priv, obj);
+                       break;
+               default:
+                       return;
+               }
        } else if (!strcmp(ev->keyname, KEY_ENTER) ||
                        !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
                _select(id, data, obj);
@@ -78,75 +202,17 @@ static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
 static void _mouse_down(int id, void *data, Evas *e, Evas_Object *obj,
                Evas_Event_Mouse_Down *ev)
 {
-       _select(id, data, obj);
-}
-
-static void _mouse_move(int id, void *data, Evas *e, Evas_Object *obj,
-               Evas_Event_Mouse_Move *ev)
-{
-       if (!elm_object_focus_get(obj))
-               elm_object_focus_set(obj, EINA_TRUE);
-}
-
-static input_handler handler = {
-       .focused = _focused,
-       .unfocused = _unfocused,
-       .key_down = _key_down,
-       .mouse_down = _mouse_down,
-       .mouse_move = _mouse_move
-};
-
-static Evas_Object *_add_item(Evas_Object *box, struct _priv *priv)
-{
-       Evas_Object *item, *ic, *focus_ic, *lbl, *focus_lbl, *bg;
-
-       item = utils_add_layout(box, GRP_ACTION_MENU_TABLE_ITEM, true, NULL);
-       if (!item) {
-               _ERR("failed to add item");
-               evas_object_del(box);
-               return false;
+       if (!data || !obj) {
+               _ERR("Invalid argument");
+               return;
        }
 
-       ic = utils_add_icon(item, IMAGE_ACTION_MENU_ICON_RESET,
-                       PART_ACTION_MENU_TABLE_ITEM_ICON);
-       if (!ic)
-               goto err;
-
-       focus_ic = utils_add_icon(item, IMAGE_ACTION_MENU_ICON_RESET_FOCUS,
-                       PART_ACTION_MENU_TABLE_ITEM_ICON_FOCUS);
-       if (!focus_ic)
-               goto err;
-
-       lbl = utils_add_label(item, ACTION_MENU_TITLE_RESET,
-                       STYLE_LABEL_ACTION_MENU_TITLE,
-                       PART_ACTION_MENU_TABLE_ITEM_TEXT);
-       if (!lbl)
-               goto err;
-
-       focus_lbl = utils_add_label(item, ACTION_MENU_TITLE_RESET,
-                       STYLE_LABEL_ACTION_MENU_TITLE_FOCUS,
-                       PART_ACTION_MENU_TABLE_ITEM_TEXT_FOCUS);
-       if (!focus_lbl)
-               goto err;
-
-       bg = utils_add_bg(item, COLOR_DEFAULT_R, COLOR_DEFAULT_G,
-                       COLOR_DEFAULT_B, COLOR_DEFAULT_A,
-                       PART_ACTION_MENU_TABLE_ITEM_BG);
-       if (!bg)
-               goto err;
-
-       evas_object_show(item);
-       inputmgr_add_callback(item, INPUT_HANDLER_ITEM, &handler, priv);
-
-       return item;
-err:
-       evas_object_del(item);
-       return NULL;
+       _select(id, data, obj);
 }
 
-static bool _add_menu(struct _priv *priv)
+static bool _add_base(struct _priv *priv)
 {
-       Evas_Object *btn, *box, *item;
+       Evas_Object *btn, *box;
 
        btn = utils_add_layout(priv->base, GRP_ACTION_MENU_LIVETV, true,
                        PART_ACTION_MENU_LIVETV);
@@ -154,7 +220,7 @@ static bool _add_menu(struct _priv *priv)
                _ERR("failed to add btn");
                return false;
        }
-       inputmgr_add_callback(btn, INPUT_HANDLER_BTN, &handler, priv);
+       inputmgr_add_callback(btn, INPUT_HANDLER_TYPE_BTN, &handler, priv);
        priv->btn = btn;
 
        box = utils_add_box(priv->base, true);
@@ -163,17 +229,9 @@ static bool _add_menu(struct _priv *priv)
                return false;
        }
        elm_object_part_content_set(priv->base, PART_ACTION_MENU_TABLE, box);
+       elm_box_padding_set(box, 1, 0);
        elm_box_align_set(box, 0.0, 0.5);
-
-       item = _add_item(box, priv);
-       if (!item) {
-               _ERR("failed to add item");
-               evas_object_del(box);
-               return false;
-       }
-
-       elm_box_pack_start(box, item);
-       priv->item = item;
+       priv->box = box;
 
        return true;
 }
@@ -202,7 +260,7 @@ static Evas_Object *_create(Evas_Object *win, void *data)
        priv->win = win;
        priv->base = base;
 
-       if (!_add_menu(priv)) {
+       if (!_add_base(priv)) {
                _ERR("failed to add menu");
                free(priv);
                evas_object_del(base);
@@ -214,6 +272,98 @@ static Evas_Object *_create(Evas_Object *win, void *data)
        return base;
 }
 
+static void _add_empty_item(struct _priv *priv, int len)
+{
+       int i;
+       Evas_Object *item;
+
+       for (i = 0; i < len; i++) {
+               item = utils_add_layout(priv->box, GRP_ACTION_MENU_TABLE_ITEM,
+                               false, NULL);
+               if (!item) {
+                       _ERR("failed to add item");
+                       return;
+               }
+
+               evas_object_show(item);
+               elm_box_pack_end(priv->box, item);
+               priv->list = eina_list_append(priv->list, item);
+       }
+}
+
+static bool _add_item(struct _priv *priv)
+{
+       Evas_Object *item, *ic, *focus_ic, *lbl, *focus_lbl, *bg;
+       struct _btn_info btn_info[] = {
+               {
+                       IMAGE_ACTION_MENU_ICON_RESET,
+                       IMAGE_ACTION_MENU_ICON_RESET_FOCUS,
+                       IMAGE_ACTION_MENU_ICON_RESET_DIS,
+                       ACTION_MENU_TITLE_RESET,
+                       INPUT_HANDLER_TYPE_ITEM_RESET
+               }
+       };
+       int i;
+
+       elm_box_clear(priv->box);
+
+       for (i = 0; i < sizeof(btn_info) / sizeof(*btn_info); i++) {
+               item = utils_add_layout(priv->box, GRP_ACTION_MENU_TABLE_ITEM,
+                               priv->edited, NULL);
+               if (!item) {
+                       _ERR("failed to add item");
+                       return false;
+               }
+
+               ic = utils_add_icon(item, priv->edited ?
+                               btn_info[i].icon : btn_info[i].dis_icon,
+                               PART_ACTION_MENU_TABLE_ITEM_ICON);
+               if (!ic)
+                       goto err;
+
+               lbl = utils_add_label(item, btn_info[i].title,
+                               priv->edited ? STYLE_LABEL_ACTION_MENU_TITLE :
+                               STYLE_LABEL_ACTION_MENU_TITLE_DIS,
+                               PART_ACTION_MENU_TABLE_ITEM_TEXT);
+               if (!lbl)
+                       goto err;
+
+               if (priv->edited) {
+                       focus_ic = utils_add_icon(item, btn_info[i].focus_icon,
+                               PART_ACTION_MENU_TABLE_ITEM_ICON_FOCUS);
+                       if (!focus_ic)
+                               goto err;
+
+                       focus_lbl = utils_add_label(item, btn_info[i].title,
+                                       STYLE_LABEL_ACTION_MENU_TITLE_FOCUS,
+                                       PART_ACTION_MENU_TABLE_ITEM_TEXT_FOCUS);
+                       if (!focus_lbl)
+                               goto err;
+
+                       bg = utils_add_bg(item, COLOR_DEFAULT_R,
+                                       COLOR_DEFAULT_G, COLOR_DEFAULT_B,
+                                       COLOR_DEFAULT_A,
+                                       PART_ACTION_MENU_TABLE_ITEM_BG);
+                       if (!bg)
+                               goto err;
+
+                       inputmgr_add_callback(item, btn_info[i].handler_id,
+                                       &handler, priv);
+               }
+               evas_object_show(item);
+               elm_box_pack_end(priv->box, item);
+               priv->list = eina_list_append(priv->list, item);
+       }
+
+       _add_empty_item(priv, COUNT_MENU_ITEM -
+                       sizeof(btn_info) / sizeof(*btn_info));
+
+       return true;
+err:
+       evas_object_del(item);
+       return false;
+}
+
 static void _show(void *data)
 {
        struct _priv *priv;
@@ -225,6 +375,11 @@ static void _show(void *data)
 
        priv = data;
 
+       if (!_add_item(priv)) {
+               _ERR("failed to add item");
+               return;
+       }
+
        evas_object_show(priv->base);
 
        if (priv->btn)
@@ -234,6 +389,7 @@ static void _show(void *data)
 static void _hide(void *data)
 {
        struct _priv *priv;
+       Evas_Object *eo;
 
        if (!data) {
                _ERR("Invalid argument");
@@ -242,7 +398,14 @@ static void _hide(void *data)
 
        priv = data;
 
+       EINA_LIST_FREE(priv->list, eo) {
+               evas_object_del(eo);
+               inputmgr_remove_callback(eo, &handler);
+       }
+
        evas_object_hide(priv->base);
+       priv->list = NULL;
+       priv->edited = false;
 }
 
 static void _destroy(void *data)
@@ -256,16 +419,40 @@ static void _destroy(void *data)
 
        priv = data;
 
+       inputmgr_remove_callback(priv->btn, &handler);
+
        evas_object_del(priv->base);
        free(priv);
 }
 
+static void _update(void *view_data, int update_type, void *data)
+{
+       struct _priv *priv;
+
+       if (!view_data) {
+               _ERR("Invalid argument");
+               return;
+       }
+
+       priv = view_data;
+
+       switch (update_type) {
+       case UPDATE_TYPE_EDIT:
+               priv->edited = *(bool *)data;
+               break;
+       default:
+               _ERR("Invalid type");
+               return;
+       }
+}
+
 static view_class vclass = {
        .view_id = VIEW_ACTION_MENU,
        .create = _create,
        .show = _show,
        .hide = _hide,
        .destroy = _destroy,
+       .update = _update
 };
 
 view_class *view_action_menu_get_vclass(void)