svn update: 48945 (latest:48959)
[framework/uifw/elementary.git] / src / lib / elm_bg.c
1 #include <string.h>
2
3 #include <Elementary.h>
4 #include "elm_priv.h"
5
6 /**
7  * @defgroup Bg Bg
8  *
9  * The bg object is used for setting a solid background to a window or packing
10  * into any container object.
11  */
12
13 typedef struct _Widget_Data Widget_Data;
14
15 struct _Widget_Data
16 {
17    Evas_Object *img, *custom_img;
18    const char  *file, *group;
19 };
20
21 static const char *widtype = NULL;
22 static void _del_hook(Evas_Object *obj);
23 static void _theme_hook(Evas_Object *obj);
24 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
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    _elm_theme_object_set(obj, wd->img, "bg", "base", elm_widget_style_get(obj));
38 }
39
40 static void
41 _custom_resize(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
42 {
43    int iw = 0, ih = 0;
44    Evas_Coord x, y, w, h, ow = 0, oh = 0;
45
46    evas_object_geometry_get(obj, NULL, NULL, &ow, &oh);
47    evas_object_image_size_get(obj, &iw, &ih);
48
49    if ((iw < 1) || (ih < 1)) return;
50    w = ow;
51    h = (ih * w) / iw;
52    if (h < oh)
53      {
54         h = oh;
55         w = (iw * h) / ih;
56      }
57    x = (ow - w) / 2;
58    y = (oh - h) / 2;
59    evas_object_image_fill_set(obj, x, y, w, h);
60 }
61
62 /**
63  * Add a new background to the parent
64  *
65  * @param parent The parent object
66  * @return The new object or NULL if it cannot be created
67  *
68  * @ingroup Bg
69  */
70 EAPI Evas_Object *
71 elm_bg_add(Evas_Object *parent)
72 {
73    Evas_Object *obj;
74    Evas *e;
75    Widget_Data *wd;
76
77    wd = ELM_NEW(Widget_Data);
78    e = evas_object_evas_get(parent);
79    obj = elm_widget_add(e);
80    ELM_SET_WIDTYPE(widtype, "bg");
81    elm_widget_type_set(obj, "bg");
82    elm_widget_sub_object_add(parent, obj);
83    elm_widget_data_set(obj, wd);
84    elm_widget_del_hook_set(obj, _del_hook);
85    elm_widget_theme_hook_set(obj, _theme_hook);
86    elm_widget_can_focus_set(obj, 0);
87
88    wd->img = edje_object_add(e);
89    _elm_theme_object_set(obj, wd->img, "bg", "base", "default");
90    elm_widget_resize_object_set(obj, wd->img);
91    return obj;
92 }
93
94 /**
95  * Set the file (image or edje) used for the background
96  *
97  * @param obj The bg object
98  * @param file The file path
99  * @param group Optional key (group in Edje) within the file
100  *
101  * This sets the image file used in the background object. The image (or edje)
102  * will be stretched (retaining aspect if its an image file) to completely fill
103  * the bg object. This may mean some parts arte not visible.
104  *
105  * @ingroup Bg
106  */
107 EAPI void
108 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
109 {
110    ELM_CHECK_WIDTYPE(obj, widtype);
111    Widget_Data *wd = elm_widget_data_get(obj);
112    const char *p;
113
114    if (wd->custom_img)
115      {
116         evas_object_del(wd->custom_img);
117         wd->custom_img = NULL;
118      }
119    if (!file) return;
120    eina_stringshare_replace(&wd->file, file);
121    eina_stringshare_replace(&wd->group, group);
122    if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
123      {
124         wd->custom_img = edje_object_add(evas_object_evas_get(wd->img));
125         edje_object_file_set(wd->custom_img, file, group);
126      }
127    else
128      {
129         wd->custom_img = evas_object_image_add(evas_object_evas_get(wd->img));
130         evas_object_event_callback_add(wd->custom_img, EVAS_CALLBACK_RESIZE, 
131                                        _custom_resize, wd);
132         evas_object_image_file_set(wd->custom_img, file, group);
133      }
134    elm_widget_sub_object_add(obj, wd->custom_img);
135    evas_object_repeat_events_set(wd->custom_img, 1);
136    edje_object_part_swallow(wd->img, "elm.swallow.background", wd->custom_img);
137    evas_object_show(wd->custom_img);
138 }