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