[Goodies-commits] r2164 - ristretto/trunk/src

Stephan Arts stephan at xfce.org
Fri Nov 10 12:49:05 CET 2006


Author: stephan
Date: 2006-11-10 11:49:04 +0000 (Fri, 10 Nov 2006)
New Revision: 2164

Added:
   ristretto/trunk/src/picture_viewer.c
   ristretto/trunk/src/picture_viewer.h
Modified:
   ristretto/trunk/src/Makefile.am
   ristretto/trunk/src/main.c
Log:
Added picture-viewer viewport



Modified: ristretto/trunk/src/Makefile.am
===================================================================
--- ristretto/trunk/src/Makefile.am	2006-11-09 22:22:18 UTC (rev 2163)
+++ ristretto/trunk/src/Makefile.am	2006-11-10 11:49:04 UTC (rev 2164)
@@ -1,7 +1,8 @@
 bin_PROGRAMS = ristretto
 
 ristretto_SOURCES = \
-	main.c
+	main.c \
+	picture_viewer.c picture_viewer.h
 
 ristretto_CFLAGS = \
 	@GTK_CFLAGS@ \

Modified: ristretto/trunk/src/main.c
===================================================================
--- ristretto/trunk/src/main.c	2006-11-09 22:22:18 UTC (rev 2163)
+++ ristretto/trunk/src/main.c	2006-11-10 11:49:04 UTC (rev 2164)
@@ -1,5 +1,26 @@
+/*
+ *  Copyright (c) 2006 Stephan Arts <stephan at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <config.h>
 #include <gtk/gtk.h>
 
+#include "picture_viewer.h"
+
 int main(int argc, char **argv)
 {
 	gtk_init(&argc, &argv);
@@ -8,6 +29,21 @@
 
 	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
 
+	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("test.png", NULL);
+
+	GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
+
+	GtkWidget *viewer = rstto_picture_viewer_new();
+
+	gtk_container_add(GTK_CONTAINER(scrolledwindow), viewer);
+	gtk_container_add(GTK_CONTAINER(window), scrolledwindow);
+
+	gtk_widget_show_all(window);
+	gtk_widget_show_all(viewer);
+	gtk_widget_set_size_request(window, 400,300);
+
+	rstto_picture_viewer_set_pixbuf(RSTTO_PICTURE_VIEWER(viewer), pixbuf);
+
 	gtk_main();
 	return 0;
 }

Added: ristretto/trunk/src/picture_viewer.c
===================================================================
--- ristretto/trunk/src/picture_viewer.c	                        (rev 0)
+++ ristretto/trunk/src/picture_viewer.c	2006-11-10 11:49:04 UTC (rev 2164)
@@ -0,0 +1,139 @@
+/*
+ *  Copyright (c) 2006 Stephan Arts <stephan at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/**TODO:
+ *
+ * Fix viewport-dimension > picture-dimension ratio
+ */
+
+#include <config.h>
+#include <gtk/gtk.h>
+
+#include "picture_viewer.h"
+
+static void
+rstto_picture_viewer_init(RsttoPictureViewer *);
+static void
+rstto_picture_viewer_class_init(RsttoPictureViewerClass *);
+
+static void
+rstto_picture_viewer_size_alloc(GtkWidget *widget, GtkAllocation*);
+
+static GtkViewportClass *rstto_picture_viewer_parent_class = NULL;
+
+GType
+rstto_picture_viewer_get_type ()
+{
+	static GType rstto_picture_viewer_type = 0;
+
+	if (!rstto_picture_viewer_type)
+	{
+		static const GTypeInfo rstto_picture_viewer_info = 
+		{
+			sizeof (RsttoPictureViewerClass),
+			(GBaseInitFunc) NULL,
+			(GBaseFinalizeFunc) NULL,
+			(GClassInitFunc) rstto_picture_viewer_class_init,
+			(GClassFinalizeFunc) NULL,
+			NULL,
+			sizeof (RsttoPictureViewer),
+			0,
+			(GInstanceInitFunc) rstto_picture_viewer_init,
+			NULL
+		};
+
+		rstto_picture_viewer_type = g_type_register_static (GTK_TYPE_VIEWPORT, "RsttoPictureViewer", &rstto_picture_viewer_info, 0);
+	}
+	return rstto_picture_viewer_type;
+}
+
+static void
+rstto_picture_viewer_init(RsttoPictureViewer *viewer)
+{
+	viewer->scale = 100;
+
+	viewer->image = gtk_image_new();
+
+	gtk_container_add(GTK_CONTAINER(viewer), viewer->image);
+
+	gtk_widget_show(viewer->image);
+}
+
+static void
+rstto_picture_viewer_class_init(RsttoPictureViewerClass *viewer_class)
+{
+	GtkWidgetClass *object_class = GTK_WIDGET_CLASS(viewer_class);
+
+	rstto_picture_viewer_parent_class = &viewer_class->parent_class;
+
+	object_class->size_allocate = rstto_picture_viewer_size_alloc;
+}
+
+static void
+rstto_picture_viewer_size_alloc(GtkWidget *widget, GtkAllocation *alloc)
+{
+	if(RSTTO_PICTURE_VIEWER(widget)->src_pixbuf)
+	{
+		RsttoPictureViewer *viewer = RSTTO_PICTURE_VIEWER(widget);
+		GtkViewport *viewport = GTK_VIEWPORT(widget);
+
+		viewport->hadjustment->page_size = alloc->width;
+		viewport->vadjustment->page_size = alloc->height;
+		viewport->hadjustment->upper = gdk_pixbuf_get_width(viewer->src_pixbuf)*viewer->scale/100;
+		viewport->vadjustment->upper = gdk_pixbuf_get_height(viewer->src_pixbuf)*viewer->scale/100;
+		viewport->hadjustment->lower = 0;
+		viewport->vadjustment->lower = 0;
+
+		if((viewport->vadjustment->value + viewport->vadjustment->page_size) > viewport->vadjustment->upper)
+		{
+			viewport->vadjustment->value = viewport->vadjustment->upper - viewport->vadjustment->page_size;
+			gtk_adjustment_value_changed(viewport->vadjustment);
+		}
+
+		if((viewport->hadjustment->value + viewport->hadjustment->page_size) > viewport->hadjustment->upper)
+		{
+			viewport->hadjustment->value = viewport->hadjustment->upper - viewport->hadjustment->page_size;
+			gtk_adjustment_value_changed(viewport->hadjustment);
+		}
+		gtk_adjustment_changed(viewport->hadjustment);
+		gtk_adjustment_changed(viewport->vadjustment);
+	}
+	gtk_widget_size_allocate(GTK_BIN(widget)->child, alloc);
+}
+
+
+void
+rstto_picture_viewer_set_pixbuf(RsttoPictureViewer *viewer, GdkPixbuf *pixbuf)
+{
+	viewer->src_pixbuf = pixbuf;
+
+	gtk_image_set_from_pixbuf(GTK_IMAGE(viewer->image), pixbuf);
+
+	gtk_widget_queue_resize(GTK_WIDGET(viewer));
+}
+
+
+GtkWidget *
+rstto_picture_viewer_new()
+{
+	GtkWidget *widget;
+
+	widget = g_object_new(RSTTO_TYPE_PICTURE_VIEWER, "hadjustment", NULL, "vadjustment", NULL, NULL);
+
+	return widget;
+}

