Revert back to Original State (Merged Elementary)
[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;
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(obj, &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(obj, &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--------------------\n");
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, void *event_info __UNUSED__)
146 {
147    _configure(obj);
148 }
149
150 static void
151 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
152 {    
153    _configure(obj);
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    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
172
173    wd = ELM_NEW(Widget_Data);
174    e = evas_object_evas_get(parent);
175    if (!e) return NULL;
176    obj = elm_widget_add(e);
177    ELM_SET_WIDTYPE(widtype, "mapbuf");
178    elm_widget_type_set(obj, "mapbuf");
179    elm_widget_sub_object_add(parent, obj);
180    elm_widget_data_set(obj, wd);
181    elm_widget_del_hook_set(obj, _del_hook);
182    elm_widget_theme_hook_set(obj, _theme_hook);
183    elm_widget_can_focus_set(obj, EINA_FALSE);
184
185    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
186    evas_object_event_callback_add(obj, EVAS_CALLBACK_MOVE, _move, NULL);
187    evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _resize, NULL);
188
189    wd->enabled = 0;
190    wd->alpha = 1;
191    wd->smooth = 1;
192    
193    _sizing_eval(obj);
194    return obj;
195 }
196
197 /**
198  * Set the mapbuf front content
199  *
200  * Once the content object is set, a previously set one will be deleted.
201  * If you want to keep that old content object, use the
202  * elm_mapbuf_content_unset() function.
203  *
204  * @param obj The mapbuf object
205  * @param content The content will be filled in this mapbuf object
206  *
207  * @ingroup Mapbuf
208  */
209 EAPI void
210 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
211 {
212    ELM_CHECK_WIDTYPE(obj, widtype);
213    Widget_Data *wd = elm_widget_data_get(obj);
214    if (!wd) return;
215    if (wd->content == content) return;
216    if (wd->content) evas_object_del(wd->content);
217    wd->content = content;
218    if (content)
219      {
220         evas_object_data_set(content, "_elm_leaveme", (void *)1);
221         elm_widget_sub_object_add(content, obj);
222         evas_object_smart_member_add(content, obj);
223         evas_object_clip_set(content, evas_object_clip_get(obj));
224         evas_object_event_callback_add(content,
225                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
226                                        _changed_size_hints, obj);
227      }
228    _sizing_eval(obj);
229    _configure(obj);
230 }
231
232 /**
233  * Get the mapbuf front content
234  *
235  * Return the content object which is set for this widget.
236  *
237  * @param obj The mapbuf object
238  * @return The content that is being used
239  *
240  * @ingroup Mapbuf
241  */
242 EAPI Evas_Object *
243 elm_mapbuf_content_get(const Evas_Object *obj)
244 {
245    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
246    Widget_Data *wd = elm_widget_data_get(obj);
247    if (!wd) return NULL;
248    return wd->content;
249 }
250
251 /**
252  * Unset the mapbuf front content
253  *
254  * Unparent and return the content object which was set for this widget.
255  *
256  * @param obj The mapbuf object
257  * @return The content that was being used
258  *
259  * @ingroup Mapbuf
260  */
261 EAPI Evas_Object *
262 elm_mapbuf_content_unset(Evas_Object *obj)
263 {
264    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
265    Widget_Data *wd = elm_widget_data_get(obj);
266    Evas_Object *content;
267    if (!wd) return NULL;
268    if (!wd->content) return NULL;
269    content = wd->content;
270    elm_widget_sub_object_del(obj, content);
271    evas_object_smart_member_del(content);
272    evas_object_clip_unset(content);
273    evas_object_data_del(content, "_elm_leaveme");
274    wd->content = NULL;
275    return content;
276 }
277
278 /**
279  * Set the mapbuf enabled state
280  *
281  * @param obj The mapbuf object
282  * @param enabled The value to set the enabled state to
283  *
284  * @ingroup Mapbuf
285  */
286 EAPI void
287 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
288 {
289    ELM_CHECK_WIDTYPE(obj, widtype);
290    Widget_Data *wd = elm_widget_data_get(obj);
291    if (!wd) return;
292    if (wd->enabled == enabled) return;
293    wd->enabled = enabled;
294    _configure(obj);
295 }
296
297 /**
298  * Get the mapbuf enabled state
299  *
300  * @param obj The mapbuf object
301  * @return The value that the enabled state is set to
302  *
303  * @ingroup Mapbuf
304  */
305 EAPI Eina_Bool
306 elm_mapbuf_enabled_get(const Evas_Object *obj)
307 {
308    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
309    Widget_Data *wd = elm_widget_data_get(obj);
310    if (!wd) return EINA_FALSE;
311    return wd->enabled;
312 }
313
314 /**
315  * Sets the mapbuf smooth state
316  *
317  * @param obj The mapbuf object
318  * @param smooth The value of the smooth state of @p obj
319  *
320  * @ingroup Mapbuf
321  */
322 EAPI void
323 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
324 {
325    ELM_CHECK_WIDTYPE(obj, widtype);
326    Widget_Data *wd = elm_widget_data_get(obj);
327    if (!wd) return;
328    if (wd->smooth == smooth) return;
329    wd->smooth = smooth;
330    _configure(obj);
331 }
332
333 /**
334  * Gets the mapbuf smooth state
335  *
336  * @param obj The mapbuf object
337  * @return The value of the smooth state of @p obj
338  *
339  * @ingroup Mapbuf
340  */
341 EAPI Eina_Bool
342 elm_mapbuf_smooth_get(const Evas_Object *obj)
343 {
344    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
345    Widget_Data *wd = elm_widget_data_get(obj);
346    if (!wd) return EINA_FALSE;
347    return wd->smooth;
348 }
349
350 /**
351  * Enables/disables the mapbuf alpha channel
352  *
353  * @param obj The mapbuf object
354  * @param alpha The state of the alpha channel
355  *
356  * @ingroup Mapbuf
357  */
358 EAPI void
359 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
360 {
361    ELM_CHECK_WIDTYPE(obj, widtype);
362    Widget_Data *wd = elm_widget_data_get(obj);
363    if (!wd) return;
364    if (wd->alpha == alpha) return;
365    wd->alpha = alpha;
366    _configure(obj);
367 }
368
369 /**
370  * Gets the state of the mapbuf alpha channel
371  *
372  * @param obj The mapbuf object
373  * @return The state of the alpha channel
374  *
375  * @ingroup Mapbuf
376  */
377 EAPI Eina_Bool
378 elm_mapbuf_alpha_get(const Evas_Object *obj)
379 {
380    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
381    Widget_Data *wd = elm_widget_data_get(obj);
382    if (!wd) return EINA_FALSE;
383    return wd->alpha;
384 }