File: ControlOverlay.cs

package info (click to toggle)
f-spot 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,784 kB
  • ctags: 16,078
  • sloc: cs: 108,718; sh: 17,053; xml: 13,852; ansic: 3,187; makefile: 2,324
file content (309 lines) | stat: -rw-r--r-- 6,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * ControlOverlay.cs
 *
 * Copyright 2007 Novell Inc.
 *
 * Author
 *   Larry Ewing <lewing@novell.com>
 *
 * See COPYING for license information.
 */
using Cairo;

using System;
using Gtk;
using FSpot.Widgets;
using FSpot.Utils;

namespace FSpot {
	public class ControlOverlay : Window {
		Widget host;
		Window host_toplevel;
		bool composited;
		VisibilityType visibility;
		double target_opacity;
		int round = 12;
		Delay hide; 
		Delay fade;
		Delay dismiss;
		bool auto_hide = true;
		double x_align = 0.5;
		double y_align = 1.0;
		
		public enum VisibilityType
		{
			None,
			Partial,
			Full
		}
		
		public double XAlign {
			get { return x_align; }
			set {
				x_align = value;
				Relocate ();
			}
		}

		public double YAlign {
			get { return  y_align; }
			set {
				y_align = value;
				Relocate ();
			}
		}
			      
		
		public bool AutoHide {
			get { return auto_hide; }
			set { auto_hide = value; }
		}

		public VisibilityType Visibility {
			get { return visibility; }
			set {
				if (dismiss.IsPending && value != VisibilityType.None)
					return;

				switch (value) {
				case VisibilityType.None:
					FadeToTarget (0.0);
					break;
				case VisibilityType.Partial:
					FadeToTarget (0.4);
					break;
				case VisibilityType.Full:
					FadeToTarget (0.8);
					break;
				}
				visibility = value;
			}

		}

		public ControlOverlay (Gtk.Widget host) : base (WindowType.Popup)
		{
			this.host = host;
			Decorated = false;
			DestroyWithParent = true;
			Name = "FullscreenContainer";
			AllowGrow = true;
			//AllowShrink = true;
			KeepAbove = true;
			
			host_toplevel = (Gtk.Window) host.Toplevel;
			
			TransientFor = host_toplevel;

			host_toplevel.ConfigureEvent += HandleHostConfigure;
			host_toplevel.SizeAllocated += HandleHostSizeAllocated;
			
			AddEvents ((int) (Gdk.EventMask.PointerMotionMask));
			hide = new Delay (2000, HideControls);
			fade = new Delay (40, FadeToTarget);
			dismiss = new Delay (2000, delegate { /* do nothing */ return false; });
		}

		protected override void OnDestroyed ()
		{
			hide.Stop ();
			fade.Stop ();
			base.OnDestroyed ();
		}

		public bool HideControls ()
		{
			int x, y;
			Gdk.ModifierType type;
			
			if (!auto_hide)
				return false;

			if (IsRealized) {
				GdkWindow.GetPointer (out x, out y, out type);
				if (Allocation.Contains (x, y)) {
					hide.Start ();
					return true;
				}
			}

			hide.Stop ();
			Visibility = VisibilityType.None;
			return false;
		}

		protected virtual void ShapeSurface (Context cr, Cairo.Color color)
		{
			cr.Operator = Operator.Source;
			Cairo.Pattern p = new Cairo.SolidPattern (new Cairo.Color (0, 0, 0, 0));
			cr.Source = p;
			p.Destroy ();
			cr.Paint ();
			cr.Operator = Operator.Over;

			Cairo.Pattern r = new SolidPattern (color);
			cr.Source = r;
			r.Destroy ();
			cr.MoveTo (round, 0);
			if (x_align == 1.0)
				cr.LineTo (Allocation.Width, 0);
			else
				cr.Arc (Allocation.Width - round, round, round, - Math.PI * 0.5, 0);
			if (x_align == 1.0 || y_align == 1.0)
				cr.LineTo (Allocation.Width, Allocation.Height);
			else
				cr.Arc (Allocation.Width - round, Allocation.Height - round, round, 0, Math.PI * 0.5);
			if (y_align == 1.0)
				cr.LineTo (0, Allocation.Height);
			else
				cr.Arc (round, Allocation.Height - round, round, Math.PI * 0.5, Math.PI);
			cr.Arc (round, round, round, Math.PI, Math.PI * 1.5);
			cr.ClosePath ();
			cr.Fill ();			
		}

