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