Merge branch 'intefl/svn_merge' of ssh://165.213.149.219:21922/slp/pkgs/e/elementary...
[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
184 static void
185 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
186 {
187    evas_object_smart_callback_call(data, SIG_CLICKED, NULL);
188 }
189
190 static void
191 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
192 {
193    Widget_Data *wd = elm_widget_data_get(data);
194
195    wd->clicked_double = EINA_TRUE;
196 }
197
198 static void
199 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
200 {
201    evas_object_smart_callback_call(data, SIG_PRESS, NULL);
202 }
203
204 static void
205 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
206 {
207    Widget_Data *wd = elm_widget_data_get(data);
208    evas_object_smart_callback_call(data, SIG_UNPRESS, NULL);
209
210    if (wd->clicked_double)
211      {
212         evas_object_smart_callback_call(data, SIG_CLICKED_DOUBLE, NULL);
213         wd->clicked_double = EINA_FALSE;
214      }
215 }
216
217 static void
218 _content_left_set(Evas_Object *obj, Evas_Object *content)
219 {
220    Widget_Data *wd = elm_widget_data_get(obj);
221    if (wd->contents.left)
222      {
223         evas_object_del(wd->contents.left);
224         wd->contents.left = NULL;
225         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
226      }
227    if (content)
228      {
229         wd->contents.left = content;
230         elm_widget_sub_object_add(obj, content);
231         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
232         if (wd->contents.right)
233           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
234      }
235 }
236
237 static void
238 _content_right_set(Evas_Object *obj, Evas_Object *content)
239 {
240    Widget_Data *wd = elm_widget_data_get(obj);
241    if (wd->contents.right)
242      {
243         evas_object_del(wd->contents.right);
244         wd->contents.right = NULL;
245         edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
246      }
247    if (content)
248      {
249         wd->contents.right = content;
250         elm_widget_sub_object_add(obj, content);
251         edje_object_part_swallow(wd->panes, "elm.swallow.right", content);
252         if (wd->contents.left)
253           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
254      }
255 }
256
257 static Evas_Object *
258 _content_left_unset(Evas_Object *obj)
259 {
260    Widget_Data *wd = elm_widget_data_get(obj);
261    if (!wd->contents.left) return NULL;
262    Evas_Object *content = wd->contents.left;
263
264    edje_object_part_unswallow(wd->panes, content);
265    evas_object_hide(wd->contents.left);
266    elm_widget_sub_object_del(obj, content);
267    wd->contents.left = NULL;
268    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
269    return content;
270 }
271
272 static Evas_Object *
273 _content_right_unset(Evas_Object *obj)
274 {
275    Widget_Data *wd = elm_widget_data_get(obj);
276    if (!wd->contents.right) return NULL;
277    Evas_Object *content = wd->contents.right;
278
279    edje_object_part_unswallow(wd->panes, content);
280    evas_object_hide(wd->contents.right);
281    elm_widget_sub_object_del(obj, content);
282    wd->contents.right = NULL;
283    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
284    return content;
285 }
286
287 static void
288 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
289 {
290    ELM_CHECK_WIDTYPE(obj, widtype);
291    Widget_Data *wd = elm_widget_data_get(obj);
292    if (!wd) return;
293    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
294    if (!part || !strcmp(part, "left"))
295      _content_left_set(obj, content);
296    else if(!strcmp(part, "right"))
297      _content_right_set(obj, content);
298 }
299
300 static Evas_Object *
301 _content_get_hook(const Evas_Object *obj, const char *part)
302 {
303    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
304    Widget_Data *wd = elm_widget_data_get(obj);
305    if (!wd) return NULL;
306    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
307    if (!part || !strcmp(part, "left"))
308      return wd->contents.left;
309    else if (!strcmp(part, "right"))
310      return wd->contents.right;
311    return NULL;
312 }
313
314 static Evas_Object *
315 _content_unset_hook(Evas_Object *obj, const char *part)
316 {
317    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
318    Widget_Data *wd = elm_widget_data_get(obj);
319    if (!wd) return NULL;
320    if (part && (!strncmp(part, "elm.swallow.", 12))) part += 12;
321    if (!part || !strcmp(part, "left"))
322      return _content_left_unset(obj);
323    else if (!strcmp(part, "right"))
324      return _content_right_unset(obj);
325    return NULL;
326 }
327
328 EAPI Evas_Object *
329 elm_panes_add(Evas_Object *parent)
330 {
331    Evas_Object *obj;
332    Evas *e;
333    Widget_Data *wd;
334
335    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
336
337    ELM_SET_WIDTYPE(widtype, "panes");
338    elm_widget_type_set(obj, "panes");
339    elm_widget_sub_object_add(parent, obj);
340    elm_widget_data_set(obj, wd);
341    elm_widget_del_hook_set(obj, _del_hook);
342    elm_widget_theme_hook_set(obj, _theme_hook);
343    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
344    elm_widget_content_set_hook_set(obj, _content_set_hook);
345    elm_widget_content_get_hook_set(obj, _content_get_hook);
346    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
347    elm_widget_can_focus_set(obj, EINA_FALSE);
348
349    wd->panes = edje_object_add(e);
350    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
351    elm_widget_resize_object_set(obj, wd->panes);
352    evas_object_show(wd->panes);
353
354    elm_panes_content_left_size_set(obj, 0.5);
355
356    edje_object_signal_callback_add(wd->panes, "elm,action,click", "",
357                                    _clicked, obj);
358    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "",
359                                    _clicked_double, obj);
360    edje_object_signal_callback_add(wd->panes, "elm,action,press", "",
361                                    _press, obj);
362    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "",
363                                    _unpress, obj);
364
365    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
366    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
367                                   _changed_size_hints, obj);
368
369    evas_object_smart_callbacks_descriptions_set(obj, _signals);
370
371    _mirrored_set(obj, elm_widget_mirrored_get(obj));
372    _sizing_eval(obj);
373    return obj;
374 }
375
376 EINA_DEPRECATED EAPI void
377 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
378 {
379    _content_set_hook(obj, "left", content);
380 }
381
382 EINA_DEPRECATED EAPI void
383 elm_panes_content_right_set(Evas_Object *obj, Evas_Object *content)
384 {
385    _content_set_hook(obj, "right", content);
386 }
387
388 EINA_DEPRECATED EAPI Evas_Object *
389 elm_panes_content_left_get(const Evas_Object *obj)
390 {
391    return _content_get_hook(obj, "left");
392 }
393
394 EINA_DEPRECATED EAPI Evas_Object *
395 elm_panes_content_right_get(const Evas_Object *obj)
396 {
397    return _content_get_hook(obj, "right");
398 }
399
400 EINA_DEPRECATED EAPI Evas_Object *
401 elm_panes_content_left_unset(Evas_Object *obj)
402 {
403    return _content_unset_hook(obj, "left");
404 }
405
406 EINA_DEPRECATED EAPI Evas_Object *
407 elm_panes_content_right_unset(Evas_Object *obj)
408 {
409    return _content_unset_hook(obj, "right");
410 }
411
412 EAPI double
413 elm_panes_content_left_size_get(const Evas_Object *obj)
414 {
415    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
416    Widget_Data *wd = elm_widget_data_get(obj);
417    double w, h;
418
419    if (!wd) return 0;
420    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
421    if (wd->horizontal) return h;
422    else return w;
423 }
424
425 EAPI void
426 elm_panes_content_left_size_set(Evas_Object *obj, double size)
427 {
428    ELM_CHECK_WIDTYPE(obj, widtype);
429    Widget_Data *wd = elm_widget_data_get(obj);
430    if (!wd) return;
431    if (size < 0.0) size = 0.0;
432    else if (size > 1.0) size = 1.0;
433    if (wd->horizontal)
434      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
435    else
436      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
437 }
438
439 EAPI double
440 elm_panes_content_right_size_get(const Evas_Object *obj)
441 {
442    return (1.0 - elm_panes_content_left_size_get(obj));
443 }
444
445 EAPI void
446 elm_panes_content_right_size_set(Evas_Object *obj, double size)
447 {
448    elm_panes_content_left_size_set(obj, (1.0 - size));
449 }
450
451 EAPI void
452 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
453 {
454    ELM_CHECK_WIDTYPE(obj, widtype);
455    Widget_Data *wd = elm_widget_data_get(obj);
456    if (!wd) return;
457    wd->horizontal = horizontal;
458    _theme_hook(obj);
459    elm_panes_content_left_size_set(obj, 0.5);
460 }
461
462 EAPI Eina_Bool
463 elm_panes_horizontal_get(const Evas_Object *obj)
464 {
465    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
466    Widget_Data *wd = elm_widget_data_get(obj);
467    if (!wd) return EINA_FALSE;
468    return wd->horizontal;
469 }
470
471 EAPI void
472 elm_panes_fixed_set(Evas_Object *obj, Eina_Bool fixed)
473 {
474    ELM_CHECK_WIDTYPE(obj, widtype);
475    Widget_Data *wd = elm_widget_data_get(obj);
476    if (!wd) return;
477    wd->fixed = !!fixed;
478    if (wd->fixed == EINA_TRUE)
479      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
480    else
481      edje_object_signal_emit(wd->panes, "elm.panes.unfixed", "elm");
482 }
483
484 EAPI Eina_Bool
485 elm_panes_fixed_get(const Evas_Object *obj)
486 {
487    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
488    Widget_Data *wd = elm_widget_data_get(obj);
489    if (!wd) return EINA_FALSE;
490    return wd->fixed;
491 }