elementary/map - map supports language,changed
[framework/uifw/elementary.git] / src / lib / elm_mapbuf.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3 #include "elm_widget_mapbuf.h"
4
5 EAPI const char ELM_MAPBUF_SMART_NAME[] = "elm_mapbuf";
6
7 EVAS_SMART_SUBCLASS_NEW
8   (ELM_MAPBUF_SMART_NAME, _elm_mapbuf, Elm_Mapbuf_Smart_Class,
9   Elm_Container_Smart_Class, elm_container_smart_class_get, NULL);
10
11 static void
12 _sizing_eval(Evas_Object *obj)
13 {
14    Evas_Coord minw = -1, minh = -1;
15    Evas_Coord maxw = -1, maxh = -1;
16
17    ELM_MAPBUF_DATA_GET(obj, sd);
18    if (sd->content)
19      {
20         evas_object_size_hint_min_get(sd->content, &minw, &minh);
21         evas_object_size_hint_max_get(sd->content, &maxw, &maxh);
22      }
23    evas_object_size_hint_min_set(obj, minw, minh);
24    evas_object_size_hint_max_set(obj, maxw, maxh);
25 }
26
27 static Eina_Bool
28 _elm_mapbuf_smart_theme(Evas_Object *obj)
29 {
30    if (!ELM_WIDGET_CLASS(_elm_mapbuf_parent_sc)->theme(obj)) return EINA_FALSE;
31
32    _sizing_eval(obj);
33
34    return EINA_TRUE;
35 }
36
37 static void
38 _changed_size_hints_cb(void *data,
39                        Evas *e __UNUSED__,
40                        Evas_Object *obj __UNUSED__,
41                        void *event_info __UNUSED__)
42 {
43    _sizing_eval(data);
44 }
45
46 static Eina_Bool
47 _elm_mapbuf_smart_sub_object_del(Evas_Object *obj,
48                                  Evas_Object *sobj)
49 {
50    ELM_MAPBUF_DATA_GET(obj, sd);
51
52    if (!ELM_WIDGET_CLASS(_elm_mapbuf_parent_sc)->sub_object_del(obj, sobj))
53      return EINA_FALSE;
54
55    if (sobj == sd->content)
56      {
57         evas_object_data_del(sobj, "_elm_leaveme");
58         evas_object_smart_member_del(sobj);
59         evas_object_clip_unset(sobj);
60         evas_object_event_callback_del_full
61           (sobj, EVAS_CALLBACK_CHANGED_SIZE_HINTS, _changed_size_hints_cb,
62           obj);
63         sd->content = NULL;
64         _sizing_eval(obj);
65      }
66
67    return EINA_TRUE;
68 }
69
70 static void
71 _mapbuf(Evas_Object *obj)
72 {
73    Evas_Coord x, y, w, h;
74
75    ELM_MAPBUF_DATA_GET(obj, sd);
76
77    evas_object_geometry_get(ELM_WIDGET_DATA(sd)->resize_obj, &x, &y, &w, &h);
78    evas_object_resize(sd->content, w, h);
79
80    if (sd->enabled)
81      {
82         Evas_Map *m;
83
84         m = evas_map_new(4);
85         evas_map_util_points_populate_from_geometry(m, x, y, w, h, 0);
86         evas_map_smooth_set(m, sd->smooth);
87         evas_map_alpha_set(m, sd->alpha);
88         evas_object_map_set(sd->content, m);
89         evas_object_map_enable_set(sd->content, EINA_TRUE);
90         evas_map_free(m);
91      }
92    else
93      {
94         evas_object_map_set(sd->content, NULL);
95         evas_object_map_enable_set(sd->content, EINA_FALSE);
96         evas_object_move(sd->content, x, y);
97      }
98 }
99
100 static void
101 _configure(Evas_Object *obj, Eina_Bool update_force)
102 {
103    ELM_MAPBUF_DATA_GET(obj, sd);
104
105    if (!sd->content) return;
106    Evas_Coord x, y, w, h, x2, y2, w2, h2;
107    evas_object_geometry_get(ELM_WIDGET_DATA(sd)->resize_obj, &x, &y, &w, &h);
108    evas_object_geometry_get(sd->content, &x2, &y2, &w2, &h2);
109    if ((update_force) || ((x != x2) || (y != y2) || (w != w2) || (h != h2)))
110      {
111         if (!sd->enabled)
112           evas_object_move(sd->content, x, y);
113         else
114           {
115              Evas *e = evas_object_evas_get(obj);
116              evas_smart_objects_calculate(e);
117              ELM_WIDGET_CHECK_OR_RETURN(obj);
118              evas_nochange_push(e);
119              evas_object_move(sd->content, x, y);
120              evas_smart_objects_calculate(e);
121              ELM_WIDGET_CHECK_OR_RETURN(obj);
122              evas_nochange_pop(e);
123           }
124         _mapbuf(obj);
125      }
126 }
127
128 static void
129 _elm_mapbuf_smart_move(Evas_Object *obj,
130                        Evas_Coord x,
131                        Evas_Coord y)
132 {
133    ELM_WIDGET_CLASS(_elm_mapbuf_parent_sc)->base.move(obj, x, y);
134
135    _configure(obj, EINA_FALSE);
136 }
137
138 static void
139 _elm_mapbuf_smart_resize(Evas_Object *obj,
140                          Evas_Coord x,
141                          Evas_Coord y)
142 {
143    ELM_WIDGET_CLASS(_elm_mapbuf_parent_sc)->base.resize(obj, x, y);
144
145    _configure(obj, EINA_FALSE);
146 }
147
148 static Eina_Bool
149 _elm_mapbuf_smart_content_set(Evas_Object *obj,
150                               const char *part,
151                               Evas_Object *content)
152 {
153    ELM_MAPBUF_DATA_GET(obj, sd);
154
155    if (part && strcmp(part, "default")) return EINA_FALSE;
156    if (sd->content == content) return EINA_TRUE;
157
158    if (sd->content) evas_object_del(sd->content);
159    sd->content = content;
160
161    if (content)
162      {
163         evas_object_data_set(content, "_elm_leaveme", (void *)1);
164         elm_widget_sub_object_add(obj, content);
165         evas_object_smart_member_add(content, obj);
166         evas_object_clip_set(content, ELM_WIDGET_DATA(sd)->resize_obj);
167         evas_object_color_set
168           (ELM_WIDGET_DATA(sd)->resize_obj, 255, 255, 255, 255);
169         evas_object_event_callback_add
170           (content, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
171           _changed_size_hints_cb, obj);
172      }
173    else
174      evas_object_color_set(ELM_WIDGET_DATA(sd)->resize_obj, 0, 0, 0, 0);
175
176    _sizing_eval(obj);
177    _configure(obj, EINA_TRUE);
178
179    return EINA_TRUE;
180 }
181
182 static Evas_Object *
183 _elm_mapbuf_smart_content_get(const Evas_Object *obj,
184                               const char *part)
185 {
186    ELM_MAPBUF_DATA_GET(obj, sd);
187
188    if (part && strcmp(part, "default")) return NULL;
189    return sd->content;
190 }
191
192 static Evas_Object *
193 _elm_mapbuf_smart_content_unset(Evas_Object *obj,
194                                 const char *part)
195 {
196    Evas_Object *content;
197
198    ELM_MAPBUF_DATA_GET(obj, sd);
199
200    if (part && strcmp(part, "default")) return NULL;
201    if (!sd->content) return NULL;
202
203    content = sd->content;
204    elm_widget_sub_object_del(obj, content);
205    evas_object_smart_member_del(content);
206    evas_object_data_del(content, "_elm_leaveme");
207    evas_object_color_set(ELM_WIDGET_DATA(sd)->resize_obj, 0, 0, 0, 0);
208    return content;
209 }
210
211 static void
212 _elm_mapbuf_smart_add(Evas_Object *obj)
213 {
214    EVAS_SMART_DATA_ALLOC(obj, Elm_Mapbuf_Smart_Data);
215
216    ELM_WIDGET_DATA(priv)->resize_obj =
217        evas_object_rectangle_add(evas_object_evas_get(obj));
218
219    ELM_WIDGET_CLASS(_elm_mapbuf_parent_sc)->base.add(obj);
220
221    evas_object_static_clip_set(ELM_WIDGET_DATA(priv)->resize_obj, EINA_TRUE);
222    evas_object_pass_events_set(ELM_WIDGET_DATA(priv)->resize_obj, EINA_TRUE);
223    evas_object_color_set(ELM_WIDGET_DATA(priv)->resize_obj, 0, 0, 0, 0);
224
225    priv->enabled = 0;
226    priv->alpha = 1;
227    priv->smooth = 1;
228
229    elm_widget_can_focus_set(obj, EINA_FALSE);
230
231    _sizing_eval(obj);
232 }
233
234 static void
235 _elm_mapbuf_smart_set_user(Elm_Mapbuf_Smart_Class *sc)
236 {
237    ELM_WIDGET_CLASS(sc)->base.add = _elm_mapbuf_smart_add;
238    ELM_WIDGET_CLASS(sc)->base.resize = _elm_mapbuf_smart_resize;
239    ELM_WIDGET_CLASS(sc)->base.move = _elm_mapbuf_smart_move;
240
241    ELM_WIDGET_CLASS(sc)->theme = _elm_mapbuf_smart_theme;
242    ELM_WIDGET_CLASS(sc)->sub_object_del = _elm_mapbuf_smart_sub_object_del;
243
244    ELM_CONTAINER_CLASS(sc)->content_set = _elm_mapbuf_smart_content_set;
245    ELM_CONTAINER_CLASS(sc)->content_get = _elm_mapbuf_smart_content_get;
246    ELM_CONTAINER_CLASS(sc)->content_unset = _elm_mapbuf_smart_content_unset;
247 }
248
249 EAPI const Elm_Mapbuf_Smart_Class *
250 elm_mapbuf_smart_class_get(void)
251 {
252    static Elm_Mapbuf_Smart_Class _sc =
253      ELM_MAPBUF_SMART_CLASS_INIT_NAME_VERSION(ELM_MAPBUF_SMART_NAME);
254    static const Elm_Mapbuf_Smart_Class *class = NULL;
255
256    if (class) return class;
257
258    _elm_mapbuf_smart_set(&_sc);
259    class = &_sc;
260
261    return class;
262 }
263
264 EAPI Evas_Object *
265 elm_mapbuf_add(Evas_Object *parent)
266 {
267    Evas_Object *obj;
268
269    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
270
271    obj = elm_widget_add(_elm_mapbuf_smart_class_new(), parent);
272    if (!obj) return NULL;
273
274    if (!elm_widget_sub_object_add(parent, obj))
275      ERR("could not add %p as sub object of %p", obj, parent);
276
277    return obj;
278 }
279
280 EAPI void
281 elm_mapbuf_enabled_set(Evas_Object *obj,
282                        Eina_Bool enabled)
283 {
284    ELM_MAPBUF_CHECK(obj);
285    ELM_MAPBUF_DATA_GET(obj, sd);
286
287    if (sd->enabled == enabled) return;
288    sd->enabled = enabled;
289
290    if (sd->content) evas_object_static_clip_set(sd->content, sd->enabled);
291    _configure(obj, EINA_TRUE);
292 }
293
294 EAPI Eina_Bool
295 elm_mapbuf_enabled_get(const Evas_Object *obj)
296 {
297    ELM_MAPBUF_CHECK(obj) EINA_FALSE;
298    ELM_MAPBUF_DATA_GET(obj, sd);
299
300    return sd->enabled;
301 }
302
303 EAPI void
304 elm_mapbuf_smooth_set(Evas_Object *obj,
305                       Eina_Bool smooth)
306 {
307    ELM_MAPBUF_CHECK(obj);
308    ELM_MAPBUF_DATA_GET(obj, sd);
309
310    if (sd->smooth == smooth) return;
311    sd->smooth = smooth;
312    _configure(obj, EINA_TRUE);
313 }
314
315 EAPI Eina_Bool
316 elm_mapbuf_smooth_get(const Evas_Object *obj)
317 {
318    ELM_MAPBUF_CHECK(obj) EINA_FALSE;
319    ELM_MAPBUF_DATA_GET(obj, sd);
320
321    return sd->smooth;
322 }
323
324 EAPI void
325 elm_mapbuf_alpha_set(Evas_Object *obj,
326                      Eina_Bool alpha)
327 {
328    ELM_MAPBUF_CHECK(obj);
329    ELM_MAPBUF_DATA_GET(obj, sd);
330
331    if (sd->alpha == alpha) return;
332    sd->alpha = alpha;
333    _configure(obj, EINA_TRUE);
334 }
335
336 EAPI Eina_Bool
337 elm_mapbuf_alpha_get(const Evas_Object *obj)
338 {
339    ELM_MAPBUF_CHECK(obj) EINA_FALSE;
340    ELM_MAPBUF_DATA_GET(obj, sd);
341
342    return sd->alpha;
343 }