[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / Mono.Cairo / Samples / gtk / circles.cs
blob5bb96c266c798601bbe30f027eb8345d73fde7f9
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 //
8 //
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:
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.
31 using System;
32 using Cairo;
33 using Gtk;
35 public class GtkCairo
37 static DrawingArea a;
39 static void Main ()
41 Application.Init ();
42 Gtk.Window w = new Gtk.Window ("Mono.Cairo Circles demo");
44 a = new CairoGraphic ();
46 Box box = new HBox (true, 0);
47 box.Add (a);
48 w.Add (box);
49 w.Resize (500,500);
50 w.ShowAll ();
52 Application.Run ();
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);
68 gr.ClosePath ();
71 /*
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);
81 oval_path (gr,
82 xc + radius / 3 * Math.Cos (M_PI * (0.5)),
83 yc - radius / 3 * Math.Sin (M_PI * (0.5)),
84 subradius, subradius);
86 gr.Fill ();
88 gr.Color = new Color (0, 1, 0, 0.5);
89 oval_path (gr,
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);
94 gr.Fill ();
96 gr.Color = new Color (0, 0, 1, 0.5);
98 oval_path (gr,
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);
103 gr.Fill ();
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);
120 gr.Save ();
121 gr.Target = overlay;
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);
129 gr.Fill ();
130 gr.Save ();
131 gr.Target = punch;
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);
139 gr.Restore ();
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
147 * without seams.
149 gr.Save ();
150 gr.Target = circles;
152 //gr.Alpha = 0.5;
153 gr.Operator = Operator.Over;
154 draw_3circles (gr, xc, yc, radius);
156 gr.Restore ();
158 gr.Operator = Operator.Add;
159 circles.Show (gr, width, height);
162 gr.Restore ();
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);
174 int x, y, w, h, d;
175 win.GetGeometry(out x, out y, out w, out h, out d);
177 draw (g, w, h);
178 return true;