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