WindowHandler.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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) 2007 Novell, Inc.
  21. //
  22. // Authors:
  23. // Geoff Norton <[email protected]>
  24. //
  25. //
  26. using System;
  27. using System.Drawing;
  28. using System.Windows.Forms;
  29. using System.Runtime.InteropServices;
  30. namespace System.Windows.Forms.CarbonInternal {
  31. internal class WindowHandler : EventHandlerBase, IEventHandler {
  32. internal const uint kEventWindowUpdate = 1;
  33. internal const uint kEventWindowDrawContent = 2;
  34. internal const uint kEventWindowActivated = 5;
  35. internal const uint kEventWindowDeactivated = 6;
  36. internal const uint kEventWindowGetClickActivation = 7;
  37. internal const uint kEventWindowShowing = 22;
  38. internal const uint kEventWindowHiding = 23;
  39. internal const uint kEventWindowShown = 24;
  40. internal const uint kEventWindowHidden = 25;
  41. internal const uint kEventWindowCollapsing = 86;
  42. internal const uint kEventWindowExpanding = 87;
  43. internal const uint kEventWindowZoomed = 76;
  44. internal const uint kEventWindowBoundsChanging = 26;
  45. internal const uint kEventWindowBoundsChanged = 27;
  46. internal const uint kEventWindowResizeStarted = 28;
  47. internal const uint kEventWindowResizeCompleted = 29;
  48. internal const uint kEventWindowDragStarted = 30;
  49. internal const uint kEventWindowDragCompleted = 31;
  50. internal const uint kEventWindowTransitionStarted = 88;
  51. internal const uint kEventWindowTransitionCompleted = 89;
  52. internal const uint kEventWindowClickDragRgn = 32;
  53. internal const uint kEventWindowClickResizeRgn = 33;
  54. internal const uint kEventWindowClickCollapseRgn = 34;
  55. internal const uint kEventWindowClickCloseRgn = 35;
  56. internal const uint kEventWindowClickZoomRgn = 36;
  57. internal const uint kEventWindowClickContentRgn = 37;
  58. internal const uint kEventWindowClickProxyIconRgn = 38;
  59. internal const uint kEventWindowClickToolbarButtonRgn = 41;
  60. internal const uint kEventWindowClickStructureRgn = 42;
  61. internal const uint kEventWindowCursorChange = 40;
  62. internal const uint kEventWindowCollapse = 66;
  63. internal const uint kEventWindowCollapsed = 67;
  64. internal const uint kEventWindowCollapseAll = 68;
  65. internal const uint kEventWindowExpand = 69;
  66. internal const uint kEventWindowExpanded = 70;
  67. internal const uint kEventWindowExpandAll = 71;
  68. internal const uint kEventWindowClose = 72;
  69. internal const uint kEventWindowClosed = 73;
  70. internal const uint kEventWindowCloseAll = 74;
  71. internal const uint kEventWindowZoom = 75;
  72. internal const uint kEventWindowZoomAll = 77;
  73. internal const uint kEventWindowContextualMenuSelect = 78;
  74. internal const uint kEventWindowPathSelect = 79;
  75. internal const uint kEventWindowGetIdealSize = 80;
  76. internal const uint kEventWindowGetMinimumSize = 81;
  77. internal const uint kEventWindowGetMaximumSize = 82;
  78. internal const uint kEventWindowConstrain = 83;
  79. internal const uint kEventWindowHandleContentClick = 85;
  80. internal const uint kEventWindowGetDockTileMenu = 90;
  81. internal const uint kEventWindowHandleActivate = 91;
  82. internal const uint kEventWindowHandleDeactivate = 92;
  83. internal const uint kEventWindowProxyBeginDrag = 128;
  84. internal const uint kEventWindowProxyEndDrag = 129;
  85. internal const uint kEventWindowToolbarSwitchMode = 150;
  86. internal const uint kEventWindowFocusAcquired = 200;
  87. internal const uint kEventWindowFocusRelinquish = 201;
  88. internal const uint kEventWindowFocusContent = 202;
  89. internal const uint kEventWindowFocusToolbar = 203;
  90. internal const uint kEventWindowDrawerOpening = 220;
  91. internal const uint kEventWindowDrawerOpened = 221;
  92. internal const uint kEventWindowDrawerClosing = 222;
  93. internal const uint kEventWindowDrawerClosed = 223;
  94. internal const uint kEventWindowDrawFrame = 1000;
  95. internal const uint kEventWindowDrawPart = 1001;
  96. internal const uint kEventWindowGetRegion = 1002;
  97. internal const uint kEventWindowHitTest = 1003;
  98. internal const uint kEventWindowInit = 1004;
  99. internal const uint kEventWindowDispose = 1005;
  100. internal const uint kEventWindowDragHilite = 1006;
  101. internal const uint kEventWindowModified = 1007;
  102. internal const uint kEventWindowSetupProxyDragImage = 1008;
  103. internal const uint kEventWindowStateChanged = 1009;
  104. internal const uint kEventWindowMeasureTitle = 1010;
  105. internal const uint kEventWindowDrawGrowBox = 1011;
  106. internal const uint kEventWindowGetGrowImageRegion = 1012;
  107. internal const uint kEventWindowPaint = 1013;
  108. internal WindowHandler (XplatUICarbon driver) : base (driver) {}
  109. public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
  110. IntPtr window = Driver.HandleToWindow (handle);
  111. Hwnd hwnd = Hwnd.ObjectFromHandle (window);
  112. if (window != IntPtr.Zero) {
  113. switch (kind) {
  114. case kEventWindowActivated: {
  115. Control c = Control.FromHandle (hwnd.client_window);
  116. if (c != null) {
  117. Form form = c.FindForm ();
  118. if (form != null && !form.IsDisposed) {
  119. Driver.SendMessage (form.Handle, Msg.WM_ACTIVATE, (IntPtr) WindowActiveFlags.WA_ACTIVE, IntPtr.Zero);
  120. XplatUICarbon.ActiveWindow = hwnd.client_window;
  121. }
  122. }
  123. foreach (IntPtr utility_window in XplatUICarbon.UtilityWindows) {
  124. if (utility_window != handle && !XplatUICarbon.IsWindowVisible (utility_window))
  125. XplatUICarbon.ShowWindow (utility_window);
  126. }
  127. break;
  128. }
  129. case kEventWindowExpanding:
  130. foreach (IntPtr utility_window in XplatUICarbon.UtilityWindows) {
  131. if (utility_window != handle && !XplatUICarbon.IsWindowVisible (utility_window))
  132. XplatUICarbon.ShowWindow (utility_window);
  133. }
  134. msg.hwnd = hwnd.Handle;
  135. msg.message = Msg.WM_ENTERSIZEMOVE;
  136. return true;
  137. case kEventWindowExpanded:
  138. NativeWindow.WndProc (hwnd.Handle, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
  139. msg.hwnd = hwnd.Handle;
  140. msg.message = Msg.WM_EXITSIZEMOVE;
  141. return true;
  142. case kEventWindowDeactivated: {
  143. Control c = Control.FromHandle (hwnd.client_window);
  144. if (c != null) {
  145. Form form = c.FindForm ();
  146. if (form != null && XplatUICarbon.UnactiveWindow != form.Handle) {
  147. Driver.SendMessage (form.Handle, Msg.WM_ACTIVATE, (IntPtr) WindowActiveFlags.WA_INACTIVE, IntPtr.Zero);
  148. XplatUICarbon.ActiveWindow = IntPtr.Zero;
  149. }
  150. }
  151. foreach (IntPtr utility_window in XplatUICarbon.UtilityWindows) {
  152. if (utility_window != handle && XplatUICarbon.IsWindowVisible (utility_window))
  153. XplatUICarbon.HideWindow (utility_window);
  154. }
  155. break;
  156. }
  157. case kEventWindowCollapsing:
  158. foreach (IntPtr utility_window in XplatUICarbon.UtilityWindows) {
  159. if (utility_window != handle && XplatUICarbon.IsWindowVisible (utility_window))
  160. XplatUICarbon.HideWindow (utility_window);
  161. }
  162. msg.hwnd = hwnd.Handle;
  163. msg.message = Msg.WM_ENTERSIZEMOVE;
  164. return true;
  165. case kEventWindowCollapsed:
  166. NativeWindow.WndProc (hwnd.Handle, Msg.WM_WINDOWPOSCHANGED, IntPtr.Zero, IntPtr.Zero);
  167. msg.hwnd = hwnd.Handle;
  168. msg.message = Msg.WM_EXITSIZEMOVE;
  169. return true;
  170. case kEventWindowClose:
  171. NativeWindow.WndProc (hwnd.Handle, Msg.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
  172. return false;
  173. case kEventWindowShown: {
  174. msg.message = Msg.WM_SHOWWINDOW;
  175. msg.lParam = (IntPtr) 1;
  176. msg.wParam = (IntPtr) 0;
  177. msg.hwnd = hwnd.Handle;
  178. return true;
  179. }
  180. case kEventWindowResizeStarted: {
  181. msg.message = Msg.WM_ENTERSIZEMOVE;
  182. msg.hwnd = hwnd.Handle;
  183. return true;
  184. }
  185. case kEventWindowResizeCompleted: {
  186. msg.message = Msg.WM_EXITSIZEMOVE;
  187. msg.hwnd = hwnd.Handle;
  188. return true;
  189. }
  190. case kEventWindowBoundsChanged: {
  191. Rect window_bounds = new Rect ();
  192. HIRect view_bounds = new HIRect ();
  193. Size size;
  194. GetWindowBounds (handle, 33, ref window_bounds);
  195. view_bounds.size.width = window_bounds.right - window_bounds.left;
  196. view_bounds.size.height = window_bounds.bottom - window_bounds.top;
  197. HIViewSetFrame (hwnd.WholeWindow, ref view_bounds);
  198. size = XplatUICarbon.TranslateQuartzWindowSizeToWindowSize (Control.FromHandle (hwnd.Handle).GetCreateParams (), (int)view_bounds.size.width, (int)view_bounds.size.height);
  199. hwnd.X = (int) window_bounds.left;
  200. hwnd.Y = (int) window_bounds.top;
  201. hwnd.Width = (int) size.Width;
  202. hwnd.Height = (int) size.Height;
  203. Driver.PerformNCCalc (hwnd);
  204. msg.hwnd = hwnd.Handle;
  205. msg.message = Msg.WM_WINDOWPOSCHANGED;
  206. Driver.SetCaretPos (XplatUICarbon.Caret.Hwnd, XplatUICarbon.Caret.X, XplatUICarbon.Caret.Y);
  207. return true;
  208. }
  209. }
  210. }
  211. return false;
  212. }
  213. [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  214. static extern int GetWindowBounds (IntPtr handle, uint region, ref Rect bounds);
  215. [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  216. static extern int HIViewSetFrame (IntPtr handle, ref HIRect bounds);
  217. }
  218. }