Merge "add dependency with live-tv" into tizen
[profile/tv/apps/native/air_home.git] / src / view / view_action_menu.c
1 /*
2  * Copyright (c) 2015 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 <Elementary.h>
18 #include <app_debug.h>
19 #include <viewmgr.h>
20 #include <inputmgr.h>
21
22 #include "defs.h"
23 #include "view.h"
24 #include "utils.h"
25
26 #define ACTION_MENU_TITLE_RESET "Reset"
27
28 enum input_handler_type {
29         INPUT_HANDLER_BTN,
30         INPUT_HANDLER_ITEM
31 };
32
33 struct _priv {
34         Evas_Object *win;
35         Evas_Object *base;
36         Evas_Object *btn;
37         Evas_Object *item;
38 };
39
40 static void _focused(int id, void *data, Evas_Object *obj,
41                 Elm_Object_Item *item)
42 {
43         elm_object_signal_emit(obj, SIG_FOCUS, SRC_PROG);
44 }
45
46 static void _unfocused(int id, void *data, Evas_Object *obj,
47                 Elm_Object_Item *item)
48 {
49         elm_object_signal_emit(obj, SIG_UNFOCUS, SRC_PROG);
50 }
51
52 static void _select(int id, void *data, Evas_Object *obj)
53 {
54         /* It should be implemented later */
55
56         switch (id) {
57         case INPUT_HANDLER_BTN:
58                 break;
59         case INPUT_HANDLER_ITEM:
60                 break;
61         default:
62                 return;
63         }
64 }
65
66 static void _key_down(int id, void *data, Evas *e, Evas_Object *obj,
67                 Evas_Event_Key_Down *ev)
68 {
69         if (!strcmp(ev->keyname, KEY_BACK) ||
70                         !strcmp(ev->keyname, KEY_BACK_REMOTE)) {
71                 viewmgr_hide_view(VIEW_ACTION_MENU);
72         } else if (!strcmp(ev->keyname, KEY_ENTER) ||
73                         !strcmp(ev->keyname, KEY_ENTER_REMOTE)) {
74                 _select(id, data, obj);
75         }
76 }
77
78 static void _mouse_down(int id, void *data, Evas *e, Evas_Object *obj,
79                 Evas_Event_Mouse_Down *ev)
80 {
81         _select(id, data, obj);
82 }
83
84 static void _mouse_move(int id, void *data, Evas *e, Evas_Object *obj,
85                 Evas_Event_Mouse_Move *ev)
86 {
87         if (!elm_object_focus_get(obj))
88                 elm_object_focus_set(obj, EINA_TRUE);
89 }
90
91 static input_handler handler = {
92         .focused = _focused,
93         .unfocused = _unfocused,
94         .key_down = _key_down,
95         .mouse_down = _mouse_down,
96         .mouse_move = _mouse_move
97 };
98
99 static Evas_Object *_add_item(Evas_Object *box, struct _priv *priv)
100 {
101         Evas_Object *item, *ic, *focus_ic, *lbl, *focus_lbl, *bg;
102
103         item = utils_add_layout(box, GRP_ACTION_MENU_TABLE_ITEM, true, NULL);
104         if (!item) {
105                 _ERR("failed to add item");
106                 evas_object_del(box);
107                 return false;
108         }
109
110         ic = utils_add_icon(item, IMAGE_ACTION_MENU_ICON_RESET,
111                         PART_ACTION_MENU_TABLE_ITEM_ICON);
112         if (!ic)
113                 goto err;
114
115         focus_ic = utils_add_icon(item, IMAGE_ACTION_MENU_ICON_RESET_FOCUS,
116                         PART_ACTION_MENU_TABLE_ITEM_ICON_FOCUS);
117         if (!focus_ic)
118                 goto err;
119
120         lbl = utils_add_label(item, ACTION_MENU_TITLE_RESET,
121                         STYLE_LABEL_ACTION_MENU_TITLE,
122                         PART_ACTION_MENU_TABLE_ITEM_TEXT);
123         if (!lbl)
124                 goto err;
125
126         focus_lbl = utils_add_label(item, ACTION_MENU_TITLE_RESET,
127                         STYLE_LABEL_ACTION_MENU_TITLE_FOCUS,
128                         PART_ACTION_MENU_TABLE_ITEM_TEXT_FOCUS);
129         if (!focus_lbl)
130                 goto err;
131
132         bg = utils_add_bg(item, COLOR_DEFAULT_R, COLOR_DEFAULT_G,
133                         COLOR_DEFAULT_B, COLOR_DEFAULT_A,
134                         PART_ACTION_MENU_TABLE_ITEM_BG);
135         if (!bg)
136                 goto err;
137
138         evas_object_show(item);
139         inputmgr_add_callback(item, INPUT_HANDLER_ITEM, &handler, priv);
140
141         return item;
142 err:
143         evas_object_del(item);
144         return NULL;
145 }
146
147 static bool _add_menu(struct _priv *priv)
148 {
149         Evas_Object *btn, *box, *item;
150
151         btn = utils_add_layout(priv->base, GRP_ACTION_MENU_LIVETV, true,
152                         PART_ACTION_MENU_LIVETV);
153         if (!btn) {
154                 _ERR("failed to add btn");
155                 return false;
156         }
157         inputmgr_add_callback(btn, INPUT_HANDLER_BTN, &handler, priv);
158         priv->btn = btn;
159
160         box = utils_add_box(priv->base, true);
161         if (!box) {
162                 _ERR("failed to add box");
163                 return false;
164         }
165         elm_object_part_content_set(priv->base, PART_ACTION_MENU_TABLE, box);
166         elm_box_align_set(box, 0.0, 0.5);
167
168         item = _add_item(box, priv);
169         if (!item) {
170                 _ERR("failed to add item");
171                 evas_object_del(box);
172                 return false;
173         }
174
175         elm_box_pack_start(box, item);
176         priv->item = item;
177
178         return true;
179 }
180
181 static Evas_Object *_create(Evas_Object *win, void *data)
182 {
183         struct _priv *priv;
184         Evas_Object *base;
185
186         priv = calloc(1, sizeof(*priv));
187         if (!priv) {
188                 _ERR("failed to calloc priv");
189                 return NULL;
190         }
191
192         base = utils_add_layout(win, GRP_ACTION_MENU, false, NULL);
193         if (!base) {
194                 _ERR("failed to create base");
195                 free(priv);
196                 return NULL;
197         }
198         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
199                         EVAS_HINT_EXPAND);
200         elm_win_resize_object_add(win, base);
201
202         priv->win = win;
203         priv->base = base;
204
205         if (!_add_menu(priv)) {
206                 _ERR("failed to add menu");
207                 free(priv);
208                 evas_object_del(base);
209                 return NULL;
210         }
211
212         viewmgr_set_view_data(VIEW_ACTION_MENU, priv);
213
214         return base;
215 }
216
217 static void _show(void *data)
218 {
219         struct _priv *priv;
220
221         if (!data) {
222                 _ERR("Invalid argument");
223                 return;
224         }
225
226         priv = data;
227
228         evas_object_show(priv->base);
229
230         if (priv->btn)
231                 elm_object_focus_set(priv->btn, EINA_TRUE);
232 }
233
234 static void _hide(void *data)
235 {
236         struct _priv *priv;
237
238         if (!data) {
239                 _ERR("Invalid argument");
240                 return;
241         }
242
243         priv = data;
244
245         evas_object_hide(priv->base);
246 }
247
248 static void _destroy(void *data)
249 {
250         struct _priv *priv;
251
252         if (!data) {
253                 _ERR("Invalid argument");
254                 return;
255         }
256
257         priv = data;
258
259         evas_object_del(priv->base);
260         free(priv);
261 }
262
263 static view_class vclass = {
264         .view_id = VIEW_ACTION_MENU,
265         .create = _create,
266         .show = _show,
267         .hide = _hide,
268         .destroy = _destroy,
269 };
270
271 view_class *view_action_menu_get_vclass(void)
272 {
273         return &vclass;
274 }