Merge branch 'master' into svn_merge
[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 };
12
13 static const char *widtype = NULL;
14 static void _del_hook(Evas_Object *obj);
15 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
16 static void _theme_hook(Evas_Object *obj);
17 static void _sizing_eval(Evas_Object *obj);
18 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
19 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
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    if (wd->label) eina_stringshare_del(wd->label);
27    free(wd);
28 }
29
30 static void
31 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
32 {
33    Widget_Data *wd = elm_widget_data_get(obj);
34    if (!wd) return;
35    edje_object_mirrored_set(wd->frm, rtl);
36 }
37
38 static void
39 _theme_hook(Evas_Object *obj)
40 {
41    Widget_Data *wd = elm_widget_data_get(obj);
42    if (!wd) return;
43    _elm_widget_mirrored_reload(obj);
44    _mirrored_set(obj, elm_widget_mirrored_get(obj));
45    _elm_theme_object_set(obj, wd->frm, "frame", "base", elm_widget_style_get(obj));
46    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
47    if (wd->content)
48      edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
49    edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) * _elm_config->scale);
50    _sizing_eval(obj);
51 }
52
53 static Eina_Bool
54 _elm_frame_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
55 {
56    Widget_Data *wd = elm_widget_data_get(obj);
57    Evas_Object *cur;
58
59    if ((!wd) || (!wd->content))
60      return EINA_FALSE;
61
62    cur = wd->content;
63
64    /* Try Focus cycle in subitem */
65    return elm_widget_focus_next_get(cur, dir, next);
66 }
67
68 static void
69 _sizing_eval(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    Evas_Coord minw = -1, minh = -1;
73    if (!wd) return;
74    edje_object_size_min_calc(wd->frm, &minw, &minh);
75    evas_object_size_hint_min_set(obj, minw, minh);
76    evas_object_size_hint_max_set(obj, -1, -1);
77 }
78
79 static void
80 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
81 {
82    Widget_Data *wd = elm_widget_data_get(data);
83    // FIXME: why is this needed? how does edje get this unswallowed or
84    // lose its callbacks to edje
85    if (!wd) return;
86    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
87    _sizing_eval(data);
88 }
89
90 static void
91 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
92 {
93    Widget_Data *wd = elm_widget_data_get(obj);
94    Evas_Object *sub = event_info;
95    if (!wd) return;
96    if (sub == wd->content)
97      {
98         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
99                                             _changed_size_hints, obj);
100         wd->content = NULL;
101         _sizing_eval(obj);
102      }
103 }
104
105 static void
106 _elm_frame_label_set(Evas_Object *obj, const char *item, const char *label)
107 {
108    ELM_CHECK_WIDTYPE(obj, widtype);
109    Widget_Data *wd = elm_widget_data_get(obj);
110    if (item && strcmp(item, "default")) return;
111    if (!wd) return;
112    eina_stringshare_replace(&(wd->label), label);
113    edje_object_part_text_set(wd->frm, "elm.text", wd->label);
114    _sizing_eval(obj);
115 }
116
117 static const char *
118 _elm_frame_label_get(const Evas_Object *obj, const char *item)
119 {
120    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
121    Widget_Data *wd = elm_widget_data_get(obj);
122    if (item && strcmp(item, "default")) return NULL;
123    if (!wd) return NULL;
124    return wd->label;
125 }
126
127 EAPI Evas_Object *
128 elm_frame_add(Evas_Object *parent)
129 {
130    Evas_Object *obj;
131    Evas *e;
132    Widget_Data *wd;
133
134    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
135
136    ELM_SET_WIDTYPE(widtype, "frame");
137    elm_widget_type_set(obj, "frame");
138    elm_widget_sub_object_add(parent, obj);
139    elm_widget_data_set(obj, wd);
140    elm_widget_del_hook_set(obj, _del_hook);
141    elm_widget_theme_hook_set(obj, _theme_hook);
142    elm_widget_focus_next_hook_set(obj, _elm_frame_focus_next_hook);
143    elm_widget_can_focus_set(obj, EINA_FALSE);
144    elm_widget_text_set_hook_set(obj, _elm_frame_label_set);
145    elm_widget_text_get_hook_set(obj, _elm_frame_label_get);
146
147    wd->frm = edje_object_add(e);
148    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
149    elm_widget_resize_object_set(obj, wd->frm);
150
151    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
152
153    _mirrored_set(obj, elm_widget_mirrored_get(obj));
154    _sizing_eval(obj);
155    return obj;
156 }
157
158 EAPI void
159 elm_frame_label_set(Evas_Object *obj, const char *label)
160 {
161    _elm_frame_label_set(obj, NULL, label);
162 }
163
164 EAPI const char *
165 elm_frame_label_get(const Evas_Object *obj)
166 {
167    return _elm_frame_label_get(obj, NULL);
168 }
169
170 EAPI void
171 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
172 {
173    ELM_CHECK_WIDTYPE(obj, widtype);
174    Widget_Data *wd = elm_widget_data_get(obj);
175    if (!wd) return;
176    if (wd->content == content) return;
177    if (wd->content) evas_object_del(wd->content);
178    wd->content = content;
179    if (content)
180      {
181         elm_widget_sub_object_add(obj, content);
182         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
183                                        _changed_size_hints, obj);
184         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
185      }
186    _sizing_eval(obj);
187 }
188
189 EAPI Evas_Object *
190 elm_frame_content_get(const Evas_Object *obj)
191 {
192    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
193    Widget_Data *wd = elm_widget_data_get(obj);
194    if (!wd) return NULL;
195    return wd->content;
196 }
197
198 EAPI Evas_Object *
199 elm_frame_content_unset(Evas_Object *obj)
200 {
201    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
202    Widget_Data *wd = elm_widget_data_get(obj);
203    Evas_Object *content;
204    if (!wd) return NULL;
205    if (!wd->content) return NULL;
206    content = wd->content;
207    elm_widget_sub_object_del(obj, wd->content);
208    edje_object_part_unswallow(wd->frm, wd->content);
209    wd->content = NULL;
210    return content;
211 }