fe948822df1f7628def412fc746dde14344dd140
[apps/native/menu-screen.git] / src / all_apps / layout.c
1 /*
2  * MENU-SCREEN
3  *
4  * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <Elementary.h>
21
22 #include "menu_screen.h"
23 #include "conf.h"
24 #include "index.h"
25 #include "item_event.h"
26 #include "layout.h"
27 #include "list.h"
28 #include "page_scroller.h"
29 #include "util.h"
30 #include "all_apps/layout.h"
31 #include "all_apps/list.h"
32 #include "all_apps/shortcut.h"
33
34 #define ALL_APPS_EDJE_PORTRAIT EDJEDIR"/all_apps_portrait.edj"
35 #define ALL_APPS_GROUP "all_apps"
36
37 #define GROUP_4X4_EDJE_PORTRAIT EDJEDIR"/group_4x4_portrait.edj";
38
39 #define ALL_APPS_EDIT_BUTTON_EDJE EDJEDIR"/button_edit.edj"
40 #define ALL_APPS_EDIT_BUTTON_GROUP "button_edit"
41 #define ALL_APPS_EDIT_BUTTON_PART "button_edit"
42
43 #define IDS_HS_BUTTON_DONE "IDS_HS_BUTTON_DONE"
44
45
46 HAPI void all_apps_layout_edit(Evas_Object *all_apps)
47 {
48         Evas_Object *edit_button;
49         Evas_Object *scroller;
50
51         edit_button = elm_object_part_content_get(all_apps, ALL_APPS_EDIT_BUTTON_PART);
52         ret_if(NULL == edit_button);
53         edje_object_signal_emit(_EDJ(edit_button), "edit_button,enable", "menu");
54         if (edje_object_part_text_set(_EDJ(edit_button), "edit_button_text", _(IDS_HS_BUTTON_DONE)) == EINA_FALSE) {
55                 _E("Failed to set text on the part, edje:%p, part:%s, text:%s", _EDJ(edit_button), "edit_button_text", _(IDS_HS_BUTTON_DONE));
56         }
57
58         scroller = elm_object_part_content_get(all_apps, "content");
59         ret_if(NULL == scroller);
60         page_scroller_edit(scroller);
61 }
62
63
64
65 HAPI void all_apps_layout_unedit(Evas_Object *all_apps)
66 {
67         Evas_Object *edit_button;
68
69         ret_if(NULL == all_apps);
70
71         do {
72                 edit_button = elm_object_part_content_get(all_apps, ALL_APPS_EDIT_BUTTON_PART);
73                 ret_if(NULL == edit_button);
74
75                 edje_object_signal_emit(_EDJ(edit_button), "edit_button,disable", "menu");
76                 if (edje_object_part_text_set(_EDJ(edit_button), "edit_button_text", "") == EINA_FALSE) {
77                         _E("Failed to set text on the part, edje:%p, part:%s, text:(null)", _EDJ(edit_button), "edit_button_text");
78                 }
79         } while (0);
80
81         do {
82                 Evas_Object *scroller;
83
84                 scroller = elm_object_part_content_get(all_apps, "content");
85                 ret_if(NULL == scroller);
86
87                 page_scroller_unedit(scroller);
88         } while (0);
89 }
90
91
92
93 static void _press_edit_button(Evas_Object *edje)
94 {
95         edje_object_signal_emit(edje, "edit_button,enable,press", "menu");
96 }
97
98
99
100 static void _release_edit_button(Evas_Object *edje)
101 {
102         edje_object_signal_emit(edje, "edit_button,enable,release", "menu");
103 }
104
105
106
107 static void _edit_button_down_cb(void *data, Evas_Object *obj, const char* emission, const char* source)
108 {
109         Evas_Object *scroller = data;
110
111         _D("Edit button is down");
112
113         if (page_scroller_is_edited(scroller)) {
114                 _press_edit_button(obj);
115         }
116 }
117
118
119
120 static void _edit_button_up_cb(void *data, Evas_Object *obj, const char* emission, const char* source)
121 {
122         Evas_Object *scroller = data;
123         Evas_Object *all_apps;
124
125         _D("Edit button is up");
126
127         all_apps = evas_object_data_get(scroller, "tab");
128         if (page_scroller_is_edited(scroller)) {
129                 Evas_Object *eo;
130                 bool pressed = false;
131
132                 eo = evas_object_data_get(obj, "evas_object");
133                 pressed = (bool) evas_object_data_get(eo, "pressed");
134                 if (pressed) {
135                         all_apps_layout_unedit(all_apps);
136                 } else {
137                         _release_edit_button(obj);
138                 }
139         } else {
140                 all_apps_layout_edit(all_apps);
141         }
142 }
143
144
145
146 static void _button_out_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
147 {
148         _D("Edit button is out");
149         evas_object_data_set(obj, "pressed", (void *) false);
150 }
151
152
153
154 static void _button_in_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
155 {
156         _D("Edit button is in");
157         evas_object_data_set(obj, "pressed", (void *) true);
158 }
159
160
161
162 static Evas_Object *_add_edit_button(Evas_Object *parent, Evas_Object *scroller)
163 {
164         Evas_Object *button;
165
166         retv_if(NULL == parent, NULL);
167         retv_if(NULL == scroller, NULL);
168
169         button = layout_load_edj(parent, ALL_APPS_EDIT_BUTTON_EDJE, ALL_APPS_EDIT_BUTTON_GROUP);
170         retv_if(NULL == button, NULL);
171
172         edje_object_signal_callback_add(_EDJ(button), "edit_button,down", "menu", _edit_button_down_cb, scroller);
173         edje_object_signal_callback_add(_EDJ(button), "edit_button,up", "menu", _edit_button_up_cb, scroller);
174         evas_object_event_callback_add(button, EVAS_CALLBACK_MOUSE_OUT, _button_out_cb, NULL);
175         evas_object_event_callback_add(button, EVAS_CALLBACK_MOUSE_IN, _button_in_cb, NULL);
176         evas_object_data_set(button, "pressed", (void *) false);
177
178         return button;
179 }
180
181
182
183 static void _remove_edit_button(Evas_Object *button)
184 {
185         ret_if(NULL == button);
186
187         edje_object_signal_callback_del(_EDJ(button), "edit_button,down", "menu", _edit_button_down_cb);
188         edje_object_signal_callback_del(_EDJ(button), "edit_button,up", "menu", _edit_button_up_cb);
189         evas_object_event_callback_del(button, EVAS_CALLBACK_MOUSE_OUT, _button_out_cb);
190         evas_object_event_callback_del(button, EVAS_CALLBACK_MOUSE_IN, _button_in_cb);
191         evas_object_data_del(button, "pressed");
192
193         layout_unload_edj(button);
194 }
195
196
197
198 static menu_screen_error_e _load_item(Evas_Object *scroller, app_list_item *item)
199 {
200         app_info_t *ai;
201
202         retv_if(NULL == item, MENU_SCREEN_ERROR_FAIL);
203         ai = item->data;
204         if (ai->nodisplay || !ai->enabled) {
205                 return MENU_SCREEN_ERROR_OK;
206         }
207
208         retv_if(NULL == page_scroller_push_item(scroller, ai), MENU_SCREEN_ERROR_FAIL);
209
210         return MENU_SCREEN_ERROR_OK;
211
212 }
213
214
215
216 static Eina_Bool _push_items_idler_cb(void *all_apps)
217 {
218         bool flag = true;
219         app_list *list;
220         app_list_item *item;
221         Evas_Object *scroller;
222
223         list = evas_object_data_get(all_apps, "list");
224         scroller = evas_object_data_get(all_apps, "scroller");
225
226         retv_if(NULL == list, ECORE_CALLBACK_CANCEL);
227         goto_if(NULL == scroller, ERROR);
228         goto_if(MENU_SCREEN_ERROR_OK != list_is_ended(list, &flag), ERROR);
229         goto_if(flag == true, ERROR);
230         goto_if(MENU_SCREEN_ERROR_OK != list_get_item(list, &item), ERROR);
231         goto_if(MENU_SCREEN_ERROR_OK != _load_item(scroller, item), ERROR);
232         goto_if(MENU_SCREEN_ERROR_OK != list_next(list), ERROR);
233
234         return ECORE_CALLBACK_RENEW;
235
236 ERROR:
237         all_apps_list_destroy(list);
238         evas_object_data_del(all_apps, "list");
239         page_scroller_bring_in(scroller, 0);
240         menu_screen_set_done(true);
241
242         return ECORE_CALLBACK_CANCEL;
243 }
244
245
246
247 static menu_screen_error_e _push_items(Evas_Object *all_apps)
248 {
249         app_list *list;
250         Ecore_Idler *idle_timer;
251
252         list = all_apps_list_create();
253         retv_if(MENU_SCREEN_ERROR_OK != list_first(list), MENU_SCREEN_ERROR_FAIL);
254
255         evas_object_data_set(all_apps, "list", list);
256         idle_timer = ecore_idler_add(_push_items_idler_cb, all_apps);
257         retv_if(NULL == idle_timer, MENU_SCREEN_ERROR_FAIL);
258
259         Evas_Object *scroller;
260         scroller = evas_object_data_get(all_apps, "scroller");
261         if (MENU_SCREEN_ERROR_OK != all_apps_shortcut_add_all(scroller))
262                 _E("Cannot add shortcuts");
263
264         do {
265                 Evas_Object *button;
266                 button = _add_edit_button(all_apps, scroller);
267                 if (NULL == button) {
268                         _D("cannot make the edit button");
269                 }
270                 elm_object_part_content_set(all_apps, ALL_APPS_EDIT_BUTTON_PART, button);
271         } while (0);
272
273         return MENU_SCREEN_ERROR_OK;
274 }
275
276
277
278 HAPI Evas_Object *all_apps_layout_create(Evas_Object *controlbar, int rotate)
279 {
280         Evas_Object *all_apps;
281         Evas_Object *index;
282         Evas_Object *scroller;
283
284         static const char item_edje[] = EDJEDIR"/item_4x4.edj";
285         static const char *page_edje;
286         static const char *all_apps_edje;
287         int item_width, item_height;
288         int width;
289         int height;
290
291         all_apps = elm_layout_add(controlbar);
292         retv_if(NULL == all_apps, NULL);
293
294         all_apps_edje = ALL_APPS_EDJE_PORTRAIT;
295         page_edje = GROUP_4X4_EDJE_PORTRAIT;
296         width = menu_screen_get_root_width();
297         height = menu_screen_get_root_height() - INDEX_HEIGHT;
298
299         item_width = ITEM_WIDTH_PROP * menu_screen_get_root_width();
300         item_height = ITEM_HEIGHT_PROP * menu_screen_get_root_height();
301
302         if (EINA_FALSE == elm_layout_file_set(all_apps, all_apps_edje, ALL_APPS_GROUP)) {
303                 evas_object_del(all_apps);
304                 _E("cannot load layout");
305                 return NULL;
306         }
307
308         evas_object_data_set(all_apps, "win", evas_object_data_get(controlbar, "win"));
309         evas_object_data_set(all_apps, "layout", evas_object_data_get(controlbar, "layout"));
310         evas_object_data_set(all_apps, "controlbar", controlbar);
311         evas_object_data_set(all_apps, "page_edje", (void *) page_edje);
312         evas_object_data_set(all_apps, "page_max_app", (void *) PAGE_MAX_APP);
313         evas_object_data_set(all_apps, "item_edje", (void *) item_edje);
314         evas_object_data_set(all_apps, "item_width", (void *) item_width);
315         evas_object_data_set(all_apps, "item_height", (void *) item_height);
316         evas_object_data_set(all_apps, "item_enable_long_press", (void *) true);
317         evas_object_data_set(all_apps, "item_text_dual_line", (void *) true);
318         evas_object_data_set(all_apps, "is_under_pkgmgr", (void *) true);
319         evas_object_data_set(all_apps, "rotate", (void *) rotate);
320         evas_object_data_set(all_apps, "enable_bg_image", (void *) true);
321         evas_object_size_hint_min_set(all_apps, width, height);
322         evas_object_size_hint_max_set(all_apps, width, height);
323         evas_object_resize(all_apps, width, height);
324
325         index = index_create(all_apps, 0,0);
326         if (NULL == index) {
327                 evas_object_del(all_apps);
328                 _E("cannot create index.");
329                 return NULL;
330         }
331         elm_object_part_content_set(all_apps, "index", index);
332         evas_object_data_set(all_apps, "index", index);
333
334         scroller = page_scroller_create(all_apps, index, PAGE_SCROLLER_SORT_BY_PACKAGE, rotate);
335         if (NULL == scroller) {
336                 evas_object_del(all_apps);
337                 index_destroy(index);
338                 _E("cannot create scroller.");
339                 return NULL;
340         }
341
342         elm_object_part_content_set(all_apps, "content", scroller);
343         evas_object_data_set(all_apps, "scroller", scroller);
344         if (!all_apps_shortcut_init(all_apps)) _E("Cannot initialize shortcut");
345         retv_if(MENU_SCREEN_ERROR_FAIL == _push_items(all_apps), NULL);
346
347         return all_apps;
348 }
349
350
351
352 HAPI void all_apps_layout_destroy(Evas_Object *all_apps)
353 {
354         Evas_Object *index;
355         Evas_Object *scroller;
356
357         ret_if(NULL == all_apps);
358
359         all_apps_shortcut_fini();
360
361         do {
362                 Evas_Object *button;
363                 if(all_apps) {
364                         button = elm_object_part_content_unset(all_apps, ALL_APPS_EDIT_BUTTON_PART);
365                         _remove_edit_button(button);
366                 } else {
367                         _D("cannot find the all_apps object");
368                 }
369         } while (0);
370
371         index = evas_object_data_get(all_apps, "index");
372         scroller = evas_object_data_get(all_apps, "scroller");
373
374         if (scroller) page_scroller_destroy(scroller);
375         if (index) index_destroy(index);
376
377         evas_object_data_del(all_apps, "win");
378         evas_object_data_del(all_apps, "layout");
379         evas_object_data_del(all_apps, "controlbar");
380         evas_object_data_del(all_apps, "index");
381         evas_object_data_del(all_apps, "scroller");
382         evas_object_data_del(all_apps, "page_edje");
383         evas_object_data_del(all_apps, "page_max_app");
384         evas_object_data_del(all_apps, "item_edje");
385         evas_object_data_del(all_apps, "item_width");
386         evas_object_data_del(all_apps, "item_height");
387         evas_object_data_del(all_apps, "item_enable_long_press");
388         evas_object_data_del(all_apps, "item_text_dual_line");
389         evas_object_data_del(all_apps, "is_under_pkgmgr");
390         evas_object_data_del(all_apps, "rotate");
391         evas_object_data_del(all_apps, "enable_bg_image");
392
393         evas_object_del(all_apps);
394 }
395
396
397
398 // End of a file