Merge branch 'master' into svn_merge
[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  * @ingroup Elementary
7  *
8  * This holds 1 content object and uses an Evas Map to move/resize etc. it.
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *content, *clip;
16    Eina_Bool enabled : 1;
17    Eina_Bool alpha : 1;
18    Eina_Bool smooth : 1;
19 };
20
21 static const char *widtype = NULL;
22 static void _del_hook(Evas_Object *obj);
23 static void _theme_hook(Evas_Object *obj);
24 static void _sizing_eval(Evas_Object *obj);
25 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
26 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
27
28 static void
29 _del_hook(Evas_Object *obj)
30 {
31    Widget_Data *wd = elm_widget_data_get(obj);
32    if (!wd) return;
33    free(wd);
34 }
35
36 static void
37 _theme_hook(Evas_Object *obj)
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40    if (!wd) return;
41    _sizing_eval(obj);
42 }
43
44 static void
45 _sizing_eval(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48    Evas_Coord minw = -1, minh = -1;
49    Evas_Coord maxw = -1, maxh = -1;
50    if (!wd) return;
51    if (wd->content)
52      {
53         evas_object_size_hint_min_get(wd->content, &minw, &minh);
54         evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
55      }
56    evas_object_size_hint_min_set(obj, minw, minh);
57    evas_object_size_hint_max_set(obj, maxw, maxh);
58 }
59
60 static void
61 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
62 {
63    Widget_Data *wd = elm_widget_data_get(data);
64    if (!wd) return;
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 static void
84 _mapbuf(Evas_Object *obj)
85 {
86    Widget_Data *wd = elm_widget_data_get(obj);
87    Evas_Coord x, y, w, h;
88    if (!wd) return;
89    evas_object_geometry_get(wd->clip, &x, &y, &w, &h);
90    if (wd->enabled)
91      {
92         Evas_Map *m;
93
94         m = evas_map_new(4);
95         evas_map_util_points_populate_from_geometry(m, x, y, w, h, 0);
96         evas_map_smooth_set(m, wd->smooth);
97         evas_map_alpha_set(m, wd->alpha);
98         evas_object_map_set(wd->content, m);
99         evas_object_map_enable_set(wd->content, wd->enabled);
100         evas_map_free(m);
101      }
102    else
103      {
104         evas_object_map_set(wd->content, NULL);
105         evas_object_map_enable_set(wd->content, 0);
106         evas_object_move(wd->content, x, y);
107         evas_object_resize(wd->content, w, h);
108      }
109 }
110
111 static void
112 _configure(Evas_Object *obj)
113 {
114    Widget_Data *wd = elm_widget_data_get(obj);
115    if (!wd) return;
116    if (wd->content)
117      {
118         Evas_Coord x, y, w, h, x2, y2;
119
120         evas_object_geometry_get(wd->clip, &x, &y, &w, &h);
121         evas_object_geometry_get(wd->content, &x2, &y2, NULL, NULL);
122         if ((x != x2) || (y != y2))
123           {
124              if (!wd->enabled)
125                evas_object_move(wd->content, x, y);
126              else
127                {
128
129                   Evas *e = evas_object_evas_get(obj);
130                   evas_smart_objects_calculate(e);
131                   evas_nochange_push(e);
132                   //                  printf("x-------------------- %i %i\n", x, y);
133                   evas_object_move(wd->content, x, y);
134                   evas_smart_objects_calculate(e);
135                   //                  printf("y--------------------\n");
136                   evas_nochange_pop(e);
137                }
138           }
139         evas_object_resize(wd->content, w, h);
140         _mapbuf(obj);
141      }
142 }
143
144 static void
145 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
146 {
147    _configure(data);
148 }
149
150 static void
151 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
152 {
153    _configure(data);
154 }
155
156 /**
157  * Add a new mapbuf to the parent
158  *
159  * @param parent The parent object
160  * @return The new object or NULL if it cannot be created
161  *
162  * @ingroup Mapbuf
163  */
164 EAPI Evas_Object *
165 elm_mapbuf_add(Evas_Object *parent)
166 {
167    Evas_Object *obj;
168    Evas *e;
169    Widget_Data *wd;
170
171    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
172
173    ELM_SET_WIDTYPE(widtype, "mapbuf");
174    elm_widget_type_set(obj, "mapbuf");
175    elm_widget_sub_object_add(parent, obj);
176    elm_widget_data_set(obj, wd);
177    elm_widget_del_hook_set(obj, _del_hook);
178    elm_widget_theme_hook_set(obj, _theme_hook);
179    elm_widget_can_focus_set(obj, EINA_FALSE);
180
181    wd->clip = evas_object_rectangle_add(e);
182    evas_object_static_clip_set(wd->clip, EINA_TRUE);
183    evas_object_pass_events_set(wd->clip, EINA_TRUE);
184    evas_object_color_set(wd->clip, 0, 0, 0, 0);
185
186    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_MOVE, _move, obj);
187    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_RESIZE, _resize, obj);
188    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
189
190    elm_widget_resize_object_set(obj, wd->clip);
191
192    wd->enabled = 0;
193    wd->alpha = 1;
194    wd->smooth = 1;
195
196    _sizing_eval(obj);
197    return obj;
198 }
199
200 /**
201  * Set the mapbuf front content
202  *
203  * Once the content object is set, a previously set one will be deleted.
204  * If you want to keep that old content object, use the
205  * elm_mapbuf_content_unset() function.
206  *
207  * @param obj The mapbuf object
208  * @param content The content will be filled in this mapbuf object
209  *
210  * @ingroup Mapbuf
211  */
212 EAPI void
213 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
214 {
215    ELM_CHECK_WIDTYPE(obj, widtype);
216    Widget_Data *wd = elm_widget_data_get(obj);
217    if (!wd) return;
218    if (wd->content == content) return;
219    if (wd->content) evas_object_del(wd->content);
220    wd->content = content;
221    if (content)
222      {
223         evas_object_data_set(content, "_elm_leaveme", (void *)1);
224         elm_widget_sub_object_add(content, obj);
225         evas_object_smart_member_add(content, obj);
226         evas_object_clip_set(content, wd->clip);
227         evas_object_color_set(wd->clip, 255, 255, 255, 255);
228         evas_object_event_callback_add(content,
229                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
230                                        _changed_size_hints, obj);
231      }
232    else
233      evas_object_color_set(wd->clip, 0, 0, 0, 0);
234    _sizing_eval(obj);
235    _configure(obj);
236 }
237
238 /**
239  * Get the mapbuf front content
240  *
241  * Return the content object which is set for this widget.
242  *
243  * @param obj The mapbuf object
244  * @return The content that is being used
245  *
246  * @ingroup Mapbuf
247  */
248 EAPI Evas_Object *
249 elm_mapbuf_content_get(const Evas_Object *obj)
250 {
251    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
252    Widget_Data *wd = elm_widget_data_get(obj);
253    if (!wd) return NULL;
254    return wd->content;
255 }
256
257 /**
258  * Unset the mapbuf front content
259  *
260  * Unparent and return the content object which was set for this widget.
261  *
262  * @param obj The mapbuf object
263  * @return The content that was being used
264  *
265  * @ingroup Mapbuf
266  */
267 EAPI Evas_Object *
268 elm_mapbuf_content_unset(Evas_Object *obj)
269 {
270    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
271    Widget_Data *wd = elm_widget_data_get(obj);
272    Evas_Object *content;
273    if (!wd) return NULL;
274    if (!wd->content) return NULL;
275    content = wd->content;
276    elm_widget_sub_object_del(obj, content);
277    evas_object_smart_member_del(content);
278    evas_object_color_set(wd->clip, 0, 0, 0, 0);
279    evas_object_clip_unset(content);
280    evas_object_data_del(content, "_elm_leaveme");
281    wd->content = NULL;
282    return content;
283 }
284
285 /**
286  * Set the mapbuf enabled state
287  *
288  * @param obj The mapbuf object
289  * @param enabled The value to set the enabled state to
290  *
291  * @ingroup Mapbuf
292  */
293 EAPI void
294 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
295 {
296    ELM_CHECK_WIDTYPE(obj, widtype);
297    Widget_Data *wd = elm_widget_data_get(obj);
298    if (!wd) return;
299    if (wd->enabled == enabled) return;
300    wd->enabled = enabled;
301    if (wd->content) evas_object_static_clip_set(wd->content, wd->enabled);
302    _configure(obj);
303 }
304
305 /**
306  * Get the mapbuf enabled state
307  *
308  * @param obj The mapbuf object
309  * @return The value that the enabled state is set to
310  *
311  * @ingroup Mapbuf
312  */
313 EAPI Eina_Bool
314 elm_mapbuf_enabled_get(const Evas_Object *obj)
315 {
316    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
317    Widget_Data *wd = elm_widget_data_get(obj);
318    if (!wd) return EINA_FALSE;
319    return wd->enabled;
320 }
321
322 /**
323  * Sets the mapbuf smooth state
324  *
325  * @param obj The mapbuf object
326  * @param smooth The value of the smooth state of @p obj
327  *
328  * @ingroup Mapbuf
329  */
330 EAPI void
331 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
332 {
333    ELM_CHECK_WIDTYPE(obj, widtype);
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return;
336    if (wd->smooth == smooth) return;
337    wd->smooth = smooth;
338    _configure(obj);
339 }
340
341 /**
342  * Gets the mapbuf smooth state
343  *
344  * @param obj The mapbuf object
345  * @return The value of the smooth state of @p obj
346  *
347  * @ingroup Mapbuf
348  */
349 EAPI Eina_Bool
350 elm_mapbuf_smooth_get(const Evas_Object *obj)
351 {
352    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
353    Widget_Data *wd = elm_widget_data_get(obj);
354    if (!wd) return EINA_FALSE;
355    return wd->smooth;
356 }
357
358 /**
359  * Enables/disables the mapbuf alpha channel
360  *
361  * @param obj The mapbuf object
362  * @param alpha The state of the alpha channel
363  *
364  * @ingroup Mapbuf
365  */
366 EAPI void
367 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
368 {
369    ELM_CHECK_WIDTYPE(obj, widtype);
370    Widget_Data *wd = elm_widget_data_get(obj);
371    if (!wd) return;
372    if (wd->alpha == alpha) return;
373    wd->alpha = alpha;
374    _configure(obj);
375 }
376
377 /**
378  * Gets the state of the mapbuf alpha channel
379  *
380  * @param obj The mapbuf object
381  * @return The state of the alpha channel
382  *
383  * @ingroup Mapbuf
384  */
385 EAPI Eina_Bool
386 elm_mapbuf_alpha_get(const Evas_Object *obj)
387 {
388    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
389    Widget_Data *wd = elm_widget_data_get(obj);
390    if (!wd) return EINA_FALSE;
391    return wd->alpha;
392 }