for TARGET_J2EE only:
[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 typeControlRef = 1668575852;
91                 internal const uint typeCGContextRef = 1668183160;
92                 internal const uint typeQDPoint = 1363439732;
93                 internal const uint typeControlPartCode = 1668313716;
94                 internal const uint typeBoolean = 1651470188;
95
96                 internal ControlHandler (XplatUICarbon driver) : base (driver) {}
97
98                 public bool ProcessEvent (IntPtr callref, IntPtr eventref, IntPtr handle, uint kind, ref MSG msg) {
99                         Hwnd hwnd;
100                         bool client;
101
102                         GetEventParameter (eventref, kEventParamDirectObject, typeControlRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref handle);
103                         hwnd = Hwnd.ObjectFromHandle (handle);
104
105                         if (hwnd == null)
106                                 return false;
107
108                         msg.hwnd = hwnd.Handle;
109                         client = (hwnd.ClientWindow == handle ? true : false);
110
111                         switch (kind) {
112                                 case kEventControlDraw: {
113                                         //TODO: This is a hack which masks our over-redraw-expose problem
114                                         if (client) {
115                                                 HIRect bounds = new HIRect ();
116                                                 HIViewGetBounds (handle, ref bounds);
117                                                 Driver.AddExpose (hwnd, client, bounds);
118                                         }
119
120                                         if (!hwnd.visible) {
121                                                 if (client) {
122                                                         hwnd.expose_pending = false;
123                                                 } else {
124                                                         hwnd.nc_expose_pending = false;
125                                                 }
126                                                 return false;
127                                         }
128
129                                         if (client) {
130                                                 if (!hwnd.expose_pending) {
131                                                         return false;
132                                                 }
133                                                 msg.message = Msg.WM_PAINT;
134                                         } else {
135                                                 if (!hwnd.nc_expose_pending) {
136                                                         return false;
137                                                 }
138                                                 //DrawBackground here
139                                                 DrawBorders (hwnd);
140                                                 Region region = new Region (hwnd.Invalid);
141                                                 IntPtr hrgn = region.GetHrgn (null); // Graphics object isn't needed
142                                                 msg.message = Msg.WM_NCPAINT;
143                                                 msg.wParam = hrgn == IntPtr.Zero ? (IntPtr)1 : hrgn;
144                                                 msg.refobject = region;
145                                                 
146                                         }
147                                         return true;
148                                 }
149                                 case kEventControlVisibilityChanged: {
150                                         HIRect bounds = new HIRect ();
151
152                                         HIViewGetBounds (handle, ref bounds);
153                                         Driver.AddExpose (hwnd, client, bounds);
154                                         if (client) {
155                                                 msg.message = Msg.WM_SHOWWINDOW;
156                                                 msg.lParam = (IntPtr) 0;
157                                                 msg.wParam = (HIViewIsVisible (handle) ? (IntPtr)1 : (IntPtr)0);
158                                                 return true;
159                                         }
160                                         return false;
161                                 }
162                                 case kEventControlBoundsChanged: {
163                                         HIRect view_frame = new HIRect ();
164
165                                         HIViewGetFrame (handle, ref view_frame);
166                                         if (!client) {
167                                                 hwnd.X = (int) view_frame.origin.x;
168                                                 hwnd.Y = (int) view_frame.origin.y;
169                                                 hwnd.Width = (int) view_frame.size.width;
170                                                 hwnd.Height = (int) view_frame.size.height;
171                                                 Driver.PerformNCCalc (hwnd);
172                                         }
173                                         return false;
174                                 }
175                                 case kEventControlGetFocusPart: {
176                                         short pcode = 0;
177                                         SetEventParameter (eventref, kEventParamControlPart, typeControlPartCode, (uint)Marshal.SizeOf (typeof (short)), ref pcode);
178                                         return false;
179                                 }
180                                 case kEventControlDragEnter: 
181                                 case kEventControlDragWithin: 
182                                 case kEventControlDragLeave: 
183                                 case kEventControlDragReceive: 
184                                         return Dnd.HandleEvent (callref, eventref, handle, kind, ref msg);
185                         }
186                         return false;
187                 }
188
189                 private void DrawBackground (Hwnd hwnd, IntPtr eventref, HIRect bounds) {
190                         if (XplatUICarbon.WindowBackgrounds [hwnd] != null) {
191                                 IntPtr context = IntPtr.Zero;
192                                 Color color = (Color) XplatUICarbon.WindowBackgrounds [hwnd];
193
194                                 GetEventParameter (eventref, kEventParamCGContextRef, typeCGContextRef, IntPtr.Zero, (uint) Marshal.SizeOf (typeof (IntPtr)), IntPtr.Zero, ref context); 
195                                 
196                                 CGContextSetRGBFillColor (context, (float) color.R / 255, (float) color.G / 255, (float) color.B / 255, (float) color.A / 255);
197                                 CGContextFillRect (context, bounds);
198                         }
199                 }
200
201                 private void DrawBorders (Hwnd hwnd) {
202                         switch (hwnd.border_style) {
203                                 case FormBorderStyle.Fixed3D: {
204                                         Graphics g;
205
206                                         g = Graphics.FromHwnd(hwnd.whole_window);
207                                         if (hwnd.border_static)
208                                                 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.SunkenOuter);
209                                         else
210                                                 ControlPaint.DrawBorder3D(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Border3DStyle.Sunken);
211                                         g.Dispose();
212                                         break;
213                                 }
214
215                                 case FormBorderStyle.FixedSingle: {
216                                         Graphics g;
217
218                                         g = Graphics.FromHwnd(hwnd.whole_window);
219                                         ControlPaint.DrawBorder(g, new Rectangle(0, 0, hwnd.Width, hwnd.Height), Color.Black, ButtonBorderStyle.Solid);
220                                         g.Dispose();
221                                         break;
222                                 }
223                         }
224                 }
225                         
226                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
227                 static extern int GetEventParameter (IntPtr eventref, uint name, uint type, IntPtr outtype, uint size, IntPtr outsize, ref IntPtr data);
228                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
229                 static extern int SetEventParameter (IntPtr eventref, uint name, uint type, uint size, ref short data);
230
231                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
232                 static extern int HIViewGetBounds (IntPtr handle, ref HIRect rect);
233                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
234                 static extern int HIViewGetFrame (IntPtr handle, ref HIRect rect);
235                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
236                 extern static bool HIViewIsVisible (IntPtr vHnd);
237
238                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
239                 static extern int CGContextSetRGBFillColor (IntPtr cgContext, float r, float g, float b, float alpha);
240                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
241                 static extern int CGContextFillRect (IntPtr context, HIRect rect);
242         }
243 }