[winforms] Small optimizations, use properties that do not compute values
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.CarbonInternal / ControlHandler.cs
index 2f56d0cd1b5e324550b4ea27a3f3786536a28e61..9064c27291aa6eb374a2363e3d191241539d2824 100644 (file)
@@ -85,16 +85,19 @@ namespace System.Windows.Forms.CarbonInternal {
 
                internal const uint kEventParamCGContextRef = 1668183160;
                internal const uint kEventParamDirectObject = 757935405;
-               internal const uint kEventParamMouseButton = 1835168878;
-               internal const uint kEventParamMouseLocation = 1835822947;
+               internal const uint kEventParamControlPart = 1668313716;
+               internal const uint kEventParamControlLikesDrag = 1668047975;
+               internal const uint kEventParamRgnHandle = 1919381096;
                internal const uint typeControlRef = 1668575852;
                internal const uint typeCGContextRef = 1668183160;
-               internal const uint typeMouseButton = 1835168878;
                internal const uint typeQDPoint = 1363439732;
+               internal const uint typeQDRgnHandle = 1919381096;
+               internal const uint typeControlPartCode = 1668313716;
+               internal const uint typeBoolean = 1651470188;
 
                internal ControlHandler (XplatUICarbon driver) : base (driver) {}
 
-               public bool ProcessEvent (IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
+               public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
                        Hwnd hwnd;
                        bool client;
 
@@ -109,15 +112,49 @@ namespace System.Windows.Forms.CarbonInternal {
 
                        switch (kind) {
                                case kEventControlDraw: {
-                                       HIRect view_bounds = new HIRect ();
+                                       IntPtr rgn = IntPtr.Zero;
+                                       HIRect bounds = new HIRect ();
+                               
+                                       GetEventParameter (eventref, kEventParamRgnHandle, typeQDRgnHandle, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref rgn);
+
+                                       if (rgn != IntPtr.Zero) {
+                                               Rect rbounds = new Rect ();
+                                               
+                                               GetRegionBounds (rgn, ref rbounds);
+                                               
+                                               bounds.origin.x = rbounds.left;
+                                               bounds.origin.y = rbounds.top;
+                                               bounds.size.width = rbounds.right - rbounds.left;
+                                               bounds.size.height = rbounds.bottom - rbounds.top;
+                                       } else {
+                                               HIViewGetBounds (handle, ref bounds);
+                                       }
+
+                                       if (!hwnd.visible) {
+                                               if (client) {
+                                                       hwnd.expose_pending = false;
+                                               } else {
+                                                       hwnd.nc_expose_pending = false;
+                                               }
+                                                return false;
+                                       }
 
-                                       HIViewGetBounds (handle, ref view_bounds);
-                                       Driver.AddExpose (hwnd, client, view_bounds);
-                                       DrawBackground (hwnd, eventref, view_bounds);
-                                       if (!client)
+                                       if (!client) {
                                                DrawBorders (hwnd);
-                                       
-                                       break;
+                                       }
+
+                                       Driver.AddExpose (hwnd, client, bounds);
+
+                                       return true;
+                               }
+                               case kEventControlVisibilityChanged: {
+                                       if (client) {
+                                               msg.message = Msg.WM_SHOWWINDOW;
+                                               msg.lParam = (IntPtr) 0;
+                                               msg.wParam = (HIViewIsVisible (handle) ? (IntPtr)1 : (IntPtr)0);
+                                               return true;
+                                       }
+                                       return false;
                                }
                                case kEventControlBoundsChanged: {
                                        HIRect view_frame = new HIRect ();
@@ -130,145 +167,26 @@ namespace System.Windows.Forms.CarbonInternal {
                                                hwnd.Height = (int) view_frame.size.height;
                                                Driver.PerformNCCalc (hwnd);
                                        }
-                                       return false;
-                               }
-                               case kEventControlClick: {
-                                       QDPoint point = new QDPoint ();
-                                       ushort button = 0;
-
-                                       if (XplatUICarbon.GrabHwnd != null) {
-                                               hwnd = XplatUICarbon.GrabHwnd; 
-                                               msg.hwnd = hwnd.Handle;
-                                               client = true;
-                                       }
-                                       
-                                       GetEventParameter (eventref, kEventParamMouseLocation, typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point);
-                                       GetEventParameter (eventref, kEventParamMouseButton, typeMouseButton, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (ushort)), IntPtr.Zero, ref button);
-
-                                       Driver.ScreenToClient (hwnd.Handle, ref point);
-
-                                       msg.wParam = ButtonTowParam (button);
-                                       msg.lParam = (IntPtr) ((ushort) point.y << 16 | (ushort) point.x);
-                                       msg.message = (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE) + ((button - 1) * 3) + 1;
 
-                                       Driver.mouse_position.X = (int) point.x;
-                                       Driver.mouse_position.Y = (int) point.y;
-
-                                       NativeWindow.WndProc (msg.hwnd, msg.message, msg.wParam, msg.lParam);
-                                       
-                                       HandleControlClick (handle, point, 0, IntPtr.Zero);
-                                       break;
-                               }
-                               case kEventControlTrack: {
-                                       QDPoint point = new QDPoint ();
-                                       MouseTrackingResult mousestatus = MouseTrackingResult.kMouseTrackingMouseDown;
-
-                                       if (XplatUICarbon.GrabHwnd != null) {
-                                               hwnd = XplatUICarbon.GrabHwnd; 
-                                               client = true;
-                                               msg.hwnd = hwnd.Handle;
-                                       }
-
-                                       GetEventParameter (eventref, kEventParamMouseLocation, typeQDPoint, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (QDPoint)), IntPtr.Zero, ref point);
-                                       
-                                       while (mousestatus != MouseTrackingResult.kMouseTrackingMouseUp) {
-                                               uint modifiers = 0; 
-
-                                               if (mousestatus == MouseTrackingResult.kMouseTrackingMouseDragged) {
-                                                       Driver.ScreenToClient (hwnd.Handle, ref point);
-                                                       NativeWindow.WndProc (hwnd.Handle, (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE), Driver.GetMousewParam (0), (IntPtr) ((ushort) point.y << 16 | (ushort) point.x));
-                                               }
-                                               Driver.FlushQueue ();
-
-                                               TrackMouseLocationWithOptions (IntPtr.Zero, 0, 0.01, ref point, ref modifiers, ref mousestatus);
-                                       }
-                                       Driver.ScreenToClient (hwnd.Handle, ref point);
-                                       
-                                       msg.message = (client ? Msg.WM_MOUSEMOVE : Msg.WM_NCMOUSEMOVE) + ((StateToButton (XplatUICarbon.MouseState) - 1) * 3) + 2;
-                                       msg.wParam = StateTowParam (XplatUICarbon.MouseState);
-                                       msg.lParam = (IntPtr) ((ushort) point.y << 16 | (ushort) point.x);
-
-                                       Driver.mouse_position.X = (int) point.x;
-                                       Driver.mouse_position.Y = (int) point.y;
+                                       msg.message = Msg.WM_WINDOWPOSCHANGED;
+                                       msg.hwnd = hwnd.Handle;
 
                                        return true;
                                }
+                               case kEventControlGetFocusPart: {
+                                       short pcode = 0;
+                                       SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
+                                       return false;
+                               }
+                               case kEventControlDragEnter: 
+                               case kEventControlDragWithin: 
+                               case kEventControlDragLeave: 
+                               case kEventControlDragReceive: 
+                                       return Dnd.HandleEvent (callref, eventref, handle, kind, ref msg);
                        }
                        return false;
                }
 
