New test.
[mono.git] / mcs / class / Managed.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 // COMPLETE
28
29 // NOTE: We made a concious decision to have only a single 'screen'
30 // due to the differences in platforms. On Win32 we could gather
31 // all information, but not for X11 (and possibly Mac). So for now
32 // we'll stick with a single screen, but the functions are still 
33 // written to support multiple screens, simply beef up the all_screens 
34 // assignment to get multiples
35 // To support multiples, we need to use GetMonitorInfo API on Win32
36
37 using System;
38 using System.Drawing;
39
40 namespace System.Windows.Forms {
41         public class Screen {
42                 #region Local Variables
43                 private static Screen[] all_screens = { new Screen(true, "Mono MWF Primary Display", SystemInformation.VirtualScreen, SystemInformation.WorkingArea) };
44                 private bool            primary;
45                 private Rectangle       bounds;
46                 private Rectangle       workarea;
47                 private string          name;
48         
49                 #endregion      // Local Variables
50
51                 #region Constructors
52                 private Screen() {
53                         this.primary = true;
54                         this.bounds = SystemInformation.WorkingArea;
55                 }
56
57                 private Screen(bool primary, string name, Rectangle bounds, Rectangle workarea) {
58                         this.primary = primary;
59                         this.name = name;
60                         this.bounds = bounds;
61                         this.workarea = workarea;
62                 }
63                 #endregion      // Constructors
64
65                 #region Public Static Properties
66                 public static Screen[] AllScreens {
67                         get {
68                                 return all_screens;
69                         }
70                 }
71
72                 public static Screen PrimaryScreen {
73                         get {
74                                 return all_screens[0];
75                         }
76                 }
77                 #endregion      // Public Static Properties
78
79                 #region Public Instance Properties
80                 public Rectangle Bounds {
81                         get {
82                                 return this.bounds;
83                         }
84                 }
85
86                 public string DeviceName {
87                         get {
88                                 return this.name;
89                         }
90                 }
91
92                 public bool Primary {
93                         get {
94                                 return this.primary;
95                         }
96                 }
97
98                 public Rectangle WorkingArea {
99                         get {
100                                 return this.workarea;
101                         }
102                 }
103                 #endregion      // Public Instance Properties
104
105                 #region Public Static Methods
106                 public static Screen FromControl(Control control) {
107                         return Screen.FromPoint(control.Location);
108                 }
109
110                 public static Screen FromHandle(IntPtr hwnd) {
111                         Control control;
112
113                         control = Control.FromHandle(hwnd);
114                         if (control != null) {
115                                 return Screen.FromPoint(control.Location);
116                         }
117                         return Screen.PrimaryScreen;
118                 }
119
120                 public static Screen FromPoint(Point point) {
121                         for (int i = 0; i < all_screens.Length; i++) {
122                                 if (all_screens[i].Bounds.Contains(point)) {
123                                         return all_screens[i];
124                                 }
125                         }
126                         return Screen.PrimaryScreen;
127                 }
128
129                 public static Screen FromRectangle(Rectangle rect) {
130                         return Screen.FromPoint(new Point(rect.Left, rect.Top));
131                 }
132
133                 public static Rectangle GetBounds(Control ctl) {
134                         return Screen.FromControl(ctl).Bounds;
135                 }
136
137                 public static Rectangle GetBounds(Point pt) {
138                         return Screen.FromPoint(pt).Bounds;
139                 }
140
141                 public static Rectangle GetBounds(Rectangle rect) {
142                         return Screen.FromRectangle(rect).Bounds;
143                 }
144
145                 public static Rectangle GetWorkingArea(Control ctl) {
146                         return Screen.FromControl(ctl).WorkingArea;
147                 }
148
149                 public static Rectangle GetWorkingArea(Point pt) {
150                         return Screen.FromPoint(pt).WorkingArea;
151                 }
152
153                 public static Rectangle GetWorkingArea(Rectangle rect) {
154                         return Screen.FromRectangle(rect).WorkingArea;
155                 }
156                 #endregion      // Public Static Methods
157
158                 #region Public Instance Methods
159                 public override bool Equals(object obj) {
160                         if (obj is Screen) {
161                                 Screen s = (Screen)obj;
162
163                                 if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
164                                         return true;
165                                 }
166                         }
167                         return false;
168                 }
169
170                 public override int GetHashCode() {
171                         return base.GetHashCode ();
172                 }
173
174                 public override string ToString() {
175                         return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
176                 }
177
178
179                 #endregion      // Public Instance Methods
180         }
181 }