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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
30 using System.Collections;
32 using System.Runtime.InteropServices;
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
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;
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
62 #region Constructors and destructors
64 //gc_handle = GCHandle.Alloc(this);
71 menu_handle = IntPtr.Zero;
72 border_style = BorderStyle.None;
73 client_window = IntPtr.Zero;
74 whole_window = IntPtr.Zero;
77 invalid = Rectangle.Empty;
78 expose_pending = false;
79 nc_expose_pending = false;
80 edge_style = Border3DStyle.Raised;
83 public void Dispose() {
88 #region Static Methods
89 public void SetObjectWindow(Hwnd obj, IntPtr window) {
90 windows[window] = obj;
93 public static Hwnd ObjectFromWindow(IntPtr window) {
94 return (Hwnd)windows[window];
97 public static Hwnd ObjectFromHandle(IntPtr handle) {
98 //return (Hwnd)(((GCHandle)handle).Target);
99 return (Hwnd)windows[handle];
102 public static IntPtr HandleFromObject(Hwnd obj) {
103 //return (IntPtr)obj.gc_handle;
107 public static Hwnd GetObjectFromWindow(IntPtr window) {
108 return (Hwnd)windows[window];
111 public static IntPtr GetHandleFromWindow(IntPtr window) {
114 hwnd = (Hwnd)windows[window];
116 //return (IntPtr)hwnd.gc_handle;
123 public static Rectangle GetWindowRectangle(BorderStyle border_style, IntPtr menu_handle, TitleStyle title_style, Rectangle client_rect) {
126 rect = new Rectangle(client_rect.Location, client_rect.Size);
128 if (menu_handle != IntPtr.Zero) {
129 rect.Y -= menu_height;
130 rect.Height += menu_height;
133 if (border_style == BorderStyle.Fixed3D) {
138 } else if (border_style == BorderStyle.FixedSingle) {
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;
156 public static Rectangle GetClientRectangle(BorderStyle border_style, IntPtr menu_handle, TitleStyle title_style, int width, int height) {
159 rect = new Rectangle(0, 0, width, height);
161 if (menu_handle != IntPtr.Zero) {
162 rect.Y += menu_height;
163 rect.Height -= menu_height;
166 if (border_style == BorderStyle.Fixed3D) {
171 } else if (border_style == BorderStyle.FixedSingle) {
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;
188 #endregion // Static Methods
190 #region Instance Properties
191 public BorderStyle BorderStyle {
197 border_style = value;
201 public Graphics ClientDC {
211 public Rectangle ClientRect {
215 rect = new Rectangle(0, 0, width, height);
217 if (menu_handle != IntPtr.Zero) {
218 rect.Y += menu_height;
219 rect.Height -= menu_height;
222 if (border_style == BorderStyle.Fixed3D) {
227 } else if (border_style == BorderStyle.FixedSingle) {
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;
246 public IntPtr ClientWindow {
248 return client_window;
252 client_window = value;
255 if (windows[client_window] == null) {
256 windows[client_window] = this;
261 public Border3DStyle EdgeStyle {
271 public bool ExposePending {
273 return expose_pending;
277 expose_pending = value;
281 public IntPtr Handle {
283 if (handle == IntPtr.Zero) {
284 throw new ArgumentNullException("Handle", "Handle is not yet assigned, need a ClientWindow");
286 //return (IntPtr)gc_handle;
301 public IntPtr MenuHandle {
311 public Rectangle Invalid {
321 public bool NCExposePending {
323 return nc_expose_pending;
327 nc_expose_pending = value;
341 public TitleStyle TitleStyle {
351 public IntPtr WholeWindow {
357 whole_window = value;
359 if (windows[whole_window] == null) {
360 windows[whole_window] = this;
375 public bool Visible {
404 #endregion // Instance properties
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);
412 invalid = Rectangle.Union (invalid, new Rectangle (x, y, width, height));
415 public void AddInvalidArea(Rectangle rect) {
416 if (invalid == Rectangle.Empty) {
420 invalid = Rectangle.Union (invalid, rect);
423 public void ClearInvalidArea() {
424 invalid = Rectangle.Empty;
425 expose_pending = false;
427 #endregion // Methods