File: Screen.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (221 lines) | stat: -rw-r--r-- 6,069 bytes parent folder | download | duplicates (10)
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
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
//
// Authors:
//	Peter Bartok	(pbartok@novell.com)
//
//

using System;
using System.Drawing;

namespace System.Windows.Forms {
	public class Screen {
		#region Local Variables
		private static Screen[] all_screens;
		private bool		primary;
		private Rectangle	bounds;
		private Rectangle	workarea;
		private string		name;
		private int		bits_per_pixel;
		#endregion	// Local Variables

		#region	Constructors
		static Screen ()
		{
			try {
				all_screens = XplatUI.AllScreens;
			}
			catch (Exception e) {
				Console.WriteLine ("{0} trying to get all screens: {1}", e.GetType (), e.Message);
			}

			if (all_screens == null || all_screens.Length == 0) {
				// just use a default one
				all_screens = new[] { new Screen(true, "Mono MWF Primary Display",
					XplatUI.VirtualScreen, XplatUI.WorkingArea) };
			}
		}

		internal Screen() {
			this.primary = true;
			this.bounds = XplatUI.WorkingArea;
		}

		internal Screen(bool primary, string name, Rectangle bounds, Rectangle workarea) {
			this.primary = primary;
			this.name = name;
			this.bounds = bounds;
			this.workarea = workarea;
			this.bits_per_pixel = 32;
		}
		#endregion	// Constructors

		#region Public Static Properties
		public static Screen[] AllScreens {
			get {
				return all_screens;
			}
		}

		public static Screen PrimaryScreen {
			get {
				return all_screens[0];
			}
		}
		#endregion	// Public Static Properties

		#region Public Instance Properties
		[MonoTODO ("Stub, always returns 32")]
		public int BitsPerPixel {
			get { return bits_per_pixel; }
		}

		public Rectangle Bounds {
			get {
				return this.bounds;
			}
		}

		public string DeviceName {
			get {
				return this.name;
			}
		}

		public bool Primary {
			get {
				return this.primary;
			}
		}

		public Rectangle WorkingArea {
			get {
				return this.workarea;
			}
		}
		#endregion	// Public Instance Properties

		#region Public Static Methods
		public static Screen FromControl(Control control) {
			var point = control.Parent != null ? control.Parent.PointToScreen(control.Location) : control.Location;
			return Screen.FromPoint(point);
		}

		public static Screen FromHandle(IntPtr hwnd) {
			Control control;

			control = Control.FromHandle(hwnd);
			if (control != null) {
				var point = control.Parent != null ? control.Parent.PointToScreen(control.Location) : control.Location;
				return Screen.FromPoint(point);
			}
			return Screen.PrimaryScreen;
		}

		public static Screen FromPoint(Point point) {
			for (int i = 0; i < all_screens.Length; i++) {
				if (all_screens[i].Bounds.Contains(point)) {
					return all_screens[i];
				}
			}
			return Screen.PrimaryScreen;
		}

		public static Screen FromRectangle(Rectangle rect) {
			Screen bestScrn = null;
			int closest = Int32.MaxValue;
			foreach (Screen scrn in Screen.AllScreens) {
				Rectangle rcBounds = scrn.Bounds;
				int distance = 0;
				if (rect.Left > rcBounds.Right)
					distance += rect.Left - rcBounds.Right;
				else if (rcBounds.Left > rect.Left)
					distance += rcBounds.Left - rect.Left;
				if (rcBounds.Left > rect.Right)
					distance += rcBounds.Left - rect.Right;
				else if (rect.Right > rcBounds.Right)
					distance += rect.Right - rcBounds.Right;
				if (rect.Top > rcBounds.Bottom)
					distance += rect.Top - rcBounds.Bottom;
				else if (rcBounds.Top > rect.Top)
					distance += rcBounds.Top - rect.Top;
				if (rcBounds.Top > rect.Bottom)
					distance += rcBounds.Top - rect.Bottom;
				else if (rect.Bottom > rcBounds.Bottom)
					distance += rect.Bottom - rcBounds.Bottom;
				if (distance < closest) {
					bestScrn = scrn;
					closest = distance;
				}
			}
			return bestScrn;
		}

		public static Rectangle GetBounds(Control ctl) {
			return Screen.FromControl(ctl).Bounds;
		}

		public static Rectangle GetBounds(Point pt) {
			return Screen.FromPoint(pt).Bounds;
		}

		public static Rectangle GetBounds(Rectangle rect) {
			return Screen.FromRectangle(rect).Bounds;
		}

		public static Rectangle GetWorkingArea(Control ctl) {
			return Screen.FromControl(ctl).WorkingArea;
		}

		public static Rectangle GetWorkingArea(Point pt) {
			return Screen.FromPoint(pt).WorkingArea;
		}

		public static Rectangle GetWorkingArea(Rectangle rect) {
			return Screen.FromRectangle(rect).WorkingArea;
		}
		#endregion	// Public Static Methods

		#region Public Instance Methods
		public override bool Equals(object obj) {
			if (obj is Screen) {
				Screen s = (Screen)obj;

				if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
					return true;
				}
			}
			return false;
		}

		public override int GetHashCode() {
			return base.GetHashCode ();
		}

		public override string ToString() {
			return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
		}


		#endregion	// Public Instance Methods
	}
}