EFL migration revision 67547
[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    if(wd->contents.left && wd->contents.right)
93      edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
94    if(wd->fixed)
95      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
96
97    edje_object_scale_set(wd->panes, elm_widget_scale_get(obj) *
98                          _elm_config->scale);
99    _sizing_eval(obj);
100    elm_panes_content_left_size_set(obj, size);
101 }
102
103 static Eina_Bool
104 _elm_panes_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
105 {
106    Widget_Data *wd = elm_widget_data_get(obj);
107    if (!wd) return EINA_FALSE;
108
109    double w, h;
110    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
111    if (((wd->horizontal) && ( h == 0.0 )) || ((!wd->horizontal) && ( w == 0.0 )))
112      return elm_widget_focus_next_get(wd->contents.right, dir, next);
113
114    Evas_Object *chain[2];
115
116    /* Direction */
117    if (dir == ELM_FOCUS_PREVIOUS)
118      {
119         chain[0] = wd->contents.right;
120         chain[1] = wd->contents.left;
121      }
122    else if (dir == ELM_FOCUS_NEXT)
123      {
124         chain[0] = wd->contents.left;
125         chain[1] = wd->contents.right;
126      }
127    else
128      return EINA_FALSE;
129
130    unsigned char i = elm_widget_focus_get(chain[1]);
131
132    if (elm_widget_focus_next_get(chain[i], dir, next))
133      return EINA_TRUE;
134
135    i = !i;
136
137    Evas_Object *to_focus;
138    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
139      {
140         *next = to_focus;
141         return !!i;
142      }
143
144    return EINA_FALSE;
145 }
146
147 static void
148 _sizing_eval(Evas_Object *obj)
149 {
150    Widget_Data *wd = elm_widget_data_get(obj);
151    if (!wd) return;
152 }
153
154 static void
155 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
156 {
157    _sizing_eval(data);
158 }
159
160 static void
161 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
162 {
163    Widget_Data *wd = elm_widget_data_get(obj);
164    Evas_Object *sub = event_info;
165
166    if (!wd) return;
167    if (sub == wd->contents.left)
168      {
169         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
170                                             _changed_size_hints, obj);
171         wd->contents.left = NULL;
172         _sizing_eval(obj);
173      }
174    else if (sub == wd->contents.right)
175      {
176         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
177                                             _changed_size_hints, obj);
178         wd->contents.right= NULL;
179         _sizing_eval(obj);
180      }
181 }
182
183 static void
184 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
185 {
186    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
187 }
188
189 static void
190 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
191 {
192    Widget_Data *wd = elm_widget_data_get(data);
193
194    wd->clicked_double = EINA_TRUE;
195 }
196
197 static void
198 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
199 {
200    evas_object_smart_callback_call(data, SIG_PRESS, NULL);
201 }
202
203 static void
204 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
205 {
206    Widget_Data *wd = elm_widget_data_get(data);
207    evas_object_smart_callback_call(data, SIG_UNPRESS, NULL);
208
209    if (wd->clicked_double)
210      {
211         evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
212         wd->clicked_double = EINA_FALSE;
213      }
214 }
215
216 static void
217 _content_left_set(Evas_Object *obj, Evas_Object *content)
218 {
219    Widget_Data *wd = elm_widget_data_get(obj);
220    if (wd->contents.left)
221      {
222         evas_object_del(wd->contents.left);
223         wd->contents.left = NULL;
224         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
225      }
226    if (content)
227      {
228         wd->contents.left = content;
229         elm_widget_sub_object_add(obj, content);
230         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
231         if (wd->contents.right)
232           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
233      }
234 }
235
236 static void
237 _content_right_set(Evas_Object *obj, Evas_Object *content)
238 {
239    Widget_Data *wd = elm_widget_data_get(obj);
240    if (wd->contents.right)
241      {
242         evas_object_del(wd->contents.right);
243         wd->contents.right = NULL;
244         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
245      }
246    if (content)
247      {
248         wd->contents.right = content;
249         elm_widget_sub_object_add(obj, content);
250         edje_object_part_swallow(wd->panes, "elm.swallow.right", content);
251         if (wd->contents.left)
252           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
253      }
254 }
255
256 static Evas_Object *
257 _content_left_unset(Evas_Object *obj)
258 {
259    Widget_Data *wd = elm_widget_data_get(obj);
260    if (!wd->contents.left) return NULL;
261    Evas_Object *content = wd->contents.left;
262
263    edje_object_part_unswallow(wd->panes, content);
264    evas_object_hide(wd->contents.left);
265    elm_widget_sub_object_del(obj, content);
266    wd->contents.left = NULL;
267    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
268    return content;
269 }
270
271 static Evas_Object *
272 _content_right_unset(Evas_Object *obj)
273 {
274    Widget_Data *wd = elm_widget_data_get(obj);
275    if (!wd->contents.right) return NULL;
276    Evas_Object *content = wd->contents.right;
277
278    edje_object_part_unswallow(wd->panes, content);
279    evas_object_hide(wd->contents.right);
280    elm_widget_sub_object_del(obj, content);
281    wd->contents.right = NULL;
282    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
283    return content;
284 }
285
286 static void
287 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
288 {
289    ELM_CHECK_WIDTYPE(obj, widtype);
290    Widget_Data *wd = elm_widget_data_get(obj);
291    if (!wd) return;
292    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
293    if (!part || !strcmp(part, "left"))
294      _content_left_set(obj, content);
295    else if(!strcmp(part, "right"))
296      _content_right_set(obj, content);
297 }
298
299 static Evas_Object *
300 _content_get_hook(const Evas_Object *obj, const char *part)
301 {
302    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
303    Widget_Data *wd = elm_widget_data_get(obj);
304    if (!wd) return NULL;
305    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
306    if (!part || !strcmp(part, "left"))
307      return wd->contents.left;
308    else if (!strcmp(part, "right"))
309      return wd->contents.right;
310    return NULL;
311 }
312
313 static Evas_Object *
314 _content_unset_hook(Evas_Object *obj, const char *part)
315 {
316    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
317    Widget_Data *wd = elm_widget_data_get(obj);
318    if (!wd) return NULL;
319    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
320    if (!part || !strcmp(part, "left"))
321      return _content_left_unset(obj);
322    else if (!strcmp(part, "right"))
323      return _content_right_unset(obj);
324    return NULL;
325 }
326
327 EAPI Evas_Object *
328 elm_panes_add(Evas_Object *parent)
329 {
330    Evas_Object *obj;
331    Evas *e;
332    Widget_Data *wd;
333
334    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
335
336    ELM_SET_WIDTYPE(widtype, "panes");
337    elm_widget_type_set(obj, "panes");
338    elm_widget_sub_object_add(parent, obj);
339    elm_widget_data_set(obj, wd);
340    elm_widget_del_hook_set(obj, _del_hook);
341    elm_widget_theme_hook_set(obj, _theme_hook);
342    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
343    elm_widget_content_set_hook_set(obj, _content_set_hook);
344    elm_widget_content_get_hook_set(obj, _content_get_hook);
345    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
346    elm_widget_can_focus_set(obj, EINA_FALSE);
347
348    wd->panes = edje_object_add(e);
349    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
350    elm_widget_resize_object_set(obj, wd->panes);
351    evas_object_show(wd->panes);
352
353    elm_panes_content_left_size_set(obj, 0.5);
354
355    edje_object_signal_callback_add(wd->panes, "elm,action,click", "",
356                                    _clicked, obj);
357    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "",
358                                    _clicked_double, obj);
359    edje_object_signal_callback_add(wd->panes, "elm,action,press", "",
360                                    _press, obj);
361    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "",
362                                    _unpress, obj);
363
364    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
365    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
366                                   _changed_size_hints, obj);
367
368    evas_object_smart_callbacks_descriptions_set(obj, _signals);
369
370    _mirrored_set(obj, elm_widget_mirrored_get(obj));
371    _sizing_eval(obj);
372    return obj;
373 }
374
375 EAPI void
376 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
377 {
378    _content_set_hook(obj, "left", content);
379 }
380
381 EAPI void
382 elm_panes_content_right_set(Evas_Object *obj, Evas_Object *content)
383 {
384    _content_set_hook(obj, "right", content);
385 }
386
387 EAPI Evas_Object *
388 elm_panes_content_left_get(const Evas_Object *obj)
389 {
390    return _content_get_hook(obj, "left");
391 }
392
393 EAPI Evas_Object *
394 elm_panes_content_right_get(const Evas_Object *obj)
395 {
396    return _content_get_hook(obj, "right");
397 }
398
399 EAPI Evas_Object *
400 elm_panes_content_left_unset(Evas_Object *obj)
401 {
402    return _content_unset_hook(obj, "left");
403 }
404
405 EAPI Evas_Object *
406 elm_panes_content_right_unset(Evas_Object *obj)
407 {
408    return _content_unset_hook(obj, "right");
409 }
410
411 EAPI double
412 elm_panes_content_left_size_get(const Evas_Object *obj)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
415    Widget_Data *wd = elm_widget_data_get(obj);
416    double w, h;
417
418    if (!wd) return 0;
419    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
420    if (wd->horizontal) return h;
421    else return w;
422 }
423
424 EAPI void
425 elm_panes_content_left_size_set(Evas_Object *obj, double size)
426 {
427    ELM_CHECK_WIDTYPE(obj, widtype);
428    Widget_Data *wd = elm_widget_data_get(obj);
429    if (!wd) return;
430    if (size < 0.0) size = 0.0;
431    else if (size > 1.0) size = 1.0;
432    if (wd->horizontal)
433      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
434    else
435      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
436 }
437
438 EAPI void
439 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
440 {
441    ELM_CHECK_WIDTYPE(obj, widtype);
442    Widget_Data *wd = elm_widget_data_get(obj);
443    if (!wd) return;
444    wd->horizontal = horizontal;
445    _theme_hook(obj);
446    elm_panes_content_left_size_set(obj, 0.5);
447 }
448
449 EAPI Eina_Bool
450 elm_panes_horizontal_get(const Evas_Object *obj)
451 {
452    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
453    Widget_Data *wd = elm_widget_data_get(obj);
454    if (!wd) return EINA_FALSE;
455    return wd->horizontal;
456 }
457
458 EAPI void
459 elm_panes_fixed_set(Evas_Object *obj, Eina_Bool fixed)
460 {
461    ELM_CHECK_WIDTYPE(obj, widtype);
462    Widget_Data *wd = elm_widget_data_get(obj);
463    if (!wd) return;
464    wd->fixed = !!fixed;
465    if (wd->fixed == EINA_TRUE)
466      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
467    else
468      edje_object_signal_emit(wd->panes, "elm.panes.unfixed", "elm");
469 }
470
471 EAPI Eina_Bool
472 elm_panes_fixed_get(const Evas_Object *obj)
473 {
474    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
475    Widget_Data *wd = elm_widget_data_get(obj);
476    if (!wd) return EINA_FALSE;
477    return wd->fixed;
478 }