[Gtk-sharp-list] pixbuf-demo; This one works

Philip Van Hoof spamfrommailing@freax.org
29 Apr 2003 10:02:07 +0200


--=-DeGtkb1OuKiPGmHAfw4R
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Have fun...

-- 
Philip Van Hoof <me at freax.org>

--=-DeGtkb1OuKiPGmHAfw4R
Content-Disposition: attachment; filename=PixbufExample.cs
Content-Type: text/plain; name=PixbufExample.cs; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

// PixbufExample.cs - port of standard GNOME pixbuf demo 
//
// Author: Philip Van Hoof <me@freax.org>
//
// (c) 2003 Philip Van Hoof

namespace GtkSamples {

	using Gnome;
	using Gtk;
	using Gdk;
	using GtkSharp;
	using System;
	using System.IO;

	public class PixbufExample {
		private int back_width, back_height;
		private Gdk.Pixbuf frame;
		private Gdk.Pixbuf background;
		private Gdk.Pixbuf[] images = new Gdk.Pixbuf[8];

		private uint timeout_id;
		private int frame_num;
		private int n_images=8;
		private Gtk.DrawingArea da;
		private Gtk.Window win;
		
		public PixbufExample () {
			win = new Gtk.Window ("Pixbufs");
			win.DeleteEvent += new DeleteEventHandler (Window_Delete);
			
			this.background = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "background.jpg");
			this.back_width = background.Width;
			this.back_height = background.Height;
  			win.SetSizeRequest (this.back_width, this.back_height);
			
			images[0] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "apple-red.png");
			images[1] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-ccdialog.png");
			images[2] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-gmenu.png");
			images[3] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gtk-sharp-logo.png");
			images[4] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-calendar.png");
			images[5] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-applets.png");
			images[6] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-color-browser.png");
			images[7] = new Gdk.Pixbuf ("pixmaps"+ Path.DirectorySeparatorChar + "gnome-mdi.png");

			this.frame = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, this.back_width, this.back_height);
			da = new Gtk.DrawingArea();
			win.Add(da);
			da.ExposeEvent += new ExposeEventHandler(DrawingArea_Expose);
			
			this.timeout_id = GLib.Timeout.Add(50, new GLib.TimeoutHandler(timeout));
			
			win.ShowAll ();
		}

		bool timeout ()
		{
			double f=0;
			int i=0;
			double xmid=0, ymid=0;
			double radius=0;

			this.background.CopyArea (0, 0, this.back_width, this.back_height, this.frame, 0, 0);
			f = (double) (this.frame_num % 60) / 60;
			xmid = this.back_width / 2.0;
			ymid = this.back_height / 2.0;
			
			radius = System.Math.Min (xmid, ymid) / 2.0;
			for (i = 0; i < this.n_images; i++)
			{
				int a=0;
				double ang=0;
				int xpos=0, ypos=0;
				int iw=0, ih=0;
				double r=0;
				Gdk.Rectangle r1;
				double k=0;

				ang = 2.0 * System.Math.PI * (double) i / this.n_images - f * 2.0 * System.Math.PI;

				iw = images[i].Width;
				ih = images[i].Height;
				r = radius + (radius / 3.0) * System.Math.Sin (f * 2.0 * System.Math.PI);
				
				xpos = (int) System.Math.Floor (xmid + r * System.Math.Cos (ang) - iw / 2.0 + 0.5);
				ypos = (int) System.Math.Floor (ymid + r * System.Math.Sin (ang) - ih / 2.0 + 0.5);

				if (i % 2 == 0){
					k = System.Math.Sin (f * 2.0 * System.Math.PI);
					a = (int) System.Math.Max (127, System.Math.Abs (255 * System.Math.Sin (f * 2.0 * System.Math.PI)));
				} else {
					k = System.Math.Cos (f * 2.0 * System.Math.PI);
					a = (int) System.Math.Max (127, System.Math.Abs (255 * System.Math.Cos (f * 2.0 * System.Math.PI)));
				}

				k = 2.0 * k * k;
				k = System.Math.Max (k, 0.25);

				r1.x = xpos;
				r1.y = ypos;
				r1.width = (int) (iw * k);
				r1.height = (int) (ih * k);

				images[i].Composite(
					frame,
					r1.x, r1.y,
					r1.width, r1.height,
					xpos, ypos,
					k, k,
		 			Gdk.InterpType.Nearest,
					a);

			}

			this.da.QueueDraw();

			frame_num++;
			return true;
		}


		void DrawingArea_Expose(object obj, ExposeEventArgs args)
		{
			Widget widget = (Widget) obj;
			Gdk.EventExpose ev = args.Event;
			Gdk.Rectangle area = ev.area;

			frame.RenderToDrawableAlpha(
					widget.GdkWindow,
					area.x, area.y,
					area.x, area.y,
					area.width, area.height,
					Gdk.PixbufAlphaMode.Full, 8,
					RgbDither.Normal,
					area.x, area.y);
		}

		void Quit (object obj, EventArgs args)
		{
			Application.Quit ();
		}

		void Window_Delete (object obj, DeleteEventArgs args)
		{
			SignalArgs sa = (SignalArgs) args;
			Application.Quit ();
			sa.RetVal = true;
		}

		public static int Main (string[] args)
		{
			Application.Init ();
			PixbufExample example = new PixbufExample ();
			Application.Run ();
			return 0;
		}
	}
}

--=-DeGtkb1OuKiPGmHAfw4R--