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 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
140 ELM_SET_WIDTYPE(widtype, "bg");
141 elm_widget_type_set(obj, "bg");
142 elm_widget_sub_object_add(parent, obj);
143 elm_widget_data_set(obj, wd);
144 elm_widget_del_hook_set(obj, _del_hook);
145 elm_widget_theme_hook_set(obj, _theme_hook);
146 elm_widget_can_focus_set(obj, EINA_FALSE);
148 wd->base = edje_object_add(e);
149 _elm_theme_object_set(obj, wd->base, "bg", "base", "default");
150 elm_widget_resize_object_set(obj, wd->base);
152 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
155 wd->option = ELM_BG_OPTION_SCALE;
160 * Set the file (image or edje) used for the background
162 * @param obj The bg object
163 * @param file The file path
164 * @param group Optional key (group in Edje) within the file
166 * This sets the image file used in the background object. The image (or edje)
167 * will be stretched (retaining aspect if its an image file) to completely fill
168 * the bg object. This may mean some parts are not visible.
170 * @note Once the image of @p obj is set, a previously set one will be deleted,
171 * even if @p file is NULL.
176 elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
178 ELM_CHECK_WIDTYPE(obj, widtype);
179 Widget_Data *wd = elm_widget_data_get(obj);
184 evas_object_del(wd->img);
189 eina_stringshare_del(wd->file);
191 eina_stringshare_del(wd->group);
195 eina_stringshare_replace(&wd->file, file);
196 eina_stringshare_replace(&wd->group, group);
197 if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
199 wd->img = edje_object_add(evas_object_evas_get(wd->base));
200 edje_object_file_set(wd->img, file, group);
204 wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
205 if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
206 evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
207 evas_object_image_file_set(wd->img, file, group);
209 evas_object_repeat_events_set(wd->img, EINA_TRUE);
210 edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
211 elm_widget_sub_object_add(obj, wd->img);
212 _custom_resize(wd, NULL, NULL, NULL);
216 * Get the file (image or edje) used for the background
218 * @param obj The bg object
219 * @param file The file path
220 * @param group Optional key (group in Edje) within the file
225 elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
227 ELM_CHECK_WIDTYPE(obj, widtype);
228 Widget_Data *wd = elm_widget_data_get(obj);
229 if (file) *file = wd->file;
230 if (group) *group = wd->group;
234 * Set the option used for the background image
236 * @param obj The bg object
237 * @param option The desired background option (TILE, SCALE)
239 * This sets the option used for manipulating the display of the background
240 * image. The image can be tiled or scaled.
245 elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
247 ELM_CHECK_WIDTYPE(obj, widtype);
250 wd = elm_widget_data_get(obj);
252 _custom_resize(wd, NULL, NULL, NULL);
256 * Get the option used for the background image
258 * @param obj The bg object
259 * @return The desired background option (TILE, SCALE)
264 elm_bg_option_get(const Evas_Object *obj)
266 ELM_CHECK_WIDTYPE(obj, widtype) 0;
269 wd = elm_widget_data_get(obj);
274 * Set the option used for the background color
276 * @param obj The bg object
281 * This sets the color used for the background rectangle.
286 elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
288 ELM_CHECK_WIDTYPE(obj, widtype);
291 wd = elm_widget_data_get(obj);
294 wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
295 edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
296 elm_widget_sub_object_add(obj, wd->rect);
297 _custom_resize(wd, NULL, NULL, NULL);
299 evas_object_color_set(wd->rect, r, g, b, 255);
303 * Get the option used for the background color
305 * @param obj The bg object
313 elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
315 ELM_CHECK_WIDTYPE(obj, widtype);
318 wd = elm_widget_data_get(obj);
319 evas_object_color_get(wd->rect, r, g, b, NULL);
323 * Set the overlay object used for the background object.
325 * @param obj The bg object
326 * @param overlay The overlay object
328 * This provides a way for elm_bg to have an 'overlay' (such as animated fog)
329 * Once the over object is set, a previously set one will be deleted.
330 * If you want to keep that old content object, use the
331 * elm_bg_overlay_unset() function.
336 elm_bg_overlay_set(Evas_Object *obj, Evas_Object *overlay)
338 ELM_CHECK_WIDTYPE(obj, widtype);
339 Widget_Data *wd = elm_widget_data_get(obj);
343 evas_object_del(wd->overlay);
348 wd->overlay = overlay;
349 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
350 elm_widget_sub_object_add(obj, wd->overlay);
353 _custom_resize(wd, NULL, NULL, NULL);
357 * Set the overlay object used for the background object.
359 * @param obj The bg object
360 * @return The content that is being used
362 * Return the content object which is set for this widget
367 elm_bg_overlay_get(const Evas_Object *obj)
369 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
370 Widget_Data *wd = elm_widget_data_get(obj);
371 if (!wd) return NULL;
376 * Get the overlay object used for the background object.
378 * @param obj The bg object
379 * @return The content that was being used
381 * Unparent and return the overlay object which was set for this widget
386 elm_bg_overlay_unset(Evas_Object *obj)
388 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
389 Widget_Data *wd = elm_widget_data_get(obj);
390 Evas_Object *overlay;
391 if (!wd) return NULL;
392 if (!wd->overlay) return NULL;
393 overlay = wd->overlay;
394 elm_widget_sub_object_del(obj, wd->overlay);
395 edje_object_part_unswallow(wd->base, wd->overlay);
397 _custom_resize(wd, NULL, NULL, NULL);
402 * Set the size of a loaded image of the canvas of the bg.
404 * @param obj The bg object
405 * @param w The new width of the canvas image given.
406 * @param h The new height of the canvas image given.
408 * This function sets a new size for the canvas image of the given the bg.
412 elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
414 ELM_CHECK_WIDTYPE(obj, widtype);
415 Widget_Data *wd = elm_widget_data_get(obj);
420 if (!wd->img) return;
421 if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
422 evas_object_image_load_size_set(wd->img, w, h);