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