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