SugarPreview API improvements

This commit is contained in:
Marco Pesenti Gritti 2007-11-04 16:40:28 +01:00
parent cd4cb6bd67
commit cd61c52c7b
4 changed files with 49 additions and 28 deletions

View File

@ -173,11 +173,24 @@
) )
) )
(define-method save (define-method set_size
(of-object "SugarPreview") (of-object "SugarPreview")
(c-name "sugar_preview_save") (c-name "sugar_preview_set_size")
(return-type "gboolean") (return-type "none")
(parameters (parameters
'("const-char*" "key") '("int" "width")
'("int" "height")
) )
) )
(define-method clear
(of-object "SugarPreview")
(c-name "sugar_preview_clear")
(return-type "none")
)
(define-method get_pixbuf
(of-object "SugarPreview")
(c-name "sugar_preview_get_pixbuf")
(return-type "GdkPixbuf*")
)

View File

@ -27,13 +27,20 @@ static void sugar_preview_init (SugarPreview *menu);
G_DEFINE_TYPE(SugarPreview, sugar_preview, G_TYPE_OBJECT) G_DEFINE_TYPE(SugarPreview, sugar_preview, G_TYPE_OBJECT)
gboolean void
sugar_preview_save(SugarPreview *preview, const char *file_name) sugar_preview_set_size(SugarPreview *preview, int width, int height)
{
preview->width = width;
preview->height = height;
}
GdkPixbuf *
sugar_preview_get_pixbuf(SugarPreview *preview)
{ {
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
if (preview->image != NULL) { if (preview->image == NULL) {
return FALSE; return NULL;
} }
pixbuf = gdk_pixbuf_get_from_image(NULL, preview->image, NULL, pixbuf = gdk_pixbuf_get_from_image(NULL, preview->image, NULL,
@ -41,14 +48,16 @@ sugar_preview_save(SugarPreview *preview, const char *file_name)
preview->image->width, preview->image->width,
preview->image->height); preview->image->height);
gdk_pixbuf_save(pixbuf, file_name, "png", NULL); return pixbuf;
}
void
sugar_preview_clear(SugarPreview *preview)
{
if (preview->image != NULL) { if (preview->image != NULL) {
g_object_unref(G_OBJECT(preview->image)); g_object_unref(G_OBJECT(preview->image));
preview->image = NULL; preview->image = NULL;
} }
return TRUE;
} }
void void
@ -59,16 +68,14 @@ sugar_preview_take_screenshot(SugarPreview *preview, GdkDrawable *drawable)
GdkColormap *colormap; GdkColormap *colormap;
gint width, height; gint width, height;
sugar_preview_clear(preview);
gdk_drawable_get_size(drawable, &width, &height); gdk_drawable_get_size(drawable, &width, &height);
screen = gdk_drawable_get_screen(drawable); screen = gdk_drawable_get_screen(drawable);
visual = gdk_drawable_get_visual(drawable); visual = gdk_drawable_get_visual(drawable);
colormap = gdk_drawable_get_colormap(drawable); colormap = gdk_drawable_get_colormap(drawable);
if (preview->image != NULL) {
g_object_unref(G_OBJECT(preview->image));
}
preview->image = gdk_image_new(GDK_IMAGE_SHARED, visual, width, height); preview->image = gdk_image_new(GDK_IMAGE_SHARED, visual, width, height);
gdk_image_set_colormap(preview->image, colormap); gdk_image_set_colormap(preview->image, colormap);
@ -79,17 +86,12 @@ sugar_preview_take_screenshot(SugarPreview *preview, GdkDrawable *drawable)
} }
static void static void
sugar_preview_dispose (GObject *object) sugar_preview_dispose(GObject *object)
{ {
SugarPreview *preview = SUGAR_PREVIEW(object); SugarPreview *preview = SUGAR_PREVIEW(object);
sugar_preview_clear(preview);
if (preview->image != NULL) {
g_object_unref(G_OBJECT(preview->image));
preview->image = NULL;
}
} }
static void static void
sugar_preview_class_init(SugarPreviewClass *preview_class) sugar_preview_class_init(SugarPreviewClass *preview_class)
{ {

View File

@ -38,17 +38,22 @@ struct _SugarPreview {
GObject base_instance; GObject base_instance;
GdkImage *image; GdkImage *image;
int width;
int height;
}; };
struct _SugarPreviewClass { struct _SugarPreviewClass {
GObjectClass base_class; GObjectClass base_class;
}; };
GType sugar_preview_get_type (void); GType sugar_preview_get_type (void);
void sugar_preview_take_screenshot (SugarPreview *menu, void sugar_preview_take_screenshot (SugarPreview *preview,
GdkDrawable *drawable); GdkDrawable *drawable);
gboolean sugar_preview_save (SugarPreview *menu, void sugar_preview_set_size (SugarPreview *preview,
const char *file_name); int width,
int height);
GdkPixbuf *sugar_preview_get_pixbuf (SugarPreview *preview);
void sugar_preview_clear (SugarPreview *preview);
G_END_DECLS G_END_DECLS

View File

@ -28,7 +28,8 @@ import common
def _preview_timeout_cb(): def _preview_timeout_cb():
preview = _sugarext.Preview() preview = _sugarext.Preview()
preview.take_screenshot(button.window) preview.take_screenshot(button.window)
preview.save('/home/marco/test.png') preview.get_pixbuf().save('/home/marco/test.png','png')
preview.clear()
test = common.Test() test = common.Test()