[Elm] Elm bg now happily inherits from elm

layout.



SVN revision: 70709
This commit is contained in:
Gustavo Lima Chaves 2012-05-03 22:41:32 +00:00
parent f4e29b50ee
commit 8723fd9e76
2 changed files with 227 additions and 211 deletions

View File

@ -1,75 +1,90 @@
#include <Elementary.h>
#include "elm_priv.h"
#include "elm_widget_layout.h"
typedef struct _Widget_Data Widget_Data;
static const char BG_SMART_NAME[] = "elm_bg";
struct _Widget_Data
typedef struct _Elm_Bg_Smart_Data Elm_Bg_Smart_Data;
struct _Elm_Bg_Smart_Data
{
Evas_Object *base, *rect, *img, *overlay;
const char *file, *group;
Elm_Bg_Option option;
Elm_Layout_Smart_Data base;
/* the basic background's edje object has three swallow spots, namely:
* - "elm.swallow.rectangle" (elm_bg_color_set),
* - "elm.swallow.background" (elm_bg_file_set) and
* - "elm.swallow.content" (elm_bg_overlay_set).
* the following three variables hold possible content to fit in each
* of them, respectively. */
Evas_Object *rect, *img;
const char *file, *group; /* path to file and group name
* to give life to "img" */
Elm_Bg_Option option;
struct
{
Evas_Coord w, h;
} load_opts;
{
Evas_Coord w, h;
} load_opts;
};
static const char *widtype = NULL;
#define ELM_BG_DATA_GET(o, sd) \
Elm_Bg_Smart_Data * sd = evas_object_smart_data_get(o)
static void _del_hook(Evas_Object *obj);
static void _theme_hook(Evas_Object *obj);
static void _custom_resize(void *data, Evas *a, Evas_Object *obj, void *event_info);
static void _content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content);
static Evas_Object *_content_get_hook(const Evas_Object *obj, const char *part);
static Evas_Object *_content_unset_hook(Evas_Object *obj, const char *part);
#define ELM_BG_DATA_GET_OR_RETURN(o, ptr) \
ELM_BG_DATA_GET(o, ptr); \
if (!ptr) \
{ \
CRITICAL("No widget data for object %p (%s)", \
o, evas_object_type_get(o)); \
return; \
}
#define ELM_BG_DATA_GET_OR_RETURN_VAL(o, ptr, val) \
ELM_BG_DATA_GET(o, ptr); \
if (!ptr) \
{ \
CRITICAL("No widget data for object %p (%s)", \
o, evas_object_type_get(o)); \
return val; \
}
#define ELM_BG_CHECK(obj) \
if (!obj || !elm_widget_type_check((obj), BG_SMART_NAME, __func__)) \
return
static const Elm_Layout_Part_Alias_Description _content_aliases[] =
{
{"overlay", "elm.swallow.content"},
{NULL, NULL}
};
/* Inheriting from elm_layout. Besides, we need no more than what is
* there */
EVAS_SMART_SUBCLASS_NEW
(BG_SMART_NAME, _elm_bg, Elm_Layout_Smart_Class, Elm_Layout_Smart_Class,
elm_layout_smart_class_get, NULL);
static void
_del_hook(Evas_Object *obj)
_elm_bg_smart_sizing_eval(Evas_Object *obj)
{
Widget_Data *wd = elm_widget_data_get(obj);
free(wd);
}
static void
_theme_hook(Evas_Object *obj)
{
Widget_Data *wd = elm_widget_data_get(obj);
Evas_Coord w, h;
_elm_theme_object_set(obj, wd->base, "bg", "base",
elm_widget_style_get(obj));
if (wd->rect)
edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
if (wd->img)
edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
if (wd->overlay)
edje_object_part_swallow(wd->base, "elm.swallow.content", wd->overlay);
// FIXME: if i don't do this, bg doesnt calc correctly. why?
evas_object_geometry_get(wd->base, NULL, NULL, &w, &h);
evas_object_resize(wd->base, w, h);
}
static void
_custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
{
Widget_Data *wd = data;
Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
Evas_Coord iw = 0, ih = 0, mw = -1, mh = -1;
Evas_Coord bx = 0, by = 0, bw = 0, bh = 0;
Evas_Coord fx = 0, fy = 0, fw = 0, fh = 0;
Evas_Coord nx = 0, ny = 0, nw = 0, nh = 0;
const char *p;
if ((!wd->img) || (!wd->file)) return;
if (((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
ELM_BG_DATA_GET(obj, sd);
if ((!sd->img) || (!sd->file)) return;
if (((p = strrchr(sd->file, '.'))) && (!strcasecmp(p, ".edj"))) return;
/* grab image size */
evas_object_image_size_get(wd->img, &iw, &ih);
evas_object_image_size_get(sd->img, &iw, &ih);
if ((iw < 1) || (ih < 1)) return;
/* grab base object dimensions */
evas_object_geometry_get(wd->base, &bx, &by, &bw, &bh);
evas_object_geometry_get
(ELM_WIDGET_DATA(sd)->resize_obj, &bx, &by, &bw, &bh);
/* set some defaults */
nx = bx;
@ -77,248 +92,246 @@ _custom_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void
nw = bw;
nh = bh;
switch (wd->option)
switch (sd->option)
{
case ELM_BG_OPTION_CENTER:
fw = nw = iw;
fh = nh = ih;
nx = ((bw - fw) / 2);
ny = ((bh - fh) / 2);
mw = iw;
mh = ih;
break;
fw = nw = iw;
fh = nh = ih;
nx = ((bw - fw) / 2);
ny = ((bh - fh) / 2);
mw = iw;
mh = ih;
break;
case ELM_BG_OPTION_SCALE:
fw = bw;
fh = ((ih * fw) / iw);
if (fh < bh)
{
fh = bh;
fw = ((iw * fh) / ih);
}
fx = ((bw - fw) / 2);
fy = ((bh - fh) / 2);
break;
fw = bw;
fh = ((ih * fw) / iw);
if (fh < bh)
{
fh = bh;
fw = ((iw * fh) / ih);
}
fx = ((bw - fw) / 2);
fy = ((bh - fh) / 2);
break;
case ELM_BG_OPTION_TILE:
fw = iw;
fh = ih;
break;
fw = iw;
fh = ih;
break;
case ELM_BG_OPTION_STRETCH:
default:
fw = bw;
fh = bh;
break;
fw = bw;
fh = bh;
break;
}
evas_object_move(wd->img, nx, ny);
evas_object_resize(wd->img, nw, nh);
evas_object_image_fill_set(wd->img, fx, fy, fw, fh);
evas_object_move(sd->img, nx, ny);
evas_object_resize(sd->img, nw, nh);
evas_object_image_fill_set(sd->img, fx, fy, fw, fh);
evas_object_size_hint_min_set(wd->img, mw, mh);
evas_object_size_hint_max_set(wd->img, mw, mh);
evas_object_size_hint_min_set(sd->img, mw, mh);
evas_object_size_hint_max_set(sd->img, mw, mh);
}
static void
_content_set_hook(Evas_Object *obj, const char *part, Evas_Object *content)
_on_resize(void *data,
Evas *e __UNUSED__,
Evas_Object *obj __UNUSED__,
void *event_info __UNUSED__)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd;
if (part && strcmp(part, "overlay")) return;
wd = elm_widget_data_get(obj);
if (!wd) return;
if (content == wd->overlay) return;
if (wd->overlay) evas_object_del(wd->overlay);
wd->overlay = content;
if (content)
{
edje_object_part_swallow(wd->base, "elm.swallow.content", content);
elm_widget_sub_object_add(obj, content);
}
_custom_resize(wd, NULL, NULL, NULL);
elm_layout_sizing_eval(data);
}
static Evas_Object *
_content_get_hook(const Evas_Object *obj, const char *part)
static void
_elm_bg_smart_add(Evas_Object *obj)
{
ELM_CHECK_WIDTYPE(obj, widtype) NULL;
Widget_Data *wd;
if (part && strcmp(part, "overlay")) return NULL;
wd = elm_widget_data_get(obj);
if (!wd) return NULL;
return wd->overlay;
EVAS_SMART_DATA_ALLOC(obj, Elm_Bg_Smart_Data);
ELM_WIDGET_CLASS(_elm_bg_parent_sc)->base.add(obj);
elm_widget_can_focus_set(obj, EINA_FALSE);
priv->option = ELM_BG_OPTION_SCALE;
evas_object_event_callback_add(obj, EVAS_CALLBACK_RESIZE, _on_resize, obj);
elm_layout_theme_set(obj, "bg", "base", elm_widget_style_get(obj));
}
static Evas_Object *
_content_unset_hook(Evas_Object *obj, const char *part)
static void
_elm_bg_smart_set_user(Elm_Layout_Smart_Class *sc)
{
ELM_CHECK_WIDTYPE(obj, widtype) NULL;
Widget_Data *wd;
Evas_Object *overlay;
if (part && strcmp(part, "overlay")) return NULL;
wd = elm_widget_data_get(obj);
if (!wd || !wd->overlay) return NULL;
overlay = wd->overlay;
elm_widget_sub_object_del(obj, wd->overlay);
edje_object_part_unswallow(wd->base, wd->overlay);
wd->overlay = NULL;
_custom_resize(wd, NULL, NULL, NULL);
return overlay;
ELM_WIDGET_CLASS(sc)->base.add = _elm_bg_smart_add;
sc->sizing_eval = _elm_bg_smart_sizing_eval;
sc->content_aliases = _content_aliases;
}
EAPI Evas_Object *
elm_bg_add(Evas_Object *parent)
{
Evas_Object *obj;
Evas *e;
Widget_Data *wd;
Evas_Object *obj;
ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(parent, NULL);
ELM_SET_WIDTYPE(widtype, "bg");
elm_widget_type_set(obj, "bg");
elm_widget_sub_object_add(parent, obj);
elm_widget_data_set(obj, wd);
elm_widget_del_hook_set(obj, _del_hook);
elm_widget_theme_hook_set(obj, _theme_hook);
elm_widget_content_set_hook_set(obj, _content_set_hook);
elm_widget_content_get_hook_set(obj, _content_get_hook);
elm_widget_content_unset_hook_set(obj, _content_unset_hook);
e = evas_object_evas_get(parent);
if (!e) return NULL;
elm_widget_can_focus_set(obj, EINA_FALSE);
obj = evas_object_smart_add(e, _elm_bg_smart_class_new());
wd->base = edje_object_add(e);
_elm_theme_object_set(obj, wd->base, "bg", "base", "default");
elm_widget_resize_object_set(obj, wd->base);
if (!elm_widget_sub_object_add(parent, obj))
ERR("could not add %p as sub object of %p", obj, parent);
evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE,
_custom_resize, wd);
wd->option = ELM_BG_OPTION_SCALE;
return obj;
}
EAPI Eina_Bool
elm_bg_file_set(Evas_Object *obj, const char *file, const char *group)
elm_bg_file_set(Evas_Object *obj,
const char *file,
const char *group)
{
ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
Widget_Data *wd = elm_widget_data_get(obj);
const char *p;
ELM_BG_CHECK(obj) EINA_FALSE;
ELM_BG_DATA_GET_OR_RETURN_VAL(obj, sd, EINA_FALSE);
if (wd->img)
const char *p;
Eina_Bool ret;
if (sd->img)
{
evas_object_del(wd->img);
wd->img = NULL;
evas_object_del(sd->img);
sd->img = NULL;
}
if (!file)
{
eina_stringshare_del(wd->file);
wd->file = NULL;
eina_stringshare_del(wd->group);
wd->group = NULL;
eina_stringshare_del(sd->file);
sd->file = NULL;
eina_stringshare_del(sd->group);
sd->group = NULL;
return EINA_TRUE;
}
eina_stringshare_replace(&wd->file, file);
eina_stringshare_replace(&wd->group, group);
eina_stringshare_replace(&sd->file, file);
eina_stringshare_replace(&sd->group, group);
if (((p = strrchr(file, '.'))) && (!strcasecmp(p, ".edj")))
{
wd->img = edje_object_add(evas_object_evas_get(wd->base));
edje_object_file_set(wd->img, file, group);
sd->img = edje_object_add
(evas_object_evas_get(ELM_WIDGET_DATA(sd)->resize_obj));
ret = edje_object_file_set(sd->img, file, group);
}
else
{
wd->img = evas_object_image_add(evas_object_evas_get(wd->base));
if ((wd->load_opts.w > 0) && (wd->load_opts.h > 0))
evas_object_image_load_size_set(wd->img, wd->load_opts.w, wd->load_opts.h);
evas_object_image_file_set(wd->img, file, group);
int err;
sd->img = evas_object_image_add
(evas_object_evas_get(ELM_WIDGET_DATA(sd)->resize_obj));
if ((sd->load_opts.w > 0) && (sd->load_opts.h > 0))
evas_object_image_load_size_set
(sd->img, sd->load_opts.w, sd->load_opts.h);
evas_object_image_file_set(sd->img, file, group);
err = evas_object_image_load_error_get(sd->img);
if (err != EVAS_LOAD_ERROR_NONE)
{
ERR("Could not load image '%s': %s\n",
file, evas_load_error_str(err));
ret = EINA_FALSE;
}
else
ret = EINA_TRUE;
}
evas_object_repeat_events_set(wd->img, EINA_TRUE);
edje_object_part_swallow(wd->base, "elm.swallow.background", wd->img);
elm_widget_sub_object_add(obj, wd->img);
_custom_resize(wd, NULL, NULL, NULL);
return EINA_TRUE;
evas_object_repeat_events_set(sd->img, EINA_TRUE);
ret &= elm_layout_content_set(obj, "elm.swallow.background", sd->img);
elm_layout_sizing_eval(obj);
return ret;
}
EAPI void
elm_bg_file_get(const Evas_Object *obj, const char **file, const char **group)
elm_bg_file_get(const Evas_Object *obj,
const char **file,
const char **group)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd = elm_widget_data_get(obj);
if (file) *file = wd->file;
if (group) *group = wd->group;
ELM_BG_CHECK(obj);
ELM_BG_DATA_GET_OR_RETURN(obj, sd);
return;
if (file) *file = sd->file;
if (group) *group = sd->group;
}
EAPI void
elm_bg_option_set(Evas_Object *obj, Elm_Bg_Option option)
elm_bg_option_set(Evas_Object *obj,
Elm_Bg_Option option)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd;
ELM_BG_CHECK(obj);
ELM_BG_DATA_GET_OR_RETURN(obj, sd);
wd = elm_widget_data_get(obj);
wd->option = option;
_custom_resize(wd, NULL, NULL, NULL);
sd->option = option;
return;
elm_layout_sizing_eval(obj);
}
EAPI Elm_Bg_Option
elm_bg_option_get(const Evas_Object *obj)
{
ELM_CHECK_WIDTYPE(obj, widtype) ELM_BG_OPTION_LAST;
Widget_Data *wd;
ELM_BG_CHECK(obj) EINA_FALSE;
ELM_BG_DATA_GET_OR_RETURN_VAL(obj, sd, ELM_BG_OPTION_LAST);
wd = elm_widget_data_get(obj);
return wd->option;
return sd->option;
}
EAPI void
elm_bg_color_set(Evas_Object *obj, int r, int g, int b)
elm_bg_color_set(Evas_Object *obj,
int r,
int g,
int b)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd;
ELM_BG_CHECK(obj);
ELM_BG_DATA_GET_OR_RETURN(obj, sd);
wd = elm_widget_data_get(obj);
if (!wd->rect)
if (!sd->rect)
{
wd->rect = evas_object_rectangle_add(evas_object_evas_get(wd->base));
edje_object_part_swallow(wd->base, "elm.swallow.rectangle", wd->rect);
elm_widget_sub_object_add(obj, wd->rect);
_custom_resize(wd, NULL, NULL, NULL);
sd->rect = evas_object_rectangle_add
(evas_object_evas_get(ELM_WIDGET_DATA(sd)->resize_obj));
elm_layout_content_set(obj, "elm.swallow.rectangle", sd->rect);
elm_layout_sizing_eval(obj);
}
evas_object_color_set(wd->rect, r, g, b, 255);
return;
evas_object_color_set(sd->rect, r, g, b, 255);
}
EAPI void
elm_bg_color_get(const Evas_Object *obj, int *r, int *g, int *b)
elm_bg_color_get(const Evas_Object *obj,
int *r,
int *g,
int *b)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd;
ELM_BG_CHECK(obj);
ELM_BG_DATA_GET_OR_RETURN(obj, sd);
wd = elm_widget_data_get(obj);
evas_object_color_get(wd->rect, r, g, b, NULL);
return;
evas_object_color_get(sd->rect, r, g, b, NULL);
}
EAPI void
elm_bg_load_size_set(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
elm_bg_load_size_set(Evas_Object *obj,
Evas_Coord w,
Evas_Coord h)
{
ELM_CHECK_WIDTYPE(obj, widtype);
Widget_Data *wd = elm_widget_data_get(obj);
ELM_BG_CHECK(obj);
ELM_BG_DATA_GET_OR_RETURN(obj, sd);
const char *p;
if (!wd) return;
wd->load_opts.w = w;
wd->load_opts.h = h;
if (!wd->img) return;
if (!(((p = strrchr(wd->file, '.'))) && (!strcasecmp(p, ".edj"))))
evas_object_image_load_size_set(wd->img, w, h);
return;
sd->load_opts.w = w;
sd->load_opts.h = h;
if (!sd->img) return;
if (!(((p = strrchr(sd->file, '.'))) && (!strcasecmp(p, ".edj"))))
evas_object_image_load_size_set(sd->img, w, h);
}

View File

@ -1,5 +1,5 @@
/**
* @defgroup Bg Bg
* @defgroup Bg Background
* @ingroup Elementary
*
* @image html img/widget/bg/preview-00.png
@ -14,8 +14,11 @@
* properties useful to a background, like setting it to tiled,
* centered, scaled or stretched.
*
* This widget inherits from the @ref Layout one, so that all the
* functions acting on it also work for background objects.
*
* Default content parts of the bg widget that you can use for are:
* @li "overlay" - overlay of the bg
* @li @c "overlay" - overlay of the bg
*
* Supported elm_object common APIs.
* @li @ref elm_object_part_content_set