svn update: 48945 (latest:48959)
[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  * @param obj The mapbuf object
179  * @param content The content will be filled in this mapbuf object
180  *
181  * @ingroup Mapbuf
182  */
183 EAPI void
184 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
185 {
186    ELM_CHECK_WIDTYPE(obj, widtype);
187    Widget_Data *wd = elm_widget_data_get(obj);
188    if (!wd) return;
189    if (wd->content == content) return;
190    if ((wd->content != content) && (wd->content))
191      {
192         elm_widget_sub_object_del(obj, wd->content);
193         evas_object_smart_member_del(wd->content);
194      }
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,
201                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
202                                        _changed_size_hints, obj);
203         _sizing_eval(obj);
204      }
205    _configure(obj);
206 }
207
208 /**
209  * Set the mapbuf enabled state
210  *
211  * @param obj The mapbuf object
212  * @param enabled The value to set the enabled state to
213  *
214  * @ingroup Mapbuf
215  */
216 EAPI void
217 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
218 {
219    ELM_CHECK_WIDTYPE(obj, widtype);
220    Widget_Data *wd = elm_widget_data_get(obj);
221    if (!wd) return;
222    if (wd->enabled == enabled) return;
223    wd->enabled = enabled;
224    _configure(obj);
225 }
226
227 /**
228  * Get the mapbuf enabled state
229  *
230  * @param obj The mapbuf object
231  * @return The value that the enabled state is set to
232  *
233  * @ingroup Mapbuf
234  */
235 EAPI Eina_Bool
236 elm_mapbuf_enabled_get(const Evas_Object *obj)
237 {
238    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
239    Widget_Data *wd = elm_widget_data_get(obj);
240    if (!wd) return EINA_FALSE;
241    return wd->enabled;
242 }
243
244 /**
245  * Sets the mapbuf smooth state
246  *
247  * @param obj The mapbuf object
248  * @param smooth The value of the smooth state of @p obj
249  *
250  * @ingroup Mapbuf
251  */
252 EAPI void
253 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
254 {
255    ELM_CHECK_WIDTYPE(obj, widtype);
256    Widget_Data *wd = elm_widget_data_get(obj);
257    if (!wd) return;
258    if (wd->smooth == smooth) return;
259    wd->smooth = smooth;
260    _configure(obj);
261 }
262
263 /**
264  * Gets the mapbuf smooth state
265  *
266  * @param obj The mapbuf object
267  * @return The value of the smooth state of @p obj
268  *
269  * @ingroup Mapbuf
270  */
271 EAPI Eina_Bool
272 elm_mapbuf_smooth_get(const Evas_Object *obj)
273 {
274    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
275    Widget_Data *wd = elm_widget_data_get(obj);
276    if (!wd) return EINA_FALSE;
277    return wd->smooth;
278 }
279
280 /**
281  * Enables/disables the mapbuf alpha channel
282  *
283  * @param obj The mapbuf object
284  * @param alpha The state of the alpha channel
285  *
286  * @ingroup Mapbuf
287  */
288 EAPI void
289 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
290 {
291    ELM_CHECK_WIDTYPE(obj, widtype);
292    Widget_Data *wd = elm_widget_data_get(obj);
293    if (!wd) return;
294    if (wd->alpha == alpha) return;
295    wd->alpha = alpha;
296    _configure(obj);
297 }
298
299 /**
300  * Gets the state of the mapbuf alpha channel
301  *
302  * @param obj The mapbuf object
303  * @return The state of the alpha channel
304  *
305  * @ingroup Mapbuf
306  */
307 EAPI Eina_Bool
308 elm_mapbuf_alpha_get(const Evas_Object *obj)
309 {
310    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
311    Widget_Data *wd = elm_widget_data_get(obj);
312    if (!wd) return EINA_FALSE;
313    return wd->alpha;
314 }