svn update: 51469 (latest:51480)
[framework/uifw/elementary.git] / src / lib / elm_frame.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Frame Frame
6  *
7  * This holds some content and has a title. Looks like a frame, but
8  * supports styles so multple frames are avaible
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *frm;
16    Evas_Object *content;
17 };
18
19 static const char *widtype = NULL;
20 static void _del_hook(Evas_Object *obj);
21 static void _theme_hook(Evas_Object *obj);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
24 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
25
26 static void
27 _del_hook(Evas_Object *obj)
28 {
29    Widget_Data *wd = elm_widget_data_get(obj);
30    if (!wd) return;
31    free(wd);
32 }
33
34 static void
35 _theme_hook(Evas_Object *obj)
36 {
37    Widget_Data *wd = elm_widget_data_get(obj);
38    if (!wd) return;
39    _elm_theme_object_set(obj, wd->frm, "frame", "base", elm_widget_style_get(obj));
40    if (wd->content)
41      edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
42    edje_object_scale_set(wd->frm, elm_widget_scale_get(obj) * _elm_config->scale);
43    _sizing_eval(obj);
44 }
45
46 static void
47 _sizing_eval(Evas_Object *obj)
48 {
49    Widget_Data *wd = elm_widget_data_get(obj);
50    Evas_Coord minw = -1, minh = -1;
51    if (!wd) return;
52    edje_object_size_min_calc(wd->frm, &minw, &minh);
53    evas_object_size_hint_min_set(obj, minw, minh);
54    evas_object_size_hint_max_set(obj, -1, -1);
55 }
56
57 static void
58 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
59 {
60    Widget_Data *wd = elm_widget_data_get(data);
61    // FIXME: why is this needed? how does edje get this unswallowed or
62    // lose its callbacks to edje
63    if (!wd) return;
64    edje_object_part_swallow(wd->frm, "elm.swallow.content", wd->content);
65    _sizing_eval(data);
66 }
67
68 static void
69 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    Evas_Object *sub = event_info;
73    if (!wd) return;
74    if (sub == wd->content)
75      {
76         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
77                                             _changed_size_hints, obj);
78         wd->content = NULL;
79         _sizing_eval(obj);
80      }
81 }
82
83 /**
84  * Add a new frame to the parent
85  *
86  * @param parent The parent object
87  * @return The new object or NULL if it cannot be created
88  *
89  * @ingroup Frame
90  */
91 EAPI Evas_Object *
92 elm_frame_add(Evas_Object *parent)
93 {
94    Evas_Object *obj;
95    Evas *e;
96    Widget_Data *wd;
97
98    wd = ELM_NEW(Widget_Data);
99    e = evas_object_evas_get(parent);
100    obj = elm_widget_add(e);
101    ELM_SET_WIDTYPE(widtype, "frame");
102    elm_widget_type_set(obj, "frame");
103    elm_widget_sub_object_add(parent, obj);
104    elm_widget_data_set(obj, wd);
105    elm_widget_del_hook_set(obj, _del_hook);
106    elm_widget_theme_hook_set(obj, _theme_hook);
107
108    wd->frm = edje_object_add(e);
109    _elm_theme_object_set(obj, wd->frm, "frame", "base", "default");
110    elm_widget_resize_object_set(obj, wd->frm);
111
112    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
113
114    _sizing_eval(obj);
115    return obj;
116 }
117
118 /**
119  * Set the frame label
120  *
121  * @param obj The frame object
122  * @param label The label of this frame object
123  *
124  * @ingroup Frame
125  */
126 EAPI void
127 elm_frame_label_set(Evas_Object *obj, const char *label)
128 {
129    ELM_CHECK_WIDTYPE(obj, widtype);
130    Widget_Data *wd = elm_widget_data_get(obj);
131    if (!wd) return;
132    edje_object_part_text_set(wd->frm, "elm.text", label);
133    _sizing_eval(obj);
134 }
135
136 /**
137  * Get the frame label
138  *
139  * @param obj The frame object
140  *
141  * @return The label of this frame objet or NULL if unable to get frame
142  *
143  * @ingroup Frame
144  */
145 EAPI const char *
146 elm_frame_label_get(const Evas_Object *obj)
147 {
148    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
149    Widget_Data *wd = elm_widget_data_get(obj);
150    if ((!wd) || (!wd->frm)) return NULL;
151    return edje_object_part_text_get(wd->frm, "elm.text");
152 }
153
154 /**
155  * Set the content of the frame widget
156  *
157  * Once the content object is set, a previously set one will be deleted.
158  * If you want to keep that old content object, use the
159  * elm_frame_content_unset() function.
160  *
161  * @param obj The frame object
162  * @param content The content will be filled in this frame object
163  *
164  * @ingroup Frame
165  */
166 EAPI void
167 elm_frame_content_set(Evas_Object *obj, Evas_Object *content)
168 {
169    ELM_CHECK_WIDTYPE(obj, widtype);
170    Widget_Data *wd = elm_widget_data_get(obj);
171    if (!wd) return;
172    if (wd->content == content) return;
173    if (wd->content) evas_object_del(wd->content);
174    wd->content = content;
175    if (content)
176      {
177         elm_widget_sub_object_add(obj, content);
178         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
179                                        _changed_size_hints, obj);
180         edje_object_part_swallow(wd->frm, "elm.swallow.content", content);
181      }
182    _sizing_eval(obj);
183 }
184
185 /**
186  * Unset the content of the frame widget
187  *
188  * Unparent and return the content object which was set for this widget
189  *
190  * @param obj The frame object
191  * @return The content that was being used
192  *
193  * @ingroup Frame
194  */
195 EAPI Evas_Object *
196 elm_frame_content_unset(Evas_Object *obj)
197 {
198    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
199    Widget_Data *wd = elm_widget_data_get(obj);
200    Evas_Object *content;
201    if (!wd) return NULL;
202    if (!wd->content) return NULL;
203    content = wd->content;
204    elm_widget_sub_object_del(obj, wd->content);
205    edje_object_part_unswallow(wd->frm, wd->content);
206    wd->content = NULL;
207    return content;
208 }