Elementary: panel documentation.
[framework/uifw/elementary.git] / src / lib / elm_panel.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5 struct _Widget_Data
6 {
7    Evas_Object *scr, *bx, *content;
8    Elm_Panel_Orient orient;
9    Eina_Bool hidden : 1;
10 };
11
12 static const char *widtype = NULL;
13 static void _del_hook(Evas_Object *obj);
14 static void _theme_hook(Evas_Object *obj);
15 static void _on_focus_hook(void *data, Evas_Object *obj);
16 static void _sizing_eval(Evas_Object *obj);
17 static void _resize(void *data, Evas *evas, Evas_Object *obj, void *event);
18 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
19 static void _toggle_panel(void *data, Evas_Object *obj, const char *emission, const char *source);
20
21 static void
22 _del_hook(Evas_Object *obj)
23 {
24    Widget_Data *wd = elm_widget_data_get(obj);
25    if (!wd) return;
26    free(wd);
27 }
28
29 static void
30 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33    if (!wd) return;
34
35    if (wd->scr)
36      {
37         elm_widget_mirrored_set(wd->bx, rtl);
38         elm_panel_orient_set(obj, elm_panel_orient_get(obj));
39      }
40 }
41
42 static void
43 _theme_hook(Evas_Object *obj)
44 {
45    Widget_Data *wd = elm_widget_data_get(obj);
46    if (!wd) return;
47    _elm_widget_mirrored_reload(obj);
48    if (wd->scr)
49      {
50         Evas_Object *edj;
51         const char *str;
52
53         _mirrored_set(obj, elm_widget_mirrored_get(obj));
54         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base",
55                                             elm_widget_style_get(obj));
56         //   scale = (elm_widget_scale_get(obj) * _elm_config->scale);
57         //   edje_object_scale_set(wd->scr, scale);
58         edj = elm_smart_scroller_edje_object_get(wd->scr);
59         str = edje_object_data_get(edj, "focus_highlight");
60         if ((str) && (!strcmp(str, "on")))
61           elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
62         else
63           elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
64      }
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, Edje_Signal_Cb func_cb, 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, Edje_Signal_Cb func_cb, 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, 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 EAPI Evas_Object *
227 elm_panel_add(Evas_Object *parent)
228 {
229    Evas_Object *obj;
230    Evas *e;
231    Widget_Data *wd;
232
233    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
234
235    ELM_SET_WIDTYPE(widtype, "panel");
236    elm_widget_type_set(obj, "panel");
237    elm_widget_sub_object_add(parent, obj);
238    elm_widget_data_set(obj, wd);
239    elm_widget_del_hook_set(obj, _del_hook);
240    elm_widget_theme_hook_set(obj, _theme_hook);
241    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
242    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
243    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
244    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
245    elm_widget_focus_next_hook_set(obj, _elm_panel_focus_next_hook);
246    elm_widget_can_focus_set(obj, EINA_TRUE);
247    elm_widget_event_hook_set(obj, _event_hook);
248
249    wd->scr = elm_smart_scroller_add(e);
250    elm_smart_scroller_widget_set(wd->scr, obj);
251    _theme_hook(obj);
252    elm_smart_scroller_bounce_allow_set(wd->scr, EINA_FALSE, EINA_FALSE);
253    elm_widget_resize_object_set(obj, wd->scr);
254    elm_smart_scroller_policy_set(wd->scr, ELM_SMART_SCROLLER_POLICY_OFF,
255                                  ELM_SMART_SCROLLER_POLICY_OFF);
256
257    wd->hidden = EINA_FALSE;
258    wd->orient = ELM_PANEL_ORIENT_LEFT;
259
260    wd->bx = evas_object_box_add(e);
261    evas_object_size_hint_align_set(wd->bx, 0.5, 0.5);
262    evas_object_box_layout_set(wd->bx, _layout, wd, NULL);
263    elm_widget_sub_object_add(obj, wd->bx);
264    elm_smart_scroller_child_set(wd->scr, wd->bx);
265    evas_object_show(wd->bx);
266
267    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr),
268                                    "elm,action,panel,toggle", "*",
269                                    _toggle_panel, obj);
270
271    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE, _resize, obj);
272
273    _mirrored_set(obj, elm_widget_mirrored_get(obj));
274    _sizing_eval(obj);
275    return obj;
276 }
277
278 EAPI void
279 elm_panel_orient_set(Evas_Object *obj, Elm_Panel_Orient orient)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype);
282    Widget_Data *wd = elm_widget_data_get(obj);
283    if (!wd) return;
284    wd->orient = orient;
285    switch (orient)
286      {
287       case ELM_PANEL_ORIENT_TOP:
288          elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "top");
289          break;
290       case ELM_PANEL_ORIENT_BOTTOM:
291          elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "bottom");
292          break;
293       case ELM_PANEL_ORIENT_LEFT:
294          if (!elm_widget_mirrored_get(obj))
295            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "left");
296          else
297            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "right");
298          break;
299       case ELM_PANEL_ORIENT_RIGHT:
300          if (!elm_widget_mirrored_get(obj))
301            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "right");
302          else
303            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "left");
304          break;
305      }
306
307    _sizing_eval(obj);
308 }
309
310 EAPI Elm_Panel_Orient
311 elm_panel_orient_get(const Evas_Object *obj)
312 {
313    ELM_CHECK_WIDTYPE(obj, widtype) ELM_PANEL_ORIENT_LEFT;
314    Widget_Data *wd = elm_widget_data_get(obj);
315    if (!wd) return ELM_PANEL_ORIENT_LEFT;
316    return wd->orient;
317 }
318
319 EAPI void
320 elm_panel_content_set(Evas_Object *obj, Evas_Object *content)
321 {
322    ELM_CHECK_WIDTYPE(obj, widtype);
323    Widget_Data *wd = elm_widget_data_get(obj);
324    if (!wd) return;
325    if (wd->content == content) return;
326    if (wd->content)
327      evas_object_box_remove_all(wd->bx, EINA_TRUE);
328    wd->content = content;
329    if (content)
330      {
331         evas_object_box_append(wd->bx, wd->content);
332         evas_object_show(wd->content);
333      }
334    _sizing_eval(obj);
335 }
336
337 EAPI Evas_Object *
338 elm_panel_content_get(const Evas_Object *obj)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return NULL;
343    return wd->content;
344 }
345
346 EAPI Evas_Object *
347 elm_panel_content_unset(Evas_Object *obj)
348 {
349    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
350    Widget_Data *wd = elm_widget_data_get(obj);
351    Evas_Object *content;
352    if (!wd) return NULL;
353    if (!wd->content) return NULL;
354    content = wd->content;
355    evas_object_box_remove_all(wd->bx, EINA_FALSE);
356    wd->content = NULL;
357    return content;
358 }
359
360 EAPI void
361 elm_panel_hidden_set(Evas_Object *obj, Eina_Bool hidden)
362 {
363    ELM_CHECK_WIDTYPE(obj, widtype);
364    Widget_Data *wd = elm_widget_data_get(obj);
365    if (!wd) return;
366    if (wd->hidden == hidden) return;
367    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
368 }
369
370 EAPI Eina_Bool
371 elm_panel_hidden_get(const Evas_Object *obj)
372 {
373    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
374    Widget_Data *wd = elm_widget_data_get(obj);
375    if (!wd) return EINA_FALSE;
376    return wd->hidden;
377 }
378
379 EAPI void
380 elm_panel_toggle(Evas_Object *obj)
381 {
382    ELM_CHECK_WIDTYPE(obj, widtype);
383    Widget_Data *wd = elm_widget_data_get(obj);
384    if (!wd) return;
385    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
386 }