Commit 86bd3068 authored by Bruno Coudoin's avatar Bruno Coudoin

Added an autocrop feature in the svg goocanvas item.


svn path=/branches/gcomprixogoo/; revision=3662
parent 337c17d9
2009-01-02 Bruno Coudoin,,, <bruno.coudoin@free.fr>
Added an autocrop feature in the svg goocanvas item.
* src/gcompris/config.c: (gc_config_start), (gc_config_stop),
(display_previous_next), (item_event_ok):
* src/goocanvas/src/goocanvassvg.c:
(goo_canvas_svg_install_common_properties), (_autocrop),
(_init_surface), (goo_canvas_svg_init),
(goo_canvas_svg_set_common_property):
* src/goocanvas/src/goocanvassvg.h:
2009-01-01 Bruno Coudoin,,, <bruno.coudoin@free.fr>
More integration of the single file skin.
......
......@@ -34,8 +34,9 @@ static GooCanvasItem *item_bad_flag = NULL;
static GooCanvasItem *item_timer_text = NULL;
static GooCanvasItem *item_skin_text = NULL;
static GooCanvasItem *item_filter_text = NULL;
static GdkPixbuf *pixmap_checked = NULL;
static GdkPixbuf *pixmap_unchecked = NULL;
static gchar *pixmap_checked = NULL;
static gchar *pixmap_unchecked = NULL;
static guint pixmap_width;
static gchar *current_locale = NULL;
static GList *skinlist = NULL;
......@@ -194,9 +195,9 @@ gc_config_start ()
"fill-color-rgba", gc_skin_color_title,
NULL);
pixmap_checked = gc_skin_pixmap_load("button_checked.png");
pixmap_unchecked = gc_skin_pixmap_load("button_unchecked.png");
pixmap_checked = "#CHECKED";
pixmap_unchecked = "#UNCHECKED";
pixmap_width = 30;
x_start += 150;
x_flag_start = x_start + 50;
......@@ -209,9 +210,9 @@ gc_config_start ()
display_previous_next(x_start, y_start, "locale_previous", "locale_next");
item_locale_flag = goo_canvas_svg_new (rootitem,
rsvg_handle_new(),
NULL);
y_flag_start = y_start - gdk_pixbuf_get_width(pixmap_checked)/2;
rsvg_handle_new(),
NULL);
y_flag_start = y_start - pixmap_width/2;
/* Display a bad icon if this locale is not available */
item_bad_flag = goo_canvas_svg_new (rootitem,
......@@ -221,7 +222,7 @@ gc_config_start ()
NULL);
SET_ITEM_LOCATION(item_bad_flag,
x_flag_start + 5,
y_start - gdk_pixbuf_get_width(pixmap_checked)/2);
y_start - pixmap_width/2);
/*
* The current locale is the one found in the config file
......@@ -242,11 +243,12 @@ gc_config_start ()
// Fullscreen / Window
y_start += Y_GAP;
item = goo_canvas_image_new (rootitem,
(properties->fullscreen ? pixmap_checked : pixmap_unchecked),
(double) x_start,
(double) y_start - gdk_pixbuf_get_width(pixmap_checked)/2,
NULL);
item = goo_canvas_svg_new (rootitem,
gc_skin_rsvg_get(),
"svg-id", (properties->fullscreen ? pixmap_checked : pixmap_unchecked),
"autocrop", TRUE,
NULL);
SET_ITEM_LOCATION(item, x_start, y_start - pixmap_width/2);
g_signal_connect(item, "button_press_event",
(GtkSignalFunc) item_event_ok,
......@@ -267,11 +269,12 @@ gc_config_start ()
// Music
y_start += Y_GAP;
item = goo_canvas_image_new (rootitem,
(properties->music ? pixmap_checked : pixmap_unchecked),
(double) x_start,
(double) y_start - gdk_pixbuf_get_width(pixmap_checked)/2,
NULL);
item = goo_canvas_svg_new (rootitem,
gc_skin_rsvg_get(),
"svg-id", (properties->music ? pixmap_checked : pixmap_unchecked),
"autocrop", TRUE,
NULL);
SET_ITEM_LOCATION(item, x_start, y_start - pixmap_width/2);
g_signal_connect(item, "button_press_event",
(GtkSignalFunc) item_event_ok,
......@@ -292,11 +295,12 @@ gc_config_start ()
// Effect
y_start += Y_GAP;
item = goo_canvas_image_new (rootitem,
(properties->fx ? pixmap_checked : pixmap_unchecked),
(double) x_start,
(double) y_start - gdk_pixbuf_get_width(pixmap_checked)/2,
NULL);
item = goo_canvas_svg_new (rootitem,
gc_skin_rsvg_get(),
"svg-id", (properties->fx ? pixmap_checked : pixmap_unchecked),
"autocrop", TRUE,
NULL);
SET_ITEM_LOCATION(item, x_start, y_start - pixmap_width/2);
g_signal_connect(item, "button_press_event",
(GtkSignalFunc) item_event_ok,
......@@ -465,14 +469,6 @@ void gc_config_stop ()
stars_group = NULL;
if(pixmap_unchecked)
gdk_pixbuf_unref(pixmap_unchecked);
pixmap_unchecked = NULL;
if(pixmap_checked)
gdk_pixbuf_unref(pixmap_checked);
pixmap_checked = NULL;
/* UnPause the board */
if(is_displayed)
gc_board_pause(FALSE);
......@@ -516,35 +512,36 @@ static void
display_previous_next(guint x_start, guint y_start,
gchar *eventname_previous, gchar *eventname_next)
{
GdkPixbuf *pixmap = NULL;
GooCanvasItem *item;
GooCanvasBounds bounds;
pixmap = gc_skin_pixmap_load("button_backward.png");
item = goo_canvas_image_new (rootitem,
pixmap,
(double) x_start - gdk_pixbuf_get_width(pixmap) - 10,
(double) y_start - gdk_pixbuf_get_width(pixmap_checked)/2,
NULL);
item = goo_canvas_svg_new (rootitem,
gc_skin_rsvg_get(),
"svg-id", "#PREVIOUS",
NULL);
goo_canvas_item_get_bounds(item, &bounds);
SET_ITEM_LOCATION(item,
x_start - (bounds.x2 - bounds.x1) - 10,
y_start - pixmap_width/2);
g_signal_connect(item, "button_press_event",
(GtkSignalFunc) item_event_ok,
eventname_previous);
gc_item_focus_init(item, NULL);
gdk_pixbuf_unref(pixmap);
pixmap = gc_skin_pixmap_load("button_forward.png");
item = goo_canvas_image_new (rootitem,
pixmap,
(double) x_start,
(double) y_start - gdk_pixbuf_get_width(pixmap_checked)/2,
NULL);
item = goo_canvas_svg_new (rootitem,
gc_skin_rsvg_get(),
"svg-id", "#NEXT",
NULL);
SET_ITEM_LOCATION(item,
x_start,
y_start - pixmap_width/2);
g_signal_connect(item, "button_press_event",
(GtkSignalFunc) item_event_ok,
eventname_next);
gc_item_focus_init(item, NULL);
gdk_pixbuf_unref(pixmap);
}
static void
......@@ -728,7 +725,7 @@ item_event_ok(GooCanvasItem *item,
gc_fullscreen_set(properties->fullscreen);
g_object_set (item,
"pixbuf", (properties->fullscreen ? pixmap_checked : pixmap_unchecked),
"svg-id", (properties->fullscreen ? pixmap_checked : pixmap_unchecked),
NULL);
gc_item_focus_init(item, NULL);
......@@ -737,7 +734,7 @@ item_event_ok(GooCanvasItem *item,
{
properties->music = (properties->music ? 0 : 1);
g_object_set (item,
"pixbuf", (properties->music ? pixmap_checked : pixmap_unchecked),
"svg-id", (properties->music ? pixmap_checked : pixmap_unchecked),
NULL);
if(!properties->music)
{
......@@ -753,7 +750,7 @@ item_event_ok(GooCanvasItem *item,
{
properties->fx = (properties->fx ? 0 : 1);
g_object_set (item,
"pixbuf", (properties->fx ? pixmap_checked : pixmap_unchecked),
"svg-id", (properties->fx ? pixmap_checked : pixmap_unchecked),
NULL);
gc_item_focus_init(item, NULL);
}
......
......@@ -13,13 +13,14 @@ enum {
/* Convenience properties. */
PROP_SVGHANDLE,
PROP_SVG_ID,
PROP_AUTOCROP,
};
static void goo_canvas_svg_finalize (GObject *object);
static void goo_canvas_svg_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
guint param_id,
const GValue *value,
GParamSpec *pspec);
/* Use the GLib convenience macro to define the type. GooCanvasSvg is the
......@@ -44,6 +45,46 @@ goo_canvas_svg_install_common_properties (GObjectClass *gobject_class)
NULL,
G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class, PROP_AUTOCROP,
g_param_spec_boolean ("autocrop",
"Auto Crop",
"Set to True to crop the image, default is False",
FALSE,
G_PARAM_WRITABLE));
}
/* Return a new surface with the item in src_surface being
* assigned to 0,0 coordinate.
*/
static cairo_surface_t*
_autocrop(cairo_surface_t *src_surface,
double x1, double x2, double y1, double y2)
{
cairo_surface_t* dst_surface =
cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
x2 - x1 + 1,
y2 - y1 + 1);
unsigned char* src_data = cairo_image_surface_get_data(src_surface);
unsigned char* dst_data = cairo_image_surface_get_data(dst_surface);
int src_stride = cairo_image_surface_get_stride(src_surface);
int dst_stride = cairo_image_surface_get_stride(dst_surface);
int x;
for (x = x1; x <= x2; x++)
{
int y;
for (y = y1; y <= y2; y++)
{
guint32 point = *(guint32*)&src_data[y*src_stride + x*4];
*(guint32*)&dst_data[((y-(int)y1)*dst_stride)
+ (x*4 - (int)x1*4)] = point;
}
}
return dst_surface;
}
static void _init_surface(GooCanvasSvg *canvas_svg,
......@@ -72,7 +113,6 @@ static void _init_surface(GooCanvasSvg *canvas_svg,
canvas_svg->cr = cairo_create (cst);
rsvg_handle_render_cairo_sub (svg_handle, canvas_svg->cr,
canvas_svg->id);
canvas_svg->pattern = cairo_pattern_create_for_surface (cst);
/* Get the real coordinates */
canvas_svg->x1 = canvas_svg->width;
......@@ -97,6 +137,21 @@ static void _init_surface(GooCanvasSvg *canvas_svg,
}
}
}
if(stride > 0 && canvas_svg->autocrop)
{
canvas_svg->pattern = \
cairo_pattern_create_for_surface ( _autocrop(cst,
canvas_svg->x1, canvas_svg->x2,
canvas_svg->y1, canvas_svg->y2) );
canvas_svg->x2 = canvas_svg->x2 - canvas_svg->x1;
canvas_svg->y2 = canvas_svg->y2 - canvas_svg->y1;
canvas_svg->x1 = 0;
canvas_svg->y1 = 0;
}
else
canvas_svg->pattern = cairo_pattern_create_for_surface (cst);
cairo_surface_destroy(cst);
}
......@@ -113,6 +168,7 @@ goo_canvas_svg_init (GooCanvasSvg *canvas_svg)
canvas_svg->id = NULL;
canvas_svg->cr = NULL;
canvas_svg->pattern = NULL;
canvas_svg->autocrop = FALSE;
}
......@@ -238,6 +294,8 @@ goo_canvas_svg_set_common_property (GObject *object,
if (canvas_svg->svg_handle)
_init_surface(canvas_svg, canvas_svg->svg_handle);
break;
case PROP_AUTOCROP:
canvas_svg->autocrop = g_value_get_boolean (value);
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......
......@@ -39,6 +39,7 @@ struct _GooCanvasSvg
double y1;
double x2;
double y2;
guint autocrop : 1;
};
struct _GooCanvasSvgClass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment