elemetary - notify, factory, frame, panel, mapbuf
[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 static void
128 _content_set_hook(Evas_Object *obj, const char *part __UNUSED__, Evas_Object *content)
129 {
130    ELM_CHECK_WIDTYPE(obj, widtype);
131    Widget_Data *wd = elm_widget_data_get(obj);
132    if (!wd) return;
133    if (wd->content == content) return;
134    if (wd->content) evas_object_del(wd->content);
135    wd->content = content;
136    if (content)
137      {
138         elm_widget_sub_object_add(obj, content);
139         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
140                                        _changed_size_hints, obj);
141         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
142      }
143    _sizing_eval(obj);
144 }
145
146 static Evas_Object *
147 _content_get_hook(const Evas_Object *obj, const char *part __UNUSED__)
148 {
149    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
150    Widget_Data *wd = elm_widget_data_get(obj);
151    if (!wd) return NULL;
152    return wd->content;
153 }
154
155 static Evas_Object *
156 _content_unset_hook(Evas_Object *obj, const char *part __UNUSED__)
157 {
158    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
159    Widget_Data *wd = elm_widget_data_get(obj);
160    Evas_Object *content;
161    if (!wd) return NULL;
162    if (!wd->content) return NULL;
163    content = wd->content;
164    elm_widget_sub_object_del(obj, wd->content);
165    edje_object_part_unswallow(wd->frm, wd->content);
166    wd->content = NULL;
167    return content;
168 }
169
170 EAPI Evas_Object *
171 elm_frame_add(Evas_Object *parent)
172 {
173    Evas_Object *obj;
174    Evas *e;
175    Widget_Data *wd;
176
177    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
178
179    ELM_SET_WIDTYPE(widtype, "frame");
180    elm_widget_type_set(obj, "frame");
181    elm_widget_sub_object_add(parent, obj);
182    elm_widget_data_set(obj, wd);
183    elm_widget_del_hook_set(obj, _del_hook);
184    elm_widget_theme_hook_set(obj, _theme_hook);
185    elm_widget_focus_next_hook_set(obj, _elm_frame_focus_next_hook);
186    elm_widget_can_focus_set(obj, EINA_FALSE);
187    elm_widget_text_set_hook_set(obj, _elm_frame_label_set);
188    elm_widget_text_get_hook_set(obj, _elm_frame_label_get);
189    elm_widget_content_set_hook_set(obj, _content_set_hook);
190    elm_widget_content_get_hook_set(obj, _content_get_hook);
191    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
192
193    wd->frm = edje_object_add(e);
194    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
195    elm_widget_resize_object_set(obj, wd->frm);
196
197    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
198
199    _mirrored_set(obj, elm_widget_mirrored_get(obj));
200    _sizing_eval(obj);
201    return obj;
202 }
203
204 EAPI void
205 elm_frame_label_set(Evas_Object *obj, const char *label)
206 {
207    _elm_frame_label_set(obj, NULL, label);
208 }
209
210 EAPI const char *
211 elm_frame_label_get(const Evas_Object *obj)
212 {
213    return _elm_frame_label_get(obj, NULL);
214 }
215
216 EAPI void
217 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
218 {
219    _content_set_hook(obj, NULL, content);
220 }
221
222 EAPI Evas_Object *
223 elm_frame_content_get(const Evas_Object *obj)
224 {
225    return _content_get_hook(obj, NULL);
226 }
227
228 EAPI Evas_Object *
229 elm_frame_content_unset(Evas_Object *obj)
230 {
231    return _content_unset_hook(obj, NULL);
232 }