19eb4f11919713a69cd97a8e7f76b289da15fffd
[framework/uifw/elementary.git] / src / lib / elm_layout.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #include <Elementary.h>
6 #include "elm_priv.h"
7
8 /**
9  * @defgroup Layout Layout
10  * @ingroup Elementary
11  *
12  * This takes a standard Edje design file and wraps it very thinly
13  * in a widget and handles swallowing widgets into swallow regions
14  * in the Edje object, allowing Edje to be used as a design and
15  * layout tool
16  */
17
18 typedef struct _Widget_Data Widget_Data;
19 typedef struct _Subinfo Subinfo;
20
21 struct _Widget_Data
22 {
23    Evas_Object *lay;
24    Eina_List *subs;
25    Eina_Bool needs_size_calc:1;
26    const char *clas, *group, *style;
27 };
28
29 struct _Subinfo
30 {
31    const char *swallow;
32    Evas_Object *obj;
33 };
34
35 static const char *widtype = NULL;
36 static void _del_hook(Evas_Object *obj);
37 static void _theme_hook(Evas_Object *obj);
38 static void _sizing_eval(Evas_Object *obj);
39 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
40 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
41
42 static void
43 _del_hook(Evas_Object *obj)
44 {
45    Widget_Data *wd = elm_widget_data_get(obj);
46    Subinfo *si;
47    if (!wd) return;
48    EINA_LIST_FREE(wd->subs, si)
49      {
50         eina_stringshare_del(si->swallow);
51         free(si);
52      }
53    free(wd);
54 }
55
56 static void
57 _theme_hook(Evas_Object *obj)
58 {
59    Widget_Data *wd = elm_widget_data_get(obj);
60    if (!wd) return;
61
62    _elm_theme_object_set(obj, wd->lay, wd->clas, wd->group, wd->style);
63    edje_object_scale_set(wd->lay, elm_widget_scale_get(obj) *
64          _elm_config->scale);
65    _sizing_eval(obj);
66 }
67
68 static void
69 _changed_hook(Evas_Object *obj)
70 {
71    Widget_Data *wd = elm_widget_data_get(obj);
72    if (!wd) return;
73    if (wd->needs_size_calc)
74      {
75         _sizing_eval(obj);
76         wd->needs_size_calc = 0;
77      }
78 }
79
80 static void
81 _sizing_eval(Evas_Object *obj)
82 {
83    Widget_Data *wd = elm_widget_data_get(obj);
84    Evas_Coord minw = -1, minh = -1;
85    if (!wd) return;
86    edje_object_size_min_calc(wd->lay, &minw, &minh);
87    evas_object_size_hint_min_set(obj, minw, minh);
88    evas_object_size_hint_max_set(obj, -1, -1);
89 }
90
91 static void
92 _request_sizing_eval(Evas_Object *obj)
93 {
94    Widget_Data *wd = elm_widget_data_get(obj);
95    if (!wd) return;
96    if (wd->needs_size_calc) return;
97    wd->needs_size_calc = 1;
98    evas_object_smart_changed(obj);
99 }
100
101 static void
102 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
103 {
104    _request_sizing_eval(data);
105 }
106
107 static void
108 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
109 {
110    Widget_Data *wd = elm_widget_data_get(obj);
111    Evas_Object *sub = event_info;
112    Eina_List *l;
113    Subinfo *si;
114    if (!wd) return;
115    EINA_LIST_FOREACH(wd->subs, l, si)
116      {
117         if (si->obj == sub)
118           {
119              evas_object_event_callback_del_full(sub,
120                    EVAS_CALLBACK_CHANGED_SIZE_HINTS,
121                    _changed_size_hints,
122                    obj);
123              wd->subs = eina_list_remove_list(wd->subs, l);
124              eina_stringshare_del(si->swallow);
125              free(si);
126              break;
127           }
128      }
129 }
130
131 static void
132 _signal_size_eval(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
133 {
134    _request_sizing_eval(data);
135 }
136
137 /**
138  * Add a new layout to the parent
139  *
140  * @param parent The parent object
141  * @return The new object or NULL if it cannot be created
142  *
143  * @ingroup Layout
144  */
145 EAPI Evas_Object *
146 elm_layout_add(Evas_Object *parent)
147 {
148    Evas_Object *obj;
149    Evas *e;
150    Widget_Data *wd;
151
152    wd = ELM_NEW(Widget_Data);
153    e = evas_object_evas_get(parent);
154    obj = elm_widget_add(e);
155    ELM_SET_WIDTYPE(widtype, "layout");
156    elm_widget_type_set(obj, "layout");
157    elm_widget_sub_object_add(parent, obj);
158    elm_widget_data_set(obj, wd);
159    elm_widget_del_hook_set(obj, _del_hook);
160    elm_widget_theme_hook_set(obj, _theme_hook);
161    elm_widget_changed_hook_set(obj, _changed_hook);
162
163    wd->lay = edje_object_add(e);
164    elm_widget_resize_object_set(obj, wd->lay);
165    edje_object_signal_callback_add(wd->lay, "size,eval", "elm",
166          _signal_size_eval, obj);
167
168    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
169
170    _request_sizing_eval(obj);
171    return obj;
172 }
173
174 /**
175  * Set the file that will be used as layout
176  *
177  * @param obj The layout object
178  * @param file The path to file (edj) that will be used as layout
179  * @param group The group that the layout belongs in edje file
180  *
181  * @return (1 = sucess, 0 = error)
182  *
183  * @ingroup Layout
184  */
185 EAPI Eina_Bool
186 elm_layout_file_set(Evas_Object *obj, const char *file, const char *group)
187 {
188    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
189    Widget_Data *wd = elm_widget_data_get(obj);
190    if (!wd) return EINA_FALSE;
191    Eina_Bool ret = edje_object_file_set(wd->lay, file, group);
192    if (ret) _request_sizing_eval(obj);
193    return ret;
194 }
195
196 /**
197  * Set the edje group from the elementary theme that will be used as layout
198  *
199  * @param obj The layout object
200  * @param clas the clas of the group
201  * @param group the group
202  * @param style the style to used
203  *
204  * @return (1 = sucess, 0 = error)
205  *
206  * @ingroup Layout
207  */
208 EAPI Eina_Bool
209 elm_layout_theme_set(Evas_Object *obj, const char *clas, const char *group, const char *style)
210 {
211    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
212    Widget_Data *wd = elm_widget_data_get(obj);
213    if (!wd) return EINA_FALSE;
214    Eina_Bool ret = _elm_theme_object_set(obj, wd->lay, clas, group, style);
215    wd->clas = clas;
216    wd->group = group;
217    wd->style = style;
218    if (ret) _request_sizing_eval(obj);
219    return ret;
220 }
221
222 /**
223  * Set the layout content
224  *
225  * @param obj The layout object
226  * @param swallow The swallow group name in the edje file
227  * @param content The content will be filled in this layout object
228  *
229  * @ingroup Layout
230  */
231 EAPI void
232 elm_layout_content_set(Evas_Object *obj, const char *swallow, Evas_Object *content)
233 {
234    ELM_CHECK_WIDTYPE(obj, widtype);
235    Widget_Data *wd = elm_widget_data_get(obj);
236    Subinfo *si;
237    const Eina_List *l;
238    if (!wd) return;
239    EINA_LIST_FOREACH(wd->subs, l, si)
240      {
241         if (!strcmp(swallow, si->swallow))
242           {
243              if (content == si->obj) return;
244              elm_widget_sub_object_del(obj, si->obj);
245              break;
246           }
247      }
248    if (content)
249      {
250         elm_widget_sub_object_add(obj, content);
251         evas_object_event_callback_add(content,
252               EVAS_CALLBACK_CHANGED_SIZE_HINTS,
253               _changed_size_hints, obj);
254         edje_object_part_swallow(wd->lay, swallow, content);
255         si = ELM_NEW(Subinfo);
256         si->swallow = eina_stringshare_add(swallow);
257         si->obj = content;
258         wd->subs = eina_list_append(wd->subs, si);
259         _request_sizing_eval(obj);
260      }
261 }
262
263 /**
264  * Unset the layout content
265  *
266  * Unparent and return the content object which was set for this widget
267  *
268  * @param obj The layout object
269  * @param swallow The swallow group name in the edje file
270  * @return The content that was being used
271  *
272  * @ingroup Layout
273  */
274 EAPI Evas_Object *
275 elm_layout_content_unset(Evas_Object *obj, const char *swallow)
276 {
277    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
278    Widget_Data *wd = elm_widget_data_get(obj);
279    Subinfo *si;
280    const Eina_List *l;
281    if (!wd) return NULL;
282    EINA_LIST_FOREACH(wd->subs, l, si)
283      {
284         if (!strcmp(swallow, si->swallow))
285           {
286              Evas_Object *content;
287              if (!si->obj) return NULL;
288              content = si->obj; /* si will die in _sub_del due elm_widget_sub_object_del() */
289              elm_widget_sub_object_del(obj, content);
290              edje_object_part_unswallow(wd->lay, content);
291              return content;
292           }
293      }
294    return NULL;
295 }
296
297 /**
298  * Get the edje layout
299  *
300  * @param obj The layout object
301  * 
302  * This returns the edje object. It is not expected to be used to then swallow
303  * objects via edje_object_part_swallow() for example. Use 
304  * elm_layout_content_set() instead so child object handling and sizing is
305  * done properly. This is more intended for setting text, emitting signals,
306  * hooking to singal callbacks etc.
307  *
308  * @return A Evas_Object with the edje layout settings loaded
309  * with function elm_layout_file_set
310  *
311  * @ingroup Layout
312  */
313 EAPI Evas_Object *
314 elm_layout_edje_get(const Evas_Object *obj)
315 {
316    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
317    Widget_Data *wd = elm_widget_data_get(obj);
318    if (!wd) return NULL;
319    return wd->lay;
320 }
321
322 /**
323  * Get the edje layout
324  * 
325  * Manually forms a sizing re-evaluation when contents changed state so that
326  * minimum size might have changed and needs re-evaluation. Also note that
327  * a standard signal of "size,eval" "elm" emitted by the edje object will
328  * cause this to happen too
329  *
330  * @param obj The layout object
331  *
332  * @ingroup Layout
333  */
334 EAPI void
335 elm_layout_sizing_eval(Evas_Object *obj)
336 {
337    ELM_CHECK_WIDTYPE(obj, widtype);
338    _request_sizing_eval(obj);
339 }