From eaef1eb0154e0526fe753ccb4be32fd8ef9246aa Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sun, 04 Nov 2007 15:20:54 +0000 Subject: Experimental preview implementation using XShmGetImage. Not hooked up yet. --- (limited to 'lib/sugar') diff --git a/lib/sugar/Makefile.am b/lib/sugar/Makefile.am index 9d3fc34..19cf1ea 100644 --- a/lib/sugar/Makefile.am +++ b/lib/sugar/Makefile.am @@ -32,7 +32,9 @@ _sugarext_la_SOURCES = \ sugar-key-grabber.c \ sugar-key-grabber.h \ sugar-menu.h \ - sugar-menu.c + sugar-menu.c \ + sugar-preview.h \ + sugar-preview.c BUILT_SOURCES = \ _sugarext.c \ diff --git a/lib/sugar/_sugarext.defs b/lib/sugar/_sugarext.defs index be5d185..18a4461 100644 --- a/lib/sugar/_sugarext.defs +++ b/lib/sugar/_sugarext.defs @@ -22,6 +22,13 @@ (gtype-id "SUGAR_TYPE_MENU") ) +(define-object Preview + (in-module "Sugar") + (parent "GObject") + (c-name "SugarPreview") + (gtype-id "SUGAR_TYPE_PREVIEW") +) + (define-object IconEntry (in-module "Sexy") (parent "GtkEntry") @@ -150,3 +157,27 @@ (return-type "none") ) +;; From sugar-preview.h + +(define-function sugar_preview_get_type + (c-name "sugar_preview_get_type") + (return-type "GType") +) + +(define-method take_screenshot + (of-object "SugarPreview") + (c-name "sugar_preview_take_screenshot") + (return-type "none") + (parameters + '("GdkDrawable" "drawable") + ) +) + +(define-method save + (of-object "SugarPreview") + (c-name "sugar_preview_save") + (return-type "gboolean") + (parameters + '("const-char*" "key") + ) +) diff --git a/lib/sugar/_sugarext.override b/lib/sugar/_sugarext.override index fbc859b..61fb815 100644 --- a/lib/sugar/_sugarext.override +++ b/lib/sugar/_sugarext.override @@ -7,6 +7,7 @@ headers #include "sugar-address-entry.h" #include "sugar-key-grabber.h" #include "sugar-menu.h" +#include "sugar-preview.h" #include "sexy-icon-entry.h" #include @@ -20,6 +21,7 @@ import gtk.Entry as PyGtkEntry_Type import gtk.Menu as PyGtkMenu_Type import gtk.Container as PyGtkContainer_Type import gtk.gdk.Window as PyGdkWindow_Type +import gtk.gdk.Drawable as PyGdkDrawable_Type import gtk.Image as PyGtkImage_Type %% ignore-glob diff --git a/lib/sugar/sugar-preview.c b/lib/sugar/sugar-preview.c new file mode 100644 index 0000000..3cf938e --- /dev/null +++ b/lib/sugar/sugar-preview.c @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2007, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include +#include + +#include "sugar-preview.h" + +static void sugar_preview_class_init (SugarPreviewClass *menu_class); +static void sugar_preview_init (SugarPreview *menu); + +G_DEFINE_TYPE(SugarPreview, sugar_preview, G_TYPE_OBJECT) + +gboolean +sugar_preview_save(SugarPreview *preview, const char *file_name) +{ + GdkPixbuf *pixbuf; + + if (preview->image != NULL) { + return FALSE; + } + + pixbuf = gdk_pixbuf_get_from_image(NULL, preview->image, NULL, + 0, 0, 0, 0, + preview->image->width, + preview->image->height); + + gdk_pixbuf_save(pixbuf, file_name, "png", NULL); + + if (preview->image != NULL) { + g_object_unref(G_OBJECT(preview->image)); + preview->image = NULL; + } + + return TRUE; +} + +void +sugar_preview_take_screenshot(SugarPreview *preview, GdkDrawable *drawable) +{ + GdkScreen *screen; + GdkVisual *visual; + GdkColormap *colormap; + gint width, height; + + gdk_drawable_get_size(drawable, &width, &height); + + screen = gdk_drawable_get_screen(drawable); + visual = gdk_drawable_get_visual(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); + gdk_image_set_colormap(preview->image, colormap); + + XShmGetImage(GDK_SCREEN_XDISPLAY(screen), + GDK_DRAWABLE_XID(drawable), + gdk_x11_image_get_ximage(preview->image), + 0, 0, AllPlanes, ZPixmap); +} + +static void +sugar_preview_dispose (GObject *object) +{ + SugarPreview *preview = SUGAR_PREVIEW(object); + + if (preview->image != NULL) { + g_object_unref(G_OBJECT(preview->image)); + preview->image = NULL; + } +} + + +static void +sugar_preview_class_init(SugarPreviewClass *preview_class) +{ + GObjectClass *g_object_class = G_OBJECT_CLASS (preview_class); + + g_object_class->dispose = sugar_preview_dispose; +} + +static void +sugar_preview_init(SugarPreview *preview) +{ + preview->image = NULL; +} diff --git a/lib/sugar/sugar-preview.h b/lib/sugar/sugar-preview.h new file mode 100644 index 0000000..3fe1056 --- /dev/null +++ b/lib/sugar/sugar-preview.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2007, Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __SUGAR_PREVIEW_H__ +#define __SUGAR_PREVIEW_H__ + +#include + +G_BEGIN_DECLS + +typedef struct _SugarPreview SugarPreview; +typedef struct _SugarPreviewClass SugarPreviewClass; + +#define SUGAR_TYPE_PREVIEW (sugar_preview_get_type()) +#define SUGAR_PREVIEW(object) (G_TYPE_CHECK_INSTANCE_CAST((object), SUGAR_TYPE_PREVIEW, SugarPreview)) +#define SUGAR_PREVIEW_CLASS(klass) (G_TYPE_CHACK_CLASS_CAST((klass), SUGAR_TYPE_PREVIEW, SugarPreviewClass)) +#define SUGAR_IS_PREVIEW(object) (G_TYPE_CHECK_INSTANCE_TYPE((object), SUGAR_TYPE_PREVIEW)) +#define SUGAR_IS_PREVIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), SUGAR_TYPE_PREVIEW)) +#define SUGAR_PREVIEW_GET_CLASS(object) (G_TYPE_INSTANCE_GET_CLASS((object), SUGAR_TYPE_PREVIEW, SugarPreviewClass)) + +struct _SugarPreview { + GObject base_instance; + + GdkImage *image; +}; + +struct _SugarPreviewClass { + GObjectClass base_class; +}; + +GType sugar_preview_get_type (void); +void sugar_preview_take_screenshot (SugarPreview *menu, + GdkDrawable *drawable); +gboolean sugar_preview_save (SugarPreview *menu, + const char *file_name); + +G_END_DECLS + +#endif /* __SUGAR_PREVIEW_H__ */ -- cgit v0.9.1