The source code moved from the SPIN with license changed to Flora 1.1
[apps/native/home/homescreen-efl.git] / src / livebox / livebox_panel.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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
19 #include "util.h"
20 #include "homescreen-efl.h"
21 #include "page_scroller.h"
22 #include "option_menu.h"
23 #include "dynamic_index.h"
24 #include "data_model.h"
25
26 #include "layout.h"
27 #include "popup.h"
28 #include "layout.h"
29 #include "conf.h"
30
31 #include "livebox/livebox_panel.h"
32 #include "livebox/livebox_animator.h"
33 #include "livebox/grid_item_resize.h"
34 #include "livebox/grid_reposition.h"
35 #include "livebox/livebox_utils.h"
36 #include "livebox/livebox_widget.h"
37
38 static Evas_Object *__livebox_panel_create_page(Evas_Object *livebox_scroller);
39 static void __livebox_panel_freeze_scroller(void);
40 static bool __livebox_panel_is_page_empty(Evas_Object *page);
41
42 static void __livebox_panel_register_longpress_callbacks(Evas_Object *livebox_scroller,
43         Evas_Object *page_layout, Evas_Object *livebox);
44 static void __livebox_panel_scrolled_cb(void *data, Evas_Object *obj,
45         void *event_info);
46 static void __livebox_panel_page_changed_cb(void *data, Evas_Object *obj,
47         void *event_info);
48 static void __livebox_panel_mouse_down_cb(void *data, Evas *evas, Evas_Object *object,
49         void *event_info);
50 static void __livebox_panel_mouse_up_cb(void *data, Evas *evas, Evas_Object *object,
51         void *event_info);
52 static Eina_Bool __livebox_panel_longpress_cb(void *data);
53
54 static void __livebox_panel_update_edit_mode_layout(Evas_Object *livebox_scroller_layout);
55 static void __livebox_panel_add_page_clicked_cb(void *data, Evas_Object *obj,
56         const char *emission, const char *source);
57
58 static void __libebox_panel_toggle_selected_livebox(Evas_Object *livebox, Evas_Object *page);
59 static void __livebox_panel_set_remove_buttons_visiblity(Evas_Object *page, bool visible);
60 static void __livebox_panel_set_reposition_bg_visibility(bool visible);
61 static void __livebox_panel_del_cb(void *data, Evas_Object *obj,
62         const char *emission, const char *source);
63 static Evas_Object *__livebox_panel_get_widget_layout(Evas_Object *obj);
64
65 static void __livebox_panel_scroll_start_cb(void *data, Evas_Object *obj, void *info);
66 static Eina_Bool __livebox_panel_change_page_timer_start_cb(void *data);
67
68 static void __livebox_panel_page_count_changed_cb(void *data, Evas_Object *obj,
69         const char *emission, const char *source);
70 static void __livebox_panel_set_remove_button_visibility(Evas_Object *livebox,
71         bool visible);
72 static void __livebox_panel_hide_remove_button(Evas_Object *livebox);
73
74 static bool __livebox_panel_iterate_page_fill_cb(Tree_node_t *parent,
75         Tree_node_t *page_node, void *data);
76 static bool __livebox_panel_iterate_node_fill_cb(Tree_node_t *page_node,
77         Tree_node_t *node, void *data);
78
79 static void __livebox_panel_show_warning_popup(void);
80 static bool __livebox_panel_find_empty_space(Evas_Object *page,
81         int span_w, int span_h, int *pos_x, int *pos_y);
82
83 static int __livebox_panel_get_mouse_move_distance_square(int x, int y);
84
85 static struct {
86         Ecore_Timer *longpress_timer;
87         int origination_page_num;
88         Ecore_Timer *page_change_timer;
89         int page_change;
90 } livebox_reposition_info_s = {
91         .longpress_timer = NULL,
92         .origination_page_num = 0,
93         .page_change_timer = NULL,
94         .page_change = LIVEBOX_PAGE_NO_CHANGE,
95 };
96
97
98 static struct {
99         Evas_Object *livebox_page_scroller;
100         Evas_Object *livebox_page_box;
101         Evas_Object *add_page;
102         dynamic_index_t *dynamic_index;
103
104         Eina_List *livebox_page_list;
105
106         int pages_count;
107         int current_page;
108
109         bool edit_mode;
110
111         bool livebox_remove_button_state;
112
113         Eina_Bool is_resizing;
114         Eina_Bool is_longpress;
115
116         Evas_Object *item_moved;
117         Eina_Rectangle no_move_pos;
118         Evas_Point mouse_press_pos;
119
120 } s_info = {
121         .livebox_page_scroller = NULL,
122         .livebox_page_box = NULL,
123         .add_page = NULL,
124         .dynamic_index = NULL,
125
126         .livebox_page_list = NULL,
127
128         .pages_count = 0,
129         .current_page = 0,
130
131         .edit_mode = false,
132
133         .livebox_remove_button_state = false,
134         .is_resizing = false,
135
136         .is_longpress = false,
137
138         .item_moved = NULL,
139         .no_move_pos.x = 0,
140         .no_move_pos.y = 0,
141         .no_move_pos.w = 0,
142         .no_move_pos.h = 0,
143
144         .mouse_press_pos = {-1, -1},
145 };
146
147 void livebox_panel_update_dynamic_index(void)
148 {
149         if (!s_info.livebox_page_scroller) {
150                 LOGE("s_info.livebox_page_scroller == NULL");
151                 return;
152         }
153
154         elm_object_signal_emit(s_info.livebox_page_scroller,
155                 SIGNAL_SCROLLER_PAGE_COUNT_CHANGED, SIGNAL_SOURCE);
156 }
157
158 void livebox_panel_create_dynamic_index(void)
159 {
160         if (!s_info.livebox_page_scroller) {
161                 LOGE("s_info.livebox_page_scroller == NULL");
162                 return;
163         }
164
165
166         s_info.dynamic_index = dynamic_index_new(s_info.livebox_page_scroller);
167         if (!s_info.dynamic_index) {
168                 LOGE("s_info.dynamic_index == NULL");
169                 return;
170         }
171 }
172
173 void livebox_panel_fill(void)
174 {
175         Tree_node_t *home = NULL;
176         int widget_pos_h = 0;
177
178         home = data_model_get_home();
179         if (!home) {
180                 LOGE("home == NULL");
181                 return;
182         }
183
184         if (home->count == 0) {
185                 LOGW("home->count == NULL");
186                 return;
187         } else {
188                 LOGI("home->count == %d", home->count);
189         }
190
191         data_model_iterate_pages(home, __livebox_panel_iterate_page_fill_cb,
192                 __livebox_panel_iterate_node_fill_cb, &widget_pos_h);
193         livebox_panel_create_dynamic_index();
194 }
195
196 Evas_Object *livebox_panel_create(Evas_Object *parent)
197 {
198         Evas_Object *liveboxes = NULL;
199
200         if (!parent) {
201                 LOGE("parent == NULL");
202                 return NULL;
203         }
204
205         liveboxes = livebox_panel_create_scroller();
206         if (!liveboxes) {
207                 LOGE("liveboxes == NULL");
208                 return NULL;
209         }
210
211         livebox_widget_init();
212
213         return liveboxes;
214 }
215
216 void livebox_panel_set_content(Evas_Object *layout)
217 {
218         if(s_info.dynamic_index->box)
219                 elm_layout_content_set(layout, PART_INDEX, s_info.dynamic_index->box);
220 }
221
222 void livebox_panel_unset_content(Evas_Object *layout)
223 {
224         Evas_Object *dynamic_index = NULL;
225
226         dynamic_index = elm_object_part_content_unset(layout, PART_INDEX);
227
228         if (!dynamic_index) {
229                 LOGE("dyamic_index == NULL");
230                 return;
231         }
232
233         evas_object_hide(dynamic_index);
234 }
235
236
237 Evas_Object *livebox_scroller_get_page(void)
238 {
239         int current_page_number = page_scroller_get_current_page(
240                 s_info.livebox_page_scroller);
241         return page_scroller_get_page(s_info.livebox_page_scroller,
242                 current_page_number);
243 }
244
245 void livebox_panel_refresh_content_size(void)
246 {
247         Evas_Object *page_container = NULL;
248         Evas_Object *item = NULL;
249         Eina_List *it = NULL;
250         Eina_List *items_in_grid = NULL;
251         Evas_Coord container_x = -1;
252         Evas_Coord container_y = -1;
253         Evas_Coord container_width = -1;
254         Evas_Coord container_height = -1;
255         Evas_Coord x = -1, y = -1, w = -1, h = -1;
256
257         if (!s_info.livebox_page_scroller) {
258                 return;
259         }
260
261         page_container = page_scroller_get_page(
262                 s_info.livebox_page_scroller,
263                 page_scroller_get_current_page(s_info.livebox_page_scroller));
264
265         if (!page_container) {
266                 LOGE("page_container == NULL");
267                 return;
268         }
269
270         evas_object_geometry_get(s_info.livebox_page_scroller,
271                 &container_x, &container_y, &container_width,
272                 &container_height);
273
274         items_in_grid = livebox_utils_get_liveboxes_on_page_list(
275                 page_container);
276
277         EINA_LIST_FOREACH(items_in_grid, it, item) {
278                 if (!item) {
279                         LOGE("item == NULL");
280                         continue;
281                 }
282
283                 elm_grid_pack_get(item, &x, &y, &w, &h);
284                 evas_object_move(item,
285                         (x*(container_width-LIVEBOX_HORIZONTAL_PADDING) / 100) +
286                         LIVEBOX_HORIZONTAL_PADDING / 2 + container_x,
287                         container_height * y / 100 + container_y);
288                 evas_object_resize(item,
289                         (int)((float) w/100 * (container_width)) -
290                         (w * LIVEBOX_HORIZONTAL_PADDING)/100,
291                         (int)((float)h/100 * (container_height)) -
292                         (y*LIVEBOX_HORIZONTAL_PADDING)/100);
293         }
294 }
295
296 Evas_Object *livebox_panel_create_scroller(void)
297 {
298         Evas_Object *livebox_page_scroller = NULL;
299         Evas_Object *win = home_screen_get_win();
300         Evas_Object *box = NULL;
301         Eina_List   *lvb_page_list = NULL;
302
303         int p_count = 0;
304         int cur_page = 0;
305
306         if (!win) {
307                 LOGE("Window is NULL");
308                 return NULL;
309         }
310
311         livebox_page_scroller =
312                 page_scroller_create(home_screen_get_root_width(),
313                 LIVEBOX_PAGE_HEIGHT_REL*home_screen_get_root_height());
314         if (!livebox_page_scroller) {
315                 LOGE("Cannot create livebox page scroller");
316                 return NULL;
317         }
318
319         box = elm_object_content_get(livebox_page_scroller);
320         if (!box) {
321                 LOGE("Cannot get the box");
322                 evas_object_del(livebox_page_scroller);
323                 return NULL;
324         }
325
326         livebox_utils_create_shadow(livebox_page_scroller);
327         evas_object_smart_callback_add(livebox_page_scroller,
328                 "scroll,drag,start", __livebox_panel_scroll_start_cb, NULL);
329         elm_object_signal_callback_add(livebox_page_scroller,
330                 SIGNAL_SCROLLER_PAGE_COUNT_CHANGED, SIGNAL_LAYOUT_SOURCE,
331                 __livebox_panel_page_count_changed_cb, NULL);
332
333         /*After creation empty one empty space should be created.*/
334
335         s_info.livebox_page_scroller = livebox_page_scroller;
336         s_info.current_page = cur_page;
337         s_info.pages_count = p_count;
338         s_info.livebox_page_list = lvb_page_list;
339
340         return livebox_page_scroller;
341 }
342
343
344 void livebox_panel_destroy(void)
345 {
346         Eina_List *pages = s_info.livebox_page_list;
347
348         Evas_Object *livebox_scroller_page = NULL;
349
350         if (!pages) {
351                 LOGE("Pages pointer is NULL");
352                 return;
353         }
354
355         EINA_LIST_FREE(pages, livebox_scroller_page) {
356                 livebox_panel_destroy_page(livebox_scroller_page);
357         }
358
359         page_scroller_destroy(s_info.livebox_page_scroller);
360
361         livebox_widget_fini();
362 }
363
364 void livebox_panel_paused(void)
365 {
366         livebox_widget_viewer_paused();
367 }
368
369 void livebox_panel_resumed(void)
370 {
371         livebox_widget_viewer_resumed();
372 }
373
374 void livebox_panel_add_livebox(Tree_node_t *node, Evas_Object *page,
375         const char *livebox_pkgname, int pos_x, int pos_y,
376         int width, int height,
377         char *content_info)
378 {
379         Evas_Object *livebox = NULL;
380         Evas_Object *livebox_layout = NULL;
381         Evas_Object *grid = NULL;
382
383
384         livebox_layout = elm_layout_add(s_info.livebox_page_scroller);
385         if (!livebox_layout) {
386                 evas_object_del(page);
387                 LOGE("livebox_layout is not created");
388                 return;
389         }
390
391         if (!node) {
392                 LOGE("node == NULL");
393                 return;
394         }
395
396 #ifdef LIVEBOX_RESIZE_TEST
397         livebox = elm_image_add(livebox_layout);
398         elm_image_file_set(livebox, livebox_pkgname, NULL);
399 #else
400         livebox = livebox_widget_add(livebox_pkgname, livebox_layout, content_info);
401 #endif
402
403         if (!livebox) {
404                 LOGE("Livebox is not created");
405                 return;
406         }
407
408         node->data->layout = livebox_layout;
409         evas_object_data_set(livebox_layout, KEY_ICON_DATA, node);
410
411         elm_layout_file_set(livebox_layout, EDJE_LIVEBOX_LAYOUT_FILENAME,
412                 GROUP_LIVEBOX_LAYOUT);
413         elm_layout_content_set(livebox_layout, PART_LIVEBOX, livebox);
414         evas_object_size_hint_weight_set(livebox_layout, EVAS_HINT_EXPAND,
415                 EVAS_HINT_EXPAND);
416         evas_object_show(livebox_layout);
417         elm_layout_signal_callback_add(livebox_layout, SIGNAL_CLICKED,
418                 SIGNAL_REMOVE_SOURCE, __livebox_panel_del_cb, NULL);
419
420         grid = elm_object_part_content_get(page, SIGNAL_CONTENT_SOURCE);
421         if (!grid) {
422                 LOGE("grid == NULL");
423                 return;
424         }
425
426         elm_grid_pack(grid,
427                 livebox_layout, pos_x * LIVEBOX_GRID_ROWS_CELLS_MULTI,
428                 pos_y * LIVEBOX_GRID_ROWS_CELLS_MULTI,
429                 width * LIVEBOX_GRID_ROWS_CELLS_MULTI,
430                 height * LIVEBOX_GRID_ROWS_CELLS_MULTI);
431
432         __livebox_panel_register_longpress_callbacks(NULL, page, livebox_layout);
433
434         LOGD("Adding [%s] at: [%d, %d] <>[%d, %d]",
435                 livebox_pkgname, pos_x, pos_y, width, height);
436 }
437
438
439 Evas_Object *livebox_panel_add_page(Tree_node_t *node,
440         Evas_Object *livebox_scroller)
441 {
442         Evas_Object *page = NULL;
443
444         page = livebox_panel_create_empty_page(node, livebox_scroller);
445
446         if (!page) {
447                 LOGE("page == NULL");
448                 return NULL;
449         }
450
451         if (!page_scroller_add_page(livebox_scroller, page)) {
452                 LOGE("Can not create page");
453
454                 livebox_panel_destroy_page(page);
455                 return NULL;
456         }
457
458         /*update pages list*/
459         s_info.livebox_page_list = page_scroller_get_all_pages(
460                 livebox_scroller);
461         __livebox_panel_freeze_scroller();
462
463         return page;
464 }
465
466 bool livebox_panel_add_empty_page(Evas_Object *livebox_scroller)
467 {
468         /*@todo add check if page count will not be over the limit*/
469         Evas_Object *page = __livebox_panel_create_page(livebox_scroller);
470         return page_scroller_add_page(livebox_scroller, page);
471 }
472
473
474 Evas_Object *livebox_panel_create_empty_page(Tree_node_t *node,
475         Evas_Object *livebox_scroller)
476 {
477         Evas_Object *page = NULL;
478
479         if (!livebox_scroller) {
480                 LOGE("Livebox panel is NULL");
481                 return NULL;
482         }
483
484         if (!node) {
485                 LOGE("node == NULL");
486                 return NULL;
487         }
488
489         page = __livebox_panel_create_page(livebox_scroller);
490         if (!page) {
491                 LOGE("page == NULL");
492                 return NULL;
493         }
494
495         evas_object_data_set(page, KEY_ICON_DATA, node);
496
497         return page;
498 }
499
500
501 void livebox_panel_change_edit_mode_state(bool close)
502 {
503         Evas_Object *livebox_scroller = s_info.livebox_page_scroller;
504         if (!livebox_scroller) {
505                 LOGE("Livebox panel is NULL");
506                 return;
507         }
508
509         if (!s_info.edit_mode && close) {
510                 /*if edit mode is disabled closing doesn't take any effect*/
511                 return;
512         }
513
514         if (s_info.edit_mode && close) {
515                 s_info.edit_mode = false;
516                 /*closing edit mode*/
517                 __livebox_panel_update_edit_mode_layout(livebox_scroller);
518         } else if (!s_info.edit_mode) {
519                 s_info.edit_mode = true;
520                 __livebox_panel_update_edit_mode_layout(livebox_scroller);
521         }
522 }
523
524 void livebox_panel_destroy_page(Evas_Object *livebox_scroller_page)
525 {
526         Eina_List *lb_layouts_list = NULL;
527         Eina_List *l               = NULL;
528         Evas_Object *lb_layout     = NULL;
529         Evas_Object *widget        = NULL;
530         Tree_node_t *page_node = NULL;
531
532         if (!livebox_scroller_page) {
533                 LOGE("Livebox panel is NULL");
534                 return;
535         }
536
537         page_node = evas_object_data_get(livebox_scroller_page, KEY_ICON_DATA);
538         if (!page_node) {
539                 LOGE("page_node == NULL");
540                 return;
541         }
542
543         data_model_del_item(page_node);
544
545         lb_layouts_list = livebox_utils_get_liveboxes_on_page_list(
546                 livebox_scroller_page);
547         if (!lb_layouts_list) {
548                 LOGE("pages == NULL");
549                 evas_object_del(livebox_scroller_page);
550                 home_screen_print_tree();
551                 return;
552         }
553
554         EINA_LIST_FOREACH(lb_layouts_list, l, lb_layout) {
555                 widget = __livebox_panel_get_widget_layout(lb_layout);
556                 if (!widget) {
557                         LOGE("widget == NULL");
558                         continue;
559                 }
560
561                 livebox_widget_del(widget);
562                 evas_object_del(lb_layout);
563         }
564
565         eina_list_free(lb_layouts_list);
566         evas_object_del(livebox_scroller_page);
567
568         __livebox_panel_freeze_scroller();
569         home_screen_print_tree();
570 }
571
572 bool livebox_panel_is_add_page_selected(void)
573 {
574         int current_page_number = page_scroller_get_current_page(
575                 s_info.livebox_page_scroller);
576         Evas_Object *page_container = page_scroller_get_page(
577                 s_info.livebox_page_scroller, current_page_number);
578
579         return (page_container == s_info.add_page &&
580                 livebox_panel_check_if_add_page_is_visible());
581 }
582
583 void livebox_panel_move_mouse_cb(int x, int y)
584 {
585         int mouse_move_distance = __livebox_panel_get_mouse_move_distance_square(x, y);
586
587         if (mouse_move_distance > LIVEBOX_MIN_MOUSE_MOVE) {
588                 ecore_timer_del(livebox_reposition_info_s.longpress_timer);
589                 livebox_reposition_info_s.longpress_timer = NULL;
590
591                 if(!s_info.is_longpress)
592                         page_scroller_unfreeze(s_info.livebox_page_scroller);
593         }
594
595         if (s_info.is_longpress &&
596                 !livebox_reposition_info_s.page_change_timer) {
597                 if (x < LIVEBOX_PAGE_CHANGE_RECT_SIZE) {
598                         livebox_reposition_info_s.page_change_timer =
599                         ecore_timer_add(LIVEBOX_PAGE_CHANGE_PERIOD,
600                                 __livebox_panel_change_page_timer_start_cb, (void *)false);
601                         LOGI("PAGE CHANGE TIMER CREATED <LEFT>");
602                 } else if (x > home_screen_get_root_width() -
603                         LIVEBOX_PAGE_CHANGE_RECT_SIZE) {
604                         livebox_reposition_info_s.page_change_timer =
605                                 ecore_timer_add(LIVEBOX_PAGE_CHANGE_PERIOD,
606                                         __livebox_panel_change_page_timer_start_cb, (void *)true);
607                         LOGI("PAGE CHANGE TIMER CREATED <RIGHT>");
608                 }
609
610                 grid_reposition_move(x, y);
611         } else if (livebox_reposition_info_s.page_change_timer &&
612                         x > LIVEBOX_PAGE_CHANGE_RECT_SIZE &&
613                         x < home_screen_get_root_width() -
614                         LIVEBOX_PAGE_CHANGE_RECT_SIZE) {
615                 ecore_timer_del(livebox_reposition_info_s.page_change_timer);
616                 livebox_reposition_info_s.page_change_timer = NULL;
617                 LOGI("PAGE CHANGE TIMER DESTROYED");
618
619                 grid_reposition_move(x, y);
620         }
621 }
622
623 Eina_List *livebox_panel_get_pages(void)
624 {
625         return s_info.livebox_page_list;
626 }
627
628 Evas_Object *livebox_panel_get(void)
629 {
630         return s_info.livebox_page_scroller;
631 }
632
633 Evas_Object *livebox_panel_get_index(void)
634 {
635         if (!s_info.dynamic_index) {
636                 LOGE("s_info.dynamic_index == NULL");
637                 return NULL;
638         }
639
640         if (!s_info.dynamic_index->box) {
641                 LOGE("s_info.dynamic_index->box == NULL");
642                 return NULL;
643         }
644
645         return s_info.dynamic_index->box;
646 }
647
648 bool livebox_panel_check_if_add_page_is_visible(void)
649 {
650         Evas_Object *lvb_scorller = NULL;
651         Evas_Object *page = NULL;
652         Evas_Object *page_edje = NULL;
653
654         int p_count = -1;
655         const char *state = NULL;
656
657         lvb_scorller = s_info.livebox_page_scroller;
658         if (!lvb_scorller) {
659                 LOGE("Faield to get livebox scroller");
660                 return false;
661         }
662
663         p_count = page_scroller_get_page_count(lvb_scorller);
664         if (p_count < 0) {
665                 LOGE("Failed to get page count");
666                 return false;
667         }
668
669         LOGD("p_count is %d ", p_count);
670
671         page = page_scroller_get_page(lvb_scorller, --p_count);
672         if (!page) {
673                 LOGE("Failed to get page");
674                 return false;
675         }
676
677         page_edje = elm_layout_edje_get(page);
678         if (!page_edje) {
679                 LOGE("Failed to get page edje");
680                 return false;
681         }
682
683         state = edje_object_part_state_get(page_edje,
684                 PART_ADD_PAGE_BUTTON, NULL);
685
686         if (!state) {
687                 LOGE("Failed to get state");
688                 return false;
689         }
690
691         LOGD("state is : %s ", state);
692
693         return strncmp(state, "default", LIVEBOX_MAX_STATE_LEN);
694 }
695
696 void livebox_panel_set_edit_mode_layout(bool turn_on)
697 {
698         Evas_Object *lvb_scorller = NULL;
699         Evas_Object *page = NULL;
700         char *signal_to_send = NULL;
701         Eina_List *all_pages = NULL;
702         Eina_List *it = NULL;
703
704         lvb_scorller = s_info.livebox_page_scroller;
705         if (!lvb_scorller) {
706                 LOGE("Failed to get livebox scroller");
707                 return;
708         }
709
710         all_pages = page_scroller_get_all_pages(lvb_scorller);
711         if (!all_pages) {
712                 LOGE("Failed to get page");
713                 return;
714         }
715
716         if (turn_on) {
717                 signal_to_send = SIGNAL_EDIT_START;
718         } else {
719                 signal_to_send = SIGNAL_EDIT_STOP;
720         }
721
722         EINA_LIST_FOREACH(all_pages, it, page) {
723                 if (!page) {
724                         LOGE("page == NULL");
725                         continue;
726                 }
727
728                 elm_object_signal_emit(page, signal_to_send,
729                         SIGNAL_LAYOUT_SOURCE);
730         }
731 }
732
733 void livebox_panel_add_page_visible_state_set(bool visible)
734 {
735         Evas_Object *page = NULL;
736
737         page = livebox_scroller_get_last_page();
738         if (!page) {
739                 LOGE("page == NULL");
740                 return;
741         }
742
743         if (visible) {
744                 s_info.add_page = page;
745
746                 elm_object_signal_emit(page, SIGNAL_ADD_SHOW,
747                         SIGNAL_LAYOUT_SOURCE);
748                 elm_object_signal_emit(page, SIGNAL_EDIT_START,
749                         SIGNAL_LAYOUT_SOURCE);
750         } else {
751                 elm_object_signal_emit(page, SIGNAL_ADD_HIDE,
752                         SIGNAL_LAYOUT_SOURCE);
753         }
754 }
755
756 bool livebox_panel_can_add_page(void)
757 {
758         int count = 0;
759         Eina_List *pages = page_scroller_get_all_pages(livebox_panel_get());
760         count = eina_list_count(pages);
761
762
763         if (count < LIVEBOX_SCROLLER_MAX_PAGES) {
764                 return true;
765         } else if (count == LIVEBOX_SCROLLER_MAX_PAGES &&
766                 livebox_panel_check_if_add_page_is_visible()) {
767                 return true;
768         }
769
770         __livebox_panel_show_warning_popup();
771         return false;
772
773 }
774
775 int livebox_panel_find_empty_space(int span_w, int span_h,
776         int *pos_x, int *pos_y)
777 {
778         Evas_Object *current_page = NULL;
779         Evas_Object *page = NULL;
780         Eina_List *l = NULL;
781         int page_index = -1;
782
783         Eina_List *pages = page_scroller_get_all_pages(livebox_panel_get());
784         if (!pages) {
785                 LOGE("pages == NULL");
786                 return -1;
787         }
788
789
790         current_page = livebox_scroller_get_page();
791         if (!current_page) {
792                 LOGE("current_page == NULL");
793                 eina_list_free(pages);
794                 return -1;
795         }
796
797         if (__livebox_panel_find_empty_space(current_page,
798                 span_w, span_h, pos_x, pos_y)) {
799                 eina_list_free(pages);
800                 return page_scroller_get_current_page(livebox_panel_get());
801         }
802
803         EINA_LIST_FOREACH(pages, l, page) {
804                 ++page_index;
805
806                 if (!page) {
807                         LOGE("page == NULL");
808                         continue;
809                 }
810
811                 if (page == current_page)
812                         continue;
813
814                 if (__livebox_panel_find_empty_space(page, span_w, span_h,
815                         pos_x, pos_y)) {
816                         eina_list_free(pages);
817                         return page_index;
818                 }
819         }
820
821         eina_list_free(pages);
822         return -1;
823 }
824
825 static int __livebox_panel_get_mouse_move_distance_square(int x, int y)
826 {
827         int dx = x - s_info.mouse_press_pos.x;
828         int dy = y - s_info.mouse_press_pos.y;
829
830         return (dx * dx) + (dy * dy);
831 }
832
833 static bool __livebox_panel_find_empty_space(Evas_Object *page, int span_w,
834         int span_h, int *pos_x, int *pos_y)
835 {
836         Evas_Object *livebox;
837         Eina_List *l;
838         Eina_List *livebox_list = NULL;
839         Eina_Rectangle *grid_item_rec =  NULL;
840         int y = 0;
841         bool ret = true;
842
843         livebox_list = livebox_utils_get_liveboxes_on_page_list(page);
844         if (!livebox_list) {
845                 LOGE("livebox_list == NULL");
846                 *pos_y = 0;
847                 *pos_x = 0;
848                 return true;
849         }
850
851         if (eina_list_count(livebox_list) == 0) {
852                 *pos_y = 0;
853                 *pos_x = 0;
854                 eina_list_free(livebox_list);
855                 return true;
856         }
857
858         Eina_Rectangle *moved_item_rec = eina_rectangle_new(0, 0,
859                 span_w, span_h);
860         if (!moved_item_rec) {
861                 LOGE("moved_item_rec == NULL");
862                 eina_list_free(livebox_list);
863                 return false;
864         }
865
866         for (y = 0; y + span_h <= LIVEBOX_GRID_ROWS; ++y) {
867                 moved_item_rec->y = y;
868
869                 EINA_LIST_FOREACH(livebox_list, l, livebox) {
870                         grid_item_rec = livebox_utils_get_grid_widget_rectangle(
871                                 livebox);
872                         if (!grid_item_rec) {
873                                 LOGE("grid_item_rec == NULL");
874                                 eina_rectangle_free(grid_item_rec);
875                                 continue;
876                         }
877
878                         if (eina_rectangle_intersection(moved_item_rec,
879                                 grid_item_rec)) {
880                                 ret = false;
881                                 eina_rectangle_free(grid_item_rec);
882                                 break;
883                         } else {
884                                 *pos_y = y;
885                                 *pos_x = 0;
886                                 ret = true;
887                         }
888
889                         eina_rectangle_free(grid_item_rec);
890                 }
891
892                 if (ret) {
893                         eina_list_free(livebox_list);
894                         eina_rectangle_free(moved_item_rec);
895                         return true;
896                 }
897         }
898
899         eina_rectangle_free(moved_item_rec);
900         eina_list_free(livebox_list);
901         return false;
902 }
903
904 static void __livebox_panel_show_warning_popup(void)
905 {
906         popup_data_t *data = popup_create_data();
907         if (!data) {
908                 LOGE("data == NULL");
909                 return;
910         }
911
912         data->orientation        = ELM_POPUP_ORIENT_CENTER;
913         data->popup_cancel_text  = NULL;
914         data->popup_confirm_text = NULL;
915         data->popup_content_text = strdup(_("IDS_HS_POP_THERE_IS_NOT_ENOUGH_SPACE_ON_THE_HOME_SCREEN_REMOVE_SOME_WIDGETS_AND_TRY_AGAIN"));
916         data->popup_title_text   = NULL;
917         data->type               = POPUP_INFO;
918         data->visible_time       = 5.0;
919
920         popup_show(data, NULL, NULL, NULL);
921 }
922
923 static void __livebox_panel_freeze_scroller(void)
924 {
925
926         if (page_scroller_get_page_count(s_info.livebox_page_scroller) > 1) {
927                 page_scroller_unfreeze(s_info.livebox_page_scroller);
928         } else {
929                 page_scroller_freeze(s_info.livebox_page_scroller);
930         }
931 }
932
933 static void __livebox_panel_page_count_changed_cb(void *data, Evas_Object *obj,
934         const char *emission, const char *source)
935 {
936         __livebox_panel_freeze_scroller();
937 }
938
939
940 static Evas_Object *__livebox_panel_create_page(Evas_Object *livebox_scroller)
941 {
942         Evas_Object *grid = NULL;
943         Evas_Object *rect = NULL;
944         Evas_Object *livebox_container = util_create_edje_layout(
945                 livebox_scroller, EDJE_LIVEBOX_CONTAINER_FILENAME,
946                 GROUP_LIVEBOX_CONTAINER);
947
948         if (!livebox_container) {
949                 LOGE("Cannot create scroller layout");
950                 return NULL;
951         }
952
953         grid = elm_grid_add(livebox_container);
954         if (!grid) {
955                 LOGE("grid == NULL");
956                 evas_object_del(livebox_container);
957                 return NULL;
958         }
959
960         evas_object_size_hint_weight_set(grid, EVAS_HINT_EXPAND,
961                 EVAS_HINT_EXPAND);
962         evas_object_size_hint_align_set(grid, EVAS_HINT_FILL,
963                 EVAS_HINT_FILL);
964         elm_grid_size_set(grid, LIVEBOX_TOTAL_COLUMNS, LIVEBOX_TOTAL_ROWS);
965
966         elm_object_part_content_set(livebox_container, SIGNAL_CONTENT_SOURCE,
967                 grid);
968         livebox_utils_create_grid_shadow_clipper(livebox_container);
969
970         rect = evas_object_rectangle_add(evas_object_evas_get(
971                 livebox_container));
972         if (!rect) {
973                 LOGE("rect == NULL");
974                 evas_object_del(grid);
975                 evas_object_del(livebox_container);
976                 return NULL;
977         }
978
979
980         evas_object_color_set(rect, 0, 0, 0, 0);
981         evas_object_size_hint_min_set(rect, home_screen_get_root_width(),
982                 LIVEBOX_SIZE_REL * home_screen_get_root_height() -
983                         (2.0*LIVEBOX_X_MIN * home_screen_get_root_width()));
984         evas_object_size_hint_max_set(rect, home_screen_get_root_width(),
985                 LIVEBOX_SIZE_REL * home_screen_get_root_height() -
986                         (2.0*LIVEBOX_X_MIN * home_screen_get_root_width()));
987
988         elm_object_part_content_set(livebox_container, PART_PAGE_BG, rect);
989
990         elm_object_signal_emit(livebox_container, SIGNAL_RESIZE_SLIDERS_HIDE,
991                 SIGNAL_LIVEBOX_SOURCE);
992         elm_object_signal_callback_add(livebox_container,
993                 SIGNAL_ADD_PAGE_CLICKED, SIGNAL_LAYOUT_SOURCE,
994                 __livebox_panel_add_page_clicked_cb, NULL);
995
996         grid_item_init_resize_callbacks(livebox_scroller, livebox_container);
997
998         return livebox_container;
999 }
1000
1001
1002 static void __livebox_panel_update_edit_mode_layout(Evas_Object *livebox_scroller_scroller)
1003 {
1004         Eina_List *livebox_container_list;
1005         Eina_List *node;
1006         Evas_Object *livebox_container = NULL;
1007
1008         if (!livebox_scroller_scroller) {
1009                 LOGE("Livebox panel layout is NULL");
1010                 return;
1011         }
1012
1013         livebox_container_list = page_scroller_get_all_pages(
1014                 livebox_scroller_scroller);
1015         if (s_info.edit_mode) {
1016                 EINA_LIST_FOREACH(livebox_container_list, node,
1017                         livebox_container)
1018                 {
1019                         if (!livebox_container) {
1020                                 LOGE("livebox_container == NULL");
1021                                 continue;
1022                         }
1023
1024                         if (!__livebox_panel_is_page_empty(livebox_container)) {
1025                                 __livebox_panel_set_remove_buttons_visiblity(
1026                                         livebox_container, true);
1027                         } else {
1028                                 elm_object_signal_emit(livebox_container,
1029                                         SIGNAL_EMPTY_PAGE_EDIT_START,
1030                                         SIGNAL_LIVEBOX_SOURCE);
1031                         }
1032                         elm_object_signal_emit(livebox_container,
1033                                 SIGNAL_EDIT_START, SIGNAL_LIVEBOX_SOURCE);
1034                 }
1035         } else {
1036                 EINA_LIST_FOREACH(livebox_container_list, node,
1037                         livebox_container) {
1038                         if (!livebox_container) {
1039                                 LOGE("livebox_container == NULL");
1040                                 continue;
1041                         }
1042
1043                         __livebox_panel_set_remove_buttons_visiblity(livebox_container,
1044                                 false);
1045
1046                         elm_object_signal_emit(livebox_container,
1047                                 SIGNAL_EMPTY_PAGE_EDIT_STOP,
1048                                 SIGNAL_LIVEBOX_SOURCE);
1049                         elm_object_signal_emit(livebox_container,
1050                                 SIGNAL_EDIT_STOP, SIGNAL_LIVEBOX_SOURCE);
1051
1052                         elm_object_signal_emit(livebox_container,
1053                          SIGNAL_RESIZE_SLIDERS_HIDE, SIGNAL_LIVEBOX_SOURCE);
1054                 }
1055         }
1056
1057         eina_list_free(livebox_container_list);
1058 }
1059
1060 static bool __livebox_panel_is_page_empty(Evas_Object *page)
1061 {
1062         Eina_List *lb_list = NULL;
1063
1064         if (!page) {
1065                 LOGE("page == NULL");
1066                 return true;
1067         }
1068
1069         if (page == s_info.add_page &&
1070                 livebox_panel_check_if_add_page_is_visible()) {
1071                 return false;
1072         }
1073
1074         lb_list = livebox_utils_get_liveboxes_on_page_list(page);
1075         if (!lb_list) {
1076                 LOGD("lb_list == NULL");
1077                 return true;
1078         }
1079
1080         if (eina_list_count(lb_list) > 0) {
1081                 return false;
1082         }
1083
1084         return true;
1085 }
1086
1087
1088 static void _livebox_remove_button_mouse_cb(void *data, Evas_Object *obj,
1089         const char *emission, const char *source)
1090 {
1091         LOGD("rm btn %s", emission);
1092
1093         if (!obj) {
1094                 LOGE("obj == NULL");
1095                 return;
1096         }
1097
1098         s_info.livebox_remove_button_state = (bool) strstr(emission,
1099                 "mouse,down");
1100 }
1101
1102 static void __livebox_panel_register_longpress_callbacks(Evas_Object *livebox_scroller,
1103         Evas_Object *page_layout, Evas_Object *livebox)
1104 {
1105         Evas_Object *scroller = s_info.livebox_page_scroller;
1106
1107         evas_object_event_callback_add(livebox, EVAS_CALLBACK_MOUSE_DOWN,
1108                 __livebox_panel_mouse_down_cb, page_layout);
1109         evas_object_event_callback_add(livebox, EVAS_CALLBACK_MOUSE_UP,
1110                 __livebox_panel_mouse_up_cb, livebox_scroller);
1111         elm_layout_signal_callback_add(livebox, SIGNAL_MOUSE_DOWN,
1112                 SIGNAL_REMOVE_SOURCE, _livebox_remove_button_mouse_cb, NULL);
1113         elm_layout_signal_callback_add(livebox, SIGNAL_MOUSE_UP,
1114                 SIGNAL_REMOVE_SOURCE, _livebox_remove_button_mouse_cb, NULL);
1115
1116         if (scroller) {
1117                 /*this callback should abort longpress*/
1118                 evas_object_smart_callback_add(scroller,
1119                         SIGNAL_SCROLLER_DRAG_START, __livebox_panel_scrolled_cb,
1120                         NULL);
1121                 evas_object_smart_callback_add(scroller,
1122                         SIGNAL_SCROLLER_SCROLL, __livebox_panel_page_changed_cb,
1123                         NULL);
1124         }
1125 }
1126
1127
1128 static void __livebox_panel_mouse_down_cb(void *data, Evas *evas,
1129         Evas_Object *object, void *event_info)
1130 {
1131         Evas_Event_Mouse_Down *ev = (Evas_Event_Mouse_Down *) event_info;
1132         int current_page_number = page_scroller_get_current_page(
1133                 s_info.livebox_page_scroller);
1134         Evas_Object *page_layout = page_scroller_get_page(
1135                 s_info.livebox_page_scroller, current_page_number);
1136         int obj_x = 0, obj_y = 0, obj_w = 0, obj_h = 0;
1137         Evas_Object *widget = NULL;
1138
1139         if (!ev || !page_layout) {
1140                 LOGE("Invalid event info");
1141                 return;
1142         }
1143
1144         page_scroller_freeze(s_info.livebox_page_scroller);
1145
1146         s_info.mouse_press_pos.x = ev->output.x;
1147         s_info.mouse_press_pos.y = ev->output.y;
1148
1149         evas_object_geometry_get(object, &obj_x, &obj_y, &obj_w, &obj_h);
1150
1151         if (s_info.edit_mode) {
1152
1153                 widget = elm_layout_content_get(object, PART_LIVEBOX);
1154                 if (!widget) {
1155                         LOGE("widget == NULL");
1156                         return;
1157                 }
1158
1159                 livebox_widget_set_event_blocking(widget);
1160         }
1161
1162         if (livebox_reposition_info_s.longpress_timer) {
1163                 LOGD("Longpress timer existed - this should not happen");
1164                 ecore_timer_del(livebox_reposition_info_s.longpress_timer);
1165                 livebox_reposition_info_s.longpress_timer = NULL;
1166         }
1167
1168         if (!s_info.livebox_remove_button_state) {
1169                 livebox_reposition_info_s.longpress_timer = ecore_timer_add(
1170                         LIVEBOX_LONGPRESS_TIME, __livebox_panel_longpress_cb, object);
1171         } else {
1172                 LOGD("Remove button pressed");
1173         }
1174 }
1175
1176 static void __libebox_panel_toggle_selected_livebox(Evas_Object *livebox, Evas_Object *page)
1177 {
1178         Evas_Object *prev_selected_page = NULL;
1179
1180         if (!livebox) {
1181                 LOGE("livebox == NULL");
1182                 return;
1183         }
1184
1185         if (!page) {
1186                 LOGE("page == NULL");
1187                 return;
1188         }
1189
1190         livebox_utils_set_selected_livebox(livebox, page);
1191         prev_selected_page = livebox_utils_get_prev_livebox_layout();
1192         if (prev_selected_page && page != prev_selected_page) {
1193         /*page != prev_selected_page' when page is not change the sliders
1194         should not disappear */
1195                 LOGD("Previous page found");
1196                 elm_object_signal_emit(prev_selected_page,
1197                         SIGNAL_RESIZE_SLIDERS_HIDE, SIGNAL_LIVEBOX_SOURCE);
1198                 elm_object_signal_emit(prev_selected_page,
1199                         SIGNAL_GRID_SHADOW_HIDE, PART_GRID_BG);
1200         }
1201
1202         elm_object_signal_emit(page, SIGNAL_GRID_SHADOW_SHOW, PART_GRID_BG);
1203 }
1204
1205 static void __livebox_panel_mouse_up_cb(void *data, Evas *evas,
1206         Evas_Object *object, void *event_info)
1207 {
1208         int current_page_number = page_scroller_get_current_page(
1209                 s_info.livebox_page_scroller);
1210         Evas_Object *livebox_container = page_scroller_get_page(
1211                 s_info.livebox_page_scroller, current_page_number);
1212
1213         s_info.is_longpress = false;
1214         page_scroller_unfreeze(s_info.livebox_page_scroller);
1215
1216         __livebox_panel_freeze_scroller();
1217
1218         __livebox_panel_set_remove_button_visibility(
1219                 grid_reposition_get_repositioned_item(), true);
1220         __livebox_panel_set_reposition_bg_visibility(false);
1221
1222         grid_reposition_end();
1223         livebox_utils_set_shadow_visibility(false);
1224
1225         LOGD("PAGE CHANGE TIMER DEL: %p",
1226                 livebox_reposition_info_s.page_change_timer);
1227         ecore_timer_del(livebox_reposition_info_s.page_change_timer);
1228         livebox_reposition_info_s.page_change_timer = NULL;
1229
1230         if (livebox_reposition_info_s.longpress_timer) {
1231                 LOGD("NOT LONGPRESS");
1232                 ecore_timer_del(livebox_reposition_info_s.longpress_timer);
1233
1234                 if (home_screen_get_view_type() != HOMESCREEN_VIEW_HOME_EDIT) {
1235                         LOGD("No edit mode exiting...");
1236                         return;
1237                 }
1238
1239                 livebox_reposition_info_s.longpress_timer = NULL;
1240                 __libebox_panel_toggle_selected_livebox(object, livebox_container);
1241                 grid_item_set_resize_livebox_sliders(object, livebox_container);
1242         }
1243 }
1244
1245 static void __livebox_panel_scrolled_cb(void *data, Evas_Object *obj,
1246         void *event_info)
1247 {
1248         if (livebox_reposition_info_s.longpress_timer) {
1249                 ecore_timer_del(livebox_reposition_info_s.longpress_timer);
1250                 livebox_reposition_info_s.longpress_timer = NULL;
1251         }
1252 }
1253
1254 static void __livebox_panel_page_changed_cb(void *data, Evas_Object *obj,
1255         void *event_info)
1256 {
1257         /*livebox_utils_set_shadow_visibility(false);*/
1258 }
1259
1260
1261 static Eina_Bool _longpress_timer_cb(void *data)
1262 {
1263         grid_reposition_start();
1264         __livebox_panel_hide_remove_button(
1265                 grid_reposition_get_repositioned_item());
1266         __livebox_panel_set_reposition_bg_visibility(true);
1267
1268         return ECORE_CALLBACK_CANCEL;
1269 }
1270
1271 /*@todo add check to proceed only if edit mode is ON*/
1272 static Eina_Bool __livebox_panel_longpress_cb(void *data)
1273 {
1274         LOGD("Longpress!");
1275         int current_page_number = page_scroller_get_current_page(
1276                 s_info.livebox_page_scroller);
1277         Evas_Object *longpressed_page = page_scroller_get_page(
1278                 s_info.livebox_page_scroller, current_page_number);
1279
1280         livebox_reposition_info_s.longpress_timer = NULL;
1281         Evas_Object *livebox = (Evas_Object *) data;
1282         Evas_Object *widget = elm_layout_content_get(livebox, PART_LIVEBOX);
1283
1284         if (!widget) {
1285                 LOGE("widget == NULL");
1286                 return EINA_FALSE;
1287         }
1288         livebox_widget_set_event_blocking(widget);
1289
1290         if (!longpressed_page) {
1291                 LOGE("No longpressed object stored");
1292                 return EINA_FALSE;
1293         }
1294
1295         if (s_info.is_resizing)
1296                 return EINA_FALSE;
1297
1298
1299         if (s_info.livebox_remove_button_state)
1300                 return EINA_FALSE;
1301
1302         s_info.is_longpress = true;
1303
1304         __libebox_panel_toggle_selected_livebox(livebox, longpressed_page);
1305
1306         grid_item_set_resize_sliders_visibility(longpressed_page, false);
1307         grid_reposition_init(livebox_utils_get_livebox_container_grid(
1308                 longpressed_page), livebox);
1309         page_scroller_freeze(s_info.livebox_page_scroller);
1310
1311         if (home_screen_get_view_type() != HOMESCREEN_VIEW_HOME_EDIT) {
1312                 home_screen_set_view_type(HOMESCREEN_VIEW_HOME_EDIT);
1313                 livebox_panel_set_edit_mode_layout(true);
1314                 livebox_panel_change_edit_mode_state(false);
1315                 ecore_timer_add(0.1, _longpress_timer_cb, longpressed_page);
1316         } else {
1317                 grid_reposition_start();
1318                 __livebox_panel_hide_remove_button(livebox);
1319                 __livebox_panel_set_reposition_bg_visibility(true);
1320         }
1321
1322         return EINA_FALSE;
1323 }
1324
1325 static void __livebox_panel_add_page_clicked_cb(void *data, Evas_Object *obj,
1326         const char *emission, const char *source)
1327 {
1328         Evas_Object *lvb_scorller = NULL;
1329         int page_count = -1;
1330
1331         lvb_scorller = livebox_panel_get();
1332         if (!lvb_scorller) {
1333                 LOGE("Failed to get scroller");
1334                 return;
1335         }
1336
1337         page_count = page_scroller_get_page_count(lvb_scorller);
1338
1339         if (page_count < LIVEBOX_SCROLLER_MAX_PAGES) {
1340                 /*remove add_page button from last page*/
1341                 livebox_panel_add_page_visible_state_set(false);
1342
1343                 /*add new page*/
1344                 livebox_panel_add_empty_page(lvb_scorller);
1345
1346                 /*show add button on last page*/
1347                 livebox_panel_add_page_visible_state_set(true);
1348         } else {
1349                 livebox_panel_add_page_visible_state_set(false);
1350                 /*elm_object_signal_emit(dynamic_index->indices[5],
1351                 EDJE_SIGNAL_ADD_PAGE_HIDE, EDJE_SIGNAL_LAYOUT_SOURCE);*/
1352         }
1353
1354         elm_object_signal_emit(lvb_scorller, SIGNAL_SCROLLER_PAGE_COUNT_CHANGED,
1355                         SIGNAL_LAYOUT_SOURCE);
1356 }
1357
1358 Evas_Object *livebox_scroller_get_last_page(void)
1359 {
1360         Evas_Object *lvb_scorller = NULL;
1361         Evas_Object *page = NULL;
1362
1363         Eina_List *all_pages = NULL;
1364         int page_count = -1;
1365
1366         lvb_scorller = s_info.livebox_page_scroller;
1367         if (!lvb_scorller) {
1368                 LOGE("Failed to get livebox scroller");
1369                 return NULL;
1370         }
1371
1372         all_pages = page_scroller_get_all_pages(lvb_scorller);
1373         if (!all_pages) {
1374                 LOGE("Failed to get page");
1375                 return NULL;
1376         }
1377
1378         page_count = eina_list_count(all_pages);
1379         if (!page_count) {
1380                 LOGE("Failed to get page count!");
1381                 return NULL;
1382         }
1383
1384         page = eina_list_nth(all_pages, --page_count);
1385         if (!page) {
1386                 LOGE("Failed to get page");
1387                 return NULL;
1388         }
1389
1390         return page;
1391 }
1392
1393 static void __livebox_panel_scroll_start_cb(void *data, Evas_Object *obj, void *info)
1394 {
1395         Evas_Object *page = livebox_utils_get_selected_livebox_layout();
1396         if (!page) {
1397                 LOGE("page == NULL");
1398                 return;
1399         }
1400
1401         grid_item_set_resize_sliders_visibility(page, false);
1402 }
1403
1404
1405 static Eina_Bool __livebox_panel_change_page_timer_start_cb(void *data)
1406 {
1407         bool is_right = (bool)data;
1408         int page_count =  -1;
1409         int current_page = -1;
1410         Evas_Object *grid = NULL;
1411         Evas_Object *page = NULL;
1412         int mx = -1, my = -1;
1413
1414         if (!s_info.livebox_page_scroller) {
1415                 LOGE("s_info.livebox_page_scroller == NULL");
1416                 return false;
1417         }
1418
1419         page_count =  page_scroller_get_page_count(
1420                 s_info.livebox_page_scroller);
1421         current_page = page_scroller_get_current_page(
1422                 s_info.livebox_page_scroller);
1423
1424         LOGD("PAGE CHANGE... %s", is_right ? "Going right" : "Going left");
1425
1426         livebox_utils_repack_grid_object(NULL, livebox_utils_get_shadow(),
1427                 livebox_utils_get_livebox_container_grid(
1428                         livebox_utils_get_selected_livebox_layout()), NULL);
1429
1430         /*TODO: Check if this can be removed*/
1431         livebox_utils_repack_grid_object(NULL,
1432                 livebox_utils_selected_livebox_get(),
1433                         livebox_utils_get_livebox_container_grid(
1434                                 livebox_utils_get_selected_livebox_layout()),
1435                                         NULL);
1436
1437         page_scroller_unfreeze(s_info.livebox_page_scroller);
1438
1439         if (is_right) {
1440                 ++current_page;
1441         } else {
1442                 --current_page;
1443         }
1444
1445         current_page = current_page % page_count;
1446         LOGD("CURRENT PAGE: %d", current_page);
1447
1448         page = page_scroller_get_page(s_info.livebox_page_scroller,
1449                 current_page);
1450         if (!page) {
1451                 LOGE("page == NULL");
1452                 return ECORE_CALLBACK_CANCEL;
1453         }
1454
1455         grid = livebox_utils_get_livebox_container_grid(page);
1456         if (!grid) {
1457                 LOGE("grid == NULL");
1458                 return ECORE_CALLBACK_CANCEL;
1459         }
1460
1461         elm_scroller_page_bring_in(s_info.livebox_page_scroller,
1462                 current_page, 0);
1463
1464         page_scroller_freeze(s_info.livebox_page_scroller);
1465         __libebox_panel_toggle_selected_livebox(livebox_utils_selected_livebox_get(), page);
1466         grid_reposition_set_current_grid(grid);
1467         livebox_utils_repack_grid_object(livebox_utils_selected_livebox_get(),
1468                 livebox_utils_get_shadow(), NULL, grid);
1469
1470         livebox_utils_get_cursor_pos(&mx, &my);
1471         grid_reposition_move(mx, my);
1472
1473         livebox_utils_set_shadow_visibility(true);
1474
1475         return ECORE_CALLBACK_RENEW;
1476 }
1477
1478 static void __livebox_panel_set_remove_button_visibility(
1479         Evas_Object *livebox, bool visible)
1480 {
1481         if (!livebox) {
1482                 LOGE("livebox == NULL");
1483                 return;
1484         }
1485
1486         if (visible) {
1487                 elm_layout_signal_emit(livebox,
1488                         SIGNAL_LIVEBOX_REMOVE_BUTTON_SHOW,
1489                         SIGNAL_LIVEBOX_SOURCE);
1490         } else {
1491                 elm_layout_signal_emit(livebox,
1492                         SIGNAL_LIVEBOX_REMOVE_BUTTON_HIDE,
1493                         SIGNAL_LIVEBOX_SOURCE);
1494         }
1495 }
1496
1497 static void __livebox_panel_hide_remove_button(Evas_Object *livebox)
1498 {
1499         if (!livebox) {
1500                 LOGE("livebox == NULL");
1501                 return;
1502         }
1503
1504         elm_layout_signal_emit(livebox, SIGNAL_REMOVE_BUTTON_INSTANT_HIDE,
1505                 SIGNAL_LIVEBOX_SOURCE);
1506 }
1507
1508 static void __livebox_panel_set_remove_buttons_visiblity(Evas_Object *page, bool visible)
1509 {
1510         Eina_List *l = NULL;
1511         Evas_Object *lb = NULL;
1512
1513         if (!page) {
1514                 LOGE("page == NULL");
1515                 return;
1516         }
1517
1518         Eina_List *list = livebox_utils_get_liveboxes_on_page_list(page);
1519
1520         if (!list) {
1521                 LOGE("list == NULL");
1522                 return;
1523         }
1524
1525         EINA_LIST_FOREACH(list, l, lb) {
1526                 if (!lb) {
1527                         LOGE("lb == NULL");
1528                         continue;
1529                 }
1530                 __livebox_panel_set_remove_button_visibility(lb, visible);
1531         }
1532 }
1533
1534 static void __livebox_panel_set_reposition_bg_visibility(bool visible)
1535 {
1536         Evas_Object *livebox = grid_reposition_get_repositioned_item();
1537         if (!livebox) {
1538                 LOGE("livebox == NULL");
1539                 return;
1540         }
1541
1542         if (visible) {
1543                 elm_object_signal_emit(livebox,
1544                         SIGNAL_LIVEBOX_REPOSITION_BG_SHOW,
1545                         SIGNAL_LIVEBOX_SOURCE);
1546         } else {
1547                 elm_object_signal_emit(livebox,
1548                         SIGNAL_LIVEBOX_REPOSITION_BG_HIDE,
1549                         SIGNAL_LIVEBOX_SOURCE);
1550         }
1551 }
1552
1553 static Evas_Object *__livebox_panel_get_widget_layout(Evas_Object* obj)
1554 {
1555         Evas_Object *widget = elm_layout_content_get(obj, PART_LIVEBOX);
1556         if (!widget) {
1557                 LOGE("widget == NULL");
1558                 return NULL;
1559         }
1560
1561         return widget;
1562 }
1563
1564 static void __livebox_panel_del_cb(void *data, Evas_Object *obj, const char *emission,
1565         const char *source)
1566 {
1567         Evas_Object *grid = NULL;
1568         Evas_Object *widget = NULL;
1569         Tree_node_t *node = NULL;
1570
1571         int current_page_number =
1572                 page_scroller_get_current_page(s_info.livebox_page_scroller);
1573
1574         Evas_Object *page_container =
1575                 page_scroller_get_page(
1576                         s_info.livebox_page_scroller, current_page_number);
1577         if (!page_container) {
1578                 LOGE("page_container == NULL");
1579                 return;
1580         }
1581         elm_object_signal_emit(page_container, SIGNAL_RESIZE_SLIDERS_HIDE,
1582                 SIGNAL_LIVEBOX_SOURCE);
1583
1584         LOGI("DELETE LIVEBOX BUTTON PRESSED");
1585
1586         node = evas_object_data_get(obj, KEY_ICON_DATA);
1587         if (!node) {
1588                 LOGE("node == NULL");
1589                 return;
1590         }
1591
1592         widget = __livebox_panel_get_widget_layout(obj);
1593         livebox_widget_del(widget);
1594         data_model_del_item(node);
1595
1596         evas_object_del(obj);
1597
1598         grid = livebox_utils_get_livebox_container_grid(page_container);
1599         if (!grid) {
1600                 LOGE("page_container == NULL");
1601                 return;
1602         }
1603
1604         livebox_utils_repack_grid_object(NULL, livebox_utils_get_shadow(),
1605                 grid, NULL);
1606
1607         home_screen_print_tree();
1608 }
1609
1610 static bool __livebox_panel_iterate_node_fill_cb(Tree_node_t *page_node, Tree_node_t *node,
1611         void *data)
1612 {
1613         int *widget_pos_h = (int *)data;
1614
1615         Evas_Object *page = page_node->data->layout;
1616         if (!page) {
1617                 LOGE("page == NULL");
1618                 return false;
1619         }
1620
1621         livebox_panel_add_livebox(node, page, node->data->appid,
1622                 node->data->col, node->data->row, node->data->col_span,
1623                 node->data->row_span,
1624                 node->data->content_info);
1625
1626         *widget_pos_h += LIVEBOX_ON_CREATE_SIZE/2;
1627         return true;
1628 }
1629
1630 static bool __livebox_panel_iterate_page_fill_cb(Tree_node_t *parent, Tree_node_t *page_node,
1631         void *data)
1632 {
1633         Evas_Object *page = NULL;
1634         Evas_Object *liveboxes = s_info.livebox_page_scroller;
1635         int *widget_pos_h = (int *)data;
1636         *widget_pos_h = 0;
1637
1638         page = livebox_panel_add_page(page_node, liveboxes);
1639         if (!page) {
1640                 LOGE("[FAILED][page]");
1641                 data_model_del_item(page_node);
1642                 return false;
1643         }
1644
1645         page_node->data->layout = page;
1646         return true;
1647 }