7d76b78d3393c9bef0cd21a4a023ee8adefa1a82
[framework/uifw/elementary.git] / src / lib / elm_mapbuf.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *content, *clip;
9    Eina_Bool enabled : 1;
10    Eina_Bool alpha : 1;
11    Eina_Bool smooth : 1;
12 };
13
14 static const char *widtype = NULL;
15 static void _del_hook(Evas_Object *obj);
16 static void _theme_hook(Evas_Object *obj);
17 static void _sizing_eval(Evas_Object *obj);
18 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
19 static void _sub_del(void *data, Evas_Object *obj, void *event_info);
20
21 static void
22 _del_hook(Evas_Object *obj)
23 {
24    Widget_Data *wd = elm_widget_data_get(obj);
25    if (!wd) return;
26    free(wd);
27 }
28
29 static void
30 _theme_hook(Evas_Object *obj)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33    if (!wd) return;
34    _sizing_eval(obj);
35 }
36
37 static void
38 _sizing_eval(Evas_Object *obj)
39 {
40    Widget_Data *wd = elm_widget_data_get(obj);
41    Evas_Coord minw = -1, minh = -1;
42    Evas_Coord maxw = -1, maxh = -1;
43    if (!wd) return;
44    if (wd->content)
45      {
46         evas_object_size_hint_min_get(wd->content, &minw, &minh);
47         evas_object_size_hint_max_get(wd->content, &maxw, &maxh);
48      }
49    evas_object_size_hint_min_set(obj, minw, minh);
50    evas_object_size_hint_max_set(obj, maxw, maxh);
51 }
52
53 static void
54 _changed_size_hints(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
55 {
56    Widget_Data *wd = elm_widget_data_get(data);
57    if (!wd) return;
58    _sizing_eval(data);
59 }
60
61 static void
62 _sub_del(void *data __UNUSED__, Evas_Object *obj, void *event_info)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    Evas_Object *sub = event_info;
66    if (!wd) return;
67    if (sub == wd->content)
68      {
69         evas_object_event_callback_del_full(sub, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
70                                             _changed_size_hints, obj);
71         wd->content = NULL;
72         _sizing_eval(obj);
73      }
74 }
75
76 static void
77 _mapbuf(Evas_Object *obj)
78 {
79    Widget_Data *wd = elm_widget_data_get(obj);
80    Evas_Coord x, y, w, h;
81    if (!wd) return;
82    evas_object_geometry_get(wd->clip, &x, &y, &w, &h);
83    if (wd->enabled)
84      {
85         Evas_Map *m;
86
87         m = evas_map_new(4);
88         evas_map_util_points_populate_from_geometry(m, x, y, w, h, 0);
89         evas_map_smooth_set(m, wd->smooth);
90         evas_map_alpha_set(m, wd->alpha);
91         evas_object_map_set(wd->content, m);
92         evas_object_map_enable_set(wd->content, wd->enabled);
93         evas_map_free(m);
94      }
95    else
96      {
97         evas_object_map_set(wd->content, NULL);
98         evas_object_map_enable_set(wd->content, 0);
99         evas_object_move(wd->content, x, y);
100         evas_object_resize(wd->content, w, h);
101      }
102 }
103
104 static void
105 _configure(Evas_Object *obj)
106 {
107    Widget_Data *wd = elm_widget_data_get(obj);
108    if (!wd) return;
109    if (wd->content)
110      {
111         Evas_Coord x, y, w, h, x2, y2;
112
113         evas_object_geometry_get(wd->clip, &x, &y, &w, &h);
114         evas_object_geometry_get(wd->content, &x2, &y2, NULL, NULL);
115         if ((x != x2) || (y != y2))
116           {
117              if (!wd->enabled)
118                evas_object_move(wd->content, x, y);
119              else
120                {
121
122                   Evas *e = evas_object_evas_get(obj);
123                   evas_smart_objects_calculate(e);
124                   evas_nochange_push(e);
125                   //                  printf("x-------------------- %i %i\n", x, y);
126                   evas_object_move(wd->content, x, y);
127                   evas_smart_objects_calculate(e);
128                   //                  printf("y--------------------\n");
129                   evas_nochange_pop(e);
130                }
131           }
132         evas_object_resize(wd->content, w, h);
133         _mapbuf(obj);
134      }
135 }
136
137 static void
138 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
139 {
140    _configure(data);
141 }
142
143 static void
144 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
145 {
146    _configure(data);
147 }
148
149 static void
150 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
151 {
152    ELM_CHECK_WIDTYPE(obj, widtype);
153    Widget_Data *wd;
154
155    if (part && strcmp(part, "default")) return;
156    wd = elm_widget_data_get(obj);
157    if (!wd) return;
158    if (wd->content == content) return;
159    if (wd->content) evas_object_del(wd->content);
160    wd->content = content;
161    if (content)
162      {
163         evas_object_data_set(content, "_elm_leaveme", (void *)1);
164 <<<<<<< HEAD
165         elm_widget_sub_object_add(content, obj);
166 =======
167         elm_widget_sub_object_add(obj, content);
168 >>>>>>> remotes/origin/upstream
169         evas_object_smart_member_add(content, obj);
170         evas_object_clip_set(content, wd->clip);
171         evas_object_color_set(wd->clip, 255, 255, 255, 255);
172         evas_object_event_callback_add(content,
173                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
174                                        _changed_size_hints, obj);
175      }
176    else
177      evas_object_color_set(wd->clip, 0, 0, 0, 0);
178    _sizing_eval(obj);
179    _configure(obj);
180 }
181
182 static Evas_Object *
183 _content_get_hook(const Evas_Object *obj, const char *part)
184 {
185    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
186    Widget_Data *wd;
187
188    if (part && strcmp(part, "default")) return NULL;
189    wd = elm_widget_data_get(obj);
190    if (!wd) return NULL;
191    return wd->content;
192 }
193
194 static Evas_Object *
195 _content_unset_hook(Evas_Object *obj, const char *part)
196 {
197    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
198    Widget_Data *wd;
199    Evas_Object *content;
200
201    if (part && strcmp(part, "default")) return NULL;
202    wd = elm_widget_data_get(obj);
203    if (!wd) return NULL;
204    if (!wd->content) return NULL;
205    content = wd->content;
206    elm_widget_sub_object_del(obj, content);
207    evas_object_event_callback_add(content,
208                                   EVAS_CALLBACK_CHANGED_SIZE_HINTS,
209                                   _changed_size_hints, obj);
210    evas_object_smart_member_del(content);
211    evas_object_color_set(wd->clip, 0, 0, 0, 0);
212    evas_object_clip_unset(content);
213    evas_object_data_del(content, "_elm_leaveme");
214    wd->content = NULL;
215    return content;
216 }
217
218 EAPI Evas_Object *
219 elm_mapbuf_add(Evas_Object *parent)
220 {
221    Evas_Object *obj;
222    Evas *e;
223    Widget_Data *wd;
224
225    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
226
227    ELM_SET_WIDTYPE(widtype, "mapbuf");
228    elm_widget_type_set(obj, "mapbuf");
229    elm_widget_sub_object_add(parent, obj);
230    elm_widget_data_set(obj, wd);
231    elm_widget_del_hook_set(obj, _del_hook);
232    elm_widget_theme_hook_set(obj, _theme_hook);
233    elm_widget_content_set_hook_set(obj, _content_set_hook);
234    elm_widget_content_get_hook_set(obj, _content_get_hook);
235    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
236    elm_widget_can_focus_set(obj, EINA_FALSE);
237
238    wd->clip = evas_object_rectangle_add(e);
239    evas_object_static_clip_set(wd->clip, EINA_TRUE);
240    evas_object_pass_events_set(wd->clip, EINA_TRUE);
241    evas_object_color_set(wd->clip, 0, 0, 0, 0);
242
243    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_MOVE, _move, obj);
244    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_RESIZE, _resize, obj);
245    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
246
247    elm_widget_resize_object_set(obj, wd->clip);
248
249    wd->enabled = 0;
250    wd->alpha = 1;
251    wd->smooth = 1;
252
253    _sizing_eval(obj);
254    return obj;
255 }
256
257 EAPI void
258 elm_mapbuf_content_set(Evas_Object *obj, Evas_Object *content)
259 {
260    _content_set_hook(obj, NULL, content);
261 }
262
263 EAPI Evas_Object *
264 elm_mapbuf_content_get(const Evas_Object *obj)
265 {
266    return _content_get_hook(obj, NULL);
267 }
268
269 EAPI Evas_Object *
270 elm_mapbuf_content_unset(Evas_Object *obj)
271 {
272    return _content_unset_hook(obj, NULL);
273 }
274
275 EAPI void
276 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
277 {
278    ELM_CHECK_WIDTYPE(obj, widtype);
279    Widget_Data *wd = elm_widget_data_get(obj);
280    if (!wd) return;
281    if (wd->enabled == enabled) return;
282    wd->enabled = enabled;
283    if (wd->content) evas_object_static_clip_set(wd->content, wd->enabled);
284    _configure(obj);
285 }
286
287 EAPI Eina_Bool
288 elm_mapbuf_enabled_get(const Evas_Object *obj)
289 {
290    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
291    Widget_Data *wd = elm_widget_data_get(obj);
292    if (!wd) return EINA_FALSE;
293    return wd->enabled;
294 }
295
296 EAPI void
297 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
298 {
299    ELM_CHECK_WIDTYPE(obj, widtype);
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (!wd) return;
302    if (wd->smooth == smooth) return;
303    wd->smooth = smooth;
304    _configure(obj);
305 }
306
307 EAPI Eina_Bool
308 elm_mapbuf_smooth_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->smooth;
314 }
315
316 EAPI void
317 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
318 {
319    ELM_CHECK_WIDTYPE(obj, widtype);
320    Widget_Data *wd = elm_widget_data_get(obj);
321    if (!wd) return;
322    if (wd->alpha == alpha) return;
323    wd->alpha = alpha;
324    _configure(obj);
325 }
326
327 EAPI Eina_Bool
328 elm_mapbuf_alpha_get(const Evas_Object *obj)
329 {
330    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
331    Widget_Data *wd = elm_widget_data_get(obj);
332    if (!wd) return EINA_FALSE;
333    return wd->alpha;
334 }