move elementary to trunk base. out of TMP/st.
[framework/uifw/elementary.git] / src / lib / elm_panel.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Panel Panel
6  *
7  * A panel is a type of animated container that contains subobjects.  It
8  * can be expanded or contracted.
9  *
10  * Orientations are as follows:
11  * ELM_PANEL_ORIENT_TOP
12  * ELM_PANEL_ORIENT_BOTTOM
13  * ELM_PANEL_ORIENT_LEFT
14  * ELM_PANEL_ORIENT_RIGHT
15  * NOTE: Only LEFT and RIGHT orientations are implemented.
16  *
17  * THIS WIDGET IS UNDER CONSTRUCTION!
18  */
19
20 typedef struct _Widget_Data Widget_Data;
21 struct _Widget_Data 
22 {
23    Evas_Object *scr, *bx, *content;
24    Elm_Panel_Orient orient;
25    Eina_Bool hidden : 1;
26 };
27
28 static const char *widtype = NULL;
29 static void _del_hook(Evas_Object *obj);
30 static void _theme_hook(Evas_Object *obj);
31 static void _on_focus_hook(void *data, Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static void _resize(void *data, Evas *evas, Evas_Object *obj, void *event);
34 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
35 static void _toggle_panel(void *data, Evas_Object *obj, const char *emission, const char *source);
36
37 static void 
38 _del_hook(Evas_Object *obj) 
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    if (!wd) return;
42    free(wd);
43 }
44
45 static void 
46 _theme_hook(Evas_Object *obj) 
47 {
48    Widget_Data *wd = elm_widget_data_get(obj);
49    if (!wd) return;
50    if (wd->scr)
51      {
52         Evas_Object *edj;
53         const char *str;
54
55         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base",
56                                             elm_widget_style_get(obj));
57         //   scale = (elm_widget_scale_get(obj) * _elm_config->scale);
58         //   edje_object_scale_set(wd->scr, scale);
59         edj = elm_smart_scroller_edje_object_get(wd->scr);
60         str = edje_object_data_get(edj, "focus_highlight");
61         if ((str) && (!strcmp(str, "on")))
62           elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
63         else
64           elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
65      }
66    _sizing_eval(obj);
67 }
68
69 static void
70 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
71 {
72    Widget_Data *wd = elm_widget_data_get(obj);
73    if (!wd) return;
74    if (elm_widget_focus_get(obj))
75      evas_object_focus_set(obj, EINA_TRUE);
76    else
77      evas_object_focus_set(obj, EINA_FALSE);
78 }
79
80 static Eina_Bool
81 _elm_panel_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    Evas_Object *cur;
85
86    if ((!wd) || (!wd->content))
87      return EINA_FALSE;
88
89    cur = wd->content;
90
91    /* Try Focus cycle in subitem */
92    if (!wd->hidden)
93       return elm_widget_focus_next_get(cur, dir, next);
94
95    /* Return */
96    *next = (Evas_Object *)obj;
97    return !elm_widget_focus_get(obj);
98 }
99
100 static void
101 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
102 {
103    Widget_Data *wd = elm_widget_data_get(obj);
104    if (!wd) return;
105    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
106          emission, source);
107 }
108
109 static void
110 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
111 {
112    Widget_Data *wd = elm_widget_data_get(obj);
113    if (!wd) return;
114    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr),
115          emission, source, func_cb, data);
116 }
117
118 static void
119 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), void *data)
120 {
121    Widget_Data *wd = elm_widget_data_get(obj);
122    edje_object_signal_callback_del_full(
123          elm_smart_scroller_edje_object_get(wd->scr), emission, source,
124          func_cb, data);
125 }
126
127 static void 
128 _sizing_eval(Evas_Object *obj) 
129 {
130    Widget_Data *wd = elm_widget_data_get(obj);
131    Evas_Coord mw = -1, mh = -1;
132    Evas_Coord vw = 0, vh = 0;
133    Evas_Coord w, h;
134    if (!wd) return;
135    evas_object_smart_calculate(wd->bx);
136    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr), 
137                              &mw, &mh);
138    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
139    if (w < mw) w = mw;
140    if (h < mh) h = mh;
141    evas_object_resize(wd->scr, w, h);
142
143    evas_object_size_hint_min_get(wd->bx, &mw, &mh);
144    if (w > mw) mw = w;
145    if (h > mh) mh = h;
146    evas_object_resize(wd->bx, mw, mh);
147
148    elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
149    mw = mw + (w - vw);
150    mh = mh + (h - vh);
151    evas_object_size_hint_min_set(obj, mw, mh);
152    evas_object_size_hint_max_set(obj, -1, -1);
153 }
154
155 static void 
156 _resize(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
157 {
158    Widget_Data *wd = elm_widget_data_get(data);
159    Evas_Coord mw, mh, vw, vh, w, h;
160    if (!wd) return;
161    elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
162    evas_object_size_hint_min_get(wd->bx, &mw, &mh);
163    evas_object_geometry_get(wd->bx, NULL, NULL, &w, &h);
164    if ((vw >= mw) || (vh >= mh))
165      {
166         if ((w != vw) || (h != vh)) evas_object_resize(wd->bx, vw, vh);
167      }
168 }
169
170 static void 
171 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data) 
172 {
173    Widget_Data *wd = data;
174    if (!wd) return;
175    _els_box_layout(o, priv, EINA_TRUE, EINA_FALSE);
176 }
177
178 static void 
179 _toggle_panel(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__) 
180 {
181    Widget_Data *wd = elm_widget_data_get(data);
182    if (!wd) return;
183    if (wd->hidden) 
184      {
185         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), 
186                                 "elm,action,show", "elm");
187         wd->hidden = EINA_FALSE;
188         evas_object_repeat_events_set(obj, EINA_FALSE);
189      }
190    else
191      {
192         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr), 
193                                 "elm,action,hide", "elm");
194         wd->hidden = EINA_TRUE;
195         evas_object_repeat_events_set(obj, EINA_TRUE);
196         if (elm_widget_focus_get(wd->content))
197           {
198              elm_widget_focused_object_clear(obj);
199              elm_widget_focus_steal(obj);
200           }
201      }
202 }
203
204 static Eina_Bool
205 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
206 {
207    if ((src != obj) || (type != EVAS_CALLBACK_KEY_DOWN)) return EINA_FALSE;
208
209    Evas_Event_Key_Down *ev = event_info;
210    Widget_Data *wd = elm_widget_data_get(obj);
211    if (!wd) return EINA_FALSE;
212
213    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
214
215    if ((strcmp(ev->keyname, "Return")) &&
216        (strcmp(ev->keyname, "KP_Enter")) &&
217        (strcmp(ev->keyname, "space")))
218      return EINA_FALSE;
219
220    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
221
222    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
223    return EINA_TRUE;
224 }
225
226 /**
227  * Adds a panel object
228  *
229  * @param parent The parent object
230  *
231  * @return The panel object, or NULL on failure
232  *
233  * @ingroup Panel
234  */
235 EAPI Evas_Object *
236 elm_panel_add(Evas_Object *parent) 
237 {
238    Evas_Object *obj;
239    Evas *evas;
240    Widget_Data *wd;
241
242    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
243
244    wd = ELM_NEW(Widget_Data);
245    evas = evas_object_evas_get(parent);
246    if (!evas) return NULL;
247    obj = elm_widget_add(evas);
248    ELM_SET_WIDTYPE(widtype, "panel");
249    elm_widget_type_set(obj, "panel");
250    elm_widget_sub_object_add(parent, obj);
251    elm_widget_data_set(obj, wd);
252    elm_widget_del_hook_set(obj, _del_hook);
253    elm_widget_theme_hook_set(obj, _theme_hook);
254    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
255    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
256    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
257    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
258    elm_widget_focus_next_hook_set(obj, _elm_panel_focus_next_hook);
259    elm_widget_can_focus_set(obj, EINA_TRUE);
260    elm_widget_event_hook_set(obj, _event_hook);
261
262    wd->scr = elm_smart_scroller_add(evas);
263    elm_smart_scroller_widget_set(wd->scr, obj);
264    _theme_hook(obj);
265    elm_smart_scroller_bounce_allow_set(wd->scr, EINA_FALSE, EINA_FALSE);
266    elm_widget_resize_object_set(obj, wd->scr);
267    elm_smart_scroller_policy_set(wd->scr, ELM_SMART_SCROLLER_POLICY_OFF, 
268                                  ELM_SMART_SCROLLER_POLICY_OFF);
269
270    wd->hidden = EINA_FALSE;
271    wd->orient = ELM_PANEL_ORIENT_LEFT;
272
273    wd->bx = evas_object_box_add(evas);
274    evas_object_size_hint_align_set(wd->bx, 0.5, 0.5);
275    evas_object_box_layout_set(wd->bx, _layout, wd, NULL);
276    elm_widget_sub_object_add(obj, wd->bx);
277    elm_smart_scroller_child_set(wd->scr, wd->bx);
278    evas_object_show(wd->bx);
279
280    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr), 
281                                    "elm,action,panel,toggle", "*", 
282                                    _toggle_panel, obj);
283
284    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE, _resize, obj);
285
286    _sizing_eval(obj);
287    return obj;
288 }
289
290 /**
291  * Sets the orientation of the panel
292  *
293  * @param parent The parent object
294  * @param orient The panel orientation.  Can be one of the following:
295  * ELM_PANEL_ORIENT_TOP
296  * ELM_PANEL_ORIENT_BOTTOM
297  * ELM_PANEL_ORIENT_LEFT
298  * ELM_PANEL_ORIENT_RIGHT
299  *
300  * NOTE: Only LEFT and RIGHT orientations are implemented.
301  *
302  * @ingroup Panel
303  */
304 EAPI void 
305 elm_panel_orient_set(Evas_Object *obj, Elm_Panel_Orient orient) 
306 {
307    ELM_CHECK_WIDTYPE(obj, widtype);
308    Widget_Data *wd = elm_widget_data_get(obj);
309    if (!wd) return;
310    wd->orient = orient;
311    switch (orient) 
312      {
313      case ELM_PANEL_ORIENT_TOP:
314         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "top");
315         break;
316      case ELM_PANEL_ORIENT_BOTTOM:
317         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "bottom");
318         break;
319      case ELM_PANEL_ORIENT_LEFT:
320         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "left");
321         break;
322      case ELM_PANEL_ORIENT_RIGHT:
323         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "right");
324         break;
325      }
326    _sizing_eval(obj);
327 }
328
329 /**
330  * Get the orientation of the panel.
331  *
332  * @param obj The panel object
333  * @return The Elm_Panel_Orient, or ELM_PANEL_ORIENT_LEFT on failure.
334  *
335  * @ingroup Panel
336  */
337 EAPI Elm_Panel_Orient
338 elm_panel_orient_get(const Evas_Object *obj)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype) ELM_PANEL_ORIENT_LEFT;
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return ELM_PANEL_ORIENT_LEFT;
343    return wd->orient;   
344 }
345
346 /**
347  * Set the content of the panel.
348  *
349  * Once the content object is set, a previously set one will be deleted.
350  * If you want to keep that old content object, use the
351  * elm_panel_content_unset() function.
352  *
353  * @param obj The panel object
354  * @param content The panel content
355  *
356  * @ingroup Panel
357  */
358 EAPI void
359 elm_panel_content_set(Evas_Object *obj, Evas_Object *content)
360 {
361    ELM_CHECK_WIDTYPE(obj, widtype);
362    Widget_Data *wd = elm_widget_data_get(obj);
363    if (!wd) return;
364    if (wd->content == content) return;
365    if (wd->content)
366      evas_object_box_remove_all(wd->bx, EINA_TRUE);
367    wd->content = content;
368    if (content)
369      {
370         evas_object_box_append(wd->bx, wd->content);
371         evas_object_show(wd->content);
372      }
373    _sizing_eval(obj);
374 }
375
376 /**
377  * Get the content of the panel.
378  *
379  * Return the content object which is set for this widget.
380  *
381  * @param obj The panel object
382  * @return The content that is being used
383  *
384  * @ingroup Panel
385  */
386 EAPI Evas_Object *
387 elm_panel_content_get(const 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    return wd->content;
393 }
394
395 /**
396  * Unset the content of the panel.
397  *
398  * Unparent and return the content object which was set for this widget.
399  *
400  * @param obj The panel object
401  * @return The content that was being used
402  *
403  * @ingroup Panel
404  */
405 EAPI Evas_Object *
406 elm_panel_content_unset(Evas_Object *obj)
407 {
408    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
409    Widget_Data *wd = elm_widget_data_get(obj);
410    Evas_Object *content;
411    if (!wd) return NULL;
412    if (!wd->content) return NULL;
413    content = wd->content;
414    evas_object_box_remove_all(wd->bx, EINA_FALSE);
415    wd->content = NULL;
416    return content;
417 }
418
419 /**
420  * Set the state of the panel.
421  *
422  * @param obj The panel object
423  * @param hidden If true, the panel will run the edje animation to contract
424  *
425  * @ingroup Panel
426  */
427 EAPI void
428 elm_panel_hidden_set(Evas_Object *obj, Eina_Bool hidden)
429 {
430    ELM_CHECK_WIDTYPE(obj, widtype);
431    Widget_Data *wd = elm_widget_data_get(obj);
432    if (!wd) return;
433    if (wd->hidden == hidden) return;
434    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
435 }
436
437 /**
438  * Get the state of the panel.
439  *
440  * @param obj The panel object
441  * @param hidden If true, the panel is in the "hide" state
442  *
443  * @ingroup Panel
444  */
445 EAPI Eina_Bool
446 elm_panel_hidden_get(const Evas_Object *obj)
447 {
448    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
449    Widget_Data *wd = elm_widget_data_get(obj);
450    if (!wd) return EINA_FALSE;
451    return wd->hidden;
452 }
453
454 /**
455  * Toggle the state of the panel from code
456  *
457  * @param obj The panel object
458  *
459  * @ingroup Panel
460  */
461 EAPI void
462 elm_panel_toggle(Evas_Object *obj)
463 {
464    ELM_CHECK_WIDTYPE(obj, widtype);
465    Widget_Data *wd = elm_widget_data_get(obj);
466    if (!wd) return;
467    wd->hidden = !(wd->hidden);
468    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
469 }