1 using System;
2 using Gtk;
3 using Pango;
4 
5 #if GTK_SHARP_2_6
6 namespace GtkDemo
7 {
8 	[Demo ("Rotated Text", "DemoRotatedText.cs")]
9 	public class DemoRotatedText : Window
10 	{
11 		const int RADIUS = 150;
12 		const int N_WORDS = 10;
13 
DemoRotatedText()14 		public DemoRotatedText () : base ("Rotated text")
15 		{
16 			DrawingArea drawingArea = new DrawingArea ();
17 			Gdk.Color white = new Gdk.Color (0xff, 0xff, 0xff);
18 
19 			// This overrides the background color from the theme
20 			drawingArea.ModifyBg (StateType.Normal, white);
21 			drawingArea.ExposeEvent += new ExposeEventHandler (RotatedTextExposeEvent);
22 
23 			this.Add (drawingArea);
24 			this.DeleteEvent += new DeleteEventHandler (OnWinDelete);
25 			this.SetDefaultSize (2 * RADIUS, 2 * RADIUS);
26 			this.ShowAll ();
27 		}
28 
RotatedTextExposeEvent(object sender, ExposeEventArgs a)29 		void RotatedTextExposeEvent (object sender, ExposeEventArgs a)
30 		{
31 			DrawingArea drawingArea = sender as DrawingArea;
32 
33 			int width = drawingArea.Allocation.Width;
34 			int height = drawingArea.Allocation.Height;
35 
36 			double deviceRadius;
37 
38 			// Get the default renderer for the screen, and set it up for drawing
39 			Gdk.PangoRenderer renderer = Gdk.PangoRenderer.GetDefault (drawingArea.Screen);
40 			renderer.Drawable = drawingArea.GdkWindow;
41 			renderer.Gc = drawingArea.Style.BlackGC;
42 
43 			// Set up a transformation matrix so that the user space coordinates for
44 			// the centered square where we draw are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
45 			// We first center, then change the scale
46 			deviceRadius = Math.Min (width, height) / 2;
47 			Matrix matrix = Pango.Matrix.Identity;
48 			matrix.Translate (deviceRadius + (width - 2 * deviceRadius) / 2, deviceRadius + (height - 2 * deviceRadius) / 2);
49 			matrix.Scale (deviceRadius / RADIUS, deviceRadius / RADIUS);
50 
51 			// Create a PangoLayout, set the font and text
52 			Context context = drawingArea.CreatePangoContext ();
53 			Pango.Layout layout = new Pango.Layout (context);
54 			layout.SetText ("Text");
55 			FontDescription desc = FontDescription.FromString ("Sans Bold 27");
56 			layout.FontDescription = desc;
57 
58 			// Draw the layout N_WORDS times in a circle
59 			for (int i = 0; i < N_WORDS; i++)
60 			{
61 				Gdk.Color color = new Gdk.Color ();
62 				Matrix rotatedMatrix = matrix;
63 				int w, h;
64 				double angle = (360 * i) / N_WORDS;
65 
66 				// Gradient from red at angle == 60 to blue at angle == 300
67 				color.Red = (ushort) (65535 * (1 + Math.Cos ((angle - 60) * Math.PI / 180)) / 2);
68 				color.Green = 0;
69 				color.Blue = (ushort) (65535 - color.Red);
70 
71 				renderer.SetOverrideColor (RenderPart.Foreground, color);
72 
73 				rotatedMatrix.Rotate (angle);
74 				context.Matrix = rotatedMatrix;
75 
76 				// Inform Pango to re-layout the text with the new transformation matrix
77 				layout.ContextChanged ();
78 				layout.GetSize (out w, out h);
79 				renderer.DrawLayout (layout, - w / 2, (int) (- RADIUS * Pango.Scale.PangoScale));
80 			}
81 
82 			// Clean up default renderer, since it is shared
83 			renderer.SetOverrideColor (RenderPart.Foreground, Gdk.Color.Zero);
84 			renderer.Drawable = null;
85 			renderer.Gc = null;
86 		}
87 
OnWinDelete(object sender, DeleteEventArgs a)88 		void OnWinDelete (object sender, DeleteEventArgs a)
89 		{
90 			this.Hide ();
91 			this.Dispose ();
92 		}
93 	}
94 }
95 #endif
96 
97