(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Cairo / Samples / gtk / circles.cs
blobea6051472ed9262928aed5e3fab3a34330cd929c
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 //
7 //
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System;
31 using System.Reflection;
32 using System.Runtime.InteropServices;
33 using Cairo;
34 using Gtk;
36 class GtkCairo
38 static DrawingArea a,b;
40 static void Main ()
42 Application.Init ();
43 Gtk.Window w = new Gtk.Window ("Mono.Cairo Circles demo");
45 a = new CairoGraphic ();
47 Box box = new HBox (true, 0);
48 box.Add (a);
49 w.Add (box);
50 w.Resize (500,500);
51 w.ShowAll ();
53 Application.Run ();
59 class CairoGraphic : DrawingArea
61 static readonly double M_PI = 3.14159265358979323846;
63 static void oval_path (Cairo.Graphics gr, double xc, double yc, double xr, double yr)
65 Matrix matrix = gr.Matrix;
67 gr.Translate (xc, yc);
68 gr.Scale (1.0, yr / xr);
69 gr.MoveTo (xr, 0.0);
70 gr.Arc (0, 0, xr, 0, 2 * M_PI);
71 gr.ClosePath ();
73 gr.Matrix = matrix;
74 matrix.Destroy();
77 /*
78 * Draw a red, green, and blue circle equally spaced inside
79 * the larger circle of radius r at (xc, yc)
81 static void draw_3circles (Cairo.Graphics gr, double xc, double yc, double radius)
83 double subradius = radius * (2 / 3.0 - 0.1);
85 gr.SetRGBColor (1, 0, 0);
87 oval_path (gr,
88 xc + radius / 3 * Math.Cos (M_PI * (0.5)),
89 yc - radius / 3 * Math.Sin (M_PI * (0.5)),
90 subradius, subradius);
92 gr.Fill ();
94 gr.SetRGBColor (0, 1, 0);
95 oval_path (gr,
96 xc + radius / 3 * Math.Cos (M_PI * (0.5 + 2/.3)),
97 yc - radius / 3 * Math.Sin (M_PI * (0.5 + 2/.3)),
98 subradius, subradius);
100 gr.Fill ();
102 gr.SetRGBColor (0, 0, 1);
104 oval_path (gr,
105 xc + radius / 3 * Math.Cos (M_PI * (0.5 + 4/.3)),
106 yc - radius / 3 * Math.Sin (M_PI * (0.5 + 4/.3)),
107 subradius, subradius);
109 gr.Fill ();
112 static void draw (Cairo.Graphics gr, int width, int height)
114 Surface overlay, punch, circles;
116 /* Fill the background */
117 double radius = 0.5 * (width < height ? width : height) - 10;
118 double xc = width / 2;
119 double yc = height / 2;
121 overlay = Surface.CreateSimilar (gr.TargetSurface, Format.ARGB32, width, height);
122 punch = Surface.CreateSimilar (gr.TargetSurface, Format.A8, width, height);
123 circles = Surface.CreateSimilar (gr.TargetSurface, Format.ARGB32, width, height);
125 gr.Save ();
126 gr.TargetSurface = overlay;
128 /* Draw a black circle on the overlay
131 gr.SetRGBColor (0, 0, 0);
133 oval_path (gr, xc, yc, radius, radius);
134 gr.Fill ();
135 gr.Save ();
136 gr.TargetSurface = punch;
139 /* Draw 3 circles to the punch surface, then cut
140 * that out of the main circle in the overlay
142 draw_3circles (gr, xc, yc, radius);
144 gr.Restore ();
146 gr.Operator = Operator.OutReverse;
147 punch.Show (gr, width, height);
150 /* Now draw the 3 circles in a subgroup again
151 * at half intensity, and use OperatorAdd to join up
152 * without seams.
154 gr.Save ();
155 gr.TargetSurface = circles;
157 gr.Alpha = 0.5;
158 gr.Operator = Operator.Over;
159 draw_3circles (gr, xc, yc, radius);
161 gr.Restore ();
163 gr.Operator = Operator.Add;
164 circles.Show (gr, width, height);
167 gr.Restore ();
169 overlay.Show (gr, width, height);
172 cairo_surface_destroy (overlay);
173 cairo_surface_destroy (punch);
174 cairo_surface_destroy (circles);
179 protected override bool OnExposeEvent (Gdk.EventExpose args)
181 Gdk.Window win = args.Window;
182 Gdk.Rectangle area = args.Area;
184 Cairo.Graphics g = new Cairo.Graphics ();
185 Gdk.Graphics.CreateDrawable (win, g);
187 draw (g, 500, 500);
189 return true;