Elementary: Added on-the-fly UI-mirroing support to all of the widgets
[framework/uifw/elementary.git] / src / lib / elm_panes.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Panes panes
6  *
7  */
8
9 typedef struct _Widget_Data Widget_Data;
10
11 struct _Widget_Data
12 {
13    Evas_Object *panes;
14
15    struct
16      {
17         Evas_Object *left;
18         Evas_Object *right;
19      } contents;
20
21    struct
22      {
23         int x_diff;
24         int y_diff;
25         Eina_Bool move;
26      } move;
27
28    Eina_Bool clicked_double;
29    Eina_Bool horizontal;
30 };
31
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
35 static void _theme_hook(Evas_Object *obj);
36 static void _sizing_eval(Evas_Object *obj);
37 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
38
39 static void
40 _del_hook(Evas_Object *obj)
41 {
42    Widget_Data *wd = elm_widget_data_get(obj);
43    if (!wd) return;
44    free(wd);
45 }
46
47 static void
48 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    edje_object_mirrored_set(wd->panes, rtl);
53 }
54
55 static void
56 _theme_hook(Evas_Object *obj)
57 {
58    Widget_Data *wd = elm_widget_data_get(obj);
59    const char *style = elm_widget_style_get(obj);
60    double size;
61
62    if (!wd) return;
63    _elm_widget_mirrored_reload(obj);
64    _mirrored_set(obj, elm_widget_mirrored_get(obj));
65    size = elm_panes_content_left_size_get(obj);
66    
67    if (wd->horizontal)
68      _elm_theme_object_set(obj, wd->panes, "panes", "horizontal", style);
69    else
70      _elm_theme_object_set(obj, wd->panes, "panes", "vertical", style);
71
72    if (wd->contents.left)
73      edje_object_part_swallow(wd->panes, "elm.swallow.left", wd->contents.left);
74    if (wd->contents.right)
75      edje_object_part_swallow(wd->panes, "elm.swallow.right", wd->contents.right);
76
77    edje_object_scale_set(wd->panes, elm_widget_scale_get(obj) *
78                          _elm_config->scale);
79    _sizing_eval(obj);
80    elm_panes_content_left_size_set(obj, size);
81 }
82
83 static Eina_Bool
84 _elm_panes_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
85 {
86    Widget_Data *wd = elm_widget_data_get(obj);
87
88    if (!wd)
89      return EINA_FALSE;
90
91    double w, h;
92    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
93    if (((wd->horizontal) && ( h == 0.0 )) || ((!wd->horizontal) && ( w == 0.0 )))
94      return elm_widget_focus_next_get(wd->contents.right, dir, next);
95
96    Evas_Object *chain[2];
97
98    /* Direction */
99    if (dir == ELM_FOCUS_PREVIOUS)
100      {
101         chain[0] = wd->contents.right;
102         chain[1] = wd->contents.left;
103      }
104    else if (dir == ELM_FOCUS_NEXT)
105      {
106         chain[0] = wd->contents.left;
107         chain[1] = wd->contents.right;
108      }
109    else
110      return EINA_FALSE;
111
112    unsigned char i = elm_widget_focus_get(chain[1]);
113
114    if (elm_widget_focus_next_get(chain[i], dir, next))
115      return EINA_TRUE;
116
117    i = !i;
118
119    Evas_Object *to_focus;
120    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
121      {
122         *next = to_focus;
123         return !!i;
124      }
125
126    return EINA_FALSE;
127 }
128
129 static void
130 _sizing_eval(Evas_Object *obj)
131 {
132    Widget_Data *wd = elm_widget_data_get(obj);
133    if (!wd) return;
134 }
135
136 static void
137 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
138 {
139    _sizing_eval(data);
140 }
141
142 static void
143 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
144 {
145    Widget_Data *wd = elm_widget_data_get(obj);
146    Evas_Object *sub = event_info;
147
148    if (!wd) return;
149    if (sub == wd->contents.left)
150      {
151         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
152                                             _changed_size_hints, obj);
153         wd->contents.left = NULL;
154         _sizing_eval(obj);
155      }
156    else if (sub == wd->contents.right)
157      {
158         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
159                                             _changed_size_hints, obj);
160         wd->contents.right= NULL;
161         _sizing_eval(obj);
162      }
163 }
164
165 static void
166 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
167 {
168    evas_object_smart_callback_call(data, "clicked", NULL);
169 }
170
171 static void
172 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
173 {
174    Widget_Data *wd = elm_widget_data_get(data);
175
176    wd->clicked_double = EINA_TRUE;
177 }
178
179 static void
180 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
181 {
182    evas_object_smart_callback_call(data, "press", NULL);
183 }
184
185 static void
186 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
187 {
188    Widget_Data *wd = elm_widget_data_get(data);
189    evas_object_smart_callback_call(data, "unpress", NULL);
190
191    if (wd->clicked_double)
192      {
193         evas_object_smart_callback_call(data, "clicked,double", NULL);
194         wd->clicked_double = EINA_FALSE;
195      }
196 }
197
198 /**
199  * Add a new panes to the parent
200  *
201  * @param parent The parent object
202  * @return The new object or NULL if it cannot be created
203  *
204  * @ingroup Panel
205  */
206 EAPI Evas_Object *
207 elm_panes_add(Evas_Object *parent)
208 {
209    Evas_Object *obj;
210    Evas *e;
211    Widget_Data *wd;
212
213    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
214
215    wd = ELM_NEW(Widget_Data);
216    e = evas_object_evas_get(parent);
217    if (!e) return NULL;
218    obj = elm_widget_add(e);
219    ELM_SET_WIDTYPE(widtype, "panes");
220    elm_widget_type_set(obj, "panes");
221    elm_widget_sub_object_add(parent, obj);
222    elm_widget_data_set(obj, wd);
223    elm_widget_del_hook_set(obj, _del_hook);
224    elm_widget_theme_hook_set(obj, _theme_hook);
225    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
226    elm_widget_can_focus_set(obj, EINA_FALSE);
227
228    wd->panes = edje_object_add(e);
229    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
230    elm_widget_resize_object_set(obj, wd->panes);
231    evas_object_show(wd->panes);
232
233    elm_panes_content_left_size_set(obj, 0.5);
234
235    edje_object_signal_callback_add(wd->panes, "elm,action,click", "", 
236                                    _clicked, obj);
237    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "", 
238                                    _clicked_double, obj);
239    edje_object_signal_callback_add(wd->panes, "elm,action,press", "", 
240                                    _press, obj);
241    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "", 
242                                    _unpress, obj);
243
244    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
245    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, 
246                                   _changed_size_hints, obj);
247
248    _mirrored_set(obj, elm_widget_mirrored_get(obj));
249    _sizing_eval(obj);
250    return obj;
251 }
252
253
254 /**
255  * Set the left content of the panes widget.
256  *
257  * Once the content object is set, a previously set one will be deleted.
258  * If you want to keep that old content object, use the
259  * elm_panes_content_left_unset() function.
260  *
261  * @param obj The panes object
262  * @param content The new left content object
263  *
264  * @ingroup Panes
265  */
266 EAPI void
267 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
268 {
269    ELM_CHECK_WIDTYPE(obj, widtype);
270    Widget_Data *wd = elm_widget_data_get(obj);
271    if (wd->contents.left)
272      {
273         evas_object_del(wd->contents.left);
274         wd->contents.left = NULL;
275      }
276    if (content)
277      {
278         wd->contents.left = content;
279         elm_widget_sub_object_add(obj, content);
280         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
281      }
282 }
283
284 /**
285  * Set the right content of the panes widget.
286  *
287  * Once the content object is set, a previously set one will be deleted.
288  * If you want to keep that old content object, use the
289  * elm_panes_content_right_unset() function.
290  *
291  * @param obj The panes object
292  * @param content The new right content object
293  *
294  * @ingroup Panes
295  */
296 EAPI void
297 elm_panes_content_right_set(Evas_Object *obj, Evas_Object *content)
298 {
299    ELM_CHECK_WIDTYPE(obj, widtype);
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (wd->contents.right)
302      {
303         evas_object_del(wd->contents.right);
304         wd->contents.right = NULL;
305      }
306    if (content)
307      {
308         wd->contents.right = content;
309         elm_widget_sub_object_add(obj, content);
310         edje_object_part_swallow(wd->panes, "elm.swallow.right", content);
311      }
312 }
313
314 /**
315  * Get the left content used for the panes
316  *
317  * Return the left content object which is set for this widget.
318  *
319  * @param obj The panes object
320  * @return The left content object that is being used
321  *
322  * @ingroup Panes
323  */
324 EAPI Evas_Object *
325 elm_panes_content_left_get(const Evas_Object *obj)
326 {
327    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
328    Widget_Data *wd = elm_widget_data_get(obj);
329    return wd->contents.left;
330 }
331
332 /**
333  * Get the right content used for the panes
334  *
335  * Return the right content object which is set for this widget.
336  *
337  * @param obj The panes object
338  * @return The right content object that is being used
339  *
340  * @ingroup Panes
341  */
342 EAPI Evas_Object *
343 elm_panes_content_right_get(const Evas_Object *obj)
344 {
345    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
346    Widget_Data *wd = elm_widget_data_get(obj);
347    return wd->contents.right;
348 }
349
350 /**
351  * Unset the left content used for the panes
352  *
353  * Unparent and return the left content object which was set for this widget.
354  *
355  * @param obj The panes object
356  * @return The left content object that was being used
357  *
358  * @ingroup Panes
359  */
360 EAPI Evas_Object *
361 elm_panes_content_left_unset(Evas_Object *obj)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return NULL;
366    if (!wd->contents.left) return NULL;
367    Evas_Object *content = wd->contents.left;
368    elm_widget_sub_object_del(obj, content);
369    edje_object_part_unswallow(wd->panes, content);
370    wd->contents.left = NULL;
371    return content;
372 }
373
374 /**
375  * Unset the right content used for the panes
376  *
377  * Unparent and return the right content object which was set for this widget.
378  *
379  * @param obj The panes object
380  * @return The right content object that was being used
381  *
382  * @ingroup Panes
383  */
384 EAPI Evas_Object *
385 elm_panes_content_right_unset(Evas_Object *obj)
386 {
387    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
388    Widget_Data *wd = elm_widget_data_get(obj);
389    if (!wd) return NULL;
390    if (!wd->contents.right) return NULL;
391    Evas_Object *content = wd->contents.right;
392    elm_widget_sub_object_del(obj, content);
393    edje_object_part_unswallow(wd->panes, content);
394    wd->contents.right = NULL;
395    return content;
396 }
397
398 EAPI double 
399 elm_panes_content_left_size_get(const Evas_Object *obj)
400 {
401    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
402    Widget_Data *wd = elm_widget_data_get(obj);
403    double w, h;
404
405    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
406    if (wd->horizontal) return h;
407    else return w;
408 }
409
410 EAPI void 
411 elm_panes_content_left_size_set(Evas_Object *obj, double size)
412 {
413    ELM_CHECK_WIDTYPE(obj, widtype);
414    Widget_Data *wd = elm_widget_data_get(obj);
415
416    if (size < 0.0) size = 0.0;
417    else if (size > 1.0) size = 1.0;
418    if (wd->horizontal)
419      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
420    else
421      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
422 }
423
424 EAPI void 
425 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
426 {
427    ELM_CHECK_WIDTYPE(obj, widtype);
428    Widget_Data *wd = elm_widget_data_get(obj);
429
430    wd->horizontal = horizontal;
431    _theme_hook(obj);
432    elm_panes_content_left_size_set(obj, 0.5);
433 }
434
435 EAPI Eina_Bool 
436 elm_panes_horizontal_get(const Evas_Object *obj)
437 {
438    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
439    Widget_Data *wd = elm_widget_data_get(obj);
440    return wd->horizontal;
441 }