Revert back to Original State (Merged Elementary)
[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  * @ingroup Elementary
7  *
8  * The bg object is used for setting a solid background to a window or packing
9  * into any container object.
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13
14 struct _Widget_Data
15 {
16    Evas_Object *base, *rect, *img, *overlay;
17    const char  *file, *group;
18    Elm_Bg_Option option;
19 };
20
21 static const char *widtype = NULL;
22
23 static void _del_hook(Evas_Object *obj);
24 static void _theme_hook(Evas_Object *obj);
25 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
26
27 static void
28 _del_hook(Evas_Object *obj)
29 {
30    Widget_Data *wd = elm_widget_data_get(obj);
31    free(wd);
32 }
33
34 static void
35 _theme_hook(Evas_Object *obj)
36 {
37    Widget_Data *wd = elm_widget_data_get(obj);
38    Evas_Coord w, h;
39
40    _elm_theme_object_set(obj, wd->base, "bg", "base", 
41                          elm_widget_style_get(obj));
42
43    if (wd->rect)
44      edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
45    if (wd->img)
46      edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
47    if (wd->overlay)
48      edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
49
50 // FIXME: if i don't do this, bg doesnt calc correctly. why?   
51    evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
52    evas_object_resize(wd->base, w, h);
53 }
54
55 static void
56 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
57 {
58    Widget_Data *wd = data;
59    Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
60    Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
61    Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
62    Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
63    const char *p;
64
65    if ((!wd->img) || (!wd->file)) return;
66    if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
67
68    /* grab image size */
69    evas_object_image_size_get(wd->img, &iw, &ih);
70    if ((iw < 1) || (ih < 1)) return;
71
72    /* grab base object dimensions */
73    evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
74
75    /* set some defaults */
76    nx = bx;
77    ny = by;
78    nw = bw;
79    nh = bh;
80
81    switch (wd->option) 
82      {
83       case ELM_BG_OPTION_CENTER:
84         fw = nw = iw;
85         fh = nh = ih;
86         nx = ((bw - fw) / 2);
87         ny = ((bh - fh) / 2);
88         mw = iw;
89         mh = ih;
90         break;
91       case ELM_BG_OPTION_SCALE:
92         fw = bw;
93         fh = ((ih * fw) / iw);
94         if (fh < bh)
95           {
96              fh = bh;
97              fw = ((iw * fh) / ih);
98           }
99         fx = ((bw - fw) / 2);
100         fy = ((bh - fh) / 2);
101         break;
102       case ELM_BG_OPTION_TILE:
103         fw = iw;
104         fh = ih;
105         break;
106       case ELM_BG_OPTION_STRETCH:
107       default:
108         fw = bw;
109         fh = bh;
110         break;
111      }
112
113    evas_object_move(wd->img, nx, ny);
114    evas_object_resize(wd->img, nw, nh);
115    evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
116
117    evas_object_size_hint_min_set(wd->img, mw, mh);
118    evas_object_size_hint_max_set(wd->img, mw, mh);
119 }
120
121 /**
122  * Add a new background to the parent
123  *
124  * @param parent The parent object
125  * @return The new object or NULL if it cannot be created
126  *
127  * @ingroup Bg
128  */
129 EAPI Evas_Object *
130 elm_bg_add(Evas_Object *parent)
131 {
132    Evas_Object *obj;
133    Evas *e;
134    Widget_Data *wd;
135
136    EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
137
138    wd = ELM_NEW(Widget_Data);
139    e = evas_object_evas_get(parent);
140    if (!e) return NULL;
141    obj = elm_widget_add(e);
142    ELM_SET_WIDTYPE(widtype, "bg");
143    elm_widget_type_set(obj, "bg");
144    elm_widget_sub_object_add(parent, obj);
145    elm_widget_data_set(obj, wd);
146    elm_widget_del_hook_set(obj, _del_hook);
147    elm_widget_theme_hook_set(obj, _theme_hook);
148    elm_widget_can_focus_set(obj, EINA_FALSE);
149
150    wd->base = edje_object_add(e);
151    _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
152    elm_widget_resize_object_set(obj, wd->base);
153
154    evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE, 
155                                   _custom_resize, wd);
156
157    wd->option = ELM_BG_OPTION_SCALE;
158    return obj;
159 }
160
161 /**
162  * Set the file (image or edje) used for the background
163  *
164  * @param obj The bg object
165  * @param file The file path
166  * @param group Optional key (group in Edje) within the file
167  *
168  * This sets the image file used in the background object. The image (or edje)
169  * will be stretched (retaining aspect if its an image file) to completely fill
170  * the bg object. This may mean some parts are not visible.
171  *
172  * @note  Once the image of @p obj is set, a previously set one will be deleted,
173  * even if @p file is NULL.
174  *
175  * @ingroup Bg
176  */
177 EAPI void
178 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
179 {
180    ELM_CHECK_WIDTYPE(obj, widtype);
181    Widget_Data *wd = elm_widget_data_get(obj);
182    const char *p;
183
184    if (wd->img)
185      {
186         evas_object_del(wd->img);
187         wd->img = NULL;
188      }
189    if (!file)
190      {
191         eina_stringshare_del(wd->file);
192         wd->file = NULL;
193         eina_stringshare_del(wd->group);
194         wd->group = NULL;
195         return;
196      }
197    eina_stringshare_replace(&wd->file, file);
198    eina_stringshare_replace(&wd->group, group);
199    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
200      {
201         wd->img = edje_object_add(evas_object_evas_get(wd->base));
202         edje_object_file_set(wd->img, file, group);
203      }
204    else
205      {
206         wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
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 }