Rename Managed.Windows.Forms to System.Windows.Forms for consistency.
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / Screen.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //
25 //
26
27 using System;
28 using System.Drawing;
29
30 namespace System.Windows.Forms {
31         public class Screen {
32                 #region Local Variables
33                 private static Screen[] all_screens;
34                 private bool            primary;
35                 private Rectangle       bounds;
36                 private Rectangle       workarea;
37                 private string          name;
38                 private int             bits_per_pixel;
39                 #endregion      // Local Variables
40
41                 #region Constructors
42                 static Screen ()
43                 {
44                         try {
45                                 all_screens = XplatUI.AllScreens;
46                         }
47                         catch (Exception e) {
48                                 Console.WriteLine ("{0} trying to get all screens: {1}", e.GetType (), e.Message);
49                         }
50
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) };
55                         }
56                 }
57
58                 internal Screen() {
59                         this.primary = true;
60                         this.bounds = XplatUI.WorkingArea;
61                 }
62
63                 internal Screen(bool primary, string name, Rectangle bounds, Rectangle workarea) {
64                         this.primary = primary;
65                         this.name = name;
66                         this.bounds = bounds;
67                         this.workarea = workarea;
68                         this.bits_per_pixel = 32;
69                 }
70                 #endregion      // Constructors
71
72                 #region Public Static Properties
73                 public static Screen[] AllScreens {
74                         get {
75                                 return all_screens;
76                         }
77                 }
78
79                 public static Screen PrimaryScreen {
80                         get {
81                                 return all_screens[0];
82                         }
83                 }
84                 #endregion      // Public Static Properties
85
86                 #region Public Instance Properties
87                 [MonoTODO ("Stub, always returns 32")]
88                 public int BitsPerPixel {
89                         get { return bits_per_pixel; }
90                 }
91
92                 public Rectangle Bounds {
93                         get {
94                                 return this.bounds;
95                         }
96                 }
97
98                 public string DeviceName {
99                         get {
100                                 return this.name;
101                         }
102                 }
103
104                 public bool Primary {
105                         get {
106                                 return this.primary;
107                         }
108                 }
109
110                 public Rectangle WorkingArea {
111                         get {
112                                 return this.workarea;
113                         }
114                 }
115                 #endregion      // Public Instance Properties
116
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);
121                 }
122
123                 public static Screen FromHandle(IntPtr hwnd) {
124                         Control control;
125
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);
130                         }
131                         return Screen.PrimaryScreen;
132                 }
133
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];
138                                 }
139                         }
140                         return Screen.PrimaryScreen;
141                 }
142
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;
148                                 int distance = 0;
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) {
166                                         bestScrn = scrn;
167                                         closest = distance;
168                                 }
169                         }
170                         return bestScrn;
171                 }
172
173                 public static Rectangle GetBounds(Control ctl) {
174                         return Screen.FromControl(ctl).Bounds;
175                 }
176
177                 public static Rectangle GetBounds(Point pt) {
178                         return Screen.FromPoint(pt).Bounds;
179                 }
180
181                 public static Rectangle GetBounds(Rectangle rect) {
182                         return Screen.FromRectangle(rect).Bounds;
183                 }
184
185                 public static Rectangle GetWorkingArea(Control ctl) {
186                         return Screen.FromControl(ctl).WorkingArea;
187                 }
188
189                 public static Rectangle GetWorkingArea(Point pt) {
190                         return Screen.FromPoint(pt).WorkingArea;
191                 }
192
193                 public static Rectangle GetWorkingArea(Rectangle rect) {
194                         return Screen.FromRectangle(rect).WorkingArea;
195                 }
196                 #endregion      // Public Static Methods
197
198                 #region Public Instance Methods
199                 public override bool Equals(object obj) {
200                         if (obj is Screen) {
201                                 Screen s = (Screen)obj;
202
203                                 if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
204                                         return true;
205                                 }
206                         }
207                         return false;
208                 }
209
210                 public override int GetHashCode() {
211                         return base.GetHashCode ();
212                 }
213
214                 public override string ToString() {
215                         return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
216                 }
217
218
219                 #endregion      // Public Instance Methods
220         }
221 }