-               private int StateToButton (MouseButtons state) {
-                       int button = 0;
-
-                       switch (state) {
-                               case MouseButtons.Left: 
-                                       button = 1;
-                                       break;
-                               case MouseButtons.Right: 
-                                       button = 2;
-                                       break;
-                               case MouseButtons.Middle:
-                                       button = 3;
-                                       break;
-                       }
-                       
-                       return button;
-               }
-
-               private IntPtr StateTowParam (MouseButtons state) {
-                       int wparam = (int) Driver.GetMousewParam (0);
-
-                       switch (state) {
-                               case MouseButtons.Left: 
-                                       XplatUICarbon.MouseState &= ~MouseButtons.Left;
-                                       wparam &= (int)MsgButtons.MK_LBUTTON;
-                                       break;
-                               case MouseButtons.Right: 
-                                       XplatUICarbon.MouseState &= ~MouseButtons.Right;
-                                       wparam &= (int)MsgButtons.MK_RBUTTON;
-                                       break;
-                               case MouseButtons.Middle:
-                                       XplatUICarbon.MouseState &= ~MouseButtons.Middle;
-                                       wparam &= (int)MsgButtons.MK_MBUTTON;
-                                       break;
-                       }
-                       
-                       return (IntPtr) wparam;
-               }
-
-               private IntPtr ButtonTowParam (ushort button) {
-                       int wparam = (int) Driver.GetMousewParam (0);
-
-                       switch (button) {
-                               case 1:
-                                       XplatUICarbon.MouseState |= MouseButtons.Left;
-                                       wparam |= (int)MsgButtons.MK_LBUTTON;
-                                       break;
-                               case 2:
-                                       XplatUICarbon.MouseState |= MouseButtons.Right;
-                                       wparam |= (int)MsgButtons.MK_RBUTTON;
-                                       break;
-                               case 3:
-                                       XplatUICarbon.MouseState |= MouseButtons.Middle;
-                                       wparam |= (int)MsgButtons.MK_MBUTTON;
-                                       break;
-                       }
-                       
-                       return (IntPtr) wparam;
-               }
-
-               private void DrawBackground (Hwnd hwnd, IntPtr eventref, HIRect bounds) {
-                       if (XplatUICarbon.WindowBackgrounds [hwnd] != null) {
-                               IntPtr context = IntPtr.Zero;
-                               Color color = (Color) XplatUICarbon.WindowBackgrounds [hwnd];
-
-                               GetEventParameter (eventref, kEventParamCGContextRef, typeCGContextRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref context); 
-                               
-                               CGContextSetRGBFillColor (context, (float) color.R / 255, (float) color.G / 255, (float) color.B / 255, (float) color.A / 255);
-                               CGContextFillRect (context, bounds);
-                       }
-               }
-
                private void DrawBorders (Hwnd hwnd) {
                        switch (hwnd.border_style) {
                                case FormBorderStyle.Fixed3D: {
@@ -294,49 +212,18 @@ namespace System.Windows.Forms.CarbonInternal {
                        }
                }
                        
-/*
-
-
-
-                               case Constants.kEventControlSetFocusPart: {
-                                       // This handles setting focus
-                                       short pcode = 1;
-                                       GetEventParameter (inEvent, Constants.EventParamName.kEventParamControlPart, Constants.EventParamType.typeControlPartCode, IntPtr.Zero, (uint)Marshal.SizeOf (typeof (short)), IntPtr.Zero, ref pcode);
-                                       switch (pcode) {
-                                               case 0:
-                                               case -1:
-                                               case -2:
-                                                       pcode = 0;
-                                                       break;
-                                       }
-                                       SetEventParameter (inEvent, Constants.EventParamName.kEventParamControlPart, Constants.EventParamType.typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
-                                       return 0;
-                               }
-                       }
-                       return -9874;
-               }
-*/
                [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
+               static extern int GetRegionBounds (IntPtr rgnhandle, ref Rect region);
                [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref ushort data);
+               static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
                [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref QDPoint data);
-               
+               static extern int SetEventParameter (IntPtr eventref, uint name, uint type, uint size, ref short data);
+
                [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
                static extern int HIViewGetBounds (IntPtr handle, ref HIRect rect);
                [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
                static extern int HIViewGetFrame (IntPtr handle, ref HIRect rect);
-
                [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern void HandleControlClick (IntPtr handle, QDPoint point, uint modifiers, IntPtr callback);
-               [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern void TrackMouseLocationWithOptions (IntPtr port, int options, double eventtimeout, ref QDPoint point, ref uint modifier, ref MouseTrackingResult status);
-               
-               [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern int CGContextSetRGBFillColor (IntPtr cgContext, float r, float g, float b, float alpha);
-               [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
-               static extern int CGContextFillRect (IntPtr context, HIRect rect);
-
+               extern static bool HIViewIsVisible (IntPtr vHnd);
        }
 }