Fixed prevent issues
[apps/core/preloaded/quickpanel.git] / daemon / notifications / noti_gridbox.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 <Ecore_X.h>
18
19 #include "quickpanel-ui.h"
20 #include "common.h"
21 #include "list_util.h"
22 #include "quickpanel_def.h"
23 #include "noti_gridbox.h"
24 #include "noti_box.h"
25
26 #define E_DATA_LAYOUT_PORTRAIT "layout_portrait"
27 #define E_DATA_LAYOUT_LANDSCAPE "layout_landscape"
28 #define E_DATA_CB_DELETE_ITEM "cb_delete_item"
29 #define E_DATA_CB_REMOVED "cb_removed"
30 #define E_DATA_APP_DATA "app_data"
31
32 typedef struct _gridbox_info_layout {
33         int n_per_rows;
34         int padding_top;
35         int padding_left;
36         int padding_right;
37         int padding_bottom;
38         int padding_between;
39         int child_w;
40         int child_h;
41         double scale;
42         int limit_w;
43 } gridbox_info_layout;
44
45 typedef struct _gridbox_info_animation {
46         Evas_Object *gridbox;
47         Evas_Object *item;
48
49         void (*update_cb)(Evas_Object *list, void *data, int is_prepend);
50         Evas_Object *container;
51         void *noti;
52         int pos;
53 } gridbox_info_animation;
54
55 static void _gridbox_layout_get_pos(int order, int *x, int *y, void *data) {
56         gridbox_info_layout *info_layout = data;
57
58         retif(data == NULL, , "invalid parameter");
59         retif(x == NULL, , "invalid parameter");
60         retif(y == NULL, , "invalid parameter");
61
62         int n_per_row = info_layout->n_per_rows;
63
64         int row = (order - 1) / n_per_row;
65         int column = (order - 1) - (row * n_per_row);
66
67         //DBG("order:%d r:%d c:%d", order, row, column);
68
69         int row_x = info_layout->padding_left
70                         + ((info_layout->child_w + info_layout->padding_between) * column);
71
72         int row_y = info_layout->padding_top
73                         + ((info_layout->child_h + info_layout->padding_between) * row);
74
75         *x = row_x;
76         *y = row_y;
77 }
78
79 static void _gridbox_layout(Evas_Object *o, Evas_Object_Box_Data *priv,
80                 void *data) {
81         int n_children;
82         int x, y, w, h;
83         int off_x = 0, off_y = 0;
84         Eina_List *l;
85         Eina_List *l_next;
86         Evas_Object_Box_Option *opt;
87         int child_w;
88         int space_w = 0;
89         int num_padding_between = 0;
90
91         retif(o == NULL, , "invalid parameter");
92         retif(priv == NULL, , "invalid parameter");
93         retif(data == NULL, , "invalid parameter");
94
95         gridbox_info_layout *info_layout = (gridbox_info_layout *) data;
96
97         n_children = eina_list_count(priv->children);
98         DBG("layout function:%d", n_children);
99         if (!n_children) {
100                 evas_object_size_hint_min_set(o, -1, 0);
101                 return;
102         }
103
104         //box geometry
105         evas_object_geometry_get(o, &x, &y, &w, &h);
106
107         num_padding_between = info_layout->n_per_rows / 2;
108         num_padding_between += (info_layout->n_per_rows > 1 && (info_layout->n_per_rows % 2) > 0) ? 1 : 0;
109
110         space_w = (info_layout->padding_left * 2) + (info_layout->padding_between * num_padding_between);
111         child_w = (info_layout->limit_w - space_w) / info_layout->n_per_rows;
112
113         info_layout->child_w = child_w;
114
115         DBG("grid layout children pos:%d %d", info_layout->child_w, info_layout->child_h);
116
117         int order_children = 1;
118         EINA_LIST_FOREACH_SAFE(priv->children, l, l_next, opt)
119         {
120                 _gridbox_layout_get_pos(order_children, &off_x, &off_y, info_layout);
121                 evas_object_move(opt->obj, x + off_x, y + off_y);
122                 evas_object_size_hint_min_set(opt->obj, info_layout->child_w,
123                                 info_layout->child_h);
124                 evas_object_resize(opt->obj, info_layout->child_w,
125                                 info_layout->child_h);
126                 order_children++;
127         }
128
129         evas_object_size_hint_min_set(o, -1,
130                         off_y + info_layout->child_h + info_layout->padding_bottom);
131 }
132
133 HAPI Evas_Object *gridbox_create(Evas_Object *parent, void *data) {
134
135         retif(parent == NULL, NULL, "invalid parameter");
136         retif(data == NULL, NULL, "invalid parameter");
137         struct appdata *ad = data;
138         Evas_Object *gridbox = NULL;
139
140         gridbox_info_layout *info_layout_portrait = NULL;
141         gridbox_info_layout *info_layout_landscape = NULL;
142
143         info_layout_portrait = (gridbox_info_layout *) malloc(
144                         sizeof(gridbox_info_layout));
145         retif(info_layout_portrait == NULL, NULL, "memory allocation failed");
146         info_layout_portrait->padding_between = 12 * ad->scale;
147         info_layout_portrait->padding_top = 0;
148         info_layout_portrait->padding_left = 14 * ad->scale;
149         info_layout_portrait->padding_bottom = 12 * ad->scale;
150         info_layout_portrait->n_per_rows = 2;
151         info_layout_portrait->child_w = 0; //340;
152         info_layout_portrait->child_h = BOX_HEIGHT_P * ad->scale; //400;
153         info_layout_portrait->limit_w = ad->win_width; //400;
154         info_layout_portrait->scale = ad->scale;
155
156         info_layout_landscape = (gridbox_info_layout *) malloc(
157                         sizeof(gridbox_info_layout));
158         retif(info_layout_landscape == NULL, NULL, "memory allocation failed");
159         info_layout_landscape->padding_between = 12 * ad->scale;
160         info_layout_landscape->padding_top = 0;
161         info_layout_landscape->padding_left = 14 * ad->scale;
162         info_layout_landscape->padding_bottom = 12 * ad->scale;
163         info_layout_landscape->n_per_rows = 3;
164         info_layout_landscape->child_w = 0; //409;
165         info_layout_landscape->child_h = BOX_HEIGHT_L * ad->scale; //400;
166         info_layout_landscape->limit_w = ad->win_height; //400;
167         info_layout_landscape->scale = ad->scale;
168
169         gridbox = elm_box_add(parent);
170         evas_object_size_hint_weight_set(gridbox, EVAS_HINT_EXPAND,
171                         EVAS_HINT_EXPAND);
172         evas_object_size_hint_align_set(gridbox, EVAS_HINT_FILL, EVAS_HINT_FILL);
173
174         if (ad->angle == 270 || ad->angle == 90)
175                 elm_box_layout_set(gridbox, _gridbox_layout, info_layout_landscape,
176                                 NULL);
177         else
178                 elm_box_layout_set(gridbox, _gridbox_layout, info_layout_portrait,
179                                 NULL);
180
181         evas_object_show(gridbox);
182
183         evas_object_data_set(gridbox, E_DATA_LAYOUT_PORTRAIT, info_layout_portrait);
184         evas_object_data_set(gridbox, E_DATA_LAYOUT_LANDSCAPE,
185                         info_layout_landscape);
186         evas_object_data_set(gridbox, E_DATA_CB_DELETE_ITEM, NULL);
187         evas_object_data_set(gridbox, E_DATA_APP_DATA, ad);
188
189         qp_item_data *qid
190                 = quickpanel_list_util_item_new(QP_ITEM_TYPE_NOTI, NULL);
191         quickpanel_list_util_item_set_tag(gridbox, qid);
192
193         return gridbox;
194 }
195
196 HAPI void gridbox_remove(Evas_Object *gridbox) {
197
198         retif(gridbox == NULL, , "invalid parameter");
199
200         gridbox_info_layout *info_layout_portrait = evas_object_data_get(gridbox,
201                         E_DATA_LAYOUT_PORTRAIT);
202         gridbox_info_layout *info_layout_landscape = evas_object_data_get(gridbox,
203                         E_DATA_LAYOUT_LANDSCAPE);
204
205         gridbox_remove_all_item(gridbox, 0);
206         evas_object_data_del(gridbox, E_DATA_LAYOUT_PORTRAIT);
207         evas_object_data_del(gridbox, E_DATA_LAYOUT_LANDSCAPE);
208         evas_object_data_del(gridbox, E_DATA_CB_DELETE_ITEM);
209         evas_object_data_del(gridbox, E_DATA_APP_DATA);
210         quickpanel_list_util_item_del_tag(gridbox);
211         evas_object_del(gridbox);
212
213         if (info_layout_portrait != NULL)
214                 free(info_layout_portrait);
215         if (info_layout_landscape != NULL)
216                 free(info_layout_landscape);
217 }
218
219 HAPI void gridbox_set_item_deleted_cb(Evas_Object *gridbox,
220                 void(*deleted_cb)(void *data, Evas_Object *obj)) {
221         retif(gridbox == NULL, , "invalid parameter");
222         retif(deleted_cb == NULL, , "invalid parameter");
223
224         evas_object_data_set(gridbox, E_DATA_CB_DELETE_ITEM, deleted_cb);
225 }
226
227 static void _gridbox_call_item_deleted_cb(Evas_Object *gridbox, void *data,
228                 Evas_Object *obj) {
229         retif(gridbox == NULL, , "invalid parameter");
230
231         void (*deleted_cb)(void *data, Evas_Object *obj) = NULL;
232
233         deleted_cb = evas_object_data_get(gridbox, E_DATA_CB_DELETE_ITEM);
234
235         if (deleted_cb != NULL) {
236                 deleted_cb(data, obj);
237         }
238 }
239
240 HAPI void gridbox_add_item(Evas_Object *gridbox, Evas_Object *item, int is_prepend) {
241         const char *signal = NULL;
242
243         retif(gridbox == NULL, , "invalid parameter");
244         retif(item == NULL, , "invalid parameter");
245
246         struct appdata *ad = evas_object_data_get(gridbox, E_DATA_APP_DATA);
247
248         if (ad != NULL) {
249                 if (ad->angle == 270 || ad->angle == 90) {
250                         signal = "box.landscape";
251                 } else {
252                         signal = "box.portrait";
253                 }
254         }
255
256         DBG("set to %s, %x", signal, item);
257
258         elm_object_signal_emit(item, signal, "box.prog");
259         edje_object_message_signal_process(_EDJ(item));
260         elm_layout_sizing_eval(item);
261
262         if (is_prepend == GRIDBOX_PREPEND)
263                 elm_box_pack_start(gridbox, item);
264         else
265                 elm_box_pack_end(gridbox, item);
266 }
267
268 static void _gridbox_remove_item_anim_cb(void *data, Elm_Transit *transit) {
269         DBG("");
270         retif(data == NULL, , "invalid parameter");
271         retif(transit == NULL, , "invalid parameter");
272
273         gridbox_info_animation *info_animation = data;
274
275         retif(info_animation->gridbox == NULL, , "invalid parameter");
276         retif(info_animation->item == NULL, , "invalid parameter");
277
278         DBG("remove:%p", info_animation->item);
279
280         void *node = noti_box_node_get(info_animation->item);
281         elm_box_unpack(info_animation->gridbox, info_animation->item);
282         noti_box_remove(info_animation->item);
283         _gridbox_call_item_deleted_cb(info_animation->gridbox,
284                         node, NULL);
285
286         if (info_animation->update_cb != NULL) {
287                 retif(info_animation->container == NULL, , "invalid parameter");
288                 retif(info_animation->noti == NULL, , "invalid parameter");
289
290                 info_animation->update_cb(info_animation->container,
291                                 info_animation->noti, info_animation->pos);
292         }
293
294         free(info_animation);
295         info_animation = NULL;
296 }
297
298 HAPI void gridbox_remove_item(Evas_Object *gridbox, Evas_Object *item, int with_animation) {
299         DBG("remove:%p", item);
300         retif(gridbox == NULL, , "invalid parameter");
301         retif(item == NULL, , "invalid parameter");
302
303         if (noti_box_get_status(item) == STATE_DELETING) {
304                 return ;
305         }
306         noti_box_set_status(item, STATE_DELETING);
307
308         if (with_animation == 1) {
309                 gridbox_info_animation *info_animation = (gridbox_info_animation *) malloc(
310                                 sizeof(gridbox_info_animation));
311                 if (info_animation == NULL)
312                         return;
313                 info_animation->gridbox = gridbox;
314                 info_animation->item = item;
315                 info_animation->update_cb = NULL;
316                 info_animation->container = NULL;
317                 info_animation->noti = NULL;
318                 info_animation->pos = 0;
319
320                 Elm_Transit *transit = elm_transit_add();
321                 //Fade in and out with layout object.
322                 elm_transit_object_add(transit, item);
323                 elm_transit_effect_fade_add(transit);
324                 elm_transit_duration_set(transit, 0.7);
325                 elm_transit_del_cb_set(transit, _gridbox_remove_item_anim_cb,
326                                 info_animation);
327                 elm_transit_go(transit);
328         } else {
329                 void *node = noti_box_node_get(item);
330                 elm_box_unpack(gridbox, item);
331                 noti_box_remove(item);
332                 _gridbox_call_item_deleted_cb(gridbox,
333                                 node, NULL);
334         }
335 }
336
337 HAPI void gridbox_remove_all_item(Evas_Object *gridbox, int with_animation) {
338         DBG("");
339         retif(gridbox == NULL, , "invalid parameter");
340
341         Eina_List *l;
342         Eina_List *l_next;
343         Evas_Object *obj;
344         Eina_List *item_list = elm_box_children_get(gridbox);
345
346         EINA_LIST_FOREACH_SAFE(item_list, l, l_next, obj)
347         {
348                 if (obj != NULL) {
349                         // call deleted callback
350                         gridbox_remove_item(gridbox, obj, with_animation);
351                 }
352         }
353 }
354
355 HAPI void gridbox_update_item(Evas_Object *gridbox, Evas_Object *item) {
356
357         retif(gridbox == NULL, , "invalid parameter");
358         retif(item == NULL, , "invalid parameter");
359 }
360
361 HAPI void gridbox_remove_and_add_item(Evas_Object *gridbox, Evas_Object *item
362                 ,void (*update_cb)(Evas_Object *list, void *data, int is_prepend)
363                 ,void *container, void *data, int pos) {
364
365         retif(gridbox == NULL, , "invalid parameter");
366         retif(item == NULL, , "invalid parameter");
367         retif(update_cb == NULL, , "invalid parameter");
368         retif(container == NULL, , "invalid parameter");
369         retif(data == NULL, , "invalid parameter");
370
371         if (noti_box_get_status(item) == STATE_DELETING) {
372                 return ;
373         }
374         noti_box_set_status(item, STATE_DELETING);
375
376         gridbox_info_animation *info_animation = (gridbox_info_animation *) malloc(
377                         sizeof(gridbox_info_animation));
378         if (info_animation == NULL)
379                 return;
380         info_animation->gridbox = gridbox;
381         info_animation->item = item;
382         info_animation->update_cb = update_cb;
383         info_animation->container = container;
384         info_animation->noti = data;
385         info_animation->pos = pos;
386
387         Elm_Transit *transit = elm_transit_add();
388         //Fade in and out with layout object.
389         elm_transit_object_add(transit, item);
390         elm_transit_effect_fade_add(transit);
391         elm_transit_duration_set(transit, 0.4);
392         elm_transit_del_cb_set(transit, _gridbox_remove_item_anim_cb,
393                         info_animation);
394         elm_transit_go(transit);
395 }
396
397 HAPI void gridbox_finalize_rotation_cb(void *data) {
398         retif(data == NULL, , "invalid parameter");
399         Evas_Object *gridbox = data;
400
401         elm_box_recalculate(gridbox);
402 }
403
404 HAPI void gridbox_rotation(Evas_Object *gridbox, int angle) {
405         const char *signal = NULL;
406
407         retif(gridbox == NULL, , "invalid parameter");
408
409         gridbox_info_layout *info_layout_portrait = evas_object_data_get(gridbox,
410                         E_DATA_LAYOUT_PORTRAIT);
411         gridbox_info_layout *info_layout_landscape = evas_object_data_get(gridbox,
412                         E_DATA_LAYOUT_LANDSCAPE);
413
414         retif(info_layout_portrait == NULL || info_layout_landscape == NULL, ,
415                         "gridbox is crashed");
416
417         Eina_List *l;
418         Eina_List *l_next;
419         Evas_Object *obj;
420         Eina_List *item_list = elm_box_children_get(gridbox);
421
422         if (angle == 270 || angle == 90) {
423                 signal = "box.landscape";
424         } else {
425                 signal = "box.portrait";
426         }
427
428         DBG("all count:%d", eina_list_count (item_list));
429
430         EINA_LIST_FOREACH_SAFE(item_list, l, l_next, obj)
431         {
432                 if (obj != NULL) {
433                         elm_object_signal_emit(obj, signal, "box.prog");
434                         edje_object_message_signal_process(_EDJ(obj));
435                         elm_layout_sizing_eval(obj);
436                         DBG("set to %s, %x", signal, obj);
437                 }
438         }
439
440         if (angle == 270 || angle == 90) {
441                 elm_box_layout_set(gridbox, _gridbox_layout, info_layout_landscape,
442                                 NULL);
443
444 #if 0
445                 layout_data = elm_box_transition_new(0.0, _gridbox_layout,
446                                 info_layout_portrait, NULL, _gridbox_layout,
447                                 info_layout_landscape, NULL, gridbox_finalize_rotation_cb,
448                                 gridbox);
449 #endif
450         } else {
451                 elm_box_layout_set(gridbox, _gridbox_layout, info_layout_portrait,
452                                 NULL);
453 #if 0
454                 layout_data = elm_box_transition_new(0.0, _gridbox_layout,
455                                 info_layout_landscape, NULL, _gridbox_layout,
456                                 info_layout_portrait, NULL, gridbox_finalize_rotation_cb,
457                                 gridbox);
458 #endif
459         }
460
461 #if 0
462         elm_box_layout_set(gridbox, elm_box_layout_transition, layout_data,
463                         elm_box_transition_free);
464 #endif
465         DBG("Angle  Rotation  is %d", angle);
466 }