1 //
2 //
3 //	Mono.Cairo drawing samples using GTK# as drawing surface
4 //	Autor: Jordi Mas <jordi@ximian.com>. Based on work from Owen Taylor
5 //	       Hisham Mardam Bey <hisham@hisham.cc>
6 //
7 
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 
31 using System;
32 using System.Reflection;
33 using System.Runtime.InteropServices;
34 using Cairo;
35 using Gtk;
36 
37 public class GtkCairo
38 {
39 	static DrawingArea a;
40 
Main()41 	static void Main ()
42 	{
43 		Application.Init ();
44 		Gtk.Window w = new Gtk.Window ("Mono.Cairo Circles demo");
45 
46 		a = new CairoGraphic ();
47 
48 		Box box = new HBox (true, 0);
49 		box.Add (a);
50 		w.Add (box);
51 		w.Resize (500,500);
52 		w.ShowAll ();
53 
54 		Application.Run ();
55 	}
56 
57 
58 }
59 
60 public class CairoGraphic : DrawingArea
61 {
draw(Cairo.Context gr, int width, int height)62 	static void draw (Cairo.Context gr, int width, int height)
63 	{
64 		double x=0.1,  y=0.5;
65 		double x1=0.4, y1=0.9, x2=0.6, y2=0.1, x3=0.9, y3=0.5;
66 
67 
68 		gr.Scale (width, height);
69 		gr.LineWidth = 0.04;
70 
71 		gr.MoveTo ( new PointD (x, y) );
72 
73 		gr.CurveTo ( new PointD (x1, y1),
74 			     new PointD (x2, y2),
75 			     new PointD (x3, y3)
76 			     );
77 
78 		gr.Stroke ();
79 
80 		gr.Color = new Color (1, 0.2, 0.2, 0.6);
81 		gr.LineWidth = 0.03;
82 		gr.MoveTo ( new PointD (x, y) );
83 		gr.LineTo ( new PointD (x1, y1) );
84 		gr.MoveTo ( new PointD (x2, y2) );
85 		gr.LineTo ( new PointD (x3, y3) );
86 		gr.Stroke ();
87 	}
88 
OnExposeEvent(Gdk.EventExpose args)89 	protected override bool OnExposeEvent (Gdk.EventExpose args)
90 	{
91 		Gdk.Window win = args.Window;
92 		//Gdk.Rectangle area = args.Area;
93 
94 		Cairo.Context g = Gdk.Context.CreateDrawable (win);
95 
96 		int x, y, w, h, d;
97 		win.GetGeometry(out x, out y, out w, out h, out d);
98 
99 		draw (g, w, h);
100 		return true;
101 	}
102 
103 }
104 
105