2005-02-13 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Hwnd.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 // NOT COMPLETE
28
29 using System;
30 using System.Collections;
31 using System.Drawing;
32 using System.Runtime.InteropServices;
33
34 namespace System.Windows.Forms {
35         internal class Hwnd : IDisposable {
36                 #region Local Variables
37                 private static Hashtable        windows = new Hashtable(100, 0.5f);
38                 private const int       menu_height = 14;                       // FIXME - Read this value from somewhere
39                 private const int       caption_height = 0;                     // FIXME - Read this value from somewhere
40                 private const int       tool_caption_height = 0;                // FIXME - Read this value from somewhere
41
42                 //private GCHandle      gc_handle;
43                 private IntPtr          handle;
44                 internal IntPtr         client_window;
45                 internal IntPtr         whole_window;
46                 internal IntPtr         menu_handle;
47                 internal TitleStyle     title_style;
48                 internal BorderStyle    border_style;
49                 internal Border3DStyle  edge_style;
50                 internal int            x;
51                 internal int            y;
52                 internal int            width;
53                 internal int            height;
54                 internal Hwnd           parent;
55                 internal bool           visible;
56                 internal Rectangle      invalid;
57                 internal bool           expose_pending;
58                 internal bool           nc_expose_pending;
59                 internal Graphics       client_dc;
60                 #endregion      // Local Variables
61
62                 #region Constructors and destructors
63                 public Hwnd() {
64                         //gc_handle = GCHandle.Alloc(this);
65
66                         x = 0;
67                         y = 0;
68                         width = 0;
69                         height = 0;
70                         visible = false;
71                         menu_handle = IntPtr.Zero;
72                         border_style = BorderStyle.None;
73                         client_window = IntPtr.Zero;
74                         whole_window = IntPtr.Zero;
75                         handle = IntPtr.Zero;
76                         parent = null;
77                         invalid = Rectangle.Empty;
78                         expose_pending = false;
79                         nc_expose_pending = false;
80                         edge_style = Border3DStyle.Raised;
81                 }
82
83                 public void Dispose() {
84                         //gc_handle.Free();
85                 }
86                 #endregion
87
88                 #region Static Methods
89                 public void SetObjectWindow(Hwnd obj, IntPtr window) {
90                         windows[window] = obj;
91                 }
92
93                 public static Hwnd ObjectFromWindow(IntPtr window) {
94                         return (Hwnd)windows[window];
95                 }
96
97                 public static Hwnd ObjectFromHandle(IntPtr handle) {
98                         //return (Hwnd)(((GCHandle)handle).Target);
99                         return (Hwnd)windows[handle];
100                 }
101
102                 public static IntPtr HandleFromObject(Hwnd obj) {
103                         //return (IntPtr)obj.gc_handle;
104                         return obj.handle;
105                 }
106
107                 public static Hwnd GetObjectFromWindow(IntPtr window) {
108                         return (Hwnd)windows[window];
109                 }
110
111                 public static IntPtr GetHandleFromWindow(IntPtr window) {
112                         Hwnd    hwnd;
113
114                         hwnd = (Hwnd)windows[window];
115                         if (hwnd != null) {
116                                 //return (IntPtr)hwnd.gc_handle;
117                                 return hwnd.handle;
118                         } else {
119                                 return IntPtr.Zero;
120                         }
121                 }
122
123                 public static Rectangle GetWindowRectangle(BorderStyle border_style, IntPtr menu_handle, TitleStyle title_style, Rectangle client_rect) {
124                         Rectangle       rect;
125
126                         rect = new Rectangle(client_rect.Location, client_rect.Size);
127
128                         if (menu_handle != IntPtr.Zero) {
129                                 rect.Y -= menu_height;
130                                 rect.Height += menu_height;
131                         }
132
133                         if (border_style == BorderStyle.Fixed3D) {
134                                 rect.X -= 2;
135                                 rect.Y -= 2;
136                                 rect.Width += 4;
137                                 rect.Height += 4;
138                         } else if (border_style == BorderStyle.FixedSingle) {
139                                 rect.X -= 1;
140                                 rect.Y -= 1;
141                                 rect.Width += 2;
142                                 rect.Height += 2;
143                         }
144
145                         if (title_style == TitleStyle.Normal) {
146                                 rect.Y -= caption_height;
147                                 rect.Height += caption_height;
148                         } else if (title_style == TitleStyle.Tool) {
149                                 rect.Y -= tool_caption_height;
150                                 rect.Height += tool_caption_height;
151                         }
152
153                         return rect;
154                 }
155
156                 public static Rectangle GetClientRectangle(BorderStyle border_style, IntPtr menu_handle, TitleStyle title_style, int width, int height) {
157                         Rectangle rect;
158
159                         rect = new Rectangle(0, 0, width, height);
160
161                         if (menu_handle != IntPtr.Zero) {
162                                 rect.Y += menu_height;
163                                 rect.Height -= menu_height;
164                         }
165
166                         if (border_style == BorderStyle.Fixed3D) {
167                                 rect.X += 2;
168                                 rect.Y += 2;
169                                 rect.Width -= 4;
170                                 rect.Height -= 4;
171                         } else if (border_style == BorderStyle.FixedSingle) {
172                                 rect.X += 1;
173                                 rect.Y += 1;
174                                 rect.Width -= 2;
175                                 rect.Height -= 2;
176                         }
177
178                         if (title_style == TitleStyle.Normal)  {
179                                 rect.Y += caption_height;
180                                 rect.Height -= caption_height;
181                         } else if (title_style == TitleStyle.Normal)  {
182                                 rect.Y += tool_caption_height;
183                                 rect.Height -= tool_caption_height;
184                         }
185
186                         return rect;
187                 }
188                 #endregion      // Static Methods
189
190                 #region Instance Properties
191                 public BorderStyle BorderStyle {
192                         get {
193                                 return border_style;
194                         }
195
196                         set {
197                                 border_style = value;
198                         }
199                 }
200
201                 public Graphics ClientDC {
202                         get {
203                                 return client_dc;
204                         }
205
206                         set {
207                                 client_dc = value;
208                         }
209                 }
210
211                 public Rectangle ClientRect {
212                         get {
213                                 Rectangle rect;
214
215                                 rect = new Rectangle(0, 0, width, height);
216
217                                 if (menu_handle != IntPtr.Zero) {
218                                         rect.Y += menu_height;
219                                         rect.Height -= menu_height;
220                                 }
221
222                                 if (border_style == BorderStyle.Fixed3D) {
223                                         rect.X += 2;
224                                         rect.Y += 2;
225                                         rect.Width -= 4;
226                                         rect.Height -= 4;
227                                 } else if (border_style == BorderStyle.FixedSingle) {
228                                         rect.X += 1;
229                                         rect.Y += 1;
230                                         rect.Width -= 2;
231                                         rect.Height -= 2;
232                                 }
233
234                                 if (this.title_style == TitleStyle.Normal)  {
235                                         rect.Y += caption_height;
236                                         rect.Height -= caption_height;
237                                 } else if (this.title_style == TitleStyle.Normal)  {
238                                         rect.Y += tool_caption_height;
239                                         rect.Height -= tool_caption_height;
240                                 }
241
242                                 return rect;
243                         }
244                 }
245
246                 public IntPtr ClientWindow {
247                         get {
248                                 return client_window;
249                         }
250
251                         set {
252                                 client_window = value;
253                                 handle = value;
254
255                                 if (windows[client_window] == null) {
256                                         windows[client_window] = this;
257                                 }
258                         }
259                 }
260
261                 public Border3DStyle EdgeStyle {
262                         get {
263                                 return edge_style;
264                         }
265
266                         set {
267                                 edge_style = value;
268                         }
269                 }
270
271                 public bool ExposePending {
272                         get {
273                                 return expose_pending;
274                         }
275
276                         set {
277                                 expose_pending = value;
278                         }
279                 }
280
281                 public IntPtr Handle {
282                         get {
283                                 if (handle == IntPtr.Zero) {
284                                         throw new ArgumentNullException("Handle", "Handle is not yet assigned, need a ClientWindow");
285                                 }
286                                 //return (IntPtr)gc_handle;
287                                 return handle;
288                         }
289                 }
290
291                 public int Height {
292                         get {
293                                 return height;
294                         }
295
296                         set {
297                                 height = value;
298                         }
299                 }
300
301                 public IntPtr MenuHandle {
302                         get {
303                                 return menu_handle;
304                         }
305
306                         set {
307                                 menu_handle = value;
308                         }
309                 }
310
311                 public Rectangle Invalid {
312                         get {
313                                 return invalid;
314                         }
315
316                         set {
317                                 invalid = value;
318                         }
319                 }
320
321                 public bool NCExposePending {
322                         get {
323                                 return nc_expose_pending;
324                         }
325
326                         set {
327                                 nc_expose_pending = value;
328                         }
329                 }
330
331                 public Hwnd Parent {
332                         get {
333                                 return parent;
334                         }
335
336                         set {
337                                 parent = value;
338                         }
339                 }
340
341                 public TitleStyle TitleStyle {
342                         get {
343                                 return title_style;
344                         }
345
346                         set {
347                                 title_style = value;
348                         }
349                 }
350
351                 public IntPtr WholeWindow {
352                         get {
353                                 return whole_window;
354                         }
355
356                         set {
357                                 whole_window = value;
358
359                                 if (windows[whole_window] == null) {
360                                         windows[whole_window] = this;
361                                 }
362                         }
363                 }
364
365                 public int Width {
366                         get {
367                                 return width;
368                         }
369
370                         set {
371                                 width = value;
372                         }
373                 }
374
375                 public bool Visible {
376                         get {
377                                 return visible;
378                         }
379
380                         set {
381                                 visible = value;
382                         }
383                 }
384
385                 public int X {
386                         get {
387                                 return x;
388                         }
389
390                         set {
391                                 x = value;
392                         }
393                 }
394
395                 public int Y {
396                         get {
397                                 return y;
398                         }
399
400                         set {
401                                 y = value;
402                         }
403                 }
404                 #endregion      // Instance properties
405
406                 #region Methods
407                 public void AddInvalidArea(int x, int y, int width, int height) {
408                         if (invalid == Rectangle.Empty) {
409                                 invalid = new Rectangle (x, y, width, height);
410                                 return;
411                         }
412                         invalid = Rectangle.Union (invalid, new Rectangle (x, y, width, height));
413                 }
414
415                 public void AddInvalidArea(Rectangle rect) {
416                         if (invalid == Rectangle.Empty) {
417                                 invalid = rect;
418                                 return;
419                         }
420                         invalid = Rectangle.Union (invalid, rect);
421                 }
422
423                 public void ClearInvalidArea() {
424                         invalid = Rectangle.Empty;
425                         expose_pending = false;
426                 }
427                 #endregion      // Methods
428         }
429 }