add api for manipulating frame collapse
[framework/uifw/elementary.git] / src / lib / elm_frame.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *frm;
9    Evas_Object *content;
10    const char *label;
11    Eina_Bool collapsed : 1;
12    Eina_Bool collapsible : 1;
13 };
14
15 static const char SIG_CLICKED[] = "clicked";
16
17 static const Evas_Smart_Cb_Description _signals[] = {
18    {SIG_CLICKED, ""},
19    {NULL, NULL}
20 };
21
22 static const char *widtype = NULL;
23 static void _del_hook(Evas_Object *obj);
24 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
25 static void _theme_hook(Evas_Object *obj);
26 static void _sizing_eval(Evas_Object *obj);
27 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
28 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
29
30 static void
31 _del_hook(Evas_Object *obj)
32 {
33    Widget_Data *wd = elm_widget_data_get(obj);
34    if (!wd) return;
35    if (wd->label) eina_stringshare_del(wd->label);
36    free(wd);
37 }
38
39 static void
40 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
41 {
42    Widget_Data *wd = elm_widget_data_get(obj);
43    if (!wd) return;
44    edje_object_mirrored_set(wd->frm, rtl);
45 }
46
47 static void
48 _theme_hook(Evas_Object *obj)
49 {
50    Widget_Data *wd = elm_widget_data_get(obj);
51    if (!wd) return;
52    _elm_widget_mirrored_reload(obj);
53    _mirrored_set(obj, elm_widget_mirrored_get(obj));
54    _elm_theme_object_set(obj, wd->frm, "frame", "base", elm_widget_style_get(obj));
55    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
56    if (wd->content)
57      edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
58    edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) * _elm_config->scale);
59    _sizing_eval(obj);
60 }
61
62 static Eina_Bool
63 _elm_frame_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
64 {
65    Widget_Data *wd = elm_widget_data_get(obj);
66    Evas_Object *cur;
67
68    if ((!wd) || (!wd->content))
69      return EINA_FALSE;
70
71    cur = wd->content;
72
73    /* Try Focus cycle in subitem */
74    return elm_widget_focus_next_get(cur, dir, next);
75 }
76
77 static void
78 _sizing_eval(Evas_Object *obj)
79 {
80    Widget_Data *wd = elm_widget_data_get(obj);
81    Evas_Coord minw = -1, minh = -1;
82    if (!wd) return;
83    edje_object_size_min_calc(wd->frm, &minw, &minh);
84    evas_object_size_hint_min_set(obj, minw, minh);
85    evas_object_size_hint_max_set(obj, -1, -1);
86 }
87
88 static void
89 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
90 {
91    Widget_Data *wd = elm_widget_data_get(data);
92    // FIXME: why is this needed? how does edje get this unswallowed or
93    // lose its callbacks to edje
94    if (!wd) return;
95    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
96    _sizing_eval(data);
97 }
98
99 static void
100 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
101 {
102    Widget_Data *wd = elm_widget_data_get(obj);
103    Evas_Object *sub = event_info;
104    if (!wd) return;
105    if (sub == wd->content)
106      {
107         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
108                                             _changed_size_hints, obj);
109         wd->content = NULL;
110         _sizing_eval(obj);
111      }
112 }
113
114 static void
115 _elm_frame_label_set(Evas_Object *obj, const char *item, const char *label)
116 {
117    ELM_CHECK_WIDTYPE(obj, widtype);
118    Widget_Data *wd = elm_widget_data_get(obj);
119    if (item && strcmp(item, "default")) return;
120    if (!wd) return;
121    eina_stringshare_replace(&(wd->label), label);
122    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
123    _sizing_eval(obj);
124 }
125
126 static const char *
127 _elm_frame_label_get(const Evas_Object *obj, const char *item)
128 {
129    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
130    Widget_Data *wd = elm_widget_data_get(obj);
131    if (item && strcmp(item, "default")) return NULL;
132    if (!wd) return NULL;
133    return wd->label;
134 }
135
136 static void
137 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
138 {
139    ELM_CHECK_WIDTYPE(obj, widtype);
140    Widget_Data *wd;
141
142    if (part && strcmp(part, "default")) return;
143    wd = elm_widget_data_get(obj);
144    if (!wd) return;
145    if (wd->content == content) return;
146    if (wd->content) evas_object_del(wd->content);
147    wd->content = content;
148    if (content)
149      {
150         elm_widget_sub_object_add(obj, content);
151         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
152                                        _changed_size_hints, obj);
153         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
154      }
155    _sizing_eval(obj);
156 }
157
158 static Evas_Object *
159 _content_get_hook(const Evas_Object *obj, const char *part)
160 {
161    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
162    Widget_Data *wd;
163
164    if (part && strcmp(part, "default")) return NULL;
165    wd = elm_widget_data_get(obj);
166    if (!wd) return NULL;
167    return wd->content;
168 }
169
170 static Evas_Object *
171 _content_unset_hook(Evas_Object *obj, const char *part)
172 {
173    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
174    Widget_Data *wd;
175    Evas_Object *content;
176    if (part && strcmp(part, "default")) return NULL;
177    wd = elm_widget_data_get(obj);
178    if (!wd) return NULL;
179    if (!wd->content) return NULL;
180    content = wd->content;
181    elm_widget_sub_object_del(obj, wd->content);
182    evas_object_event_callback_del_full(wd->content,
183                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
184                                        _changed_size_hints, obj);
185    edje_object_part_unswallow(wd->frm, wd->content);
186    wd->content = NULL;
187    return content;
188 }
189
190 static void
191 _signal_click(Evas_Object *fr, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
192 {
193    Widget_Data *wd;
194    wd = elm_widget_data_get(fr);
195    if (!wd) return;
196    evas_object_smart_callback_call(fr, SIG_CLICKED, NULL);
197    if (!wd->collapsible) return;
198    edje_object_signal_emit(wd->frm, "elm,action,collapse", "elm");
199    wd->collapsed++;
200 }
201
202 EAPI Evas_Object *
203 elm_frame_add(Evas_Object *parent)
204 {
205    Evas_Object *obj;
206    Evas *e;
207    Widget_Data *wd;
208
209    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
210
211    ELM_SET_WIDTYPE(widtype, "frame");
212    elm_widget_type_set(obj, "frame");
213    elm_widget_sub_object_add(parent, obj);
214    elm_widget_data_set(obj, wd);
215    elm_widget_del_hook_set(obj, _del_hook);
216    elm_widget_theme_hook_set(obj, _theme_hook);
217    elm_widget_focus_next_hook_set(obj, _elm_frame_focus_next_hook);
218    elm_widget_can_focus_set(obj, EINA_FALSE);
219    elm_widget_text_set_hook_set(obj, _elm_frame_label_set);
220    elm_widget_text_get_hook_set(obj, _elm_frame_label_get);
221    elm_widget_content_set_hook_set(obj, _content_set_hook);
222    elm_widget_content_get_hook_set(obj, _content_get_hook);
223    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
224
225    wd->frm = edje_object_add(e);
226    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
227    elm_widget_resize_object_set(obj, wd->frm);
228
229    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
230    edje_object_signal_callback_add(wd->frm, "elm,action,click", "elm",
231                                    (Edje_Signal_Cb)_signal_click, obj);
232    evas_object_smart_callbacks_descriptions_set(obj, _signals);
233
234    _mirrored_set(obj, elm_widget_mirrored_get(obj));
235    _sizing_eval(obj);
236    return obj;
237 }
238
239 EAPI void
240 elm_frame_autocollapse_set(Evas_Object *obj, Eina_Bool enable)
241 {
242    Widget_Data *wd;
243    ELM_CHECK_WIDTYPE(obj, widtype);
244    wd = elm_widget_data_get(obj);
245    if (!wd) return;
246    wd->collapsible = !!enable;
247 }
248
249 EAPI Eina_Bool
250 elm_frame_autocollapse_get(Evas_Object *obj)
251 {
252    Widget_Data *wd;
253    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
254    wd = elm_widget_data_get(obj);
255    if (!wd) return EINA_FALSE;
256    return wd->collapsible;
257 }
258
259 EAPI void
260 elm_frame_collapse_set(Evas_Object *obj, Eina_Bool enable)
261 {
262    Widget_Data *wd;
263    ELM_CHECK_WIDTYPE(obj, widtype);
264    wd = elm_widget_data_get(obj);
265    if (!wd) return;
266    enable = !!enable;
267    if (wd->collapsed == enable) return;
268    edje_object_signal_emit(wd->frm, "elm,action,collapse", "elm");
269    wd->collapsed = enable;
270 }
271
272 EAPI Eina_Bool
273 elm_frame_collapse_get(Evas_Object *obj)
274 {
275    Widget_Data *wd;
276    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
277    wd = elm_widget_data_get(obj);
278    if (!wd) return EINA_FALSE;
279    return wd->collapsed;
280 }
281
282
283 EAPI void
284 elm_frame_label_set(Evas_Object *obj, const char *label)
285 {
286    _elm_frame_label_set(obj, NULL, label);
287 }
288
289 EAPI const char *
290 elm_frame_label_get(const Evas_Object *obj)
291 {
292    return _elm_frame_label_get(obj, NULL);
293 }
294
295 EAPI void
296 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
297 {
298    _content_set_hook(obj, NULL, content);
299 }
300
301 EAPI Evas_Object *
302 elm_frame_content_get(const Evas_Object *obj)
303 {
304    return _content_get_hook(obj, NULL);
305 }
306
307 EAPI Evas_Object *
308 elm_frame_content_unset(Evas_Object *obj)
309 {
310    return _content_unset_hook(obj, NULL);
311 }