fixed plugin image size problem
[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    evas_object_resize(wd->content, w, h);
84    if (wd->enabled)
85      {
86         Evas_Map *m;
87
88         m = evas_map_new(4);
89         evas_map_util_points_populate_from_geometry(m, x, y, w, h, 0);
90         evas_map_smooth_set(m, wd->smooth);
91         evas_map_alpha_set(m, wd->alpha);
92         evas_object_map_set(wd->content, m);
93         evas_object_map_enable_set(wd->content, EINA_TRUE);
94         evas_map_free(m);
95      }
96    else
97      {
98         evas_object_map_set(wd->content, NULL);
99         evas_object_map_enable_set(wd->content, EINA_FALSE);
100         evas_object_move(wd->content, x, y);
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                   evas_object_move(wd->content, x, y);
126                   evas_smart_objects_calculate(e);
127                   evas_nochange_pop(e);
128                }
129           }
130         _mapbuf(obj);
131      }
132 }
133
134 static void
135 _move(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
136 {
137    _configure(data);
138 }
139
140 static void
141 _resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
142 {
143    _configure(data);
144 }
145
146 static void
147 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
148 {
149    ELM_CHECK_WIDTYPE(obj, widtype);
150    Widget_Data *wd;
151
152    if (part && strcmp(part, "default")) return;
153    wd = elm_widget_data_get(obj);
154    if (!wd) return;
155    if (wd->content == content) return;
156    if (wd->content) evas_object_del(wd->content);
157    wd->content = content;
158    if (content)
159      {
160         evas_object_data_set(content, "_elm_leaveme", (void *)1);
161         elm_widget_sub_object_add(obj, content);
162         evas_object_smart_member_add(content, obj);
163         evas_object_clip_set(content, wd->clip);
164         evas_object_color_set(wd->clip, 255, 255, 255, 255);
165         evas_object_event_callback_add(content,
166                                        EVAS_CALLBACK_CHANGED_SIZE_HINTS,
167                                        _changed_size_hints, obj);
168      }
169    else
170      evas_object_color_set(wd->clip, 0, 0, 0, 0);
171    _sizing_eval(obj);
172    _configure(obj);
173 }
174
175 static Evas_Object *
176 _content_get_hook(const Evas_Object *obj, const char *part)
177 {
178    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
179    Widget_Data *wd;
180
181    if (part && strcmp(part, "default")) return NULL;
182    wd = elm_widget_data_get(obj);
183    if (!wd) return NULL;
184    return wd->content;
185 }
186
187 static Evas_Object *
188 _content_unset_hook(Evas_Object *obj, const char *part)
189 {
190    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
191    Widget_Data *wd;
192    Evas_Object *content;
193
194    if (part && strcmp(part, "default")) return NULL;
195    wd = elm_widget_data_get(obj);
196    if (!wd) return NULL;
197    if (!wd->content) return NULL;
198    content = wd->content;
199    elm_widget_sub_object_del(obj, content);
200    evas_object_smart_member_del(content);
201    evas_object_color_set(wd->clip, 0, 0, 0, 0);
202    evas_object_data_del(content, "_elm_leaveme");
203    return content;
204 }
205
206 EAPI Evas_Object *
207 elm_mapbuf_add(Evas_Object *parent)
208 {
209    Evas_Object *obj;
210    Evas *e;
211    Widget_Data *wd;
212
213    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
214
215    ELM_SET_WIDTYPE(widtype, "mapbuf");
216    elm_widget_type_set(obj, "mapbuf");
217    elm_widget_sub_object_add(parent, obj);
218    elm_widget_data_set(obj, wd);
219    elm_widget_del_hook_set(obj, _del_hook);
220    elm_widget_theme_hook_set(obj, _theme_hook);
221    elm_widget_content_set_hook_set(obj, _content_set_hook);
222    elm_widget_content_get_hook_set(obj, _content_get_hook);
223    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
224    elm_widget_can_focus_set(obj, EINA_FALSE);
225
226    wd->clip = evas_object_rectangle_add(e);
227    evas_object_static_clip_set(wd->clip, EINA_TRUE);
228    evas_object_pass_events_set(wd->clip, EINA_TRUE);
229    evas_object_color_set(wd->clip, 0, 0, 0, 0);
230
231    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_MOVE, _move, obj);
232    evas_object_event_callback_add(wd->clip, EVAS_CALLBACK_RESIZE, _resize, obj);
233    evas_object_smart_callback_add(obj, "sub-object-del", _sub_del, obj);
234
235    elm_widget_resize_object_set(obj, wd->clip);
236
237    wd->enabled = 0;
238    wd->alpha = 1;
239    wd->smooth = 1;
240
241    _sizing_eval(obj);
242    return obj;
243 }
244
245 EAPI void
246 elm_mapbuf_enabled_set(Evas_Object *obj, Eina_Bool enabled)
247 {
248    ELM_CHECK_WIDTYPE(obj, widtype);
249    Widget_Data *wd = elm_widget_data_get(obj);
250    if (!wd) return;
251    if (wd->enabled == enabled) return;
252    wd->enabled = enabled;
253    if (wd->content) evas_object_static_clip_set(wd->content, wd->enabled);
254    _configure(obj);
255 }
256
257 EAPI Eina_Bool
258 elm_mapbuf_enabled_get(const Evas_Object *obj)
259 {
260    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
261    Widget_Data *wd = elm_widget_data_get(obj);
262    if (!wd) return EINA_FALSE;
263    return wd->enabled;
264 }
265
266 EAPI void
267 elm_mapbuf_smooth_set(Evas_Object *obj, Eina_Bool smooth)
268 {
269    ELM_CHECK_WIDTYPE(obj, widtype);
270    Widget_Data *wd = elm_widget_data_get(obj);
271    if (!wd) return;
272    if (wd->smooth == smooth) return;
273    wd->smooth = smooth;
274    _configure(obj);
275 }
276
277 EAPI Eina_Bool
278 elm_mapbuf_smooth_get(const Evas_Object *obj)
279 {
280    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
281    Widget_Data *wd = elm_widget_data_get(obj);
282    if (!wd) return EINA_FALSE;
283    return wd->smooth;
284 }
285
286 EAPI void
287 elm_mapbuf_alpha_set(Evas_Object *obj, Eina_Bool alpha)
288 {
289    ELM_CHECK_WIDTYPE(obj, widtype);
290    Widget_Data *wd = elm_widget_data_get(obj);
291    if (!wd) return;
292    if (wd->alpha == alpha) return;
293    wd->alpha = alpha;
294    _configure(obj);
295 }
296
297 EAPI Eina_Bool
298 elm_mapbuf_alpha_get(const Evas_Object *obj)
299 {
300    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
301    Widget_Data *wd = elm_widget_data_get(obj);
302    if (!wd) return EINA_FALSE;
303    return wd->alpha;
304 }