1 #include <Elementary.h>
7 * The bg object is used for setting a solid background to a window or packing
8 * into any container object.
11 typedef struct _Widget_Data Widget_Data;
15 Evas_Object *base, *rect, *img, *overlay;
16 const char *file, *group;
20 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);
27 _del_hook(Evas_Object *obj)
29 Widget_Data *wd = elm_widget_data_get(obj);
34 _theme_hook(Evas_Object *obj)
36 Widget_Data *wd = elm_widget_data_get(obj);
39 _elm_theme_object_set(obj, wd->base, "bg", "base",
40 elm_widget_style_get(obj));
43 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
45 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
47 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
49 // FIXME: if i don't do this, bg doesnt calc correctly. why?
50 evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
51 evas_object_resize(wd->base, w, h);
55 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
57 Widget_Data *wd = data;
58 Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
59 Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
60 Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
61 Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
64 if ((!wd->img) || (!wd->file)) return;
65 if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
68 evas_object_image_size_get(wd->img, &iw, &ih);
69 if ((iw < 1) || (ih < 1)) return;
71 /* grab base object dimensions */
72 evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
74 /* set some defaults */
82 case ELM_BG_OPTION_CENTER:
90 case ELM_BG_OPTION_SCALE:
92 fh = ((ih * fw) / iw);
96 fw = ((iw * fh) / ih);
101 case ELM_BG_OPTION_TILE:
105 case ELM_BG_OPTION_STRETCH:
112 evas_object_move(wd->img, nx, ny);
113 evas_object_resize(wd->img, nw, nh);
114 evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
116 evas_object_size_hint_min_set(wd->img, mw, mh);
117 evas_object_size_hint_max_set(wd->img, mw, mh);
121 * Add a new background to the parent
123 * @param parent The parent object
124 * @return The new object or NULL if it cannot be created
129 elm_bg_add(Evas_Object *parent)
135 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
137 wd = ELM_NEW(Widget_Data);
138 e = evas_object_evas_get(parent);
140 obj = elm_widget_add(e);
141 ELM_SET_WIDTYPE(widtype, "bg");
142 elm_widget_type_set(obj, "bg");
143 elm_widget_sub_object_add(parent, obj);
144 elm_widget_data_set(obj, wd);
145 elm_widget_del_hook_set(obj, _del_hook);
146 elm_widget_theme_hook_set(obj, _theme_hook);
147 elm_widget_can_focus_set(obj, EINA_FALSE);
149 wd->base = edje_object_add(e);
150 _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
151 elm_widget_resize_object_set(obj, wd->base);
153 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
156 wd->option = ELM_BG_OPTION_SCALE;
161 * Set the file (image or edje) used for the background
163 * @param obj The bg object
164 * @param file The file path
165 * @param group Optional key (group in Edje) within the file
167 * This sets the image file used in the background object. The image (or edje)
168 * will be stretched (retaining aspect if its an image file) to completely fill
169 * the bg object. This may mean some parts are not visible.
171 * @note Once the image of @p obj is set, a previously set one will be deleted,
172 * even if @p file is NULL.
177 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
179 ELM_CHECK_WIDTYPE(obj, widtype);
180 Widget_Data *wd = elm_widget_data_get(obj);
185 evas_object_del(wd->img);
190 eina_stringshare_del(wd->file);
192 eina_stringshare_del(wd->group);
196 eina_stringshare_replace(&wd->file, file);
197 eina_stringshare_replace(&wd->group, group);
198 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
200 wd->img = edje_object_add(evas_object_evas_get(wd->base));
201 edje_object_file_set(wd->img, file, group);
205 wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
206 evas_object_image_file_set(wd->img, file, group);
208 evas_object_repeat_events_set(wd->img, EINA_TRUE);
209 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
210 elm_widget_sub_object_add(obj, wd->img);
211 _custom_resize(wd, NULL, NULL, NULL);
215 * Get the file (image or edje) used for the background
217 * @param obj The bg object
218 * @param file The file path
219 * @param group Optional key (group in Edje) within the file
224 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
226 ELM_CHECK_WIDTYPE(obj, widtype);
227 Widget_Data *wd = elm_widget_data_get(obj);
228 if (file) *file = wd->file;
229 if (group) *group = wd->group;
233 * Set the option used for the background image
235 * @param obj The bg object
236 * @param option The desired background option (TILE, SCALE)
238 * This sets the option used for manipulating the display of the background
239 * image. The image can be tiled or scaled.
244 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
246 ELM_CHECK_WIDTYPE(obj, widtype);
249 wd = elm_widget_data_get(obj);
251 _custom_resize(wd, NULL, NULL, NULL);
255 * Get the option used for the background image
257 * @param obj The bg object
258 * @return The desired background option (TILE, SCALE)
263 elm_bg_option_get(const Evas_Object *obj)
265 ELM_CHECK_WIDTYPE(obj, widtype) 0;
268 wd = elm_widget_data_get(obj);
273 * Set the option used for the background color
275 * @param obj The bg object
280 * This sets the color used for the background rectangle.
285 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
287 ELM_CHECK_WIDTYPE(obj, widtype);
290 wd = elm_widget_data_get(obj);
293 wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
294 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
295 elm_widget_sub_object_add(obj, wd->rect);
296 _custom_resize(wd, NULL, NULL, NULL);
298 evas_object_color_set(wd->rect, r, g, b, 255);
302 * Get the option used for the background color
304 * @param obj The bg object
312 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
314 ELM_CHECK_WIDTYPE(obj, widtype);
317 wd = elm_widget_data_get(obj);
318 evas_object_color_get(wd->rect, r, g, b, NULL);
322 * Set the overlay object used for the background object.
324 * @param obj The bg object
325 * @param overlay The overlay object
327 * This provides a way for elm_bg to have an 'overlay' (such as animated fog)
328 * Once the over object is set, a previously set one will be deleted.
329 * If you want to keep that old content object, use the
330 * elm_bg_overlay_unset() function.
335 elm_bg_overlay_set(Evas_Object *obj, Evas_Object *overlay)
337 ELM_CHECK_WIDTYPE(obj, widtype);
338 Widget_Data *wd = elm_widget_data_get(obj);
342 evas_object_del(wd->overlay);
347 wd->overlay = overlay;
348 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
349 elm_widget_sub_object_add(obj, wd->overlay);
352 _custom_resize(wd, NULL, NULL, NULL);
356 * Set the overlay object used for the background object.
358 * @param obj The bg object
359 * @return The content that is being used
361 * Return the content object which is set for this widget
366 elm_bg_overlay_get(const Evas_Object *obj)
368 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
369 Widget_Data *wd = elm_widget_data_get(obj);
370 if (!wd) return NULL;
375 * Get the overlay object used for the background object.
377 * @param obj The bg object
378 * @return The content that was being used
380 * Unparent and return the overlay object which was set for this widget
385 elm_bg_overlay_unset(Evas_Object *obj)
387 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
388 Widget_Data *wd = elm_widget_data_get(obj);
389 Evas_Object *overlay;
390 if (!wd) return NULL;
391 if (!wd->overlay) return NULL;
392 overlay = wd->overlay;
393 elm_widget_sub_object_del(obj, wd->overlay);
394 edje_object_part_unswallow(wd->base, wd->overlay);
396 _custom_resize(wd, NULL, NULL, NULL);