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;
23 static const char *widtype = NULL;
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
30 _del_hook(Evas_Object *obj)
32 Widget_Data *wd = elm_widget_data_get(obj);
37 _theme_hook(Evas_Object *obj)
39 Widget_Data *wd = elm_widget_data_get(obj);
42 _elm_theme_object_set(obj, wd->base, "bg", "base",
43 elm_widget_style_get(obj));
46 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
48 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
50 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
52 // FIXME: if i don't do this, bg doesnt calc correctly. why?
53 evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
54 evas_object_resize(wd->base, w, h);
58 _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
60 Widget_Data *wd = data;
61 Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
62 Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
63 Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
64 Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
67 if ((!wd->img) || (!wd->file)) return;
68 if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
71 evas_object_image_size_get(wd->img, &iw, &ih);
72 if ((iw < 1) || (ih < 1)) return;
74 /* grab base object dimensions */
75 evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
77 /* set some defaults */
85 case ELM_BG_OPTION_CENTER:
93 case ELM_BG_OPTION_SCALE:
95 fh = ((ih * fw) / iw);
99 fw = ((iw * fh) / ih);
101 fx = ((bw - fw) / 2);
102 fy = ((bh - fh) / 2);
104 case ELM_BG_OPTION_TILE:
108 case ELM_BG_OPTION_STRETCH:
115 evas_object_move(wd->img, nx, ny);
116 evas_object_resize(wd->img, nw, nh);
117 evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
119 evas_object_size_hint_min_set(wd->img, mw, mh);
120 evas_object_size_hint_max_set(wd->img, mw, mh);
124 * Add a new background to the parent
126 * @param parent The parent object
127 * @return The new object or NULL if it cannot be created
132 elm_bg_add(Evas_Object *parent)
138 EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
140 wd = ELM_NEW(Widget_Data);
141 e = evas_object_evas_get(parent);
143 obj = elm_widget_add(e);
144 ELM_SET_WIDTYPE(widtype, "bg");
145 elm_widget_type_set(obj, "bg");
146 elm_widget_sub_object_add(parent, obj);
147 elm_widget_data_set(obj, wd);
148 elm_widget_del_hook_set(obj, _del_hook);
149 elm_widget_theme_hook_set(obj, _theme_hook);
150 elm_widget_can_focus_set(obj, EINA_FALSE);
152 wd->base = edje_object_add(e);
153 _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
154 elm_widget_resize_object_set(obj, wd->base);
156 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
159 wd->option = ELM_BG_OPTION_SCALE;
164 * Set the file (image or edje) used for the background
166 * @param obj The bg object
167 * @param file The file path
168 * @param group Optional key (group in Edje) within the file
170 * This sets the image file used in the background object. The image (or edje)
171 * will be stretched (retaining aspect if its an image file) to completely fill
172 * the bg object. This may mean some parts are not visible.
174 * @note Once the image of @p obj is set, a previously set one will be deleted,
175 * even if @p file is NULL.
180 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
182 ELM_CHECK_WIDTYPE(obj, widtype);
183 Widget_Data *wd = elm_widget_data_get(obj);
188 evas_object_del(wd->img);
193 eina_stringshare_del(wd->file);
195 eina_stringshare_del(wd->group);
199 eina_stringshare_replace(&wd->file, file);
200 eina_stringshare_replace(&wd->group, group);
201 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
203 wd->img = edje_object_add(evas_object_evas_get(wd->base));
204 edje_object_file_set(wd->img, file, group);
208 wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
209 if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
210 evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
211 evas_object_image_file_set(wd->img, file, group);
213 evas_object_repeat_events_set(wd->img, EINA_TRUE);
214 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
215 elm_widget_sub_object_add(obj, wd->img);
216 _custom_resize(wd, NULL, NULL, NULL);
220 * Get the file (image or edje) used for the background
222 * @param obj The bg object
223 * @param file The file path
224 * @param group Optional key (group in Edje) within the file
229 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
231 ELM_CHECK_WIDTYPE(obj, widtype);
232 Widget_Data *wd = elm_widget_data_get(obj);
233 if (file) *file = wd->file;
234 if (group) *group = wd->group;
238 * Set the option used for the background image
240 * @param obj The bg object
241 * @param option The desired background option (TILE, SCALE)
243 * This sets the option used for manipulating the display of the background
244 * image. The image can be tiled or scaled.
249 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
251 ELM_CHECK_WIDTYPE(obj, widtype);
254 wd = elm_widget_data_get(obj);
256 _custom_resize(wd, NULL, NULL, NULL);
260 * Get the option used for the background image
262 * @param obj The bg object
263 * @return The desired background option (TILE, SCALE)
268 elm_bg_option_get(const Evas_Object *obj)
270 ELM_CHECK_WIDTYPE(obj, widtype) 0;
273 wd = elm_widget_data_get(obj);
278 * Set the option used for the background color
280 * @param obj The bg object
285 * This sets the color used for the background rectangle.
290 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
292 ELM_CHECK_WIDTYPE(obj, widtype);
295 wd = elm_widget_data_get(obj);
298 wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
299 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
300 elm_widget_sub_object_add(obj, wd->rect);
301 _custom_resize(wd, NULL, NULL, NULL);
303 evas_object_color_set(wd->rect, r, g, b, 255);
307 * Get the option used for the background color
309 * @param obj The bg object
317 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
319 ELM_CHECK_WIDTYPE(obj, widtype);
322 wd = elm_widget_data_get(obj);
323 evas_object_color_get(wd->rect, r, g, b, NULL);
327 * Set the overlay object used for the background object.
329 * @param obj The bg object
330 * @param overlay The overlay object
332 * This provides a way for elm_bg to have an 'overlay' (such as animated fog)
333 * Once the over object is set, a previously set one will be deleted.
334 * If you want to keep that old content object, use the
335 * elm_bg_overlay_unset() function.
340 elm_bg_overlay_set(Evas_Object *obj, Evas_Object *overlay)
342 ELM_CHECK_WIDTYPE(obj, widtype);
343 Widget_Data *wd = elm_widget_data_get(obj);
347 evas_object_del(wd->overlay);
352 wd->overlay = overlay;
353 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
354 elm_widget_sub_object_add(obj, wd->overlay);
357 _custom_resize(wd, NULL, NULL, NULL);
361 * Set the overlay object used for the background object.
363 * @param obj The bg object
364 * @return The content that is being used
366 * Return the content object which is set for this widget
371 elm_bg_overlay_get(const Evas_Object *obj)
373 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
374 Widget_Data *wd = elm_widget_data_get(obj);
375 if (!wd) return NULL;
380 * Get the overlay object used for the background object.
382 * @param obj The bg object
383 * @return The content that was being used
385 * Unparent and return the overlay object which was set for this widget
390 elm_bg_overlay_unset(Evas_Object *obj)
392 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
393 Widget_Data *wd = elm_widget_data_get(obj);
394 Evas_Object *overlay;
395 if (!wd) return NULL;
396 if (!wd->overlay) return NULL;
397 overlay = wd->overlay;
398 elm_widget_sub_object_del(obj, wd->overlay);
399 edje_object_part_unswallow(wd->base, wd->overlay);
401 _custom_resize(wd, NULL, NULL, NULL);
406 * Set the size of a loaded image of the canvas of the bg.
408 * @param obj The bg object
409 * @param w The new width of the canvas image given.
410 * @param h The new height of the canvas image given.
412 * This function sets a new size for the canvas image of the given the bg.
416 elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
418 ELM_CHECK_WIDTYPE(obj, widtype);
419 Widget_Data *wd = elm_widget_data_get(obj);
424 if (!wd->img) return;
425 if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
426 evas_object_image_load_size_set(wd->img, w, h);