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