1 #include <Elementary.h>
8 * The bg object is used for setting a solid background to a window or packing
9 * into any container object.
12 typedef struct _Widget_Data Widget_Data;
16 Evas_Object *base, *rect, *img, *overlay;
17 const char *file, *group;
24 static const char *widtype = NULL;
26 static void _del_hook(Evas_Object *obj);
27 static void _theme_hook(Evas_Object *obj);
28 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
31 _del_hook(Evas_Object *obj)
33 Widget_Data *wd = elm_widget_data_get(obj);
38 _theme_hook(Evas_Object *obj)
40 Widget_Data *wd = elm_widget_data_get(obj);
43 _elm_theme_object_set(obj, wd->base, "bg", "base",
44 elm_widget_style_get(obj));
47 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
49 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
51 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
53 // FIXME: if i don't do this, bg doesnt calc correctly. why?
54 evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
55 evas_object_resize(wd->base, w, h);
59 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
61 Widget_Data *wd = data;
62 Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
63 Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
64 Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
65 Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
68 if ((!wd->img) || (!wd->file)) return;
69 if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
72 evas_object_image_size_get(wd->img, &iw, &ih);
73 if ((iw < 1) || (ih < 1)) return;
75 /* grab base object dimensions */
76 evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
78 /* set some defaults */
86 case ELM_BG_OPTION_CENTER:
94 case ELM_BG_OPTION_SCALE:
96 fh = ((ih * fw) / iw);
100 fw = ((iw * fh) / ih);
102 fx = ((bw - fw) / 2);
103 fy = ((bh - fh) / 2);
105 case ELM_BG_OPTION_TILE:
109 case ELM_BG_OPTION_STRETCH:
116 evas_object_move(wd->img, nx, ny);
117 evas_object_resize(wd->img, nw, nh);
118 evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
120 evas_object_size_hint_min_set(wd->img, mw, mh);
121 evas_object_size_hint_max_set(wd->img, mw, mh);
125 * Add a new background to the parent
127 * @param parent The parent object
128 * @return The new object or NULL if it cannot be created
133 elm_bg_add(Evas_Object *parent)
139 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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 if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
207 evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
208 evas_object_image_file_set(wd->img, file, group);
210 evas_object_repeat_events_set(wd->img, EINA_TRUE);
211 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
212 elm_widget_sub_object_add(obj, wd->img);
213 _custom_resize(wd, NULL, NULL, NULL);
217 * Get the file (image or edje) used for the background
219 * @param obj The bg object
220 * @param file The file path
221 * @param group Optional key (group in Edje) within the file
226 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
228 ELM_CHECK_WIDTYPE(obj, widtype);
229 Widget_Data *wd = elm_widget_data_get(obj);
230 if (file) *file = wd->file;
231 if (group) *group = wd->group;
235 * Set the option used for the background image
237 * @param obj The bg object
238 * @param option The desired background option (TILE, SCALE)
240 * This sets the option used for manipulating the display of the background
241 * image. The image can be tiled or scaled.
246 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
248 ELM_CHECK_WIDTYPE(obj, widtype);
251 wd = elm_widget_data_get(obj);
253 _custom_resize(wd, NULL, NULL, NULL);
257 * Get the option used for the background image
259 * @param obj The bg object
260 * @return The desired background option (TILE, SCALE)
265 elm_bg_option_get(const Evas_Object *obj)
267 ELM_CHECK_WIDTYPE(obj, widtype) 0;
270 wd = elm_widget_data_get(obj);
275 * Set the option used for the background color
277 * @param obj The bg object
282 * This sets the color used for the background rectangle.
287 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
289 ELM_CHECK_WIDTYPE(obj, widtype);
292 wd = elm_widget_data_get(obj);
295 wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
296 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
297 elm_widget_sub_object_add(obj, wd->rect);
298 _custom_resize(wd, NULL, NULL, NULL);
300 evas_object_color_set(wd->rect, r, g, b, 255);
304 * Get the option used for the background color
306 * @param obj The bg object
314 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
316 ELM_CHECK_WIDTYPE(obj, widtype);
319 wd = elm_widget_data_get(obj);
320 evas_object_color_get(wd->rect, r, g, b, NULL);
324 * Set the overlay object used for the background object.
326 * @param obj The bg object
327 * @param overlay The overlay object
329 * This provides a way for elm_bg to have an 'overlay' (such as animated fog)
330 * Once the over object is set, a previously set one will be deleted.
331 * If you want to keep that old content object, use the
332 * elm_bg_overlay_unset() function.
337 elm_bg_overlay_set(Evas_Object *obj, Evas_Object *overlay)
339 ELM_CHECK_WIDTYPE(obj, widtype);
340 Widget_Data *wd = elm_widget_data_get(obj);
344 evas_object_del(wd->overlay);
349 wd->overlay = overlay;
350 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
351 elm_widget_sub_object_add(obj, wd->overlay);
354 _custom_resize(wd, NULL, NULL, NULL);
358 * Set the overlay object used for the background object.
360 * @param obj The bg object
361 * @return The content that is being used
363 * Return the content object which is set for this widget
368 elm_bg_overlay_get(const Evas_Object *obj)
370 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
371 Widget_Data *wd = elm_widget_data_get(obj);
372 if (!wd) return NULL;
377 * Get the overlay object used for the background object.
379 * @param obj The bg object
380 * @return The content that was being used
382 * Unparent and return the overlay object which was set for this widget
387 elm_bg_overlay_unset(Evas_Object *obj)
389 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
390 Widget_Data *wd = elm_widget_data_get(obj);
391 Evas_Object *overlay;
392 if (!wd) return NULL;
393 if (!wd->overlay) return NULL;
394 overlay = wd->overlay;
395 elm_widget_sub_object_del(obj, wd->overlay);
396 edje_object_part_unswallow(wd->base, wd->overlay);
398 _custom_resize(wd, NULL, NULL, NULL);
403 * Set the size of a loaded image of the canvas of the bg.
405 * @param obj The bg object
406 * @param w The new width of the canvas image given.
407 * @param h The new height of the canvas image given.
409 * This function sets a new size for the canvas image of the given the bg.
413 elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
415 ELM_CHECK_WIDTYPE(obj, widtype);
416 Widget_Data *wd = elm_widget_data_get(obj);
421 if (!wd->img) return;
422 if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
423 evas_object_image_load_size_set(wd->img, w, h);