ControlHandler.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.Runtime.InteropServices;
  29. namespace System.Windows.Forms.CarbonInternal {
  30. internal class ControlHandler : EventHandlerBase, IEventHandler {
  31. internal const uint kEventControlInitialize = 1000;
  32. internal const uint kEventControlDispose = 1001;
  33. internal const uint kEventControlGetOptimalBounds = 1003;
  34. internal const uint kEventControlDefInitialize = kEventControlInitialize;
  35. internal const uint kEventControlDefDispose = kEventControlDispose;
  36. internal const uint kEventControlHit = 1;
  37. internal const uint kEventControlSimulateHit = 2;
  38. internal const uint kEventControlHitTest = 3;
  39. internal const uint kEventControlDraw = 4;
  40. internal const uint kEventControlApplyBackground = 5;
  41. internal const uint kEventControlApplyTextColor = 6;
  42. internal const uint kEventControlSetFocusPart = 7;
  43. internal const uint kEventControlGetFocusPart = 8;
  44. internal const uint kEventControlActivate = 9;
  45. internal const uint kEventControlDeactivate = 10;
  46. internal const uint kEventControlSetCursor = 11;
  47. internal const uint kEventControlContextualMenuClick = 12;
  48. internal const uint kEventControlClick = 13;
  49. internal const uint kEventControlGetNextFocusCandidate = 14;
  50. internal const uint kEventControlGetAutoToggleValue = 15;
  51. internal const uint kEventControlInterceptSubviewClick = 16;
  52. internal const uint kEventControlGetClickActivation = 17;
  53. internal const uint kEventControlDragEnter = 18;
  54. internal const uint kEventControlDragWithin = 19;
  55. internal const uint kEventControlDragLeave = 20;
  56. internal const uint kEventControlDragReceive = 21;
  57. internal const uint kEventControlInvalidateForSizeChange = 22;
  58. internal const uint kEventControlTrackingAreaEntered = 23;
  59. internal const uint kEventControlTrackingAreaExited = 24;
  60. internal const uint kEventControlTrack = 51;
  61. internal const uint kEventControlGetScrollToHereStartPoint = 52;
  62. internal const uint kEventControlGetIndicatorDragConstraint = 53;
  63. internal const uint kEventControlIndicatorMoved = 54;
  64. internal const uint kEventControlGhostingFinished = 55;
  65. internal const uint kEventControlGetActionProcPart = 56;
  66. internal const uint kEventControlGetPartRegion = 101;
  67. internal const uint kEventControlGetPartBounds = 102;
  68. internal const uint kEventControlSetData = 103;
  69. internal const uint kEventControlGetData = 104;
  70. internal const uint kEventControlGetSizeConstraints= 105;
  71. internal const uint kEventControlGetFrameMetrics = 106;
  72. internal const uint kEventControlValueFieldChanged = 151;
  73. internal const uint kEventControlAddedSubControl = 152;
  74. internal const uint kEventControlRemovingSubControl = 153;
  75. internal const uint kEventControlBoundsChanged = 154;
  76. internal const uint kEventControlVisibilityChanged = 157;
  77. internal const uint kEventControlTitleChanged = 158;
  78. internal const uint kEventControlOwningWindowChanged = 159;
  79. internal const uint kEventControlHiliteChanged = 160;
  80. internal const uint kEventControlEnabledStateChanged = 161;
  81. internal const uint kEventControlLayoutInfoChanged = 162;
  82. internal const uint kEventControlArbitraryMessage = 201;
  83. internal const uint kEventParamCGContextRef = 1668183160;
  84. internal const uint kEventParamDirectObject = 757935405;
  85. internal const uint kEventParamControlPart = 1668313716;
  86. internal const uint kEventParamControlLikesDrag = 1668047975;
  87. internal const uint kEventParamRgnHandle = 1919381096;
  88. internal const uint typeControlRef = 1668575852;
  89. internal const uint typeCGContextRef = 1668183160;
  90. internal const uint typeQDPoint = 1363439732;
  91. internal const uint typeQDRgnHandle = 1919381096;
  92. internal const uint typeControlPartCode = 1668313716;
  93. internal const uint typeBoolean = 1651470188;
  94. internal ControlHandler (XplatUICarbon driver) : base (driver) {}
  95. public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
  96. Hwnd hwnd;
  97. bool client;
  98. GetEventParameter (eventref, kEventParamDirectObject, typeControlRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
  99. hwnd = Hwnd.ObjectFromHandle (handle);
  100. if (hwnd == null)
  101. return false;
  102. msg.hwnd = hwnd.Handle;
  103. client = (hwnd.ClientWindow == handle ? true : false);
  104. switch (kind) {
  105. case kEventControlDraw: {
  106. IntPtr rgn = IntPtr.Zero;
  107. HIRect bounds = new HIRect ();
  108. GetEventParameter (eventref, kEventParamRgnHandle, typeQDRgnHandle, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref rgn);
  109. if (rgn != IntPtr.Zero) {
  110. Rect rbounds = new Rect ();
  111. GetRegionBounds (rgn, ref rbounds);
  112. bounds.origin.x = rbounds.left;
  113. bounds.origin.y = rbounds.top;
  114. bounds.size.width = rbounds.right - rbounds.left;
  115. bounds.size.height = rbounds.bottom - rbounds.top;
  116. } else {
  117. HIViewGetBounds (handle, ref bounds);
  118. }
  119. if (!hwnd.visible) {
  120. if (client) {
  121. hwnd.expose_pending = false;
  122. } else {
  123. hwnd.nc_expose_pending = false;
  124. }
  125. return false;
  126. }
  127. if (!client) {
  128. DrawBorders (hwnd);
  129. }
  130. Driver.AddExpose (hwnd, client, bounds);
  131. return true;
  132. }
  133. case kEventControlVisibilityChanged: {
  134. if (client) {
  135. msg.message = Msg.WM_SHOWWINDOW;
  136. msg.lParam = (IntPtr) 0;
  137. msg.wParam = (HIViewIsVisible (handle) ? (IntPtr)1 : (IntPtr)0);
  138. return true;
  139. }
  140. return false;
  141. }
  142. case kEventControlBoundsChanged: {
  143. HIRect view_frame = new HIRect ();
  144. HIViewGetFrame (handle, ref view_frame);
  145. if (!client) {
  146. hwnd.X = (int) view_frame.origin.x;
  147. hwnd.Y = (int) view_frame.origin.y;
  148. hwnd.Width = (int) view_frame.size.width;
  149. hwnd.Height = (int) view_frame.size.height;
  150. Driver.PerformNCCalc (hwnd);
  151. }
  152. msg.message = Msg.WM_WINDOWPOSCHANGED;
  153. msg.hwnd = hwnd.Handle;
  154. return true;
  155. }
  156. case kEventControlGetFocusPart: {
  157. short pcode = 0;
  158. SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
  159. return false;
  160. }
  161. case kEventControlDragEnter:
  162. case kEventControlDragWithin:
  163. case kEventControlDragLeave:
  164. case kEventControlDragReceive:
  165. return Dnd.HandleEvent (callref, eventref, handle, kind, ref msg);
  166. }
  167. return false;
  168. }
  169. private void DrawBorders (Hwnd hwnd) {
  170. switch (hwnd.border_style) {
  171. case FormBorderStyle.Fixed3D: {
  172. Graphics g;
  173. g = Graphics.FromHwnd(hwnd.whole_window);
  174. if (hwnd.border_static)
  175. ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.SunkenOuter);
  176. else
  177. ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
  178. g.Dispose();
  179. break;
  180. }
  181. case FormBorderStyle.FixedSingle: {
  182. Graphics g;
  183. g = Graphics.FromHwnd(hwnd.whole_window);
  184. ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
  185. g.Dispose();
  186. break;
  187. }
  188. }
  189. }
  190. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  191. static extern int GetRegionBounds (IntPtr rgnhandle, ref Rect region);
  192. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  193. static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
  194. [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  195. static extern int SetEventParameter (IntPtr eventref, uint name, uint type, uint size, ref short data);
  196. [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  197. static extern int HIViewGetBounds (IntPtr handle, ref HIRect rect);
  198. [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  199. static extern int HIViewGetFrame (IntPtr handle, ref HIRect rect);
  200. [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
  201. extern static bool HIViewIsVisible (IntPtr vHnd);
  202. }
  203. }