svn update: 51469 (latest:51480)
[framework/uifw/elementary.git] / src / lib / elm_mapbuf.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Mapbuf Mapbuf
6  *
7  * This holds 1 content object and uses an Evas Map to move/resize etc. it.
8  */
9
10 typedef struct _Widget_Data Widget_Data;
11
12 struct _Widget_Data
13 {
14    Evas_Object *content;
15    Eina_Bool enabled : 1;
16    Eina_Bool alpha : 1;
17    Eina_Bool smooth : 1;
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    _sizing_eval(obj);
41 }
42
43 static void
44 _sizing_eval(Evas_Object *obj)
45 {
46    Widget_Data *wd = elm_widget_data_get(obj);
47    Evas_Coord minw = -1, minh = -1;
48    Evas_Coord maxw = -1, maxh = -1;
49    if (!wd) return;
50    if (wd->content)
51      {
52         evas_object_size_hint_min_get(wd->content, &minw, &minh);
53         evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
54      }
55    evas_object_size_hint_min_set(obj, minw, minh);
56    evas_object_size_hint_max_set(obj, maxw, maxh);
57 }
58
59 static void
60 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
61 {
62    Widget_Data *wd = elm_widget_data_get(data);
63    if (!wd) return;
64    _sizing_eval(data);
65 }
66
67 static void
68 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
69 {
70    Widget_Data *wd = elm_widget_data_get(obj);
71    Evas_Object *sub = event_info;
72    if (!wd) return;
73    if (sub == wd->content)
74      {
75         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
76                                             _changed_size_hints, obj);
77         wd->content = NULL;
78         _sizing_eval(obj);
79      }
80 }
81
82 static void
83 _mapbuf(Evas_Object *obj)
84 {
85    Widget_Data *wd = elm_widget_data_get(obj);
86    Evas_Coord x, y, w, h;
87    if (!wd) return;
88    evas_object_geometry_get(obj, &x, &y, &w, &h);
89    if (wd->enabled)
90      {
91         Evas_Map *m;
92
93         m = evas_map_new(4);
94         evas_map_util_points_populate_from_geometry(m, x, y, w, h, 0);
95         evas_map_smooth_set(m, wd->smooth);
96         evas_map_alpha_set(m, wd->alpha);
97         evas_object_map_set(wd->content, m);
98         evas_object_map_enable_set(wd->content, wd->enabled);
99         evas_map_free(m);
100      }
101    else
102      {
103         evas_object_map_set(wd->content, NULL);
104         evas_object_map_enable_set(wd->content, 0);
105         evas_object_move(wd->content, x, y);
106         evas_object_resize(wd->content, w, h);
107      }
108 }
109
110 static void
111 _configure(Evas_Object *obj)
112 {
113    Widget_Data *wd = elm_widget_data_get(obj);
114    if (!wd) return;
115    if (wd->content)
116      {
117         Evas_Coord x, y, w, h;
118         
119         evas_object_geometry_get(obj, &x, &y, &w, &h);
120         if (!wd->enabled) evas_object_move(wd->content, x, y);
121         evas_object_resize(wd->content, w, h);
122         _mapbuf(obj);
123      }
124 }
125
126 static void
127 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
128 {
129    _configure(obj);
130 }
131
132 static void
133 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
134 {    
135    _configure(obj);
136 }
137
138 /**
139  * Add a new mapbuf to the parent
140  *
141  * @param parent The parent object
142  * @return The new object or NULL if it cannot be created
143  *
144  * @ingroup Mapbuf
145  */
146 EAPI Evas_Object *
147 elm_mapbuf_add(Evas_Object *parent)
148 {
149    Evas_Object *obj;
150    Evas *e;
151    Widget_Data *wd;
152
153    wd = ELM_NEW(Widget_Data);
154    e = evas_object_evas_get(parent);
155    obj = elm_widget_add(e);
156    ELM_SET_WIDTYPE(widtype, "mapbuf");
157    elm_widget_type_set(obj, "mapbuf");
158    elm_widget_sub_object_add(parent, obj);
159    elm_widget_data_set(obj, wd);
160    elm_widget_del_hook_set(obj, _del_hook);
161    elm_widget_theme_hook_set(obj, _theme_hook);
162
163    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
164    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
165    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
166
167    wd->enabled = 0;
168    wd->alpha = 1;
169    wd->smooth = 1;
170    
171    _sizing_eval(obj);
172    return obj;
173 }
174
175 /**
176  * Set the mapbuf front content
177  *
178  * Once the content object is set, a previously set one will be deleted.
179  * If you want to keep that old content object, use the
180  * elm_mapbuf_content_unset() function.
181  *
182  * @param obj The mapbuf object
183  * @param content The content will be filled in this mapbuf object
184  *
185  * @ingroup Mapbuf
186  */
187 EAPI void
188 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
189 {
190    ELM_CHECK_WIDTYPE(obj, widtype);
191    Widget_Data *wd = elm_widget_data_get(obj);
192    if (!wd) return;
193    if (wd->content == content) return;
194    if (wd->content) evas_object_del(wd->content);
195    wd->content = content;
196    if (content)
197      {
198         elm_widget_sub_object_add(content, obj);
199         evas_object_smart_member_add(content, obj);
200         evas_object_event_callback_add(content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
201                                        _changed_size_hints, obj);
202      }
203    _sizing_eval(obj);
204    _configure(obj);
205 }
206
207 /**
208  * Unset the mapbuf front content
209  *
210  * Unparent and return the content object which was set for this widget.
211  *
212  * @param obj The mapbuf object
213  * @return The content that was being used
214  *
215  * @ingroup Mapbuf
216  */
217 EAPI Evas_Object *
218 elm_mapbuf_content_unset(Evas_Object *obj)
219 {
220    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
221    Widget_Data *wd = elm_widget_data_get(obj);
222    Evas_Object *content;
223    if (!wd) return NULL;
224    if (!wd->content) return NULL;
225    content = wd->content;
226    elm_widget_sub_object_del(obj, wd->content);
227    evas_object_smart_member_del(wd->content);
228    wd->content = NULL;
229    return content;
230 }
231
232 /**
233  * Set the mapbuf enabled state
234  *
235  * @param obj The mapbuf object
236  * @param enabled The value to set the enabled state to
237  *
238  * @ingroup Mapbuf
239  */
240 EAPI void
241 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
242 {
243    ELM_CHECK_WIDTYPE(obj, widtype);
244    Widget_Data *wd = elm_widget_data_get(obj);
245    if (!wd) return;
246    if (wd->enabled == enabled) return;
247    wd->enabled = enabled;
248    _configure(obj);
249 }
250
251 /**
252  * Get the mapbuf enabled state
253  *
254  * @param obj The mapbuf object
255  * @return The value that the enabled state is set to
256  *
257  * @ingroup Mapbuf
258  */
259 EAPI Eina_Bool
260 elm_mapbuf_enabled_get(const Evas_Object *obj)
261 {
262    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
263    Widget_Data *wd = elm_widget_data_get(obj);
264    if (!wd) return EINA_FALSE;
265    return wd->enabled;
266 }
267
268 /**
269  * Sets the mapbuf smooth state
270  *
271  * @param obj The mapbuf object
272  * @param smooth The value of the smooth state of @p obj
273  *
274  * @ingroup Mapbuf
275  */
276 EAPI void
277 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
278 {
279    ELM_CHECK_WIDTYPE(obj, widtype);
280    Widget_Data *wd = elm_widget_data_get(obj);
281    if (!wd) return;
282    if (wd->smooth == smooth) return;
283    wd->smooth = smooth;
284    _configure(obj);
285 }
286
287 /**
288  * Gets the mapbuf smooth state
289  *
290  * @param obj The mapbuf object
291  * @return The value of the smooth state of @p obj
292  *
293  * @ingroup Mapbuf
294  */
295 EAPI Eina_Bool
296 elm_mapbuf_smooth_get(const Evas_Object *obj)
297 {
298    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
299    Widget_Data *wd = elm_widget_data_get(obj);
300    if (!wd) return EINA_FALSE;
301    return wd->smooth;
302 }
303
304 /**
305  * Enables/disables the mapbuf alpha channel
306  *
307  * @param obj The mapbuf object
308  * @param alpha The state of the alpha channel
309  *
310  * @ingroup Mapbuf
311  */
312 EAPI void
313 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
314 {
315    ELM_CHECK_WIDTYPE(obj, widtype);
316    Widget_Data *wd = elm_widget_data_get(obj);
317    if (!wd) return;
318    if (wd->alpha == alpha) return;
319    wd->alpha = alpha;
320    _configure(obj);
321 }
322
323 /**
324  * Gets the state of the mapbuf alpha channel
325  *
326  * @param obj The mapbuf object
327  * @return The state of the alpha channel
328  *
329  * @ingroup Mapbuf
330  */
331 EAPI Eina_Bool
332 elm_mapbuf_alpha_get(const Evas_Object *obj)
333 {
334    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
335    Widget_Data *wd = elm_widget_data_get(obj);
336    if (!wd) return EINA_FALSE;
337    return wd->alpha;
338 }