[winforms] Small optimizations, use properties that do not compute values
[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 kEventParamControlPart = 1668313716;
89                 internal const uint kEventParamControlLikesDrag = 1668047975;
90                 internal const uint kEventParamRgnHandle = 1919381096;
91                 internal const uint typeControlRef = 1668575852;
92                 internal const uint typeCGContextRef = 1668183160;
93                 internal const uint typeQDPoint = 1363439732;
94                 internal const uint typeQDRgnHandle = 1919381096;
95                 internal const uint typeControlPartCode = 1668313716;
96                 internal const uint typeBoolean = 1651470188;
97
98                 internal ControlHandler (XplatUICarbon driver) : base (driver) {}
99
100                 public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
101                         Hwnd hwnd;
102                         bool client;
103
104                         GetEventParameter (eventref, kEventParamDirectObject, typeControlRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
105                         hwnd = Hwnd.ObjectFromHandle (handle);
106
107                         if (hwnd == null)
108                                 return false;
109
110                         msg.hwnd = hwnd.Handle;
111                         client = (hwnd.ClientWindow == handle ? true : false);
112
113                         switch (kind) {
114                                 case kEventControlDraw: {
115                                         IntPtr rgn = IntPtr.Zero;
116                                         HIRect bounds = new HIRect ();
117                                 
118                                         GetEventParameter (eventref, kEventParamRgnHandle, typeQDRgnHandle, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref rgn);
119
120                                         if (rgn != IntPtr.Zero) {
121                                                 Rect rbounds = new Rect ();
122                                                 
123                                                 GetRegionBounds (rgn, ref rbounds);
124                                                 
125                                                 bounds.origin.x = rbounds.left;
126                                                 bounds.origin.y = rbounds.top;
127                                                 bounds.size.width = rbounds.right - rbounds.left;
128                                                 bounds.size.height = rbounds.bottom - rbounds.top;
129                                         } else {
130                                                 HIViewGetBounds (handle, ref bounds);
131                                         }
132
133                                         if (!hwnd.visible) {
134                                                 if (client) {
135                                                         hwnd.expose_pending = false;
136                                                 } else {
137                                                         hwnd.nc_expose_pending = false;
138                                                 }
139                                                 return false;
140                                         }
141
142                                         if (!client) {
143                                                 DrawBorders (hwnd);
144                                         }
145
146                                         Driver.AddExpose (hwnd, client, bounds);
147
148                                         return true;
149                                 }
150                                 case kEventControlVisibilityChanged: {
151                                         if (client) {
152                                                 msg.message = Msg.WM_SHOWWINDOW;
153                                                 msg.lParam = (IntPtr) 0;
154                                                 msg.wParam = (HIViewIsVisible (handle) ? (IntPtr)1 : (IntPtr)0);
155                                                 return true;
156                                         }
157                                         return false;
158                                 }
159                                 case kEventControlBoundsChanged: {
160                                         HIRect view_frame = new HIRect ();
161
162                                         HIViewGetFrame (handle, ref view_frame);
163                                         if (!client) {
164                                                 hwnd.X = (int) view_frame.origin.x;
165                                                 hwnd.Y = (int) view_frame.origin.y;
166                                                 hwnd.Width = (int) view_frame.size.width;
167                                                 hwnd.Height = (int) view_frame.size.height;
168                                                 Driver.PerformNCCalc (hwnd);
169                                         }
170
171                                         msg.message = Msg.WM_WINDOWPOSCHANGED;
172                                         msg.hwnd = hwnd.Handle;
173
174                                         return true;
175                                 }
176                                 case kEventControlGetFocusPart: {
177                                         short pcode = 0;
178                                         SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
179                                         return false;
180                                 }
181                                 case kEventControlDragEnter: 
182                                 case kEventControlDragWithin: 
183                                 case kEventControlDragLeave: 
184                                 case kEventControlDragReceive: 
185                                         return Dnd.HandleEvent (callref, eventref, handle, kind, ref msg);
186                         }
187                         return false;
188                 }
189
190                 private void DrawBorders (Hwnd hwnd) {
191                         switch (hwnd.border_style) {
192                                 case FormBorderStyle.Fixed3D: {
193                                         Graphics g;
194
195                                         g = Graphics.FromHwnd(hwnd.whole_window);
196                                         if (hwnd.border_static)
197                                                 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.SunkenOuter);
198                                         else
199                                                 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
200                                         g.Dispose();
201                                         break;
202                                 }
203
204                                 case FormBorderStyle.FixedSingle: {
205                                         Graphics g;
206
207                                         g = Graphics.FromHwnd(hwnd.whole_window);
208                                         ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
209                                         g.Dispose();
210                                         break;
211                                 }
212                         }
213                 }
214                         
215                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
216                 static extern int GetRegionBounds (IntPtr rgnhandle, ref Rect region);
217                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
218                 static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
219                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
220                 static extern int SetEventParameter (IntPtr eventref, uint name, uint type, uint size, ref short data);
221
222                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
223                 static extern int HIViewGetBounds (IntPtr handle, ref HIRect rect);
224                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
225                 static extern int HIViewGetFrame (IntPtr handle, ref HIRect rect);
226                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
227                 extern static bool HIViewIsVisible (IntPtr vHnd);
228         }
229 }