Merge "[Slideshow] Applying Open source patch r72492 to fix Switching in slideshow...
[framework/uifw/elementary.git] / src / lib / elm_bg.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 *base, *rect, *img, *overlay;
9    const char  *file, *group;
10    Elm_Bg_Option option;
11    struct
12      {
13         Evas_Coord w, h;
14      } load_opts;
15 };
16
17 static const char *widtype = NULL;
18
19 static void _del_hook(Evas_Object *obj);
20 static void _theme_hook(Evas_Object *obj);
21 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
22 static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
23 static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
24 static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
25
26 static void
27 _del_hook(Evas_Object *obj)
28 {
29    Widget_Data *wd = elm_widget_data_get(obj);
30    free(wd);
31 }
32
33 static void
34 _theme_hook(Evas_Object *obj)
35 {
36    Widget_Data *wd = elm_widget_data_get(obj);
37    Evas_Coord w, h;
38
39    _elm_theme_object_set(obj, wd->base, "bg", "base",
40                          elm_widget_style_get(obj));
41
42    if (wd->rect)
43      edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
44    if (wd->img)
45      edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
46    if (wd->overlay)
47      edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
48
49    // FIXME: if i don't do this, bg doesnt calc correctly. why?
50    evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
51    evas_object_resize(wd->base, w, h);
52 }
53
54 static void
55 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
56 {
57    Widget_Data *wd = data;
58    Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
59    Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
60    Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
61    Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
62    const char *p;
63
64    if ((!wd->img) || (!wd->file)) return;
65    if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
66
67    /* grab image size */
68    evas_object_image_size_get(wd->img, &iw, &ih);
69    if ((iw < 1) || (ih < 1)) return;
70
71    /* grab base object dimensions */
72    evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
73
74    /* set some defaults */
75    nx = bx;
76    ny = by;
77    nw = bw;
78    nh = bh;
79
80    switch (wd->option)
81      {
82       case ELM_BG_OPTION_CENTER:
83          fw = nw = iw;
84          fh = nh = ih;
85          nx = ((bw - fw) / 2);
86          ny = ((bh - fh) / 2);
87          mw = iw;
88          mh = ih;
89          break;
90       case ELM_BG_OPTION_SCALE:
91          fw = bw;
92          fh = ((ih * fw) / iw);
93          if (fh < bh)
94            {
95               fh = bh;
96               fw = ((iw * fh) / ih);
97            }
98          fx = ((bw - fw) / 2);
99          fy = ((bh - fh) / 2);
100          break;
101       case ELM_BG_OPTION_TILE:
102          fw = iw;
103          fh = ih;
104          break;
105       case ELM_BG_OPTION_STRETCH:
106       default:
107          fw = bw;
108          fh = bh;
109          break;
110      }
111
112    evas_object_move(wd->img, nx, ny);
113    evas_object_resize(wd->img, nw, nh);
114    evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
115
116    evas_object_size_hint_min_set(wd->img, mw, mh);
117    evas_object_size_hint_max_set(wd->img, mw, mh);
118 }
119
120 static void
121 _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
122 {
123    ELM_CHECK_WIDTYPE(obj, widtype);
124    Widget_Data *wd;
125    if (part && strcmp(part, "overlay")) return;
126    wd = elm_widget_data_get(obj);
127    if (!wd) return;
128
129    if (content == wd->overlay) return;
130    if (wd->overlay) evas_object_del(wd->overlay);
131
132    wd->overlay = content;
133    if (content)
134      {
135         edje_object_part_swallow(wd->base, "elm.swallow.content", content);
136         elm_widget_sub_object_add(obj, content);
137      }
138
139    _custom_resize(wd, NULL, NULL, NULL);
140 }
141
142 static Evas_Object *
143 _content_get_hook(const Evas_Object *obj, const char *part)
144 {
145    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
146    Widget_Data *wd;
147    if (part && strcmp(part, "overlay")) return NULL;
148    wd = elm_widget_data_get(obj);
149    if (!wd) return NULL;
150    return wd->overlay;
151 }
152
153 static Evas_Object *
154 _content_unset_hook(Evas_Object *obj, const char *part)
155 {
156    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
157    Widget_Data *wd;
158    Evas_Object *overlay;
159    if (part && strcmp(part, "overlay")) return NULL;
160    wd = elm_widget_data_get(obj);
161    if (!wd || !wd->overlay) return NULL;
162    overlay = wd->overlay;
163    elm_widget_sub_object_del(obj, wd->overlay);
164    edje_object_part_unswallow(wd->base, wd->overlay);
165    wd->overlay = NULL;
166    _custom_resize(wd, NULL, NULL, NULL);
167    return overlay;
168 }
169
170 EAPI Evas_Object *
171 elm_bg_add(Evas_Object *parent)
172 {
173    Evas_Object *obj;
174    Evas *e;
175    Widget_Data *wd;
176
177    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
178
179    ELM_SET_WIDTYPE(widtype, "bg");
180    elm_widget_type_set(obj, "bg");
181    elm_widget_sub_object_add(parent, obj);
182    elm_widget_data_set(obj, wd);
183    elm_widget_del_hook_set(obj, _del_hook);
184    elm_widget_theme_hook_set(obj, _theme_hook);
185    elm_widget_content_set_hook_set(obj, _content_set_hook);
186    elm_widget_content_get_hook_set(obj, _content_get_hook);
187    elm_widget_content_unset_hook_set(obj, _content_unset_hook);
188
189    elm_widget_can_focus_set(obj, EINA_FALSE);
190
191    wd->base = edje_object_add(e);
192    _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
193    elm_widget_resize_object_set(obj, wd->base);
194
195    evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
196                                   _custom_resize, wd);
197
198    wd->option = ELM_BG_OPTION_SCALE;
199    return obj;
200 }
201
202 EAPI Eina_Bool
203 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
204 {
205    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
206    Widget_Data *wd = elm_widget_data_get(obj);
207    const char *p;
208
209    if (wd->img)
210      {
211         evas_object_del(wd->img);
212         wd->img = NULL;
213      }
214    if (!file)
215      {
216         eina_stringshare_del(wd->file);
217         wd->file = NULL;
218         eina_stringshare_del(wd->group);
219         wd->group = NULL;
220         return EINA_TRUE;
221      }
222    eina_stringshare_replace(&wd->file, file);
223    eina_stringshare_replace(&wd->group, group);
224    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
225      {
226         wd->img = edje_object_add(evas_object_evas_get(wd->base));
227         edje_object_file_set(wd->img, file, group);
228      }
229    else
230      {
231         wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
232         if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
233           evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
234         evas_object_image_file_set(wd->img, file, group);
235      }
236    evas_object_repeat_events_set(wd->img, EINA_TRUE);
237    edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
238    elm_widget_sub_object_add(obj, wd->img);
239    _custom_resize(wd, NULL, NULL, NULL);
240
241    return EINA_TRUE;
242 }
243
244 EAPI void
245 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
246 {
247    ELM_CHECK_WIDTYPE(obj, widtype);
248    Widget_Data *wd = elm_widget_data_get(obj);
249    if (file) *file = wd->file;
250    if (group) *group = wd->group;
251
252    return;
253 }
254
255 EAPI void
256 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
257 {
258    ELM_CHECK_WIDTYPE(obj, widtype);
259    Widget_Data *wd;
260
261    wd = elm_widget_data_get(obj);
262    wd->option = option;
263    _custom_resize(wd, NULL, NULL, NULL);
264
265    return;
266 }
267
268 EAPI Elm_Bg_Option
269 elm_bg_option_get(const Evas_Object *obj)
270 {
271    ELM_CHECK_WIDTYPE(obj, widtype) ELM_BG_OPTION_LAST;
272    Widget_Data *wd;
273
274    wd = elm_widget_data_get(obj);
275    return wd->option;
276 }
277
278 EAPI void
279 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
280 {
281    ELM_CHECK_WIDTYPE(obj, widtype);
282    Widget_Data *wd;
283
284    wd = elm_widget_data_get(obj);
285    if (!wd->rect)
286      {
287         wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
288         edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
289         elm_widget_sub_object_add(obj, wd->rect);
290         _custom_resize(wd, NULL, NULL, NULL);
291      }
292    evas_object_color_set(wd->rect, r, g, b, 255);
293
294    return;
295 }
296
297 EAPI void
298 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
299 {
300    ELM_CHECK_WIDTYPE(obj, widtype);
301    Widget_Data *wd;
302
303    wd = elm_widget_data_get(obj);
304    evas_object_color_get(wd->rect, r, g, b, NULL);
305
306    return;
307 }
308
309 EAPI void
310 elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
311 {
312    ELM_CHECK_WIDTYPE(obj, widtype);
313    Widget_Data *wd = elm_widget_data_get(obj);
314    const char *p;
315    if (!wd) return;
316    wd->load_opts.w = w;
317    wd->load_opts.h = h;
318    if (!wd->img) return;
319    if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
320      evas_object_image_load_size_set(wd->img, w, h);
321
322    return;
323 }
324