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