[button,panes] Merged and updated the Button and Panes.
[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
69    edje_object_scale_set(wd->panes, elm_widget_scale_get(obj) *
70                          _elm_config->scale);
71    _sizing_eval(obj);
72    elm_panes_content_left_size_set(obj, size);
73 }
74
75 static Eina_Bool
76 _elm_panes_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
77 {
78    Widget_Data *wd = elm_widget_data_get(obj);
79
80    if (!wd)
81      return EINA_FALSE;
82
83    double w, h;
84    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
85    if (((wd->horizontal) && ( h == 0.0 )) || ((!wd->horizontal) && ( w == 0.0 )))
86      return elm_widget_focus_next_get(wd->contents.right, dir, next);
87
88    Evas_Object *chain[2];
89
90    /* Direction */
91    if (dir == ELM_FOCUS_PREVIOUS)
92      {
93         chain[0] = wd->contents.right;
94         chain[1] = wd->contents.left;
95      }
96    else if (dir == ELM_FOCUS_NEXT)
97      {
98         chain[0] = wd->contents.left;
99         chain[1] = wd->contents.right;
100      }
101    else
102      return EINA_FALSE;
103
104    unsigned char i = elm_widget_focus_get(chain[1]);
105
106    if (elm_widget_focus_next_get(chain[i], dir, next))
107      return EINA_TRUE;
108
109    i = !i;
110
111    Evas_Object *to_focus;
112    if (elm_widget_focus_next_get(chain[i], dir, &to_focus))
113      {
114         *next = to_focus;
115         return !!i;
116      }
117
118    return EINA_FALSE;
119 }
120
121 static void
122 _sizing_eval(Evas_Object *obj)
123 {
124    Widget_Data *wd = elm_widget_data_get(obj);
125    if (!wd) return;
126 }
127
128 static void
129 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
130 {
131    _sizing_eval(data);
132 }
133
134 static void
135 _sub_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
136 {
137    Widget_Data *wd = elm_widget_data_get(obj);
138    Evas_Object *sub = event_info;
139
140    if (!wd) return;
141    if (sub == wd->contents.left)
142      {
143         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
144                                             _changed_size_hints, obj);
145         wd->contents.left = NULL;
146         _sizing_eval(obj);
147      }
148    else if (sub == wd->contents.right)
149      {
150         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
151                                             _changed_size_hints, obj);
152         wd->contents.right= NULL;
153         _sizing_eval(obj);
154      }
155 }
156
157 static void
158 _clicked(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
159 {
160    evas_object_smart_callback_call(data, "clicked", NULL);
161 }
162
163 static void
164 _clicked_double(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
165 {
166    Widget_Data *wd = elm_widget_data_get(data);
167
168    wd->clicked_double = EINA_TRUE;
169 }
170
171 static void
172 _press(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
173 {
174    evas_object_smart_callback_call(data, "press", NULL);
175 }
176
177 static void
178 _unpress(void *data, Evas_Object *obj __UNUSED__ , const char *emission __UNUSED__, const char *source __UNUSED__)
179 {
180    Widget_Data *wd = elm_widget_data_get(data);
181    evas_object_smart_callback_call(data, "unpress", NULL);
182
183    if (wd->clicked_double)
184      {
185         evas_object_smart_callback_call(data, "clicked,double", NULL);
186         wd->clicked_double = EINA_FALSE;
187      }
188 }
189
190 /**
191  * Add a new panes to the parent
192  *
193  * @param[in] parent The parent object
194  * @return The new object or NULL if it cannot be created
195  *
196  * @ingroup Panes
197  */
198 EAPI Evas_Object *
199 elm_panes_add(Evas_Object *parent)
200 {
201    Evas_Object *obj;
202    Evas *e;
203    Widget_Data *wd;
204
205    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
206
207    wd = ELM_NEW(Widget_Data);
208    e = evas_object_evas_get(parent);
209    if (!e) return NULL;
210    obj = elm_widget_add(e);
211    ELM_SET_WIDTYPE(widtype, "panes");
212    elm_widget_type_set(obj, "panes");
213    elm_widget_can_focus_set(obj, EINA_FALSE);
214    elm_widget_sub_object_add(parent, obj);
215    elm_widget_data_set(obj, wd);
216    elm_widget_del_hook_set(obj, _del_hook);
217    elm_widget_theme_hook_set(obj, _theme_hook);
218    elm_widget_focus_next_hook_set(obj, _elm_panes_focus_next_hook);
219    wd->contents.left = NULL;
220    wd->contents.right = NULL;
221
222    wd->panes = edje_object_add(e);
223    _elm_theme_object_set(obj, wd->panes, "panes", "vertical", "default");
224    elm_widget_resize_object_set(obj, wd->panes);
225    evas_object_show(wd->panes);
226
227    elm_panes_content_left_size_set(obj, 0.5);
228
229    edje_object_signal_callback_add(wd->panes, "elm,action,click", "", 
230                                    _clicked, obj);
231    edje_object_signal_callback_add(wd->panes, "elm,action,click,double", "", 
232                                    _clicked_double, obj);
233    edje_object_signal_callback_add(wd->panes, "elm,action,press", "", 
234                                    _press, obj);
235    edje_object_signal_callback_add(wd->panes, "elm,action,unpress", "", 
236                                    _unpress, obj);
237
238    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
239    evas_object_event_callback_add(obj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, 
240                                   _changed_size_hints, obj);
241
242    _sizing_eval(obj);
243    return obj;
244 }
245
246 /**
247  * Set the left/top content of the panes widget
248  *
249  * Once the content object is set, a previously set one will be deleted.
250  * If you want to keep that old content object, use the
251  * elm_panes_content_left_unset() function.
252  *
253  * @param[in] obj The panes object
254  * @param[in] content The new left/top content object
255  *
256  * @ingroup Panes
257  */
258 EAPI void
259 elm_panes_content_left_set(Evas_Object *obj, Evas_Object *content)
260 {
261    ELM_CHECK_WIDTYPE(obj, widtype);
262    Widget_Data *wd = elm_widget_data_get(obj);
263    if (wd->contents.left)
264      {
265         evas_object_del(wd->contents.left);
266         wd->contents.left = NULL;
267      }
268    if (content)
269      {
270         wd->contents.left = content;
271         elm_widget_sub_object_add(obj, content);
272         edje_object_part_swallow(wd->panes, "elm.swallow.left", content);
273         if (wd->contents.right)
274           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
275      }
276    else
277       edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
278 }
279
280 /**
281  * Set the right/bottom content of the panes widget
282  *
283  * Once the content object is set, a previously set one will be deleted.
284  * If you want to keep that old content object, use the
285  * elm_panes_content_right_unset() function.
286  *
287  * @param[in] obj The panes object
288  * @param[in] content The new right/bottom content object
289  *
290  * @ingroup Panes
291  */
292 EAPI void
293 elm_panes_content_right_set(Evas_Object *obj, Evas_Object *content)
294 {
295    ELM_CHECK_WIDTYPE(obj, widtype);
296    Widget_Data *wd = elm_widget_data_get(obj);
297    if (wd->contents.right)
298      {
299         evas_object_del(wd->contents.right);
300         wd->contents.right = NULL;
301      }
302    if (content)
303      {
304         wd->contents.right = content;
305         elm_widget_sub_object_add(obj, content);
306         edje_object_part_swallow(wd->panes, "elm.swallow.right", content);
307         if (wd->contents.left)
308           edje_object_signal_emit(wd->panes, "elm.panes.pair", "elm");
309      }
310    else
311      edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
312 }
313
314 /**
315  * Get the left/top content used for the panes
316  *
317  * Return the left/top content object which is set for this widget.
318  *
319  * @param[in] obj The panes object
320  * @return The left/top 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/bottom content used for the panes
334  *
335  * Return the right/bottom content object which is set for this widget.
336  *
337  * @param[in] obj The panes object
338  * @return The right/bottom 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/top content used for the panes
352  *
353  * Unparent and return the left content object which was set for this widget.
354  *
355  * @param[in] obj The panes object
356  * @return The left/top 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    edje_object_part_unswallow(wd->panes, content);
369    elm_widget_sub_object_del(obj, content);
370    evas_object_hide(content);
371    wd->contents.left = NULL;
372    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
373    return content;
374 }
375
376 /**
377  * Unset the right/bottom content used for the panes
378  *
379  * Unparent and return the right content object which was set for this widget.
380  *
381  * @param[in] obj The panes object
382   * @return The right/bottom content object that was being used
383  *
384  * @ingroup Panes
385  */
386 EAPI Evas_Object *
387 elm_panes_content_right_unset(Evas_Object *obj)
388 {
389    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
390    Widget_Data *wd = elm_widget_data_get(obj);
391    if (!wd) return NULL;
392    if (!wd->contents.right) return NULL;
393    Evas_Object *content = wd->contents.right;
394    edje_object_part_unswallow(wd->panes, content);
395    elm_widget_sub_object_del(obj, content);
396    evas_object_hide(content);
397    wd->contents.right = NULL;
398    edje_object_signal_emit(wd->panes, "elm.panes.unpair", "elm");
399    return content;
400 }
401
402 /**
403  * Get the relative normalized size of left/top content of the pane
404  *
405  * @param[in] obj The panes object
406  * @return The value of type double in the range [0.0,1.0]
407  *
408  * @ingroup Panes
409  */
410 EAPI double 
411 elm_panes_content_left_size_get(const Evas_Object *obj)
412 {
413    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
414    Widget_Data *wd = elm_widget_data_get(obj);
415    double w, h;
416
417    edje_object_part_drag_value_get(wd->panes, "elm.bar", &w, &h);
418    if (wd->horizontal) return h;
419    else return w;
420 }
421
422 /**
423  * Set a size of the left/top content with a relative normalized double value
424  *
425  * @param[in] obj The panes object
426  * @param[in] size The value of type double in the range [0.0,1.0]
427  *
428  * @ingroup Panes
429  */
430 EAPI void 
431 elm_panes_content_left_size_set(Evas_Object *obj, double size)
432 {
433    ELM_CHECK_WIDTYPE(obj, widtype);
434    Widget_Data *wd = elm_widget_data_get(obj);
435
436    if (size < 0.0) size = 0.0;
437    else if (size > 1.0) size = 1.0;
438    if (wd->horizontal)
439      edje_object_part_drag_value_set(wd->panes, "elm.bar", 0.0, size);
440    else
441      edje_object_part_drag_value_set(wd->panes, "elm.bar", size, 0.0);
442 }
443
444 /**
445  * Set the type of an existing panes object to horizontal/vertical
446  *
447  * By default the panes is of vertical type
448  *
449  * @param[in] obj The panes object
450  * @param[in] horizontal Boolean value. If true, then the type is set to horizontal else vertical
451  *
452  * @ingroup Panes
453  */
454 EAPI void 
455 elm_panes_horizontal_set(Evas_Object *obj, Eina_Bool horizontal)
456 {
457    ELM_CHECK_WIDTYPE(obj, widtype);
458    Widget_Data *wd = elm_widget_data_get(obj);
459
460    wd->horizontal = horizontal;
461    _theme_hook(obj);
462    elm_panes_content_left_size_set(obj, 0.5);
463 }
464
465 /**
466  * Indicate if the type of pane object is horizontal or not
467  *
468  * @param[in] obj The panes object
469  * @return true if it is of horizontal type else false
470  *
471  * @ingroup Panes
472  */
473 EAPI Eina_Bool 
474 elm_panes_horizontal_get(const Evas_Object *obj)
475 {
476    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
477    Widget_Data *wd = elm_widget_data_get(obj);
478    return wd->horizontal;
479 }
480
481 /**
482  * Set a handler of the pane object non-movable or movable
483  *
484  * @param[in] obj The panes object
485  * @param[in] fixed If set to true then the views size can't be changed using handler otherwise using handler they can be resized
486  *
487  * @ingroup Panes
488  */
489 EAPI void
490 elm_panes_fixed_set(Evas_Object *obj, Eina_Bool fixed)
491 {
492    ELM_CHECK_WIDTYPE(obj, widtype);
493    Widget_Data *wd = elm_widget_data_get(obj);
494    wd->fixed = fixed;
495    if (wd->fixed == EINA_TRUE)
496      edje_object_signal_emit(wd->panes, "elm.panes.fixed", "elm");
497    else
498      edje_object_signal_emit(wd->panes, "elm.panes.unfixed", "elm");
499 }
500
501 /**
502  * Indicate if the handler of the panes object can be moved with user interaction
503  *
504  * @param[in] obj The panes object
505  * @return false if the views can be resized using handler else true
506  *
507  * @ingroup Panes
508  */
509 EAPI Eina_Bool
510 elm_panes_fixed_get(const Evas_Object *obj)
511 {
512    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
513    Widget_Data *wd = elm_widget_data_get(obj);
514    return wd->fixed;
515 }