[Gtk-sharp-list] Porting test-pixbufs

Duncan Mak duncan@ximian.com
11 Oct 2002 15:47:41 -0400


--=-hbaCRyYJlkqaBur+NXRm
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hey guys,

I tried to port test-pixbufs to gtk-sharp. I got the code ported and it
compiles fine, but I get this exception when I try to run it. Anyone got
any ideas why this is happening?

Thanks,

Unhandled Exception: System.Reflection.TargetInvocationException:
Argument cannot be null
Parameter name: null key
in <0x000dc> 00 System.Reflection.MonoMethod:Invoke
(object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
in <0x00109> 00 System.Delegate:DynamicInvokeImpl (object[])
in <0x00053> 00 System.MulticastDelegate:DynamicInvokeImpl (object[])
in <0x00218> 00
GtkSharp.Gtk.boolObjectEventExposeSignal:boolObjectEventExposeCallback
(intptr,Gdk.EventExpose&,int)
in <0x00109> 05
GtkSharp.Gtk.boolObjectEventExposeSignal:boolObjectEventExposeCallback
(intptr,Gdk.EventExpose&,int)
in (unmanaged) 06 Gtk.Application:gtk_main ()
in <0x00004> 06 Gtk.Application:gtk_main ()
in <0x0003b> 00 WidgetViewer.TestPixbuf:Main ()

-- 
Duncan Mak <duncan@ximian.com>

--=-hbaCRyYJlkqaBur+NXRm
Content-Disposition: attachment; filename=pixbufs.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=pixbufs.cs; charset=ISO-8859-1

//
// TestPixbuf.cs
//
// Author: Duncan Mak  (duncan@ximian.com)
//
// Copyright (C) 2002, Duncan Mak, Ximian Inc.
//

using System;

using GLib;
using Gtk;
using Gdk;
using GtkSharp;

namespace WidgetViewer {=09
	public class TestPixbuf
	{
		static Gtk.Window window =3D null;

		// current frame
		static Gdk.Pixbuf frame =3D null;

		// background image
		static Gdk.Pixbuf background;
	=09
		static int width, height;

		static int FRAME_DELAY =3D 50;
		static int CYCLE_LEN =3D 60;

		// names of the images
		static string [] image_names =3D {
			"apple-red.png", "gnome-applets.png",
			"gnome-calendar.png", "gnome-foot.png",
			"gnome-gmush.png", "gnome-gimp.png",
			"gnome-gsame.png", "gnu-keys.png"
		};

		// images
		static Gdk.Pixbuf [] images =3D new Gdk.Pixbuf [image_names.Length];

		static Gtk.DrawingArea da;

		static uint timeout_id;

		static void Main ()
		{
			Application.Init ();
		=09
			window =3D Create ();
			window.ShowAll ();

			Application.Run ();
		}
	=09
		public static Gtk.Window Create ()
		{
			Gtk.MessageDialog dialog =3D null;
		=09
			try {
				window =3D new Gtk.Window ("Pixbufs");
				window.Resizable =3D false;
				window.DestroyEvent +=3D new DestroyEventHandler (Cleanup);

				LoadPixbuf ();

				window.SetSizeRequest (width, height);
				frame =3D new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, width, height);

				da =3D new Gtk.DrawingArea ();
				da.ExposeEvent +=3D new ExposeEventHandler (OnDaExposed);

				window.Add (da);
				timeout_id =3D GLib.Timeout.Add ((uint) FRAME_DELAY, new TimeoutHandler=
 (OnTimeout));

				return window;
			=09
			} catch (Exception e) {
				dialog =3D new Gtk.MessageDialog (window, DialogFlags.DestroyWithParent=
,
								MessageType.Error, ButtonsType.Close,
								String.Format ("Failed to load image: {0}", e.Message));

				return dialog;
			}=20
		}

		static void LoadPixbuf ()
		{
			if (background !=3D null)
				return;

			background =3D new Gdk.Pixbuf ("background.jpg");
			width =3D background.Width;
			height =3D background.Height;

			int i =3D 0;
			foreach (string name in image_names) {
				try {
					images [i] =3D new Gdk.Pixbuf (name);
					i ++;
				} catch (Exception e) {
					throw new Exception (name);
				}
			}
		}
	=09
		static void OnDaExposed (object sender, ExposeEventArgs a)
		{
			int rowstride =3D frame.Rowstride;
			byte pixels =3D Convert.ToByte (frame.Pixels + rowstride * a.Event.area.=
y + a.Event.area.x * 3);

			((Gtk.Widget) sender).GdkWindow.DrawRgbImageDithalign (da.Style.BlackGC,
									       a.Event.area.x, a.Event.area.y,
									       a.Event.area.width, a.Event.area.height,
									       RgbDither.Normal, pixels, rowstride,
									       a.Event.area.x, a.Event.area.y);
			a.RetVal =3D true;
		}

		static void Cleanup (object sender, DestroyEventArgs a)
		{
			Source.Remove (timeout_id);
			timeout_id =3D 0;

			a.RetVal =3D true;
		}

		static bool OnTimeout ()
		{
			background.CopyArea (0, 0, width, height, frame, 0, 0);

			int frame_num =3D 0;
		=09
			double f =3D (double) (frame_num % CYCLE_LEN) / (CYCLE_LEN);

			double xmid =3D width / 2.0;
			double ymid =3D height / 2.0;
			double radius =3D Math.Min (xmid, ymid) / 2.0;

			for (int i =3D 0; i < images.Length; i ++) {

				double ang =3D 2.0 * Math.PI * (double) i / images.Length - f * 2.0 * M=
ath.PI;
			=09
				int iw =3D images [i].Width;
				int ih =3D images [i].Height;

				double r =3D radius + (radius / 3.0) * Math.Sin (f * 2.0 * Math.PI);

				double xpos =3D Math.Floor (xmid + r * Math.Cos (ang) - iw / 2.0 + 0.5)=
;
				double ypos =3D Math.Floor (ymid + r * Math.Sin (ang) - iw / 2.0 + 0.5)=
;

				double k =3D (Convert.ToBoolean (i) & 1 !=3D 0) ? Math.Sin (f * 2.0 * M=
ath.PI) : Math.Cos (f * 2.0 * Math.PI);
				k =3D 2.0 * k * k;
				k =3D Math.Max (0.25, k);

				Gdk.Rectangle r1 =3D Gdk.Rectangle.New (IntPtr.Zero);
				r1.x =3D (int) xpos;
				r1.y =3D (int) ypos;
				r1.width =3D (int) (iw * k);
				r1.height =3D (int) (ih * k);

				Gdk.Rectangle r2 =3D Gdk.Rectangle.New (IntPtr.Zero);
				r2.x =3D 0;
				r2.y =3D 0;
				r2.width =3D width;
				r2.height =3D height;

				Gdk.Rectangle dest =3D Gdk.Rectangle.New (IntPtr.Zero);

				int alpha =3D 0;

				if (Convert.ToBoolean (i) & 1 !=3D 0)
					alpha =3D Math.Max (127, (int) Math.Abs (255 * Math.Sin (f * 2.0 * Mat=
h.PI)));
				else
					alpha =3D Math.Max (127, (int) Math.Abs (255 * Math.Cos (f * 2.0 * Mat=
h.PI)));

				if (r1.Intersect (r2, dest))
					images [i].Composite (frame,
							      dest.x, dest.y,
							      dest.width, dest.height,
							      xpos, ypos,
							      k, k,
							      InterpType.Nearest,
							      alpha);
			}

			((Gtk.Widget) da).QueueDraw ();
		=09
			frame_num ++;
			return true;
		}
	}
}

--=-hbaCRyYJlkqaBur+NXRm--