Added: ristretto/trunk/src/picture_viewer.h
===================================================================
--- ristretto/trunk/src/picture_viewer.h	                        (rev 0)
+++ ristretto/trunk/src/picture_viewer.h	2006-11-10 11:49:04 UTC (rev 2164)
@@ -0,0 +1,70 @@
+/*
+ *  Copyright (c) 2006 Stephan Arts <stephan at xfce.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __RISTRETTO_PICTURE_VIEWER__
+#define __RISTRETTO_PICTURE_VIEWER__
+
+G_BEGIN_DECLS
+
+#define RSTTO_TYPE_PICTURE_VIEWER rstto_picture_viewer_get_type()
+
+#define RSTTO_PICTURE_VIEWER(obj)( \
+        G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+				RSTTO_TYPE_PICTURE_VIEWER, \
+				RsttoPictureViewer))
+
+#define RSTTO_IS_PICTURE_VIEWER(obj)( \
+        G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+				RSTTO_TYPE_PICTURE_VIEWER))
+
+#define RSTTO_PICTURE_VIEWER_CLASS(klass)( \
+        G_TYPE_CHECK_CLASS_CAST ((klass), \
+				RSTTO_TYPE_PICTURE_VIEWER, \
+				RsttoPictureViewerClass))
+
+#define RSTTO_IS_PICTURE_VIEWER_CLASS(klass)( \
+        G_TYPE_CHECK_CLASS_TYPE ((klass), \
+				RSTTO_TYPE_PICTURE_VIEWER()))
+
+typedef struct _RsttoPictureViewer RsttoPictureViewer;
+
+struct _RsttoPictureViewer
+{
+	GtkViewport        parent;
+	GtkWidget         *image;
+	GdkPixbuf         *src_pixbuf;
+	GdkPixbuf         *dst_pixbuf; /* The pixbuf which ends up on screen */
+	gint               scale;
+};
+
+typedef struct _RsttoPictureViewerClass RsttoPictureViewerClass;
+
+struct _RsttoPictureViewerClass
+{
+	GtkViewportClass parent_class;
+};
+
+GType      rstto_picture_viewer_get_type();
+
+GtkWidget *rstto_picture_viewer_new();
+void       rstto_picture_viewer_set_pixbuf(RsttoPictureViewer *viewer, GdkPixbuf *);
+
+
+G_END_DECLS
+
+#endif /* __RISTRETTO_PICTURE_VIEWER__ */




More information about the Goodies-commits mailing list