Elementary src/lib: Removed all trailing whitespaces.
[framework/uifw/elementary.git] / src / lib / elm_bg.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Bg Bg
6  *
7  * The bg object is used for setting a solid background to a window or packing
8  * into any container object.
9  */
10
11 typedef struct _Widget_Data Widget_Data;
12
13 struct _Widget_Data
14 {
15    Evas_Object *base, *rect, *img, *overlay;
16    const char  *file, *group;
17    Elm_Bg_Option option;
18    struct {
19       Evas_Coord w, h;
20    } load_opts;
21 };
22
23 static const char *widtype = NULL;
24
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
28
29 static void
30 _del_hook(Evas_Object *obj)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33    free(wd);
34 }
35
36 static void
37 _theme_hook(Evas_Object *obj)
38 {
39    Widget_Data *wd = elm_widget_data_get(obj);
40    Evas_Coord w, h;
41
42    _elm_theme_object_set(obj, wd->base, "bg", "base",
43                          elm_widget_style_get(obj));
44
45    if (wd->rect)
46      edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
47    if (wd->img)
48      edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
49    if (wd->overlay)
50      edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
51
52    // FIXME: if i don't do this, bg doesnt calc correctly. why?
53    evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
54    evas_object_resize(wd->base, w, h);
55 }
56
57 static void
58 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
59 {
60    Widget_Data *wd = data;
61    Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
62    Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
63    Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
64    Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
65    const char *p;
66
67    if ((!wd->img) || (!wd->file)) return;
68    if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
69
70    /* grab image size */
71    evas_object_image_size_get(wd->img, &iw, &ih);
72    if ((iw < 1) || (ih < 1)) return;
73
74    /* grab base object dimensions */
75    evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
76
77    /* set some defaults */
78    nx = bx;
79    ny = by;
80    nw = bw;
81    nh = bh;
82
83    switch (wd->option)
84      {
85       case ELM_BG_OPTION_CENTER:
86          fw = nw = iw;
87          fh = nh = ih;
88          nx = ((bw - fw) / 2);
89          ny = ((bh - fh) / 2);
90          mw = iw;
91          mh = ih;
92          break;
93       case ELM_BG_OPTION_SCALE:
94          fw = bw;
95          fh = ((ih * fw) / iw);
96          if (fh < bh)
97            {
98               fh = bh;
99               fw = ((iw * fh) / ih);
100            }
101          fx = ((bw - fw) / 2);
102          fy = ((bh - fh) / 2);
103          break;
104       case ELM_BG_OPTION_TILE:
105          fw = iw;
106          fh = ih;
107          break;
108       case ELM_BG_OPTION_STRETCH:
109       default:
110          fw = bw;
111          fh = bh;
112          break;
113      }
114
115    evas_object_move(wd->img, nx, ny);
116    evas_object_resize(wd->img, nw, nh);
117    evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
118
119    evas_object_size_hint_min_set(wd->img, mw, mh);
120    evas_object_size_hint_max_set(wd->img, mw, mh);
121 }
122
123 /**
124  * Add a new background to the parent
125  *
126  * @param parent The parent object
127  * @return The new object or NULL if it cannot be created
128  *
129  * @ingroup Bg
130  */
131 EAPI Evas_Object *
132 elm_bg_add(Evas_Object *parent)
133 {
134    Evas_Object *obj;
135    Evas *e;
136    Widget_Data *wd;
137
138    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
139
140    ELM_SET_WIDTYPE(widtype, "bg");
141    elm_widget_type_set(obj, "bg");
142    elm_widget_sub_object_add(parent, obj);
143    elm_widget_data_set(obj, wd);
144    elm_widget_del_hook_set(obj, _del_hook);
145    elm_widget_theme_hook_set(obj, _theme_hook);
146    elm_widget_can_focus_set(obj, EINA_FALSE);
147
148    wd->base = edje_object_add(e);
149    _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
150    elm_widget_resize_object_set(obj, wd->base);
151
152    evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
153                                   _custom_resize, wd);
154
155    wd->option = ELM_BG_OPTION_SCALE;
156    return obj;
157 }
158
159 /**
160  * Set the file (image or edje) used for the background
161  *
162  * @param obj The bg object
163  * @param file The file path
164  * @param group Optional key (group in Edje) within the file
165  *
166  * This sets the image file used in the background object. The image (or edje)
167  * will be stretched (retaining aspect if its an image file) to completely fill
168  * the bg object. This may mean some parts are not visible.
169  *
170  * @note  Once the image of @p obj is set, a previously set one will be deleted,
171  * even if @p file is NULL.
172  *
173  * @ingroup Bg
174  */
175 EAPI void
176 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
177 {
178    ELM_CHECK_WIDTYPE(obj, widtype);
179    Widget_Data *wd = elm_widget_data_get(obj);
180    const char *p;
181
182    if (wd->img)
183      {
184         evas_object_del(wd->img);
185         wd->img = NULL;
186      }
187    if (!file)
188      {
189         eina_stringshare_del(wd->file);
190         wd->file = NULL;
191         eina_stringshare_del(wd->group);
192         wd->group = NULL;
193         return;
194      }
195    eina_stringshare_replace(&wd->file, file);
196    eina_stringshare_replace(&wd->group, group);
197    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
198      {
199         wd->img = edje_object_add(evas_object_evas_get(wd->base));
200         edje_object_file_set(wd->img, file, group);
201      }
202    else
203      {
204         wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
205         if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
206           evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
207         evas_object_image_file_set(wd->img, file, group);
208      }
209    evas_object_repeat_events_set(wd->img, EINA_TRUE);
210    edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
211    elm_widget_sub_object_add(obj, wd->img);
212    _custom_resize(wd, NULL, NULL, NULL);
213 }
214
215 /**
216  * Get the file (image or edje) used for the background
217  *
218  * @param obj The bg object
219  * @param file The file path
220  * @param group Optional key (group in Edje) within the file
221  *
222  * @ingroup Bg
223  */
224 EAPI void
225 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
226 {
227    ELM_CHECK_WIDTYPE(obj, widtype);
228    Widget_Data *wd = elm_widget_data_get(obj);
229    if (file) *file = wd->file;
230    if (group) *group = wd->group;
231 }
232
233 /**
234  * Set the option used for the background image
235  *
236  * @param obj The bg object
237  * @param option The desired background option (TILE, SCALE)
238  *
239  * This sets the option used for manipulating the display of the background
240  * image. The image can be tiled or scaled.
241  *
242  * @ingroup Bg
243  */
244 EAPI void
245 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
246 {
247    ELM_CHECK_WIDTYPE(obj, widtype);
248    Widget_Data *wd;
249
250    wd = elm_widget_data_get(obj);
251    wd->option = option;
252    _custom_resize(wd, NULL, NULL, NULL);
253 }
254
255 /**
256  * Get the option used for the background image
257  *
258  * @param obj The bg object
259  * @return The desired background option (TILE, SCALE)
260  *
261  * @ingroup Bg
262  */
263 EAPI Elm_Bg_Option
264 elm_bg_option_get(const Evas_Object *obj)
265 {
266    ELM_CHECK_WIDTYPE(obj, widtype) 0;
267    Widget_Data *wd;
268
269    wd = elm_widget_data_get(obj);
270    return wd->option;
271 }
272
273 /**
274  * Set the option used for the background color
275  *
276  * @param obj The bg object
277  * @param r
278  * @param g
279  * @param b
280  *
281  * This sets the color used for the background rectangle.
282  *
283  * @ingroup Bg
284  */
285 EAPI void
286 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
287 {
288    ELM_CHECK_WIDTYPE(obj, widtype);
289    Widget_Data *wd;
290
291    wd = elm_widget_data_get(obj);
292    if (!wd->rect)
293      {
294         wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
295         edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
296         elm_widget_sub_object_add(obj, wd->rect);
297         _custom_resize(wd, NULL, NULL, NULL);
298      }
299    evas_object_color_set(wd->rect, r, g, b, 255);
300 }
301
302 /**
303  * Get the option used for the background color
304  *
305  * @param obj The bg object
306  * @param r
307  * @param g
308  * @param b
309  *
310  * @ingroup Bg
311  */
312 EAPI void
313 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
314 {
315    ELM_CHECK_WIDTYPE(obj, widtype);
316    Widget_Data *wd;
317
318    wd = elm_widget_data_get(obj);
319    evas_object_color_get(wd->rect, r, g, b, NULL);
320 }
321
322 /**
323  * Set the overlay object used for the background object.
324  *
325  * @param obj The bg object
326  * @param overlay The overlay object
327  *
328  * This provides a way for elm_bg to have an 'overlay' (such as animated fog)
329  * Once the over object is set, a previously set one will be deleted.
330  * If you want to keep that old content object, use the
331  * elm_bg_overlay_unset() function.
332  *
333  * @ingroup Bg
334  */
335 EAPI void
336 elm_bg_overlay_set(Evas_Object *obj, Evas_Object *overlay)
337 {
338    ELM_CHECK_WIDTYPE(obj, widtype);
339    Widget_Data *wd = elm_widget_data_get(obj);
340    if (!wd) return;
341    if (wd->overlay)
342      {
343         evas_object_del(wd->overlay);
344         wd->overlay = NULL;
345      }
346    if (overlay)
347      {
348         wd->overlay = overlay;
349         edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
350         elm_widget_sub_object_add(obj, wd->overlay);
351      }
352
353    _custom_resize(wd, NULL, NULL, NULL);
354 }
355
356 /**
357  * Set the overlay object used for the background object.
358  *
359  * @param obj The bg object
360  * @return The content that is being used
361  *
362  * Return the content object which is set for this widget
363  *
364  * @ingroup Bg
365  */
366 EAPI Evas_Object *
367 elm_bg_overlay_get(const Evas_Object *obj)
368 {
369    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
370    Widget_Data *wd = elm_widget_data_get(obj);
371    if (!wd) return NULL;
372    return wd->overlay;
373 }
374
375 /**
376  * Get the overlay object used for the background object.
377  *
378  * @param obj The bg object
379  * @return The content that was being used
380  *
381  * Unparent and return the overlay object which was set for this widget
382  *
383  * @ingroup Bg
384  */
385 EAPI Evas_Object *
386 elm_bg_overlay_unset(Evas_Object *obj)
387 {
388    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
389    Widget_Data *wd = elm_widget_data_get(obj);
390    Evas_Object *overlay;
391    if (!wd) return NULL;
392    if (!wd->overlay) return NULL;
393    overlay = wd->overlay;
394    elm_widget_sub_object_del(obj, wd->overlay);
395    edje_object_part_unswallow(wd->base, wd->overlay);
396    wd->overlay = NULL;
397    _custom_resize(wd, NULL, NULL, NULL);
398    return overlay;
399 }
400
401 /**
402  * Set the size of a loaded image of the canvas of the bg.
403  *
404  * @param obj The bg object
405  * @param w The new width of the canvas image given.
406  * @param h The new height of the canvas image given.
407  *
408  * This function sets a new size for the canvas image of the given the bg.
409  *
410  */
411 EAPI void
412 elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
413 {
414    ELM_CHECK_WIDTYPE(obj, widtype);
415    Widget_Data *wd = elm_widget_data_get(obj);
416    const char *p;
417    if (!wd) return;
418    wd->load_opts.w = w;
419    wd->load_opts.h = h;
420    if (!wd->img) return;
421    if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
422      evas_object_image_load_size_set(wd->img, w, h);
423 }
424