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