2007-11-21 Geoff Norton <gnorton@novell.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.CarbonInternal / ControlHandler.cs
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  <gnorton@novell.com>
24 //
25 //
26
27 using System;
28 using System.Drawing;
29 using System.Runtime.InteropServices;
30
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;
85
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;
96
97                 internal ControlHandler (XplatUICarbon driver) : base (driver) {}
98
99                 public bool ProcessEvent (IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
100                         Hwnd hwnd;
101                         bool client;
102
103                         GetEventParameter (eventref, kEventParamDirectObject, typeControlRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
104                         hwnd = Hwnd.ObjectFromHandle (handle);
105
106                         if (hwnd == null)
107                                 return false;
108
109                         msg.hwnd = hwnd.Handle;
110                         client = (hwnd.ClientWindow == handle ? true : false);
111
112                         switch (kind) {
113                                 case kEventControlDraw: {
114                                         HIRect view_bounds = new HIRect ();
115
116                                         HIViewGetBounds (handle, ref view_bounds);
117                                         Driver.AddExpose (hwnd, client, view_bounds);
118                                         DrawBackground (hwnd, eventref, view_bounds);
119                                         if (!client)
120                                                 DrawBorders (hwnd);
121                                         
122                                         break;
123                                 }
124                                 case kEventControlBoundsChanged: {
125                                         HIRect view_frame = new HIRect ();
126
127                                         HIViewGetFrame (handle, ref view_frame);
128                                         if (!client) {
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);
134                                         }
135                                         return false;
136                                 }
137                                 case kEventControlClick: {
138                                         QDPoint point = new QDPoint ();
139                                         ushort button = 0;
140
141                                         if (XplatUICarbon.GrabHwnd != null) {
142                                                 hwnd = XplatUICarbon.GrabHwnd; 
143                                                 msg.hwnd = hwnd.Handle;
144                                                 client = true;
145                                         }
146                                         
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);
149
150                                         Driver.ScreenToClient (hwnd.Handle, ref point);
151
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;
155
156                                         Driver.mouse_position.X = (int) point.x;
157                                         Driver.mouse_position.Y = (int) point.y;
158
159                                         NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
160                                         
161                                         HandleControlClick (handle, point, 0, IntPtr.Zero);
162                                         break;
163                                 }
164                                 case kEventControlTrack: {
165                                         QDPoint point = new QDPoint ();
166                                         MouseTrackingResult mousestatus = MouseTrackingResult.kMouseTrackingMouseDown;
167
168                                         if (XplatUICarbon.GrabHwnd != null) {
169                                                 hwnd = XplatUICarbon.GrabHwnd; 
170                                                 client = true;
171                                                 msg.hwnd = hwnd.Handle;
172                                         }
173
174                                         GetEventParameter (eventref, kEventParamMouseLocation, typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point);
175                                         
176                                         while (mousestatus != MouseTrackingResult.kMouseTrackingMouseUp) {
177                                                 uint modifiers = 0; 
178
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));
182                                                 }
183                                                 Driver.FlushQueue ();
184
185                                                 TrackMouseLocationWithOptions (IntPtr.Zero, 0, 0.01, ref point, ref modifiers, ref mousestatus);
186                                         }
187                                         Driver.ScreenToClient (hwnd.Handle, ref point);
188                                         
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);
192
193                                         Driver.mouse_position.X = (int) point.x;
194                                         Driver.mouse_position.Y = (int) point.y;
195
196                                         SetKeyboardFocus (GetControlOwner (hwnd.Handle), hwnd.Handle, 1);
197                                         return true;
198                                 }
199                                 case kEventControlGetFocusPart: {
200                                         short pcode = 0;
201                                         SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
202                                         return false;
203                                 }
204                         }
205                         return false;
206                 }
207
208                 private int StateToButton (MouseButtons state) {
209                         int button = 0;
210
211                         switch (state) {
212                                 case MouseButtons.Left: 
213                                         button = 1;
214                                         break;
215                                 case MouseButtons.Right: 
216                                         button = 2;
217                                         break;
218                                 case MouseButtons.Middle:
219                                         button = 3;
220                                         break;
221                         }
222                         
223                         return button;
224                 }
225
226                 private IntPtr StateTowParam (MouseButtons state) {
227                         int wparam = (int) Driver.GetMousewParam (0);
228
229                         switch (state) {
230                                 case MouseButtons.Left: 
231                                         XplatUICarbon.MouseState &= ~MouseButtons.Left;
232                                         wparam &= (int)MsgButtons.MK_LBUTTON;
233                                         break;
234                                 case MouseButtons.Right: 
235                                         XplatUICarbon.MouseState &= ~MouseButtons.Right;
236                                         wparam &= (int)MsgButtons.MK_RBUTTON;
237                                         break;
238                                 case MouseButtons.Middle:
239                                         XplatUICarbon.MouseState &= ~MouseButtons.Middle;
240                                         wparam &= (int)MsgButtons.MK_MBUTTON;
241                                         break;
242                         }
243                         
244                         return (IntPtr) wparam;
245                 }
246
247                 private IntPtr ButtonTowParam (ushort button) {
248                         int wparam = (int) Driver.GetMousewParam (0);
249
250                         switch (button) {
251                                 case 1:
252                                         XplatUICarbon.MouseState |= MouseButtons.Left;
253                                         wparam |= (int)MsgButtons.MK_LBUTTON;
254                                         break;
255                                 case 2:
256                                         XplatUICarbon.MouseState |= MouseButtons.Right;
257                                         wparam |= (int)MsgButtons.MK_RBUTTON;
258                                         break;
259                                 case 3:
260                                         XplatUICarbon.MouseState |= MouseButtons.Middle;
261                                         wparam |= (int)MsgButtons.MK_MBUTTON;
262                                         break;
263                         }
264                         
265                         return (IntPtr) wparam;
266                 }
267
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];
272
273                                 GetEventParameter (eventref, kEventParamCGContextRef, typeCGContextRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref context); 
274                                 
275                                 CGContextSetRGBFillColor (context, (float) color.R / 255, (float) color.G / 255, (float) color.B / 255, (float) color.A / 255);
276                                 CGContextFillRect (context, bounds);
277                         }
278                 }
279
280                 private void DrawBorders (Hwnd hwnd) {
281                         switch (hwnd.border_style) {
282                                 case FormBorderStyle.Fixed3D: {
283                                         Graphics g;
284
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);
288                                         else
289                                                 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
290                                         g.Dispose();
291                                         break;
292                                 }
293
294                                 case FormBorderStyle.FixedSingle: {
295                                         Graphics g;
296
297                                         g = Graphics.FromHwnd(hwnd.whole_window);
298                                         ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
299                                         g.Dispose();
300                                         break;
301                                 }
302                         }
303                 }
304                         
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);
317                 
318
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);
323
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);
328                 
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);
333
334         }
335 }