* TabControl.cs: Show the tooltip depending on the value
[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 #if NET_2_0
47                 private int             bits_per_pixel;
48 #endif
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 #if NET_2_0
63                         this.bits_per_pixel = 32;
64 #endif
65                 }
66                 #endregion      // Constructors
67
68                 #region Public Static Properties
69                 public static Screen[] AllScreens {
70                         get {
71                                 return all_screens;
72                         }
73                 }
74
75                 public static Screen PrimaryScreen {
76                         get {
77                                 return all_screens[0];
78                         }
79                 }
80                 #endregion      // Public Static Properties
81
82                 #region Public Instance Properties
83 #if NET_2_0
84                 [MonoTODO ("Stub, always returns 32")]
85                 public int BitsPerPixel {
86                         get { return bits_per_pixel; }
87                 }
88 #endif
89
90                 public Rectangle Bounds {
91                         get {
92                                 return this.bounds;
93                         }
94                 }
95
96                 public string DeviceName {
97                         get {
98                                 return this.name;
99                         }
100                 }
101
102                 public bool Primary {
103                         get {
104                                 return this.primary;
105                         }
106                 }
107
108                 public Rectangle WorkingArea {
109                         get {
110                                 return this.workarea;
111                         }
112                 }
113                 #endregion      // Public Instance Properties
114
115                 #region Public Static Methods
116                 public static Screen FromControl(Control control) {
117                         return Screen.FromPoint(control.Location);
118                 }
119
120                 public static Screen FromHandle(IntPtr hwnd) {
121                         Control control;
122
123                         control = Control.FromHandle(hwnd);
124                         if (control != null) {
125                                 return Screen.FromPoint(control.Location);
126                         }
127                         return Screen.PrimaryScreen;
128                 }
129
130                 public static Screen FromPoint(Point point) {
131                         for (int i = 0; i < all_screens.Length; i++) {
132                                 if (all_screens[i].Bounds.Contains(point)) {
133                                         return all_screens[i];
134                                 }
135                         }
136                         return Screen.PrimaryScreen;
137                 }
138
139                 public static Screen FromRectangle(Rectangle rect) {
140                         return Screen.FromPoint(new Point(rect.Left, rect.Top));
141                 }
142
143                 public static Rectangle GetBounds(Control ctl) {
144                         return Screen.FromControl(ctl).Bounds;
145                 }
146
147                 public static Rectangle GetBounds(Point pt) {
148                         return Screen.FromPoint(pt).Bounds;
149                 }
150
151                 public static Rectangle GetBounds(Rectangle rect) {
152                         return Screen.FromRectangle(rect).Bounds;
153                 }
154
155                 public static Rectangle GetWorkingArea(Control ctl) {
156                         return Screen.FromControl(ctl).WorkingArea;
157                 }
158
159                 public static Rectangle GetWorkingArea(Point pt) {
160                         return Screen.FromPoint(pt).WorkingArea;
161                 }
162
163                 public static Rectangle GetWorkingArea(Rectangle rect) {
164                         return Screen.FromRectangle(rect).WorkingArea;
165                 }
166                 #endregion      // Public Static Methods
167
168                 #region Public Instance Methods
169                 public override bool Equals(object obj) {
170                         if (obj is Screen) {
171                                 Screen s = (Screen)obj;
172
173                                 if (name.Equals(s.name) && (primary == s.primary) && (bounds.Equals(s.Bounds)) && (workarea.Equals(s.workarea))) {
174                                         return true;
175                                 }
176                         }
177                         return false;
178                 }
179
180                 public override int GetHashCode() {
181                         return base.GetHashCode ();
182                 }
183
184                 public override string ToString() {
185                         return "Screen[Bounds={" + this.Bounds + "} WorkingArea={" + this.WorkingArea + "} Primary={" + this.Primary + "} DeviceName=" + this.DeviceName;
186                 }
187
188
189                 #endregion      // Public Instance Methods
190         }
191 }