		double target;
		double opacity = 0;

		bool FadeToTarget ()
		{
			//Log.Debug ("op {0}\ttarget{1}", opacity, target);
			Visible = (opacity > 0.05);
			if (Math.Abs (target - opacity) < .05)
				return false;
			if (target > opacity)
				opacity += .04;
			else
				opacity -= .04;
			if (Visible)
				CompositeUtils.SetWinOpacity (this, opacity);
			else
				Hide ();
			return true;
		}

		bool FadeToTarget (double target)
		{
			//Log.Debug ("FadeToTarget {0}", target);
			Realize ();
			this.target = target;
			fade.Start ();

			if (target > 0.0)
				hide.Restart ();

			return false;
		}

		private void ShapeWindow ()
		{
			if (composited)
				return;

			Gdk.Pixmap bitmap = new Gdk.Pixmap (GdkWindow, 
							    Allocation.Width, 
							    Allocation.Height, 1);

#if CAIRO_1_2_5			
			Context cr = Gdk.CairoHelper.Create (bitmap);
#else			
			Context cr = CairoUtils.CreateContext (bitmap);
#endif			
			ShapeCombineMask (bitmap, 0, 0);
			ShapeSurface (cr, new Color (1, 1, 1));
			ShapeCombineMask (bitmap, 0, 0);
			((IDisposable)cr).Dispose ();
			bitmap.Dispose ();

		}

		protected override bool OnExposeEvent (Gdk.EventExpose args)
		{
			Gdk.Color c = Style.Background (State);
#if CAIRO_1_2_5			
			Context cr = Gdk.CairoHelper.Create (GdkWindow);
#else
			Context cr = CairoUtils.CreateContext (GdkWindow);
#endif						

			ShapeSurface (cr, new Cairo.Color (c.Red / (double) ushort.MaxValue,
							   c.Blue / (double) ushort.MaxValue, 
							   c.Green / (double) ushort.MaxValue,
							   0.8));

			((IDisposable)cr).Dispose ();
			return base.OnExposeEvent (args);
		}

		protected override bool OnMotionNotifyEvent (Gdk.EventMotion args)
		{
			this.Visibility = VisibilityType.Full;
			base.OnMotionNotifyEvent (args);
			return false;
		}

		protected override void OnSizeAllocated (Gdk.Rectangle rec)
		{
			base.OnSizeAllocated (rec);
			Relocate ();
			ShapeWindow ();
			QueueDraw ();
		}

		private void HandleHostSizeAllocated (object o, SizeAllocatedArgs args)
		{
			Relocate ();
		}
		
		private void HandleHostConfigure (object o, ConfigureEventArgs args)
		{
			Relocate ();
		}
		
		private void Relocate ()
		{
			int x, y;
			if (!IsRealized || !host_toplevel.IsRealized)
				return;
			
			host.GdkWindow.GetOrigin (out x, out y);

			int xOrigin = x;
			int yOrigin = y;

			x += (int) (host.Allocation.Width * x_align);
			y += (int) (host.Allocation.Height * y_align);
			
			x -= (int) (Allocation.Width * 0.5);
			y -= (int) (Allocation.Height * 0.5);

			x = Math.Max (0, Math.Min (x, xOrigin + host.Allocation.Width - Allocation.Width));
			y = Math.Max (0, Math.Min (y, yOrigin + host.Allocation.Height - Allocation.Height));
			
			Move (x, y);
		}

		protected override void OnRealized ()
		{
			composited = CompositeUtils.IsComposited (Screen) && CompositeUtils.SetRgbaColormap (this);
			AppPaintable = composited;

			base.OnRealized ();
			
			ShapeWindow ();
			Relocate ();
		}
		
		public void Dismiss ()
		{
			Visibility = VisibilityType.None;
			dismiss.Start ();
		}

		protected override void OnMapped ()
		{
			base.OnMapped ();
			Relocate ();
		}
	}
}