Merge branch 'master' of 165.213.180.234: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  * @defgroup Panes panes
6  * @ingroup Elementary
7  *
8  */
9
10 typedef struct _Widget_Data Widget_Data;
11
12 struct _Widget_Data
13 {
14    Evas_Object *panes;
15
16    struct
17      {
18         Evas_Object *left;
19         Evas_Object *right;
20      } contents;
21
22    struct
23      {
24         int x_diff;
25         int y_diff;
26         Eina_Bool move;
27      } move;
28
29    Eina_Bool clicked_double;
30    Eina_Bool horizontal;
31    Eina_Bool fixed;
32 };
33
34 static const char *widtype = NULL;
35 static void _del_hook(Evas_Object *obj);
36 static void _theme_hook(Evas_Object *obj);
37 static void _sizing_eval(Evas_Object *obj);
38 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
39
40 static void
41 _del_hook(Evas_Object *obj)
42 {
43    Widget_Data *wd = elm_widget_data_get(obj);
44    if (!wd) return;
45    if (wd->panes) evas_object_del(wd->panes);
46    free(wd);
47 }
48
49 static void
50 _theme_hook(Evas_Object *obj)
51 {
52    Widget_Data *wd = elm_widget_data_get(obj);
53    const char *style = elm_widget_style_get(obj);
54    double size;
55
56    if (!wd) return;
57    size = elm_panes_content_left_size_get(obj);
58    
59    if (wd->horizontal)
60      _elm_theme_object_set(obj, wd->panes, "panes", "horizontal", style);
61    else
62      _elm_theme_object_set(obj, wd->panes, "panes", "vertical", style);
63
64    if (wd->contents.left)
65      edje_object_part_swallow(wd->panes, "elm.swallow.left", wd->contents.left);
66    if (wd->contents.right)
67      edje_object_part_swallow(wd->panes, "elm.swallow.right", wd->contents.right);
68    if(wd->contents.left && wd->contents.right)
69      edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
70    if(wd->fixed)
71            edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
72    edje_object_scale_set(wd->panes, elm_widget_scale_get(obj) *
73                          _elm_config->scale);
74    _sizing_eval(obj);
75    elm_panes_content_left_size_set(obj, size);
76 }
77
78 static Eina_Bool
79 _elm_panes_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
80 {
81    Widget_Data *wd = elm_widget_data_get(obj);
82
83    if (!wd)
84      return EINA_FALSE;
85
86    double w, h;
87    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
88    if (((wd->horizontal) && ( h == 0.0 )) || ((!wd->horizontal) && ( w == 0.0 )))
89      return elm_widget_focus_next_get(wd->contents.right, dir, next);
90
91    Evas_Object *chain[2];
92
93    /* Direction */
94    if (dir == ELM_FOCUS_PREVIOUS)
95      {
96         chain[0] = wd->contents.right;
97         chain[1] = wd->contents.left;
98      }
99    else if (dir == ELM_FOCUS_NEXT)
100      {
101         chain[0] = wd->contents.left;
102         chain[1] = wd->contents.right;
103      }
104    else
105      return EINA_FALSE;
106
107    unsigned char i = elm_widget_focus_get(chain[1]);
108
109    if (elm_widget_focus_next_get(chain[i], dir, next))
110      return EINA_TRUE;
111
112    i = !i;
113
114    Evas_Object *to_focus;
115    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
116      {
117         *next = to_focus;
118         return !!i;
119      }
120
121    return EINA_FALSE;
122 }
123
124 static void
125 _sizing_eval(Evas_Object *obj)
126 {
127    Widget_Data *wd = elm_widget_data_get(obj);
128    if (!wd) return;
129 }
130
131 static void
132 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
133 {
134    _sizing_eval(data);
135 }
136
137 static void
138 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
139 {
140    Widget_Data *wd = elm_widget_data_get(obj);
141    Evas_Object *sub = event_info;
142
143    if (!wd) return;
144    if (sub == wd->contents.left)
145      {
146         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
147                                             _changed_size_hints, obj);
148         wd->contents.left = NULL;
149         _sizing_eval(obj);
150      }
151    else if (sub == wd->contents.right)
152      {
153         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
154                                             _changed_size_hints, obj);
155         wd->contents.right= NULL;
156         _sizing_eval(obj);
157      }
158 }
159
160 static void
161 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
162 {
163    evas_object_smart_callback_call(data, "clicked", NULL);
164 }
165
166 static void
167 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
168 {
169    Widget_Data *wd = elm_widget_data_get(data);
170
171    wd->clicked_double = EINA_TRUE;
172 }
173
174 static void
175 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
176 {
177    evas_object_smart_callback_call(data, "press", NULL);
178 }
179
180 static void
181 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
182 {
183    Widget_Data *wd = elm_widget_data_get(data);
184    evas_object_smart_callback_call(data, "unpress", NULL);
185
186    if (wd->clicked_double)
187      {
188         evas_object_smart_callback_call(data, "clicked,double", NULL);
189         wd->clicked_double = EINA_FALSE;
190      }
191 }
192
193 /**
194  * Add a new panes to the parent
195  *
196  * @param[in] parent The parent object
197  * @return The new object or NULL if it cannot be created
198  *
199  * @ingroup Panes
200  */
201 EAPI Evas_Object *
202 elm_panes_add(Evas_Object *parent)
203 {
204    Evas_Object *obj;
205    Evas *e;
206    Widget_Data *wd;
207
208    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
209
210    wd = ELM_NEW(Widget_Data);
211    e = evas_object_evas_get(parent);
212    if (!e) return NULL;
213    obj = elm_widget_add(e);
214    ELM_SET_WIDTYPE(widtype, "panes");
215    elm_widget_type_set(obj, "panes");
216    elm_widget_can_focus_set(obj, EINA_FALSE);
217    elm_widget_sub_object_add(parent, obj);
218    elm_widget_data_set(obj, wd);
219    elm_widget_del_hook_set(obj, _del_hook);
220    elm_widget_theme_hook_set(obj, _theme_hook);
221    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
222    wd->contents.left = NULL;
223    wd->contents.right = NULL;
224
225    wd->panes = edje_object_add(e);
226    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
227    elm_widget_resize_object_set(obj, wd->panes);
228    evas_object_show(wd->panes);
229
230    elm_panes_content_left_size_set(obj, 0.5);
231
232    edje_object_signal_callback_add(wd->panes, "elm,action,click", "", 
233                                    _clicked, obj);
234    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "", 
235                                    _clicked_double, obj);
236    edje_object_signal_callback_add(wd->panes, "elm,action,press", "", 
237                                    _press, obj);
238    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "", 
239                                    _unpress, obj);
240
241    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
242    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, 
243                                   _changed_size_hints, obj);
244
245    _sizing_eval(obj);
246    return obj;
247 }
248
249 /**
250  * Set the left/top content of the panes widget
251  *
252  * Once the content object is set, a previously set one will be deleted.
253  * If you want to keep that old content object, use the
254  * elm_panes_content_left_unset() function.
255  *
256  * @param[in] obj The panes object
257  * @param[in] content The new left/top content object
258  *
259  * @ingroup Panes
260  */
261 EAPI void
262 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype);
265    Widget_Data *wd = elm_widget_data_get(obj);
266    if (wd->contents.left)
267      {
268         evas_object_del(wd->contents.left);
269         wd->contents.left = NULL;
270      }
271    if (content)
272      {
273         wd->contents.left = content;
274         elm_widget_sub_object_add(obj, content);
275         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
276         if (wd->contents.right)
277           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
278      }
279    else
280       edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
281 }
282
283 /**
284  * Set the right/bottom 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[in] obj The panes object
291  * @param[in] content The new right/bottom 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         if (wd->contents.left)
311           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
312      }
313    else
314      edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
315 }
316
317 /**
318  * Get the left/top content used for the panes
319  *
320  * Return the left/top content object which is set for this widget.
321  *
322  * @param[in] obj The panes object
323  * @return The left/top content object that is being used
324  *
325  * @ingroup Panes
326  */
327 EAPI Evas_Object *
328 elm_panes_content_left_get(const Evas_Object *obj)
329 {
330    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
331    Widget_Data *wd = elm_widget_data_get(obj);
332    return wd->contents.left;
333 }
334
335 /**
336  * Get the right/bottom content used for the panes
337  *
338  * Return the right/bottom content object which is set for this widget.
339  *
340  * @param[in] obj The panes object
341  * @return The right/bottom content object that is being used
342  *
343  * @ingroup Panes
344  */
345 EAPI Evas_Object *
346 elm_panes_content_right_get(const Evas_Object *obj)
347 {
348    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
349    Widget_Data *wd = elm_widget_data_get(obj);
350    return wd->contents.right;
351 }
352
353 /**
354  * Unset the left/top content used for the panes
355  *
356  * Unparent and return the left content object which was set for this widget.
357  *
358  * @param[in] obj The panes object
359  * @return The left/top content object that was being used
360  *
361  * @ingroup Panes
362  */
363 EAPI Evas_Object *
364 elm_panes_content_left_unset(Evas_Object *obj)
365 {
366    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
367    Widget_Data *wd = elm_widget_data_get(obj);
368    if (!wd) return NULL;
369    if (!wd->contents.left) return NULL;
370    Evas_Object *content = wd->contents.left;
371    elm_widget_sub_object_del(obj, content);
372    edje_object_part_unswallow(wd->panes, content);
373    evas_object_hide(content);
374    wd->contents.left = NULL;
375    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
376    return content;
377 }
378
379 /**
380  * Unset the right/bottom content used for the panes
381  *
382  * Unparent and return the right content object which was set for this widget.
383  *
384  * @param[in] obj The panes object
385   * @return The right/bottom content object that was being used
386  *
387  * @ingroup Panes
388  */
389 EAPI Evas_Object *
390 elm_panes_content_right_unset(Evas_Object *obj)
391 {
392    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
393    Widget_Data *wd = elm_widget_data_get(obj);
394    if (!wd) return NULL;
395    if (!wd->contents.right) return NULL;
396    Evas_Object *content = wd->contents.right;
397    elm_widget_sub_object_del(obj, content);
398    edje_object_part_unswallow(wd->panes, content);
399    evas_object_hide(content);
400    wd->contents.right = NULL;
401    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
402    return content;
403 }
404
405 /**
406  * Get the relative normalized size of left/top content of the pane
407  *
408  * @param[in] obj The panes object
409  * @return The value of type double in the range [0.0,1.0]
410  *
411  * @ingroup Panes
412  */
413 EAPI double 
414 elm_panes_content_left_size_get(const Evas_Object *obj)
415 {
416    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
417    Widget_Data *wd = elm_widget_data_get(obj);
418    double w, h;
419
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 /**
426  * Set a size of the left/top content with a relative normalized double value
427  *
428  * @param[in] obj The panes object
429  * @param[in] size The value of type double in the range [0.0,1.0]
430  *
431  * @ingroup Panes
432  */
433 EAPI void 
434 elm_panes_content_left_size_set(Evas_Object *obj, double size)
435 {
436    ELM_CHECK_WIDTYPE(obj, widtype);
437    Widget_Data *wd = elm_widget_data_get(obj);
438
439    if (size < 0.0) size = 0.0;
440    else if (size > 1.0) size = 1.0;
441    if (wd->horizontal)
442      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
443    else
444      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
445 }
446
447 /**
448  * Set the type of an existing panes object to horizontal/vertical
449  *
450  * By default the panes is of vertical type
451  *
452  * @param[in] obj The panes object
453  * @param[in] horizontal Boolean value. If true, then the type is set to horizontal else vertical
454  *
455  * @ingroup Panes
456  */
457 EAPI void 
458 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
459 {
460    ELM_CHECK_WIDTYPE(obj, widtype);
461    Widget_Data *wd = elm_widget_data_get(obj);
462
463    wd->horizontal = horizontal;
464    _theme_hook(obj);
465    elm_panes_content_left_size_set(obj, 0.5);
466 }
467
468 /**
469  * Indicate if the type of pane object is horizontal or not
470  *
471  * @param[in] obj The panes object
472  * @return true if it is of horizontal type else false
473  *
474  * @ingroup Panes
475  */
476 EAPI Eina_Bool 
477 elm_panes_horizontal_get(const Evas_Object *obj)
478 {
479    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
480    Widget_Data *wd = elm_widget_data_get(obj);
481    return wd->horizontal;
482 }
483
484 /**
485  * Set a handler of the pane object movable or non-movable
486  *
487  * @param[in] obj The panes object
488  * @param[in] fixed If set to true then the views size can't be changed using handler otherwise using handler they can be resized
489  *
490  * @ingroup Panes
491  */
492 EAPI void
493 elm_panes_fixed_set(Evas_Object *obj, Eina_Bool fixed)
494 {
495    ELM_CHECK_WIDTYPE(obj, widtype);
496    Widget_Data *wd = elm_widget_data_get(obj);
497    wd->fixed = fixed;
498    if (wd->fixed == EINA_TRUE)
499      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
500    else
501      edje_object_signal_emit(wd->panes, "elm.panes.unfixed", "elm");
502 }
503
504 /**
505  * Indicate if the handler of the panes object can be moved with user interaction
506  *
507  * @param[in] obj The panes object
508  * @return false if the views can be resized using handler else true
509  *
510  * @ingroup Panes
511  */
512 EAPI Eina_Bool
513 elm_panes_fixed_get(const Evas_Object *obj)
514 {
515    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
516    Widget_Data *wd = elm_widget_data_get(obj);
517    return wd->fixed;
518 }