b007ef7d81e3e84b137dbab888983919ec05c0dc
[profile/tv/apps/native/air_mediahub.git] / src / view / base.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 <inputmgr.h>
20 #include <viewmgr.h>
21 #include <layoutmgr.h>
22
23 #include "define.h"
24 #include "layout.h"
25
26 #define TITLE_TEXT "Media Hub"
27
28 #define MENU_BTN_SIZE 3
29
30 struct _priv {
31         Evas_Object *win;
32         Evas_Object *base;
33         Evas_Object *btn[MENU_BTN_SIZE];
34
35         layoutmgr *lmgr;
36
37         int current_layout;
38 };
39
40 struct _menu_item {
41         const char *name;
42         const char *layout_id;
43 };
44
45 static struct _menu_item g_menu_item[MENU_BTN_SIZE] = {
46         {
47                 "Movie",
48                 LAYOUT_MOVIE
49         },
50         {
51                 "Gallery",
52                 LAYOUT_GALLERY
53         },
54         {
55                 "Music",
56                 LAYOUT_MUSIC
57         }
58 };
59
60 static void _mouse_move_cb(int id, void *data, Evas *e,
61                         Evas_Object *obj, Evas_Event_Mouse_Move *ev)
62 {
63         if (!elm_object_focus_get(obj))
64                 elm_object_focus_set(obj, EINA_TRUE);
65 }
66
67 static void _focused_cb(int id, void *data, Evas_Object *obj,
68                         Elm_Object_Item *it)
69 {
70         struct _priv *priv;
71         int i;
72
73         if (!data) {
74                 _ERR("failed to get data");
75                 return;
76         }
77
78         priv = data;
79
80         for (i = 0; i < MENU_BTN_SIZE; i++) {
81                 if (priv->btn[i] == obj)
82                         break;
83         }
84
85         if (i == priv->current_layout || i == MENU_BTN_SIZE)
86                 return;
87
88         layoutmgr_hide_layout(priv->lmgr,
89                         g_menu_item[priv->current_layout].layout_id);
90         layoutmgr_show_layout(priv->lmgr, g_menu_item[i].layout_id);
91         layoutmgr_update_layout(priv->lmgr, g_menu_item[i].layout_id, 0, NULL);
92
93         priv->current_layout = i;
94 }
95
96 static input_handler handler = {
97         .mouse_move = _mouse_move_cb,
98         .focused = _focused_cb
99 };
100
101 static bool _draw_title(struct _priv *priv)
102 {
103         if (!priv)
104                 return false;
105
106         elm_object_part_text_set(priv->base, PART_TITLE, TITLE_TEXT);
107
108         return true;
109 }
110
111 static bool _draw_menu_btn(struct _priv *priv)
112 {
113         Evas_Object *box, *btn;
114         int i;
115
116         if (!priv)
117                 return false;
118
119         box = elm_box_add(priv->base);
120         if (!box) {
121                 _ERR("failed to create box object");
122                 return false;
123         }
124
125         elm_box_horizontal_set(box, EINA_TRUE);
126
127         evas_object_size_hint_weight_set(box,
128                         EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
129         evas_object_size_hint_align_set(box, EVAS_HINT_FILL, EVAS_HINT_FILL);
130
131         for (i = 0; i < MENU_BTN_SIZE; i++) {
132                 btn = elm_button_add(box);
133                 if (!btn) {
134                         _ERR("failed to create button object");
135                         return false;
136                 }
137
138                 elm_object_style_set(btn, STYLE_MENU_BTN);
139                 elm_object_text_set(btn, g_menu_item[i].name);
140
141                 elm_box_pack_end(box, btn);
142
143                 evas_object_show(btn);
144
145                 inputmgr_add_callback(btn, 0, &handler, priv);
146
147                 priv->btn[i] = btn;
148         }
149
150         evas_object_show(box);
151
152         elm_object_part_content_set(priv->base, PART_MENU_AREA, box);
153
154         elm_object_focus_set(priv->btn[0], EINA_TRUE);
155
156         elm_object_focus_next_object_set(priv->btn[MENU_BTN_SIZE - 1],
157                                 priv->btn[0], ELM_FOCUS_RIGHT);
158         elm_object_focus_next_object_set(priv->btn[0],
159                                 priv->btn[MENU_BTN_SIZE - 1], ELM_FOCUS_LEFT);
160
161         return true;
162 }
163
164 static bool _draw_items(struct _priv *priv)
165 {
166         if (!priv)
167                 return false;
168
169         if (!_draw_title(priv))
170                 return false;
171
172         if (!_draw_menu_btn(priv))
173                 return false;
174
175         return true;
176 }
177
178 static Evas_Object *_create(Evas_Object *win, void *data)
179 {
180         struct _priv *priv;
181         Evas_Object *base;
182         layoutmgr *lmgr;
183
184         if (!win) {
185                 _ERR("failed to get win object");
186                 return NULL;
187         }
188
189         priv = calloc(1, sizeof(*priv));
190         if (!priv) {
191                 _ERR("failed to allocate priv");
192                 return NULL;
193         }
194
195         base = elm_layout_add(win);
196         if (!base) {
197                 _ERR("failed to create base object");
198                 free(priv);
199                 return NULL;
200         }
201
202         elm_layout_file_set(base, EDJEFILE, GRP_BASE_VIEW);
203
204         evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
205         elm_win_resize_object_add(win, base);
206
207         lmgr = layoutmgr_create(base);
208         layoutmgr_add_layout(lmgr, layout_movie_get_lclass(), NULL);
209         layoutmgr_add_layout(lmgr, layout_gallery_get_lclass(), NULL);
210         layoutmgr_add_layout(lmgr, layout_music_get_lclass(), NULL);
211
212         priv->win = win;
213         priv->base = base;
214         priv->lmgr = lmgr;
215
216         if (!_draw_items(priv)) {
217                 _ERR("failed to draw items");
218                 free(priv);
219                 return NULL;
220         }
221
222         viewmgr_set_view_data(VIEW_BASE, priv);
223
224         return base;
225 }
226
227 static void _show(void *view_data)
228 {
229         struct _priv *priv;
230
231         if (!view_data) {
232                 _ERR("failed to get view data");
233                 return;
234         }
235
236         priv = view_data;
237
238         evas_object_show(priv->base);
239 }
240
241 static void _hide(void *view_data)
242 {
243         struct _priv *priv;
244
245         if (!view_data) {
246                 _ERR("failed to get view data");
247                 return;
248         }
249
250         priv = view_data;
251
252         evas_object_hide(priv->base);
253 }
254
255 static void _destroy(void *view_data)
256 {
257         struct _priv *priv;
258         int i;
259
260         if (!view_data) {
261                 _ERR("failed to get view data");
262                 return;
263         }
264
265         priv = view_data;
266
267         for (i = 0; i < MENU_BTN_SIZE; i++)
268                 inputmgr_remove_callback(priv->btn[i], &handler);
269
270         layoutmgr_remove_layout(priv->lmgr, LAYOUT_MOVIE);
271         layoutmgr_remove_layout(priv->lmgr, LAYOUT_GALLERY);
272         layoutmgr_remove_layout(priv->lmgr, LAYOUT_MUSIC);
273
274         layoutmgr_destroy(priv->lmgr);
275
276         evas_object_del(priv->base);
277
278         free(priv);
279 }
280
281 static view_class _vclass = {
282         .view_id = VIEW_BASE,
283         .create = _create,
284         .show = _show,
285         .hide = _hide,
286         .destroy = _destroy,
287 };
288
289 view_class *view_base_get_vclass(void)
290 {
291         return &_vclass;
292 }