Merge remote-tracking branch 'remotes/origin/upstream'
[framework/uifw/elementary.git] / src / lib / elm_panes.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * TODO
6  * Update the minimun height of the bar in the theme.
7  * No minimun should be set in the vertical theme
8  * Add events (move, start ...)
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *panes;
16
17    struct
18      {
19         Evas_Object *left;
20         Evas_Object *right;
21      } contents;
22
23    struct
24      {
25         int x_diff;
26         int y_diff;
27         Eina_Bool move;
28      } move;
29
30    Eina_Bool clicked_double;
31    Eina_Bool horizontal;
32    Eina_Bool fixed;
33 };
34
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
38 static void _theme_hook(Evas_Object *obj);
39 static void _sizing_eval(Evas_Object *obj);
40 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
41
42 static const char SIG_CLICKED[] = "clicked";
43 static const char SIG_PRESS[] = "press";
44 static const char SIG_UNPRESS[] = "unpress";
45 static const char SIG_CLICKED_DOUBLE[] = "clicked,double";
46
47 static const Evas_Smart_Cb_Description _signals[] = {
48    {SIG_CLICKED, ""},
49    {SIG_PRESS, ""},
50    {SIG_UNPRESS, ""},
51    {SIG_CLICKED_DOUBLE, ""},
52    {NULL, NULL}
53 };
54
55 static void
56 _del_hook(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    if (!wd) return;
60    free(wd);
61 }
62
63 static void
64 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
65 {
66    Widget_Data *wd = elm_widget_data_get(obj);
67    if (!wd) return;
68    edje_object_mirrored_set(wd->panes, rtl);
69 }
70
71 static void
72 _theme_hook(Evas_Object *obj)
73 {
74    Widget_Data *wd = elm_widget_data_get(obj);
75    const char *style = elm_widget_style_get(obj);
76    double size;
77
78    if (!wd) return;
79    _elm_widget_mirrored_reload(obj);
80    _mirrored_set(obj, elm_widget_mirrored_get(obj));
81    size = elm_panes_content_left_size_get(obj);
82
83    if (wd->horizontal)
84      _elm_theme_object_set(obj, wd->panes, "panes", "horizontal", style);
85    else
86      _elm_theme_object_set(obj, wd->panes, "panes", "vertical", style);
87
88    if (wd->contents.left)
89      edje_object_part_swallow(wd->panes, "elm.swallow.left", wd->contents.left);
90    if (wd->contents.right)
91      edje_object_part_swallow(wd->panes, "elm.swallow.right", wd->contents.right);
92 <<<<<<< HEAD
93    if(wd->contents.left && wd->contents.right)
94      edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
95    if(wd->fixed)
96 =======
97    if (wd->fixed)
98 >>>>>>> remotes/origin/upstream
99      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
100
101    edje_object_scale_set(wd->panes, elm_widget_scale_get(obj) *
102                          _elm_config->scale);
103    _sizing_eval(obj);
104    elm_panes_content_left_size_set(obj, size);
105 }
106
107 static Eina_Bool
108 _elm_panes_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
109 {
110    Widget_Data *wd = elm_widget_data_get(obj);
111    if (!wd) return EINA_FALSE;
112
113    double w, h;
114    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
115    if (((wd->horizontal) && ( h == 0.0 )) || ((!wd->horizontal) && ( w == 0.0 )))
116      return elm_widget_focus_next_get(wd->contents.right, dir, next);
117
118    Evas_Object *chain[2];
119
120    /* Direction */
121    if (dir == ELM_FOCUS_PREVIOUS)
122      {
123         chain[0] = wd->contents.right;
124         chain[1] = wd->contents.left;
125      }
126    else if (dir == ELM_FOCUS_NEXT)
127      {
128         chain[0] = wd->contents.left;
129         chain[1] = wd->contents.right;
130      }
131    else
132      return EINA_FALSE;
133
134    unsigned char i = elm_widget_focus_get(chain[1]);
135
136    if (elm_widget_focus_next_get(chain[i], dir, next))
137      return EINA_TRUE;
138
139    i = !i;
140
141    Evas_Object *to_focus;
142    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
143      {
144         *next = to_focus;
145         return !!i;
146      }
147
148    return EINA_FALSE;
149 }
150
151 static void
152 _sizing_eval(Evas_Object *obj)
153 {
154    Widget_Data *wd = elm_widget_data_get(obj);
155    if (!wd) return;
156 }
157
158 static void
159 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
160 {
161    _sizing_eval(data);
162 }
163
164 static void
165 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
166 {
167    Widget_Data *wd = elm_widget_data_get(obj);
168    Evas_Object *sub = event_info;
169
170    if (!wd) return;
171    if (sub == wd->contents.left)
172      {
173         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
174                                             _changed_size_hints, obj);
175         wd->contents.left = NULL;
176         _sizing_eval(obj);
177      }
178    else if (sub == wd->contents.right)
179      {
180         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
181                                             _changed_size_hints, obj);
182         wd->contents.right= NULL;
183         _sizing_eval(obj);
184      }
185 }
186
187 <<<<<<< HEAD
188 =======
189
190 >>>>>>> remotes/origin/upstream
191 static void
192 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
193 {
194    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
195 }
196
197 static void
198 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
199 {
200    Widget_Data *wd = elm_widget_data_get(data);
201
202    wd->clicked_double = EINA_TRUE;
203 }
204
205 static void
206 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
207 {
208    evas_object_smart_callback_call(data, SIG_PRESS, NULL);
209 }
210
211 static void
212 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
213 {
214    Widget_Data *wd = elm_widget_data_get(data);
215    evas_object_smart_callback_call(data, SIG_UNPRESS, NULL);
216
217    if (wd->clicked_double)
218      {
219         evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
220         wd->clicked_double = EINA_FALSE;
221      }
222 }
223
224 static void
225 _content_left_set(Evas_Object *obj, Evas_Object *content)
226 {
227    Widget_Data *wd = elm_widget_data_get(obj);
228    if (wd->contents.left)
229      {
230         evas_object_del(wd->contents.left);
231         wd->contents.left = NULL;
232 <<<<<<< HEAD
233         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
234 =======
235 >>>>>>> remotes/origin/upstream
236      }
237    if (content)
238      {
239         wd->contents.left = content;
240         elm_widget_sub_object_add(obj, content);
241         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
242 <<<<<<< HEAD
243         if (wd->contents.right)
244           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
245 =======
246 >>>>>>> remotes/origin/upstream
247      }
248 }
249
250 static void
251 _content_right_set(Evas_Object *obj, Evas_Object *content)
252 {
253    Widget_Data *wd = elm_widget_data_get(obj);
254    if (wd->contents.right)
255      {
256         evas_object_del(wd->contents.right);
257         wd->contents.right = NULL;
258 <<<<<<< HEAD
259         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
260 =======
261 >>>>>>> remotes/origin/upstream
262      }
263    if (content)
264      {
265         wd->contents.right = content;
266         elm_widget_sub_object_add(obj, content);
267         edje_object_part_swallow(wd->panes, "elm.swallow.right", content);
268 <<<<<<< HEAD
269         if (wd->contents.left)
270           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
271 =======
272 >>>>>>> remotes/origin/upstream
273      }
274 }
275
276 static Evas_Object *
277 _content_left_unset(Evas_Object *obj)
278 {
279    Widget_Data *wd = elm_widget_data_get(obj);
280    if (!wd->contents.left) return NULL;
281    Evas_Object *content = wd->contents.left;
282 <<<<<<< HEAD
283
284    edje_object_part_unswallow(wd->panes, content);
285    evas_object_hide(wd->contents.left);
286    elm_widget_sub_object_del(obj, content);
287    wd->contents.left = NULL;
288    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
289 =======
290    elm_widget_sub_object_del(obj, content);
291    edje_object_part_unswallow(wd->panes, content);
292    wd->contents.left = NULL;
293 >>>>>>> remotes/origin/upstream
294    return content;
295 }
296
297 static Evas_Object *
298 _content_right_unset(Evas_Object *obj)
299 {
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (!wd->contents.right) return NULL;
302    Evas_Object *content = wd->contents.right;
303 <<<<<<< HEAD
304
305    edje_object_part_unswallow(wd->panes, content);
306    evas_object_hide(wd->contents.right);
307    elm_widget_sub_object_del(obj, content);
308    wd->contents.right = NULL;
309    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
310 =======
311    elm_widget_sub_object_del(obj, content);
312    edje_object_part_unswallow(wd->panes, content);
313    wd->contents.right = NULL;
314 >>>>>>> remotes/origin/upstream
315    return content;
316 }
317
318 static void
319 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype);
322    Widget_Data *wd = elm_widget_data_get(obj);
323    if (!wd) return;
324    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
325    if (!part || !strcmp(part, "left"))
326      _content_left_set(obj, content);
327    else if(!strcmp(part, "right"))
328      _content_right_set(obj, content);
329 }
330
331 static Evas_Object *
332 _content_get_hook(const Evas_Object *obj, const char *part)
333 {
334    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
335    Widget_Data *wd = elm_widget_data_get(obj);
336    if (!wd) return NULL;
337    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
338    if (!part || !strcmp(part, "left"))
339      return wd->contents.left;
340    else if (!strcmp(part, "right"))
341      return wd->contents.right;
342    return NULL;
343 }
344
345 static Evas_Object *
346 _content_unset_hook(Evas_Object *obj, const char *part)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
349    Widget_Data *wd = elm_widget_data_get(obj);
350    if (!wd) return NULL;
351    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
352    if (!part || !strcmp(part, "left"))
353      return _content_left_unset(obj);
354    else if (!strcmp(part, "right"))
355      return _content_right_unset(obj);
356    return NULL;
357 }
358
359 EAPI Evas_Object *
360 elm_panes_add(Evas_Object *parent)
361 {
362    Evas_Object *obj;
363    Evas *e;
364    Widget_Data *wd;
365
366    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
367
368    ELM_SET_WIDTYPE(widtype, "panes");
369    elm_widget_type_set(obj, "panes");
370    elm_widget_sub_object_add(parent, obj);
371    elm_widget_data_set(obj, wd);
372    elm_widget_del_hook_set(obj, _del_hook);
373    elm_widget_theme_hook_set(obj, _theme_hook);
374    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
375    elm_widget_content_set_hook_set(obj, _content_set_hook);
376    elm_widget_content_get_hook_set(obj, _content_get_hook);
377    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
378    elm_widget_can_focus_set(obj, EINA_FALSE);
379
380    wd->panes = edje_object_add(e);
381    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
382    elm_widget_resize_object_set(obj, wd->panes);
383    evas_object_show(wd->panes);
384
385    elm_panes_content_left_size_set(obj, 0.5);
386
387    edje_object_signal_callback_add(wd->panes, "elm,action,click", "",
388                                    _clicked, obj);
389    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "",
390                                    _clicked_double, obj);
391    edje_object_signal_callback_add(wd->panes, "elm,action,press", "",
392                                    _press, obj);
393    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "",
394                                    _unpress, obj);
395
396    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
397    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
398                                   _changed_size_hints, obj);
399
400    evas_object_smart_callbacks_descriptions_set(obj, _signals);
401
402    _mirrored_set(obj, elm_widget_mirrored_get(obj));
403    _sizing_eval(obj);
404    return obj;
405 }
406
407 <<<<<<< HEAD
408 EAPI void
409 =======
410 EINA_DEPRECATED EAPI void
411 >>>>>>> remotes/origin/upstream
412 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
413 {
414    _content_set_hook(obj, "left", content);
415 }
416
417 <<<<<<< HEAD
418 EAPI void
419 =======
420 EINA_DEPRECATED EAPI void
421 >>>>>>> remotes/origin/upstream
422 elm_panes_content_right_set(Evas_Object *obj, Evas_Object *content)
423 {
424    _content_set_hook(obj, "right", content);
425 }
426
427 <<<<<<< HEAD
428 EAPI Evas_Object *
429 =======
430 EINA_DEPRECATED EAPI Evas_Object *
431 >>>>>>> remotes/origin/upstream
432 elm_panes_content_left_get(const Evas_Object *obj)
433 {
434    return _content_get_hook(obj, "left");
435 }
436
437 <<<<<<< HEAD
438 EAPI Evas_Object *
439 =======
440 EINA_DEPRECATED EAPI Evas_Object *
441 >>>>>>> remotes/origin/upstream
442 elm_panes_content_right_get(const Evas_Object *obj)
443 {
444    return _content_get_hook(obj, "right");
445 }
446
447 <<<<<<< HEAD
448 EAPI Evas_Object *
449 =======
450 EINA_DEPRECATED EAPI Evas_Object *
451 >>>>>>> remotes/origin/upstream
452 elm_panes_content_left_unset(Evas_Object *obj)
453 {
454    return _content_unset_hook(obj, "left");
455 }
456
457 <<<<<<< HEAD
458 EAPI Evas_Object *
459 =======
460 EINA_DEPRECATED EAPI Evas_Object *
461 >>>>>>> remotes/origin/upstream
462 elm_panes_content_right_unset(Evas_Object *obj)
463 {
464    return _content_unset_hook(obj, "right");
465 }
466
467 EAPI double
468 elm_panes_content_left_size_get(const Evas_Object *obj)
469 {
470    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
471    Widget_Data *wd = elm_widget_data_get(obj);
472    double w, h;
473
474    if (!wd) return 0;
475    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
476    if (wd->horizontal) return h;
477    else return w;
478 }
479
480 EAPI void
481 elm_panes_content_left_size_set(Evas_Object *obj, double size)
482 {
483    ELM_CHECK_WIDTYPE(obj, widtype);
484    Widget_Data *wd = elm_widget_data_get(obj);
485    if (!wd) return;
486    if (size < 0.0) size = 0.0;
487    else if (size > 1.0) size = 1.0;
488    if (wd->horizontal)
489      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
490    else
491      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
492 }
493
494 <<<<<<< HEAD
495 =======
496 EAPI double
497 elm_panes_content_right_size_get(const Evas_Object *obj)
498 {
499    return (1.0 - elm_panes_content_left_size_get(obj));
500 }
501
502 EAPI void
503 elm_panes_content_right_size_set(Evas_Object *obj, double size)
504 {
505    elm_panes_content_left_size_set(obj, (1.0 - size));
506 }
507
508 >>>>>>> remotes/origin/upstream
509 EAPI void
510 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
511 {
512    ELM_CHECK_WIDTYPE(obj, widtype);
513    Widget_Data *wd = elm_widget_data_get(obj);
514    if (!wd) return;
515    wd->horizontal = horizontal;
516    _theme_hook(obj);
517    elm_panes_content_left_size_set(obj, 0.5);
518 }
519
520 EAPI Eina_Bool
521 elm_panes_horizontal_get(const Evas_Object *obj)
522 {
523    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
524    Widget_Data *wd = elm_widget_data_get(obj);
525    if (!wd) return EINA_FALSE;
526    return wd->horizontal;
527 }
528
529 EAPI void
530 elm_panes_fixed_set(Evas_Object *obj, Eina_Bool fixed)
531 {
532    ELM_CHECK_WIDTYPE(obj, widtype);
533    Widget_Data *wd = elm_widget_data_get(obj);
534    if (!wd) return;
535    wd->fixed = !!fixed;
536    if (wd->fixed == EINA_TRUE)
537      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
538    else
539      edje_object_signal_emit(wd->panes, "elm.panes.unfixed", "elm");
540 }
541
542 EAPI Eina_Bool
543 elm_panes_fixed_get(const Evas_Object *obj)
544 {
545    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
546    Widget_Data *wd = elm_widget_data_get(obj);
547    if (!wd) return EINA_FALSE;
548    return wd->fixed;
549 }