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-2006 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
30 using System.Collections;
32 using System.Runtime.InteropServices;
34 // NOTE: Possible optimization:
35 // Several properties calculate dimensions on the fly; instead; they can
36 // be stored in a field and only be recalculated when a style is changed (DefaultClientRect, for example)
38 namespace System.Windows.Forms {
39 internal class Hwnd : IDisposable {
40 #region Local Variables
41 private static Hashtable windows = new Hashtable(100, 0.5f);
42 //private const int menu_height = 14; // FIXME - Read this value from somewhere
44 private IntPtr handle;
45 internal IntPtr client_window;
46 internal IntPtr whole_window;
47 internal IntPtr cursor;
49 internal TitleStyle title_style;
50 internal FormBorderStyle border_style;
51 internal bool border_static;
56 internal bool allow_drop;
58 internal bool visible;
60 internal uint opacity;
61 internal bool enabled;
62 internal bool zero_sized;
63 internal ArrayList invalid_list;
64 internal Rectangle nc_invalid;
65 internal bool expose_pending;
66 internal bool nc_expose_pending;
67 internal bool configure_pending;
68 internal bool resizing_or_moving; // Used by the X11 backend to track form resize/move
69 internal bool reparented;
70 internal Stack drawing_stack;
71 internal object user_data;
72 internal Rectangle client_rectangle;
73 internal ArrayList marshal_free_list;
74 internal int caption_height;
75 internal int tool_caption_height;
76 internal bool whacky_wm;
77 internal bool fixed_size;
78 internal bool zombie; /* X11 only flag. true if the X windows have been destroyed but we haven't been Disposed */
79 internal Region user_clip;
80 internal XEventQueue queue;
81 internal WindowExStyles initial_ex_style;
82 internal WindowStyles initial_style;
83 internal FormWindowState cached_window_state = (FormWindowState)(-1); /* X11 only field */
84 internal Point previous_child_startup_location = new Point (int.MinValue, int.MinValue);
85 static internal Point previous_main_startup_location = new Point (int.MinValue, int.MinValue);
86 internal ArrayList children;
89 private static Bitmap bmp;
91 private static Graphics bmp_g;
92 #endregion // Local Variables
94 // locks for some operations (used in XplatUIX11.cs)
95 internal object configure_lock = new object ();
96 internal object expose_lock = new object ();
98 #region Constructors and destructors
106 border_style = FormBorderStyle.None;
107 client_window = IntPtr.Zero;
108 whole_window = IntPtr.Zero;
109 cursor = IntPtr.Zero;
110 handle = IntPtr.Zero;
112 invalid_list = new ArrayList();
113 expose_pending = false;
114 nc_expose_pending = false;
117 client_rectangle = Rectangle.Empty;
118 marshal_free_list = new ArrayList(2);
119 opacity = 0xffffffff;
121 drawing_stack = new Stack ();
122 children = new ArrayList ();
123 resizing_or_moving = false;
127 public void Dispose() {
128 expose_pending = false;
129 nc_expose_pending = false;
132 windows.Remove(client_window);
133 windows.Remove(whole_window);
135 client_window = IntPtr.Zero;
136 whole_window = IntPtr.Zero;
138 for (int i = 0; i < marshal_free_list.Count; i++) {
139 Marshal.FreeHGlobal((IntPtr)marshal_free_list[i]);
141 marshal_free_list.Clear();
145 #region Static Methods
146 public static Hwnd ObjectFromWindow(IntPtr window) {
149 rv = (Hwnd)windows[window];
154 public static Hwnd ObjectFromHandle(IntPtr handle) {
155 //return (Hwnd)(((GCHandle)handle).Target);
158 rv = (Hwnd)windows[handle];
163 public static IntPtr HandleFromObject(Hwnd obj) {
167 public static Hwnd GetObjectFromWindow(IntPtr window) {
170 rv = (Hwnd)windows[window];
175 public static IntPtr GetHandleFromWindow(IntPtr window) {
179 hwnd = (Hwnd)windows[window];
188 public static Borders GetBorderWidth (CreateParams cp)
190 Borders border_size = new Borders ();
192 Size windowborder = ThemeEngine.Current.BorderSize; /*new Size (1, 1);*/ // This is the only one that can be changed from the display properties in windows.
193 Size border = ThemeEngine.Current.BorderStaticSize; /*new Size (1, 1);*/
194 Size clientedge = ThemeEngine.Current.Border3DSize; /*new Size (2, 2);*/
195 Size thickframe = new Size (2 + windowborder.Width, 2 + windowborder.Height);
196 Size dialogframe = ThemeEngine.Current.BorderSizableSize; /* new Size (3, 3);*/
198 if (cp.IsSet (WindowStyles.WS_CAPTION)) {
199 border_size.Inflate (dialogframe);
200 } else if (cp.IsSet (WindowStyles.WS_BORDER)) {
201 if (cp.IsSet (WindowExStyles.WS_EX_DLGMODALFRAME)) {
202 if (cp.IsSet (WindowStyles.WS_THICKFRAME) && (cp.IsSet (WindowExStyles.WS_EX_STATICEDGE) || cp.IsSet (WindowExStyles.WS_EX_CLIENTEDGE))) {
203 border_size.Inflate (border);
206 border_size.Inflate (border);
208 } else if (cp.IsSet (WindowStyles.WS_DLGFRAME)) {
209 border_size.Inflate (dialogframe);
212 if (cp.IsSet (WindowStyles.WS_THICKFRAME)) {
213 if (cp.IsSet (WindowStyles.WS_DLGFRAME)) {
214 border_size.Inflate (border);
216 border_size.Inflate (thickframe);
220 bool only_small_border;
221 Size small_border = Size.Empty;
223 only_small_border = cp.IsSet (WindowStyles.WS_THICKFRAME) || cp.IsSet (WindowStyles.WS_DLGFRAME);
224 if (only_small_border && cp.IsSet (WindowStyles.WS_THICKFRAME) && !cp.IsSet (WindowStyles.WS_BORDER) && !cp.IsSet (WindowStyles.WS_DLGFRAME)) {
225 small_border = border;
228 if (cp.IsSet (WindowExStyles.WS_EX_CLIENTEDGE | WindowExStyles.WS_EX_DLGMODALFRAME)) {
229 border_size.Inflate (clientedge + (only_small_border ? small_border : dialogframe));
230 } else if (cp.IsSet (WindowExStyles.WS_EX_STATICEDGE | WindowExStyles.WS_EX_DLGMODALFRAME)) {
231 border_size.Inflate (only_small_border ? small_border : dialogframe);
232 } else if (cp.IsSet (WindowExStyles.WS_EX_STATICEDGE | WindowExStyles.WS_EX_CLIENTEDGE)) {
233 border_size.Inflate (border + (only_small_border ? Size.Empty : clientedge));
235 if (cp.IsSet (WindowExStyles.WS_EX_CLIENTEDGE)) {
236 border_size.Inflate (clientedge);
238 if (cp.IsSet (WindowExStyles.WS_EX_DLGMODALFRAME) && !cp.IsSet (WindowStyles.WS_DLGFRAME)) {
239 border_size.Inflate (cp.IsSet (WindowStyles.WS_THICKFRAME) ? border : dialogframe);
241 if (cp.IsSet (WindowExStyles.WS_EX_STATICEDGE)) {
242 if (cp.IsSet (WindowStyles.WS_THICKFRAME) || cp.IsSet (WindowStyles.WS_DLGFRAME)) {
243 border_size.Inflate (new Size (-border.Width, -border.Height));
245 border_size.Inflate (border);
253 public static Rectangle GetWindowRectangle (CreateParams cp, Menu menu)
255 return GetWindowRectangle (cp, menu, Rectangle.Empty);
258 public static Rectangle GetWindowRectangle (CreateParams cp, Menu menu, Rectangle client_rect)
263 borders = GetBorders (cp, menu);
265 rect = new Rectangle (Point.Empty, client_rect.Size);
266 rect.Y -= borders.top;
267 rect.Height += borders.top + borders.bottom;
268 rect.X -= borders.left;
269 rect.Width += borders.left + borders.right;
272 Console.WriteLine ("GetWindowRectangle ({0}, {1}, {2}): {3}", cp, menu != null, client_rect, rect);
277 public Rectangle GetClientRectangle (int width, int height)
279 CreateParams cp = new CreateParams ();
280 cp.WindowStyle = initial_style;
281 cp.WindowExStyle = initial_ex_style;
282 return GetClientRectangle (cp, menu, width, height);
285 // This could be greatly optimized by caching the outputs and only updating when something is moved
286 // in the parent planar space. To do that we need to track z-order in the parent space as well
287 public ArrayList GetClippingRectangles ()
289 ArrayList masks = new ArrayList ();
292 masks.Add (new Rectangle (0, 0, x*-1, Height));
294 masks.Add (new Rectangle (x*-1, 0, Width, y*-1));
297 masks.Add (new Rectangle (0, 0, Width, y*-1));
300 foreach (Hwnd child in children) {
302 masks.Add (new Rectangle (child.X, child.Y, child.Width, child.Height));
305 if (parent == null) {
309 ArrayList siblings = parent.children;
311 foreach (Hwnd sibling in siblings) {
312 IntPtr sibling_handle = whole_window;
317 // This entire method should be cached to find all higher views at the time of query
319 sibling_handle = XplatUI.GetPreviousWindow (sibling_handle);
321 if (sibling_handle == sibling.WholeWindow && sibling.visible) {
323 Rectangle intersect = Rectangle.Intersect (new Rectangle (X, Y, Width, Height), new Rectangle (sibling.X, sibling.Y, sibling.Width, sibling.Height));
325 if (intersect == Rectangle.Empty)
331 masks.Add (intersect);
333 } while (sibling_handle != IntPtr.Zero);
339 public static Borders GetBorders (CreateParams cp, Menu menu)
342 Borders borders = new Borders ();
345 int menu_height = menu.Rect.Height;
346 if (menu_height == 0)
347 menu_height = ThemeEngine.Current.CalcMenuBarSize (GraphicsContext, menu, cp.Width);
348 borders.top += menu_height;
351 if (cp.IsSet (WindowStyles.WS_CAPTION)) {
353 if (cp.IsSet (WindowExStyles.WS_EX_TOOLWINDOW)) {
354 caption_height = ThemeEngine.Current.ToolWindowCaptionHeight;
356 caption_height = ThemeEngine.Current.CaptionHeight;
358 borders.top += caption_height;
361 Borders border_width = GetBorderWidth (cp);
363 borders.left += border_width.left;
364 borders.right += border_width.right;
365 borders.top += border_width.top;
366 borders.bottom += border_width.bottom;
371 public static Rectangle GetClientRectangle(CreateParams cp, Menu menu, int width, int height) {
375 borders = GetBorders (cp, menu);
377 rect = new Rectangle(0, 0, width, height);
378 rect.Y += borders.top;
379 rect.Height -= borders.top + borders.bottom;
380 rect.X += borders.left;
381 rect.Width -= borders.left + borders.right;
384 Console.WriteLine ("GetClientRectangle ({0}, {1}, {2}, {3}): {4}", cp, menu != null, width, height, rect);
390 public static Graphics GraphicsContext {
393 bmp = new Bitmap (1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
394 bmp_g = Graphics.FromImage (bmp);
400 #endregion // Static Methods
402 #region Instance Properties
403 public FormBorderStyle BorderStyle {
409 border_style = value;
413 public Rectangle ClientRect {
415 if (client_rectangle == Rectangle.Empty) {
416 return DefaultClientRect;
418 return client_rectangle;
422 client_rectangle = value;
426 public IntPtr Cursor {
436 public IntPtr ClientWindow {
438 return client_window;
442 client_window = value;
447 if (client_window != IntPtr.Zero) {
449 if (windows[client_window] == null) {
450 windows[client_window] = this;
457 public Region UserClip {
467 public Rectangle DefaultClientRect {
469 // We pass a Zero for the menu handle so the menu size is
470 // not computed this is done via an WM_NCCALC
471 CreateParams cp = new CreateParams ();
474 cp.WindowStyle = initial_style;
475 cp.WindowExStyle = initial_ex_style;
477 rect = GetClientRectangle (cp, null, width, height);
483 public bool ExposePending {
485 return expose_pending;
489 public IntPtr Handle {
491 if (handle == IntPtr.Zero) {
492 throw new ArgumentNullException("Handle", "Handle is not yet assigned, need a ClientWindow");
518 public bool Reparented {
528 public uint Opacity {
538 public XEventQueue Queue {
548 public bool Enabled {
554 if (parent != null) {
555 return parent.Enabled;
566 public IntPtr EnabledHwnd {
568 if (Enabled || parent == null) {
572 return parent.EnabledHwnd;
576 public Point MenuOrigin {
578 Form frm = Control.FromHandle (handle) as Form;
579 if (frm != null && frm.window_manager != null)
580 return frm.window_manager.GetMenuOrigin ();
583 Size border_3D_size = ThemeEngine.Current.Border3DSize;
585 pt = new Point(0, 0);
587 if (border_style == FormBorderStyle.Fixed3D) {
588 pt.X += border_3D_size.Width;
589 pt.Y += border_3D_size.Height;
590 } else if (border_style == FormBorderStyle.FixedSingle) {
595 if (this.title_style == TitleStyle.Normal) {
596 pt.Y += caption_height;
597 } else if (this.title_style == TitleStyle.Normal) {
598 pt.Y += tool_caption_height;
605 public Rectangle Invalid {
607 if (invalid_list.Count == 0)
608 return Rectangle.Empty;
610 Rectangle result = (Rectangle)invalid_list[0];
611 for (int i = 1; i < invalid_list.Count; i ++) {
612 result = Rectangle.Union (result, (Rectangle)invalid_list[i]);
618 public Rectangle[] ClipRectangles {
620 return (Rectangle[]) invalid_list.ToArray (typeof (Rectangle));
624 public Rectangle NCInvalid {
625 get { return nc_invalid; }
626 set { nc_invalid = value; }
630 public bool NCExposePending {
632 return nc_expose_pending;
643 parent.children.Remove (this);
646 parent.children.Add (this);
656 if (parent != null) {
657 return parent.Mapped;
668 public int CaptionHeight {
669 get { return caption_height; }
670 set { caption_height = value; }
673 public int ToolCaptionHeight {
674 get { return tool_caption_height; }
675 set { tool_caption_height = value; }
678 public TitleStyle TitleStyle {
688 public object UserData {
698 public IntPtr WholeWindow {
704 whole_window = value;
708 if (whole_window != IntPtr.Zero) {
710 if (windows[whole_window] == null) {
711 windows[whole_window] = this;
728 public bool Visible {
758 #endregion // Instance properties
761 public void AddInvalidArea(int x, int y, int width, int height) {
762 AddInvalidArea(new Rectangle(x, y, width, height));
765 public void AddInvalidArea(Rectangle rect) {
766 ArrayList tmp = new ArrayList ();
767 foreach (Rectangle r in invalid_list) {
768 if (!rect.Contains (r)) {
776 public void ClearInvalidArea() {
777 invalid_list.Clear();
778 expose_pending = false;
781 public void AddNcInvalidArea(int x, int y, int width, int height) {
782 if (nc_invalid == Rectangle.Empty) {
783 nc_invalid = new Rectangle (x, y, width, height);
788 right = Math.Max (nc_invalid.Right, x + width);
789 bottom = Math.Max (nc_invalid.Bottom, y + height);
790 nc_invalid.X = Math.Min (nc_invalid.X, x);
791 nc_invalid.Y = Math.Min (nc_invalid.Y, y);
793 nc_invalid.Width = right - nc_invalid.X;
794 nc_invalid.Height = bottom - nc_invalid.Y;
797 public void AddNcInvalidArea(Rectangle rect) {
798 if (nc_invalid == Rectangle.Empty) {
802 nc_invalid = Rectangle.Union (nc_invalid, rect);
805 public void ClearNcInvalidArea() {
806 nc_invalid = Rectangle.Empty;
807 nc_expose_pending = false;
810 public override string ToString() {
811 return String.Format("Hwnd, Mapped:{3} ClientWindow:0x{0:X}, WholeWindow:0x{1:X}, Zombie={4}, Parent:[{2:X}]", client_window.ToInt32(), whole_window.ToInt32(), parent != null ? parent.ToString() : "<null>", Mapped, zombie);
814 public static Point GetNextStackedFormLocation (CreateParams cp, Hwnd parent_hwnd)
816 if (cp.control == null)
821 Point previous, next;
824 if (parent_hwnd != null) {
825 Control parent = cp.control.Parent;
826 previous = parent_hwnd.previous_child_startup_location;
827 if (parent_hwnd.client_rectangle == Rectangle.Empty && parent != null) {
828 within = parent.ClientRectangle;
830 within = parent_hwnd.client_rectangle;
833 previous = Hwnd.previous_main_startup_location;
834 within = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
837 if (previous.X == int.MinValue || previous.Y == int.MinValue) {
840 next = new Point (previous.X + 22, previous.Y + 22);
843 if (!within.Contains (next.X * 3, next.Y * 3)) {
847 if (next == Point.Empty && cp.Parent == IntPtr.Zero) {
848 next = new Point (22, 22);
851 if (parent_hwnd != null) {
852 parent_hwnd.previous_child_startup_location = next;
854 Hwnd.previous_main_startup_location = next;
857 if (X == int.MinValue && Y == int.MinValue) {
862 return new Point (X, Y);
865 #endregion // Methods
867 internal struct Borders
874 public void Inflate (Size size)
879 bottom += size.Height;
882 public override string ToString ()
884 return string.Format("{{top={0}, bottom={1}, left={2}, right={3}}}", top, bottom, left, right);
887 public static bool operator == (Borders a, Borders b)
889 return (a.left == b.left && a.right == b.right && a.top == b.top && a.bottom == b.bottom);
892 public static bool operator != (Borders a, Borders b)
897 public override bool Equals (object obj)
899 return base.Equals (obj);
902 public override int GetHashCode ()
904 return base.GetHashCode ();