1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
30 namespace System.Windows.Forms {
32 #region Local Variables
33 private static Screen[] all_screens;
35 private Rectangle bounds;
36 private Rectangle workarea;
38 private int bits_per_pixel;
39 #endregion // Local Variables
45 all_screens = XplatUI.AllScreens;
48 Console.WriteLine ("{0} trying to get all screens: {1}", e.GetType (), e.Message);
51 if (all_screens == null || all_screens.Length == 0) {
52 // just use a default one
53 all_screens = new[] { new Screen(true, "Mono MWF Primary Display",
54 XplatUI.VirtualScreen, XplatUI.WorkingArea) };
60 this.bounds = XplatUI.WorkingArea;
63 internal Screen(bool primary, string name, Rectangle bounds, Rectangle workarea) {
64 this.primary = primary;
67 this.workarea = workarea;
68 this.bits_per_pixel = 32;
70 #endregion // Constructors
72 #region Public Static Properties
73 public static Screen[] AllScreens {
79 public static Screen PrimaryScreen {
81 return all_screens[0];
84 #endregion // Public Static Properties
86 #region Public Instance Properties
87 [MonoTODO ("Stub, always returns 32")]
88 public int BitsPerPixel {
89 get { return bits_per_pixel; }
92 public Rectangle Bounds {
98 public string DeviceName {
104 public bool Primary {
110 public Rectangle WorkingArea {
112 return this.workarea;
115 #endregion // Public Instance Properties
117 #region Public Static Methods
118 public static Screen FromControl(Control control) {
119 var point = control.Parent != null ? control.Parent.PointToScreen(control.Location) : control.Location;
120 return Screen.FromPoint(point);
123 public static Screen FromHandle(IntPtr hwnd) {
126 control = Control.FromHandle(hwnd);
127 if (control != null) {
128 var point = control.Parent != null ? control.Parent.PointToScreen(control.Location) : control.Location;
129 return Screen.FromPoint(point);
131 return Screen.PrimaryScreen;
134 public static Screen FromPoint(Point point) {
135 for (int i = 0; i < all_screens.Length; i++) {
136 if (all_screens[i].Bounds.Contains(point)) {
137 return all_screens[i];
140 return Screen.PrimaryScreen;
143 public static Screen FromRectangle(Rectangle rect) {
144 Screen bestScrn = null;
145 int closest = Int32.MaxValue;
146 foreach (Screen scrn in Screen.AllScreens) {
147 Rectangle rcBounds = scrn.Bounds;
149 if (rect.Left > rcBounds.Right)
150 distance += rect.Left - rcBounds.Right;
151 else if (rcBounds.Left > rect.Left)
152 distance += rcBounds.Left - rect.Left;
153 if (rcBounds.Left > rect.Right)
154 distance += rcBounds.Left - rect.Right;
155 else if (rect.Right > rcBounds.Right)
156 distance += rect.Right - rcBounds.Right;
157 if (rect.Top > rcBounds.Bottom)
158 distance += rect.Top - rcBounds.Bottom;
159 else if (rcBounds.Top > rect.Top)
160 distance += rcBounds.Top - rect.Top;
161 if (rcBounds.Top > rect.Bottom)
162 distance += rcBounds.Top - rect.Bottom;
163 else if (rect.Bottom > rcBounds.Bottom)
164 distance += rect.Bottom - rcBounds.Bottom;
165 if (distance < closest) {
173 public static Rectangle GetBounds(Control ctl) {
174 return Screen.FromControl(ctl).Bounds;
177 public static Rectangle GetBounds(Point pt) {
178 return Screen.FromPoint(pt).Bounds;
181 public static Rectangle GetBounds(Rectangle rect) {
182 return Screen.FromRectangle(rect).Bounds;
185 public static Rectangle GetWorkingArea(Control ctl) {
186 return Screen.FromControl(ctl).WorkingArea;
189 public static Rectangle GetWorkingArea(Point pt) {
190 return Screen.FromPoint(pt).WorkingArea;
193 public static Rectangle GetWorkingArea(Rectangle rect) {
194 return Screen.FromRectangle(rect).WorkingArea;
196 #endregion // Public Static Methods
198 #region Public Instance Methods
199 public override bool Equals(object obj) {
201 Screen s = (Screen)obj;
203 if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
210 public override int GetHashCode() {
211 return base.GetHashCode ();
214 public override string ToString() {
215 return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
219 #endregion // Public Instance Methods