From 977c36bd98b0f9c32a15fb7acfae17d2a8ed6a37 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Fri, 28 May 2004 19:19:04 +0000 Subject: [PATCH] Add samples that use System.Drawing svn path=/trunk/gtk-sharp/; revision=28384 --- sample/DrawingSample.cs | 81 +++++++++++++++++++++++++++++++++ sample/DrawingSample.exe.config | 6 +++ sample/sysdraw.cs | 52 +++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 sample/DrawingSample.cs create mode 100644 sample/DrawingSample.exe.config create mode 100644 sample/sysdraw.cs diff --git a/sample/DrawingSample.cs b/sample/DrawingSample.cs new file mode 100644 index 000000000..f721bbcbd --- /dev/null +++ b/sample/DrawingSample.cs @@ -0,0 +1,81 @@ +// +// Sample program demostrating using Cairo with Gtk# +// +using Gtk; +using System; +using System.Drawing; + +class X { + static DrawingArea a, b; + + static void Main () + { + Application.Init (); + Gtk.Window w = new Gtk.Window ("Hello"); + + // Custom widget sample + a = new PrettyGraphic (); + + // Event-based drawing + b = new DrawingArea (); + b.ExposeEvent += ExposeHandler; + b.SizeAllocated += SizeAllocatedHandler; + + Box box = new HBox (true, 0); + box.Add (a); + //box.Add (b); + w.Add (box); + + w.ShowAll (); + Application.Run (); + } + + static Gdk.Rectangle rect; + + static void SizeAllocatedHandler (object obj, SizeAllocatedArgs args) + { + rect = args.Allocation; + } + + static void ExposeHandler (object obj, ExposeEventArgs args) + { + Gdk.EventExpose ev = args.Event; + Gdk.Window window = ev.Window; + + using (Graphics g = Gdk.Graphics.FromDrawable (window)){ + Console.WriteLine ("{0} and {1}", -ev.Area.X, -ev.Area.Y); + g.TranslateTransform (ev.Area.X, ev.Area.Y); + using (Pen p = new Pen (Color.Red)){ + g.DrawPie (p, 0, 0, rect.Width, rect.Height, 50, 90); + } + } + } +} + +// +// A sample using inheritance to draw +// +class PrettyGraphic : DrawingArea { + + protected override bool OnExposeEvent (Gdk.EventExpose args) + { + Gdk.Window win = args.Window; + Gdk.Rectangle area = args.Area; + + using (Graphics g = Gdk.Graphics.FromDrawable (args.Window)){ + //Console.WriteLine ("{0} and {1}", -args.Area.X, -args.Area.Y); + //g.TranslateTransform (-args.Area.X, -args.Area.Y); + Pen p = new Pen (Color.Blue, 1.0f); + Pen q = new Pen (Color.Red, 1.0f); + + g.DrawLine (p, 0, 0, 100, 100); + g.DrawLine (q, 0, 0, 100, 100); + return true; + + for (int i = 0; i < 600; i += 60) + for (int j = 0; j < 600; j += 60) + g.DrawLine (p, i, 0, 0, j); + } + return true; + } +} diff --git a/sample/DrawingSample.exe.config b/sample/DrawingSample.exe.config new file mode 100644 index 000000000..3b2b4ff6a --- /dev/null +++ b/sample/DrawingSample.exe.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/sample/sysdraw.cs b/sample/sysdraw.cs new file mode 100644 index 000000000..a048a7308 --- /dev/null +++ b/sample/sysdraw.cs @@ -0,0 +1,52 @@ +// +// System.Drawing integration with Gtk# +// +// Miguel de Icaza +// +// API issues: +// Maybe make the translation `out' parameters so they are explicit and the user knows about it? +// Add a way to copy a Graphics into a drawable? +// + +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace Gdk { + public class Graphics { + + [DllImport("libgdk-win32-2.0-0.dll")] + internal static extern IntPtr gdk_x11_drawable_get_xdisplay (IntPtr raw); + + [DllImport("libgdk-win32-2.0-0.dll")] + internal static extern IntPtr gdk_x11_drawable_get_xid (IntPtr raw); + + public static System.Drawing.Graphics FromDrawable (Gdk.Drawable drawable) + { + IntPtr x_drawable; + int x_off = 0, y_off = 0; + + + if (drawable is Gdk.Window){ + ((Gdk.Window) drawable).GetInternalPaintInfo(out drawable, out x_off, out y_off); + } + x_drawable = drawable.Handle; + + IntPtr display = gdk_x11_drawable_get_xdisplay (x_drawable); + + Type graphics = typeof (System.Drawing.Graphics); + MethodInfo mi = graphics.GetMethod ("FromXDrawable", BindingFlags.Static | BindingFlags.NonPublic); + if (mi == null) + throw new NotImplementedException ("In this implementation I can not get a graphics from a drawable"); + object [] args = new object [2] { (IntPtr) gdk_x11_drawable_get_xid (drawable.Handle), (IntPtr) display }; + object r = mi.Invoke (null, args); + System.Drawing.Graphics g = (System.Drawing.Graphics) r; + + Console.WriteLine ("-> {0} / {1}", x_off, y_off); + g.TranslateTransform (-x_off, -y_off); + + return g; + } + } + +}