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