move elementary to trunk base. out of TMP/st.
[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, *clip;
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(wd->clip, &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, x2, y2;
118
119         evas_object_geometry_get(wd->clip, &x, &y, &w, &h);
120         evas_object_geometry_get(wd->content, &x2, &y2, NULL, NULL);
121         if ((x != x2) || (y != y2))
122           {
123              if (!wd->enabled)
124                 evas_object_move(wd->content, x, y);
125              else
126                {
127                   
128                   Evas *e = evas_object_evas_get(obj);
129                   evas_smart_objects_calculate(e);
130                   evas_nochange_push(e);
131 //                  printf("x-------------------- %i %i\n", x, y);
132                   evas_object_move(wd->content, x, y);
133                   evas_smart_objects_calculate(e);
134 //                  printf("y--------------------\n");
135                   evas_nochange_pop(e);
136                }
137           }
138         evas_object_resize(wd->content, w, h);
139         _mapbuf(obj);
140      }
141 }
142
143 static void
144 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
145 {
146    _configure(data);
147 }
148
149 static void
150 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
151 {    
152    _configure(data);
153 }
154
155 /**
156  * Add a new mapbuf to the parent
157  *
158  * @param parent The parent object
159  * @return The new object or NULL if it cannot be created
160  *
161  * @ingroup Mapbuf
162  */
163 EAPI Evas_Object *
164 elm_mapbuf_add(Evas_Object *parent)
165 {
166    Evas_Object *obj;
167    Evas *e;
168    Widget_Data *wd;
169
170    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
171
172    wd = ELM_NEW(Widget_Data);
173    e = evas_object_evas_get(parent);
174    if (!e) return NULL;
175    obj = elm_widget_add(e);
176    ELM_SET_WIDTYPE(widtype, "mapbuf");
177    elm_widget_type_set(obj, "mapbuf");
178    elm_widget_sub_object_add(parent, obj);
179    elm_widget_data_set(obj, wd);
180    elm_widget_del_hook_set(obj, _del_hook);
181    elm_widget_theme_hook_set(obj, _theme_hook);
182    elm_widget_can_focus_set(obj, EINA_FALSE);
183
184    wd->clip = evas_object_rectangle_add(e);
185    evas_object_static_clip_set(wd->clip, EINA_TRUE);
186    evas_object_pass_events_set(wd->clip, EINA_TRUE);
187    evas_object_color_set(wd->clip, 0, 0, 0, 0);
188
189    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_MOVE, _move, obj);
190    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_RESIZE, _resize, obj);
191    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
192   
193    elm_widget_resize_object_set(obj, wd->clip);
194    
195    wd->enabled = 0;
196    wd->alpha = 1;
197    wd->smooth = 1;
198    
199    _sizing_eval(obj);
200    return obj;
201 }
202
203 /**
204  * Set the mapbuf front content
205  *
206  * Once the content object is set, a previously set one will be deleted.
207  * If you want to keep that old content object, use the
208  * elm_mapbuf_content_unset() function.
209  *
210  * @param obj The mapbuf object
211  * @param content The content will be filled in this mapbuf object
212  *
213  * @ingroup Mapbuf
214  */
215 EAPI void
216 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
217 {
218    ELM_CHECK_WIDTYPE(obj, widtype);
219    Widget_Data *wd = elm_widget_data_get(obj);
220    if (!wd) return;
221    if (wd->content == content) return;
222    if (wd->content) evas_object_del(wd->content);
223    wd->content = content;
224    if (content)
225      {
226         evas_object_data_set(content, "_elm_leaveme", (void *)1);
227         elm_widget_sub_object_add(content, obj);
228         evas_object_smart_member_add(content, obj);
229         evas_object_clip_set(content, wd->clip);
230         evas_object_color_set(wd->clip, 255, 255, 255, 255);
231         evas_object_event_callback_add(content,
232                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
233                                        _changed_size_hints, obj);
234      }
235    else
236      evas_object_color_set(wd->clip, 0, 0, 0, 0);
237    _sizing_eval(obj);
238    _configure(obj);
239 }
240
241 /**
242  * Get the mapbuf front content
243  *
244  * Return the content object which is set for this widget.
245  *
246  * @param obj The mapbuf object
247  * @return The content that is being used
248  *
249  * @ingroup Mapbuf
250  */
251 EAPI Evas_Object *
252 elm_mapbuf_content_get(const Evas_Object *obj)
253 {
254    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
255    Widget_Data *wd = elm_widget_data_get(obj);
256    if (!wd) return NULL;
257    return wd->content;
258 }
259
260 /**
261  * Unset the mapbuf front content
262  *
263  * Unparent and return the content object which was set for this widget.
264  *
265  * @param obj The mapbuf object
266  * @return The content that was being used
267  *
268  * @ingroup Mapbuf
269  */
270 EAPI Evas_Object *
271 elm_mapbuf_content_unset(Evas_Object *obj)
272 {
273    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
274    Widget_Data *wd = elm_widget_data_get(obj);
275    Evas_Object *content;
276    if (!wd) return NULL;
277    if (!wd->content) return NULL;
278    content = wd->content;
279    elm_widget_sub_object_del(obj, content);
280    evas_object_smart_member_del(content);
281    evas_object_color_set(wd->clip, 0, 0, 0, 0);
282    evas_object_clip_unset(content);
283    evas_object_data_del(content, "_elm_leaveme");
284    wd->content = NULL;
285    return content;
286 }
287
288 /**
289  * Set the mapbuf enabled state
290  *
291  * @param obj The mapbuf object
292  * @param enabled The value to set the enabled state to
293  *
294  * @ingroup Mapbuf
295  */
296 EAPI void
297 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
298 {
299    ELM_CHECK_WIDTYPE(obj, widtype);
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (!wd) return;
302    if (wd->enabled == enabled) return;
303    wd->enabled = enabled;
304    if (wd->content) evas_object_static_clip_set(wd->content, wd->enabled);
305    _configure(obj);
306 }
307
308 /**
309  * Get the mapbuf enabled state
310  *
311  * @param obj The mapbuf object
312  * @return The value that the enabled state is set to
313  *
314  * @ingroup Mapbuf
315  */
316 EAPI Eina_Bool
317 elm_mapbuf_enabled_get(const Evas_Object *obj)
318 {
319    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
320    Widget_Data *wd = elm_widget_data_get(obj);
321    if (!wd) return EINA_FALSE;
322    return wd->enabled;
323 }
324
325 /**
326  * Sets the mapbuf smooth state
327  *
328  * @param obj The mapbuf object
329  * @param smooth The value of the smooth state of @p obj
330  *
331  * @ingroup Mapbuf
332  */
333 EAPI void
334 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
335 {
336    ELM_CHECK_WIDTYPE(obj, widtype);
337    Widget_Data *wd = elm_widget_data_get(obj);
338    if (!wd) return;
339    if (wd->smooth == smooth) return;
340    wd->smooth = smooth;
341    _configure(obj);
342 }
343
344 /**
345  * Gets the mapbuf smooth state
346  *
347  * @param obj The mapbuf object
348  * @return The value of the smooth state of @p obj
349  *
350  * @ingroup Mapbuf
351  */
352 EAPI Eina_Bool
353 elm_mapbuf_smooth_get(const Evas_Object *obj)
354 {
355    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
356    Widget_Data *wd = elm_widget_data_get(obj);
357    if (!wd) return EINA_FALSE;
358    return wd->smooth;
359 }
360
361 /**
362  * Enables/disables the mapbuf alpha channel
363  *
364  * @param obj The mapbuf object
365  * @param alpha The state of the alpha channel
366  *
367  * @ingroup Mapbuf
368  */
369 EAPI void
370 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
371 {
372    ELM_CHECK_WIDTYPE(obj, widtype);
373    Widget_Data *wd = elm_widget_data_get(obj);
374    if (!wd) return;
375    if (wd->alpha == alpha) return;
376    wd->alpha = alpha;
377    _configure(obj);
378 }
379
380 /**
381  * Gets the state of the mapbuf alpha channel
382  *
383  * @param obj The mapbuf object
384  * @return The state of the alpha channel
385  *
386  * @ingroup Mapbuf
387  */
388 EAPI Eina_Bool
389 elm_mapbuf_alpha_get(const Evas_Object *obj)
390 {
391    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
392    Widget_Data *wd = elm_widget_data_get(obj);
393    if (!wd) return EINA_FALSE;
394    return wd->alpha;
395 }