c32437d3520982aff67298b1806e664be86734e6
[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  * Once the content object is set, a previously set one will be deleted.
226  * If you want to keep that old content object, use the
227  * elm_layout_content_unset() function.
228  *
229  * @param obj The layout object
230  * @param swallow The swallow group name in the edje file
231  * @param content The content will be filled in this layout object
232  *
233  * @ingroup Layout
234  */
235 EAPI void
236 elm_layout_content_set(Evas_Object *obj, const char *swallow, Evas_Object *content)
237 {
238    ELM_CHECK_WIDTYPE(obj, widtype);
239    Widget_Data *wd = elm_widget_data_get(obj);
240    Subinfo *si;
241    const Eina_List *l;
242    if (!wd) return;
243    EINA_LIST_FOREACH(wd->subs, l, si)
244      {
245         if (!strcmp(swallow, si->swallow))
246           {
247              if (content == si->obj) return;
248              evas_object_del(si->obj);
249              break;
250           }
251      }
252    if (content)
253      {
254         elm_widget_sub_object_add(obj, content);
255         evas_object_event_callback_add(content,
256               EVAS_CALLBACK_CHANGED_SIZE_HINTS,
257               _changed_size_hints, obj);
258         edje_object_part_swallow(wd->lay, swallow, content);
259         si = ELM_NEW(Subinfo);
260         si->swallow = eina_stringshare_add(swallow);
261         si->obj = content;
262         wd->subs = eina_list_append(wd->subs, si);
263      }
264    _request_sizing_eval(obj);
265 }
266
267 /**
268  * Unset the layout content
269  *
270  * Unparent and return the content object which was set for this widget
271  *
272  * @param obj The layout object
273  * @param swallow The swallow group name in the edje file
274  * @return The content that was being used
275  *
276  * @ingroup Layout
277  */
278 EAPI Evas_Object *
279 elm_layout_content_unset(Evas_Object *obj, const char *swallow)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
282    Widget_Data *wd = elm_widget_data_get(obj);
283    Subinfo *si;
284    const Eina_List *l;
285    if (!wd) return NULL;
286    EINA_LIST_FOREACH(wd->subs, l, si)
287      {
288         if (!strcmp(swallow, si->swallow))
289           {
290              Evas_Object *content;
291              if (!si->obj) return NULL;
292              content = si->obj; /* si will die in _sub_del due elm_widget_sub_object_del() */
293              elm_widget_sub_object_del(obj, content);
294              edje_object_part_unswallow(wd->lay, content);
295              return content;
296           }
297      }
298    return NULL;
299 }
300
301 /**
302  * Get the edje layout
303  *
304  * @param obj The layout object
305  * 
306  * This returns the edje object. It is not expected to be used to then swallow
307  * objects via edje_object_part_swallow() for example. Use 
308  * elm_layout_content_set() instead so child object handling and sizing is
309  * done properly. This is more intended for setting text, emitting signals,
310  * hooking to singal callbacks etc.
311  *
312  * @return A Evas_Object with the edje layout settings loaded
313  * with function elm_layout_file_set
314  *
315  * @ingroup Layout
316  */
317 EAPI Evas_Object *
318 elm_layout_edje_get(const Evas_Object *obj)
319 {
320    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
321    Widget_Data *wd = elm_widget_data_get(obj);
322    if (!wd) return NULL;
323    return wd->lay;
324 }
325
326 /**
327  * Get the edje layout
328  * 
329  * Manually forms a sizing re-evaluation when contents changed state so that
330  * minimum size might have changed and needs re-evaluation. Also note that
331  * a standard signal of "size,eval" "elm" emitted by the edje object will
332  * cause this to happen too
333  *
334  * @param obj The layout object
335  *
336  * @ingroup Layout
337  */
338 EAPI void
339 elm_layout_sizing_eval(Evas_Object *obj)
340 {
341    ELM_CHECK_WIDTYPE(obj, widtype);
342    _request_sizing_eval(obj);
343 }