3 // Mono.Cairo drawing samples using GTK# as drawing surface
4 // Autor: Jordi Mas <jordi@ximian.com>. Based on work from Owen Taylor
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System
.Reflection
;
32 using System
.Runtime
.InteropServices
;
38 static DrawingArea a
,b
;
43 Gtk
.Window w
= new Gtk
.Window ("Mono.Cairo Circles demo");
45 a
= new CairoGraphic ();
47 Box box
= new HBox (true, 0);
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
);
70 gr
.Arc (0, 0, xr
, 0, 2 * M_PI
);
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);
88 xc
+ radius
/ 3 * Math
.Cos (M_PI
* (0.5)),
89 yc
- radius
/ 3 * Math
.Sin (M_PI
* (0.5)),
90 subradius
, subradius
);
94 gr
.SetRGBColor (0, 1, 0);
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
);
102 gr
.SetRGBColor (0, 0, 1);
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
);
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
);
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
);
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
);
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
155 gr
.TargetSurface
= circles
;
158 gr
.Operator
= Operator
.Over
;
159 draw_3circles (gr
, xc
, yc
, radius
);
163 gr
.Operator
= Operator
.Add
;
164 circles
.Show (gr
, width
, height
);
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
);