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) 2007 Novell, Inc.
23 // Geoff Norton <gnorton@novell.com>
29 using System.Runtime.InteropServices;
31 namespace System.Windows.Forms.CarbonInternal {
32 internal class ControlHandler : EventHandlerBase, IEventHandler {
33 internal const uint kEventControlInitialize = 1000;
34 internal const uint kEventControlDispose = 1001;
35 internal const uint kEventControlGetOptimalBounds = 1003;
36 internal const uint kEventControlDefInitialize = kEventControlInitialize;
37 internal const uint kEventControlDefDispose = kEventControlDispose;
38 internal const uint kEventControlHit = 1;
39 internal const uint kEventControlSimulateHit = 2;
40 internal const uint kEventControlHitTest = 3;
41 internal const uint kEventControlDraw = 4;
42 internal const uint kEventControlApplyBackground = 5;
43 internal const uint kEventControlApplyTextColor = 6;
44 internal const uint kEventControlSetFocusPart = 7;
45 internal const uint kEventControlGetFocusPart = 8;
46 internal const uint kEventControlActivate = 9;
47 internal const uint kEventControlDeactivate = 10;
48 internal const uint kEventControlSetCursor = 11;
49 internal const uint kEventControlContextualMenuClick = 12;
50 internal const uint kEventControlClick = 13;
51 internal const uint kEventControlGetNextFocusCandidate = 14;
52 internal const uint kEventControlGetAutoToggleValue = 15;
53 internal const uint kEventControlInterceptSubviewClick = 16;
54 internal const uint kEventControlGetClickActivation = 17;
55 internal const uint kEventControlDragEnter = 18;
56 internal const uint kEventControlDragWithin = 19;
57 internal const uint kEventControlDragLeave = 20;
58 internal const uint kEventControlDragReceive = 21;
59 internal const uint kEventControlInvalidateForSizeChange = 22;
60 internal const uint kEventControlTrackingAreaEntered = 23;
61 internal const uint kEventControlTrackingAreaExited = 24;
62 internal const uint kEventControlTrack = 51;
63 internal const uint kEventControlGetScrollToHereStartPoint = 52;
64 internal const uint kEventControlGetIndicatorDragConstraint = 53;
65 internal const uint kEventControlIndicatorMoved = 54;
66 internal const uint kEventControlGhostingFinished = 55;
67 internal const uint kEventControlGetActionProcPart = 56;
68 internal const uint kEventControlGetPartRegion = 101;
69 internal const uint kEventControlGetPartBounds = 102;
70 internal const uint kEventControlSetData = 103;
71 internal const uint kEventControlGetData = 104;
72 internal const uint kEventControlGetSizeConstraints= 105;
73 internal const uint kEventControlGetFrameMetrics = 106;
74 internal const uint kEventControlValueFieldChanged = 151;
75 internal const uint kEventControlAddedSubControl = 152;
76 internal const uint kEventControlRemovingSubControl = 153;
77 internal const uint kEventControlBoundsChanged = 154;
78 internal const uint kEventControlVisibilityChanged = 157;
79 internal const uint kEventControlTitleChanged = 158;
80 internal const uint kEventControlOwningWindowChanged = 159;
81 internal const uint kEventControlHiliteChanged = 160;
82 internal const uint kEventControlEnabledStateChanged = 161;
83 internal const uint kEventControlLayoutInfoChanged = 162;
84 internal const uint kEventControlArbitraryMessage = 201;
86 internal const uint kEventParamCGContextRef = 1668183160;
87 internal const uint kEventParamDirectObject = 757935405;
88 internal const uint kEventParamMouseButton = 1835168878;
89 internal const uint kEventParamMouseLocation = 1835822947;
90 internal const uint kEventParamControlPart = 1668313716;
91 internal const uint typeControlRef = 1668575852;
92 internal const uint typeCGContextRef = 1668183160;
93 internal const uint typeMouseButton = 1835168878;
94 internal const uint typeQDPoint = 1363439732;
95 internal const uint typeControlPartCode = 1668313716;
97 internal ControlHandler (XplatUICarbon driver) : base (driver) {}
99 public bool ProcessEvent (IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
103 GetEventParameter (eventref, kEventParamDirectObject, typeControlRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
104 hwnd = Hwnd.ObjectFromHandle (handle);
109 msg.hwnd = hwnd.Handle;
110 client = (hwnd.ClientWindow == handle ? true : false);
113 case kEventControlDraw: {
114 HIRect view_bounds = new HIRect ();
116 HIViewGetBounds (handle, ref view_bounds);
117 Driver.AddExpose (hwnd, client, view_bounds);
118 DrawBackground (hwnd, eventref, view_bounds);
124 case kEventControlBoundsChanged: {
125 HIRect view_frame = new HIRect ();
127 HIViewGetFrame (handle, ref view_frame);
129 hwnd.X = (int) view_frame.origin.x;
130 hwnd.Y = (int) view_frame.origin.y;
131 hwnd.Width = (int) view_frame.size.width;
132 hwnd.Height = (int) view_frame.size.height;
133 Driver.PerformNCCalc (hwnd);
137 case kEventControlClick: {
138 QDPoint point = new QDPoint ();
141 if (XplatUICarbon.GrabHwnd != null) {
142 hwnd = XplatUICarbon.GrabHwnd;
143 msg.hwnd = hwnd.Handle;
147 GetEventParameter (eventref, kEventParamMouseLocation, typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point);
148 GetEventParameter (eventref, kEventParamMouseButton, typeMouseButton, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (ushort)), IntPtr.Zero, ref button);
150 Driver.ScreenToClient (hwnd.Handle, ref point);
152 msg.wParam = ButtonTowParam (button);
153 msg.lParam = (IntPtr) ((ushort) point.y << 16 | (ushort) point.x);
154 msg.message = (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE) + ((button - 1) * 3) + 1;
156 Driver.mouse_position.X = (int) point.x;
157 Driver.mouse_position.Y = (int) point.y;
159 NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
161 HandleControlClick (handle, point, 0, IntPtr.Zero);
164 case kEventControlTrack: {
165 QDPoint point = new QDPoint ();
166 MouseTrackingResult mousestatus = MouseTrackingResult.kMouseTrackingMouseDown;
168 if (XplatUICarbon.GrabHwnd != null) {
169 hwnd = XplatUICarbon.GrabHwnd;
171 msg.hwnd = hwnd.Handle;
174 GetEventParameter (eventref, kEventParamMouseLocation, typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point);
176 while (mousestatus != MouseTrackingResult.kMouseTrackingMouseUp) {
179 if (mousestatus == MouseTrackingResult.kMouseTrackingMouseDragged) {
180 Driver.ScreenToClient (hwnd.Handle, ref point);
181 NativeWindow.WndProc (hwnd.Handle, (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE), Driver.GetMousewParam (0), (IntPtr) ((ushort) point.y << 16 | (ushort) point.x));
183 Driver.FlushQueue ();
185 TrackMouseLocationWithOptions (IntPtr.Zero, 0, 0.01, ref point, ref modifiers, ref mousestatus);
187 Driver.ScreenToClient (hwnd.Handle, ref point);
189 msg.message = (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE) + ((StateToButton (XplatUICarbon.MouseState) - 1) * 3) + 2;
190 msg.wParam = StateTowParam (XplatUICarbon.MouseState);
191 msg.lParam = (IntPtr) ((ushort) point.y << 16 | (ushort) point.x);
193 Driver.mouse_position.X = (int) point.x;
194 Driver.mouse_position.Y = (int) point.y;
196 SetKeyboardFocus (GetControlOwner (hwnd.Handle), hwnd.Handle, 1);
199 case kEventControlGetFocusPart: {
201 SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
208 private int StateToButton (MouseButtons state) {
212 case MouseButtons.Left:
215 case MouseButtons.Right:
218 case MouseButtons.Middle:
226 private IntPtr StateTowParam (MouseButtons state) {
227 int wparam = (int) Driver.GetMousewParam (0);
230 case MouseButtons.Left:
231 XplatUICarbon.MouseState &= ~MouseButtons.Left;
232 wparam &= (int)MsgButtons.MK_LBUTTON;
234 case MouseButtons.Right:
235 XplatUICarbon.MouseState &= ~MouseButtons.Right;
236 wparam &= (int)MsgButtons.MK_RBUTTON;
238 case MouseButtons.Middle:
239 XplatUICarbon.MouseState &= ~MouseButtons.Middle;
240 wparam &= (int)MsgButtons.MK_MBUTTON;
244 return (IntPtr) wparam;
247 private IntPtr ButtonTowParam (ushort button) {
248 int wparam = (int) Driver.GetMousewParam (0);
252 XplatUICarbon.MouseState |= MouseButtons.Left;
253 wparam |= (int)MsgButtons.MK_LBUTTON;
256 XplatUICarbon.MouseState |= MouseButtons.Right;
257 wparam |= (int)MsgButtons.MK_RBUTTON;
260 XplatUICarbon.MouseState |= MouseButtons.Middle;
261 wparam |= (int)MsgButtons.MK_MBUTTON;
265 return (IntPtr) wparam;
268 private void DrawBackground (Hwnd hwnd, IntPtr eventref, HIRect bounds) {
269 if (XplatUICarbon.WindowBackgrounds [hwnd] != null) {
270 IntPtr context = IntPtr.Zero;
271 Color color = (Color) XplatUICarbon.WindowBackgrounds [hwnd];
273 GetEventParameter (eventref, kEventParamCGContextRef, typeCGContextRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref context);
275 CGContextSetRGBFillColor (context, (float) color.R / 255, (float) color.G / 255, (float) color.B / 255, (float) color.A / 255);
276 CGContextFillRect (context, bounds);
280 private void DrawBorders (Hwnd hwnd) {
281 switch (hwnd.border_style) {
282 case FormBorderStyle.Fixed3D: {
285 g = Graphics.FromHwnd(hwnd.whole_window);
286 if (hwnd.border_static)
287 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.SunkenOuter);
289 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
294 case FormBorderStyle.FixedSingle: {
297 g = Graphics.FromHwnd(hwnd.whole_window);
298 ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
305 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
306 static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
307 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
308 static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref ushort data);
309 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
310 static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref QDPoint data);
311 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
312 static extern int SetEventParameter (IntPtr eventref, uint name, uint type, uint size, ref short data);
313 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
314 static extern void SetKeyboardFocus (IntPtr window, IntPtr handle, short part);
315 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
316 static extern IntPtr GetControlOwner (IntPtr handle);
319 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
320 static extern int HIViewGetBounds (IntPtr handle, ref HIRect rect);
321 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
322 static extern int HIViewGetFrame (IntPtr handle, ref HIRect rect);
324 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
325 static extern void HandleControlClick (IntPtr handle, QDPoint point, uint modifiers, IntPtr callback);
326 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
327 static extern void TrackMouseLocationWithOptions (IntPtr port, int options, double eventtimeout, ref QDPoint point, ref uint modifier, ref MouseTrackingResult status);
329 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
330 static extern int CGContextSetRGBFillColor (IntPtr cgContext, float r, float g, float b, float alpha);
331 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
332 static extern int CGContextFillRect (IntPtr context, HIRect rect);