Merge "[stackicon] remove trailing whitespaces"
[framework/uifw/elementary.git] / src / lib / elm_panel.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "els_scroller.h"
4 #include "els_box.h"
5
6 typedef struct _Widget_Data Widget_Data;
7 struct _Widget_Data
8 {
9    Evas_Object *scr, *bx, *content;
10    Elm_Panel_Orient orient;
11    Eina_Bool hidden : 1;
12 };
13
14 static const char *widtype = NULL;
15 static void _del_hook(Evas_Object *obj);
16 static void _theme_hook(Evas_Object *obj);
17 static void _on_focus_hook(void *data, Evas_Object *obj);
18 static void _sizing_eval(Evas_Object *obj);
19 static void _resize(void *data, Evas *evas, Evas_Object *obj, void *event);
20 static void _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data);
21 static void _toggle_panel(void *data, Evas_Object *obj, const char *emission, const char *source);
22
23 static void
24 _del_hook(Evas_Object *obj)
25 {
26    Widget_Data *wd = elm_widget_data_get(obj);
27    if (!wd) return;
28    free(wd);
29 }
30
31 static void
32 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
33 {
34    Widget_Data *wd = elm_widget_data_get(obj);
35    if (!wd) return;
36
37    if (wd->scr)
38      {
39         elm_widget_mirrored_set(wd->bx, rtl);
40         elm_panel_orient_set(obj, elm_panel_orient_get(obj));
41      }
42 }
43
44 static void
45 _theme_hook(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48    if (!wd) return;
49    _elm_widget_mirrored_reload(obj);
50    if (wd->scr)
51      {
52         Evas_Object *edj;
53         const char *str;
54
55         _mirrored_set(obj, elm_widget_mirrored_get(obj));
56         elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base",
57                                             elm_widget_style_get(obj));
58         //   scale = (elm_widget_scale_get(obj) * _elm_config->scale);
59         //   edje_object_scale_set(wd->scr, scale);
60         edj = elm_smart_scroller_edje_object_get(wd->scr);
61         str = edje_object_data_get(edj, "focus_highlight");
62         if ((str) && (!strcmp(str, "on")))
63           elm_widget_highlight_in_theme_set(obj, EINA_TRUE);
64         else
65           elm_widget_highlight_in_theme_set(obj, EINA_FALSE);
66      }
67
68    _sizing_eval(obj);
69 }
70
71 static void
72 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
73 {
74    Widget_Data *wd = elm_widget_data_get(obj);
75    if (!wd) return;
76    if (elm_widget_focus_get(obj))
77      evas_object_focus_set(obj, EINA_TRUE);
78    else
79      evas_object_focus_set(obj, EINA_FALSE);
80 }
81
82 static Eina_Bool
83 _elm_panel_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
84 {
85    Widget_Data *wd = elm_widget_data_get(obj);
86    Evas_Object *cur;
87
88    if ((!wd) || (!wd->content))
89      return EINA_FALSE;
90
91    cur = wd->content;
92
93    /* Try Focus cycle in subitem */
94    if (!wd->hidden)
95      return elm_widget_focus_next_get(cur, dir, next);
96
97    /* Return */
98    *next = (Evas_Object *)obj;
99    return !elm_widget_focus_get(obj);
100 }
101
102 static void
103 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
104 {
105    Widget_Data *wd = elm_widget_data_get(obj);
106    if (!wd) return;
107    edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
108                            emission, source);
109 }
110
111 static void
112 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
113 {
114    Widget_Data *wd = elm_widget_data_get(obj);
115    if (!wd) return;
116    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr),
117                                    emission, source, func_cb, data);
118 }
119
120 static void
121 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
122 {
123    Widget_Data *wd = elm_widget_data_get(obj);
124    edje_object_signal_callback_del_full(
125       elm_smart_scroller_edje_object_get(wd->scr), emission, source,
126       func_cb, data);
127 }
128
129 static void
130 _sizing_eval(Evas_Object *obj)
131 {
132    Widget_Data *wd = elm_widget_data_get(obj);
133    Evas_Coord mw = -1, mh = -1;
134    Evas_Coord vw = 0, vh = 0;
135    Evas_Coord w, h;
136    if (!wd) return;
137    evas_object_smart_calculate(wd->bx);
138    edje_object_size_min_calc(elm_smart_scroller_edje_object_get(wd->scr),
139                              &mw, &mh);
140    evas_object_geometry_get(obj, NULL, NULL, &w, &h);
141    if (w < mw) w = mw;
142    if (h < mh) h = mh;
143    evas_object_resize(wd->scr, w, h);
144
145    evas_object_size_hint_min_get(wd->bx, &mw, &mh);
146    if (w > mw) mw = w;
147    if (h > mh) mh = h;
148    evas_object_resize(wd->bx, mw, mh);
149
150    elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
151    mw = mw + (w - vw);
152    mh = mh + (h - vh);
153    evas_object_size_hint_min_set(obj, mw, mh);
154    evas_object_size_hint_max_set(obj, -1, -1);
155 }
156
157 static void
158 _resize(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
159 {
160    Widget_Data *wd = elm_widget_data_get(data);
161    Evas_Coord mw, mh, vw, vh, w, h;
162    if (!wd) return;
163    elm_smart_scroller_child_viewport_size_get(wd->scr, &vw, &vh);
164    evas_object_size_hint_min_get(wd->bx, &mw, &mh);
165    evas_object_geometry_get(wd->bx, NULL, NULL, &w, &h);
166    if ((vw >= mw) || (vh >= mh))
167      {
168         if ((w != vw) || (h != vh)) evas_object_resize(wd->bx, vw, vh);
169      }
170 }
171
172 static void
173 _layout(Evas_Object *o, Evas_Object_Box_Data *priv, void *data)
174 {
175    Widget_Data *wd = data;
176    if (!wd) return;
177    _els_box_layout(o, priv, EINA_TRUE, EINA_FALSE, EINA_FALSE);
178 }
179
180 static void
181 _toggle_panel(void *data, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
182 {
183    Widget_Data *wd = elm_widget_data_get(data);
184    if (!wd) return;
185    if (wd->hidden)
186      {
187         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
188                                 "elm,action,show", "elm");
189         wd->hidden = EINA_FALSE;
190         evas_object_repeat_events_set(obj, EINA_FALSE);
191      }
192    else
193      {
194         edje_object_signal_emit(elm_smart_scroller_edje_object_get(wd->scr),
195                                 "elm,action,hide", "elm");
196         wd->hidden = EINA_TRUE;
197         evas_object_repeat_events_set(obj, EINA_TRUE);
198         if (elm_widget_focus_get(wd->content))
199           {
200              elm_widget_focused_object_clear(obj);
201              elm_widget_focus_steal(obj);
202           }
203      }
204 }
205
206 static Eina_Bool
207 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
208 {
209    if ((src != obj) || (type != EVAS_CALLBACK_KEY_DOWN)) return EINA_FALSE;
210
211    Evas_Event_Key_Down *ev = event_info;
212    Widget_Data *wd = elm_widget_data_get(obj);
213    if (!wd) return EINA_FALSE;
214
215    if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return EINA_FALSE;
216
217    if ((strcmp(ev->keyname, "Return")) &&
218        (strcmp(ev->keyname, "KP_Enter")) &&
219        (strcmp(ev->keyname, "space")))
220      return EINA_FALSE;
221
222    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
223
224    ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
225    return EINA_TRUE;
226 }
227
228 static void
229 _content_set_hook(Evas_Object *obj, const char *part __UNUSED__, Evas_Object *content)
230 {
231    ELM_CHECK_WIDTYPE(obj, widtype);
232    Widget_Data *wd = elm_widget_data_get(obj);
233    if (!wd) return;
234    if (wd->content == content) return;
235    if (wd->content)
236      evas_object_box_remove_all(wd->bx, EINA_TRUE);
237    wd->content = content;
238    if (content)
239      {
240         evas_object_box_append(wd->bx, wd->content);
241         evas_object_show(wd->content);
242      }
243    _sizing_eval(obj);
244 }
245
246 static Evas_Object *
247 _content_get_hook(const Evas_Object *obj, const char *part __UNUSED__)
248 {
249    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return NULL;
252    return wd->content;
253 }
254
255 static Evas_Object *
256 _content_unset_hook(Evas_Object *obj, const char *part __UNUSED__)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
259    Widget_Data *wd = elm_widget_data_get(obj);
260    Evas_Object *content;
261    if (!wd) return NULL;
262    if (!wd->content) return NULL;
263    content = wd->content;
264    evas_object_box_remove_all(wd->bx, EINA_FALSE);
265    wd->content = NULL;
266    return content;
267 }
268
269 EAPI Evas_Object *
270 elm_panel_add(Evas_Object *parent)
271 {
272    Evas_Object *obj;
273    Evas *e;
274    Widget_Data *wd;
275
276    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
277
278    ELM_SET_WIDTYPE(widtype, "panel");
279    elm_widget_type_set(obj, "panel");
280    elm_widget_sub_object_add(parent, obj);
281    elm_widget_data_set(obj, wd);
282    elm_widget_del_hook_set(obj, _del_hook);
283    elm_widget_theme_hook_set(obj, _theme_hook);
284    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
285    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
286    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
287    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
288    elm_widget_focus_next_hook_set(obj, _elm_panel_focus_next_hook);
289    elm_widget_can_focus_set(obj, EINA_TRUE);
290    elm_widget_event_hook_set(obj, _event_hook);
291    elm_widget_content_set_hook_set(obj, _content_set_hook);
292    elm_widget_content_get_hook_set(obj, _content_get_hook);
293    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
294
295    wd->scr = elm_smart_scroller_add(e);
296    elm_smart_scroller_widget_set(wd->scr, obj);
297    _theme_hook(obj);
298    elm_smart_scroller_bounce_allow_set(wd->scr, EINA_FALSE, EINA_FALSE);
299    elm_widget_resize_object_set(obj, wd->scr);
300    elm_smart_scroller_policy_set(wd->scr, ELM_SMART_SCROLLER_POLICY_OFF,
301                                  ELM_SMART_SCROLLER_POLICY_OFF);
302
303    wd->hidden = EINA_FALSE;
304    wd->orient = ELM_PANEL_ORIENT_LEFT;
305
306    wd->bx = evas_object_box_add(e);
307    evas_object_size_hint_align_set(wd->bx, 0.5, 0.5);
308    evas_object_box_layout_set(wd->bx, _layout, wd, NULL);
309    elm_widget_sub_object_add(obj, wd->bx);
310    elm_smart_scroller_child_set(wd->scr, wd->bx);
311    evas_object_show(wd->bx);
312
313    edje_object_signal_callback_add(elm_smart_scroller_edje_object_get(wd->scr),
314                                    "elm,action,panel,toggle", "*",
315                                    _toggle_panel, obj);
316
317    evas_object_event_callback_add(wd->scr, EVAS_CALLBACK_RESIZE, _resize, obj);
318
319    _mirrored_set(obj, elm_widget_mirrored_get(obj));
320    _sizing_eval(obj);
321    return obj;
322 }
323
324 EAPI void
325 elm_panel_orient_set(Evas_Object *obj, Elm_Panel_Orient orient)
326 {
327    ELM_CHECK_WIDTYPE(obj, widtype);
328    Widget_Data *wd = elm_widget_data_get(obj);
329    if (!wd) return;
330    wd->orient = orient;
331    switch (orient)
332      {
333       case ELM_PANEL_ORIENT_TOP:
334          elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "top");
335          break;
336       case ELM_PANEL_ORIENT_BOTTOM:
337          elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "bottom");
338          break;
339       case ELM_PANEL_ORIENT_LEFT:
340          if (!elm_widget_mirrored_get(obj))
341            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "left");
342          else
343            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "right");
344          break;
345       case ELM_PANEL_ORIENT_RIGHT:
346          if (!elm_widget_mirrored_get(obj))
347            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "right");
348          else
349            elm_smart_scroller_object_theme_set(obj, wd->scr, "panel", "base", "left");
350          break;
351      }
352
353    _sizing_eval(obj);
354 }
355
356 EAPI Elm_Panel_Orient
357 elm_panel_orient_get(const Evas_Object *obj)
358 {
359    ELM_CHECK_WIDTYPE(obj, widtype) ELM_PANEL_ORIENT_LEFT;
360    Widget_Data *wd = elm_widget_data_get(obj);
361    if (!wd) return ELM_PANEL_ORIENT_LEFT;
362    return wd->orient;
363 }
364
365 EAPI void
366 elm_panel_content_set(Evas_Object *obj, Evas_Object *content)
367 {
368    _content_set_hook(obj, NULL, content);
369 }
370
371 EAPI Evas_Object *
372 elm_panel_content_get(const Evas_Object *obj)
373 {
374    return _content_get_hook(obj, NULL);
375 }
376
377 EAPI Evas_Object *
378 elm_panel_content_unset(Evas_Object *obj)
379 {
380    return _content_unset_hook(obj, NULL);
381 }
382
383 EAPI void
384 elm_panel_hidden_set(Evas_Object *obj, Eina_Bool hidden)
385 {
386    ELM_CHECK_WIDTYPE(obj, widtype);
387    Widget_Data *wd = elm_widget_data_get(obj);
388    if (!wd) return;
389    if (wd->hidden == hidden) return;
390    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
391 }
392
393 EAPI Eina_Bool
394 elm_panel_hidden_get(const Evas_Object *obj)
395 {
396    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
397    Widget_Data *wd = elm_widget_data_get(obj);
398    if (!wd) return EINA_FALSE;
399    return wd->hidden;
400 }
401
402 EAPI void
403 elm_panel_toggle(Evas_Object *obj)
404 {
405    ELM_CHECK_WIDTYPE(obj, widtype);
406    Widget_Data *wd = elm_widget_data_get(obj);
407    if (!wd) return;
408    _toggle_panel(obj, NULL, "elm,action,panel,toggle", "*");
409 }