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