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>
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
42 Gtk
.Window w
= new Gtk
.Window ("Mono.Cairo Circles demo");
44 a
= new CairoGraphic ();
46 Box box
= new HBox (true, 0);
58 public class CairoGraphic
: DrawingArea
60 static readonly double M_PI
= 3.14159265358979323846;
62 static void oval_path (Cairo
.Context gr
, double xc
, double yc
, double xr
, double yr
)
64 gr
.Translate (xc
, yc
);
65 gr
.Scale (1.0, yr
/ xr
);
66 gr
.MoveTo (new PointD (xr
, 0.0) );
67 gr
.Arc (0, 0, xr
, 0, 2 * M_PI
);
72 * Draw a red, green, and blue circle equally spaced inside
73 * the larger circle of radius r at (xc, yc)
75 static void draw_3circles (Cairo
.Context gr
, double xc
, double yc
, double radius
)
77 double subradius
= radius
* (2 / 3.0 - 0.1);
79 gr
.Color
= new Color (1, 0, 0, 0.5);
82 xc
+ radius
/ 3 * Math
.Cos (M_PI
* (0.5)),
83 yc
- radius
/ 3 * Math
.Sin (M_PI
* (0.5)),
84 subradius
, subradius
);
88 gr
.Color
= new Color (0, 1, 0, 0.5);
90 xc
+ radius
/ 3 * Math
.Cos (M_PI
* (0.5 + 2/.3)),
91 yc
- radius
/ 3 * Math
.Sin (M_PI
* (0.5 + 2/.3)),
92 subradius
, subradius
);
96 gr
.Color
= new Color (0, 0, 1, 0.5);
99 xc
+ radius
/ 3 * Math
.Cos (M_PI
* (0.5 + 4/.3)),
100 yc
- radius
/ 3 * Math
.Sin (M_PI
* (0.5 + 4/.3)),
101 subradius
, subradius
);
106 static void draw (Cairo
.Context gr
, int width
, int height
)
108 Surface overlay
, punch
, circles
;
110 /* Fill the background */
111 double radius
= 0.5 * (width
< height
? width
: height
) - 10;
112 double xc
= width
/ 2;
113 double yc
= height
/ 2;
115 Surface target
= gr
.Target
;
116 overlay
= target
.CreateSimilar (Content
.ColorAlpha
, width
, height
);
117 punch
= target
.CreateSimilar (Content
.Alpha
, width
, height
);
118 circles
= target
.CreateSimilar (Content
.ColorAlpha
, width
, height
);
123 /* Draw a black circle on the overlay
126 gr
.Color
= new Color (0, 0, 0, 1);
128 oval_path (gr
, xc
, yc
, radius
, radius
);
134 /* Draw 3 circles to the punch surface, then cut
135 * that out of the main circle in the overlay
137 draw_3circles (gr
, xc
, yc
, radius
);
141 gr
.Operator
= Operator
.DestOut
;
142 punch
.Show (gr
, width
, height
);
145 /* Now draw the 3 circles in a subgroup again
146 * at half intensity, and use OperatorAdd to join up
153 gr
.Operator
= Operator
.Over
;
154 draw_3circles (gr
, xc
, yc
, radius
);
158 gr
.Operator
= Operator
.Add
;
159 circles
.Show (gr
, width
, height
);
164 overlay
.Show (gr
, width
, height
);
167 protected override bool OnExposeEvent (Gdk
.EventExpose args
)
169 Gdk
.Window win
= args
.Window
;
170 //Gdk.Rectangle area = args.Area;
172 Cairo
.Context g
= Gdk
.Context
.CreateDrawable (win
);
175 win
.GetGeometry(out x
, out y
, out w
, out h
, out d
);