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