Merge pull request #853 from echampet/onclick
[mono.git] / mcs / class / System.Windows.Forms / System.Windows.Forms.CarbonInternal / EventHandler.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 // 
8 // The above copyright notice and this permission notice shall be
9 // included in all copies or substantial portions of the Software.
10 // 
11 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
15 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
16 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
17 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 //
19 // Copyright (c) 2007 Novell, Inc.
20 //
21 // Authors:
22 //      Geoff Norton  <gnorton@novell.com>
23 //
24 //
25
26 using System;
27 using System.Runtime.InteropServices;
28
29 namespace System.Windows.Forms.CarbonInternal {
30         internal delegate int EventDelegate (IntPtr callref, IntPtr eventref, IntPtr user_data);
31
32         internal class EventHandler {
33                 internal static EventDelegate EventHandlerDelegate = new EventDelegate (EventCallback);
34                 internal static XplatUICarbon Driver;
35
36                 internal const int EVENT_NOT_HANDLED = 0;
37                 internal const int EVENT_HANDLED = -9874;
38
39                 internal const uint kEventClassMouse = 1836021107;
40                 internal const uint kEventClassKeyboard = 1801812322;
41                 internal const uint kEventClassTextInput = 1952807028;
42                 internal const uint kEventClassApplication = 1634758764;
43                 internal const uint kEventClassAppleEvent = 1701867619;
44                 internal const uint kEventClassMenu = 1835363957;
45                 internal const uint kEventClassWindow = 2003398244;
46                 internal const uint kEventClassControl = 1668183148;
47                 internal const uint kEventClassCommand = 1668113523;
48                 internal const uint kEventClassTablet = 1952607348;
49                 internal const uint kEventClassVolume = 1987013664;
50                 internal const uint kEventClassAppearance = 1634758765;
51                 internal const uint kEventClassService = 1936028278;
52                 internal const uint kEventClassToolbar = 1952604530;
53                 internal const uint kEventClassToolbarItem = 1952606580;
54                 internal const uint kEventClassAccessibility = 1633903461;
55                 internal const uint kEventClassHIObject = 1751740258;
56                 
57                 internal static EventTypeSpec [] HIObjectEvents = new EventTypeSpec [] {
58                                                                         new EventTypeSpec (kEventClassHIObject, HIObjectHandler.kEventHIObjectConstruct),
59                                                                         new EventTypeSpec (kEventClassHIObject, HIObjectHandler.kEventHIObjectInitialize),
60                                                                         new EventTypeSpec (kEventClassHIObject, HIObjectHandler.kEventHIObjectDestruct)
61                                                                         };
62                 internal static EventTypeSpec [] ControlEvents = new EventTypeSpec [] {
63                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlBoundsChanged), 
64                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlDraw),
65                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlDragEnter),
66                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlDragWithin),
67                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlDragLeave),
68                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlDragReceive),
69                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlGetFocusPart), 
70                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlInitialize), 
71                                                                         new EventTypeSpec (kEventClassControl, ControlHandler.kEventControlVisibilityChanged) 
72                                                                         };
73
74                 internal static EventTypeSpec [] ApplicationEvents = new EventTypeSpec[] {
75                                                                         new EventTypeSpec (kEventClassApplication, ApplicationHandler.kEventAppActivated),
76                                                                         new EventTypeSpec (kEventClassApplication, ApplicationHandler.kEventAppDeactivated)
77                                                                         };
78                 
79                 private static EventTypeSpec [] WindowEvents = new EventTypeSpec[] {
80                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseMoved),
81                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseDragged),
82                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseDown),
83                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseUp),
84                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseWheelMoved),
85                                                                         new EventTypeSpec (kEventClassMouse, MouseHandler.kEventMouseScroll),
86
87                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowDeactivated),
88                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowActivated),
89                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowDeactivated),
90                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowCollapsed),
91                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowCollapsing),
92                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowExpanded),
93                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowExpanding),
94                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowBoundsChanged),
95                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowResizeStarted),
96                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowResizeCompleted),
97                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowClose),
98                                                                         new EventTypeSpec (kEventClassWindow, WindowHandler.kEventWindowShown),
99
100                                                                         new EventTypeSpec (kEventClassKeyboard, KeyboardHandler.kEventRawKeyModifiersChanged),
101                                                                         new EventTypeSpec (kEventClassKeyboard, KeyboardHandler.kEventRawKeyDown),
102                                                                         new EventTypeSpec (kEventClassKeyboard, KeyboardHandler.kEventRawKeyRepeat),
103                                                                         new EventTypeSpec (kEventClassKeyboard, KeyboardHandler.kEventRawKeyUp),
104                                                                         new EventTypeSpec (kEventClassTextInput, KeyboardHandler.kEventTextInputUnicodeForKeyEvent)
105                                                                         };
106
107                 internal static int EventCallback (IntPtr callref, IntPtr eventref, IntPtr handle) {
108                         uint klass = GetEventClass (eventref);
109                         uint kind = GetEventKind (eventref);
110                         MSG msg = new MSG ();
111                         IEventHandler handler = null;
112
113                         switch (klass) {
114                                 case kEventClassHIObject: {
115                                         handler = (IEventHandler) Driver.HIObjectHandler;
116                                         break;
117                                 }
118                                 case kEventClassKeyboard:
119                                 case kEventClassTextInput:
120                                         handler = (IEventHandler) Driver.KeyboardHandler;
121                                         break;
122                                 case kEventClassWindow:
123                                         handler = (IEventHandler) Driver.WindowHandler;
124                                         break;
125                                 case kEventClassMouse:
126                                         handler = (IEventHandler) Driver.MouseHandler;
127                                         break;
128                                 case kEventClassControl:
129                                         handler = (IEventHandler) Driver.ControlHandler;
130                                         break;
131                                 case kEventClassApplication:
132                                         handler = (IEventHandler) Driver.ApplicationHandler;
133                                         break;
134                                 default:
135                                         return EVENT_NOT_HANDLED;
136                         }
137
138                         if (handler.ProcessEvent (callref, eventref, handle, kind, ref msg)) {
139                                 Driver.EnqueueMessage (msg);
140                                 return EVENT_HANDLED;
141                         }
142                         
143                         return EVENT_NOT_HANDLED;
144                 }
145
146                 internal static bool TranslateMessage (ref MSG msg) {
147                         bool result = false;
148  
149                         if (!result)
150                                 result = Driver.KeyboardHandler.TranslateMessage (ref msg);
151                         if (!result)
152                                 result = Driver.MouseHandler.TranslateMessage (ref msg);
153
154                         return result;
155                 }
156
157                 internal static void InstallApplicationHandler () {
158                         InstallEventHandler (GetApplicationEventTarget (), EventHandlerDelegate, (uint)ApplicationEvents.Length, ApplicationEvents, IntPtr.Zero, IntPtr.Zero);
159                 }
160
161                 internal static void InstallControlHandler (IntPtr control) {
162                         InstallEventHandler (GetControlEventTarget (control), EventHandlerDelegate, (uint)ControlEvents.Length, ControlEvents, control, IntPtr.Zero);
163                 }
164                 
165                 internal static void InstallWindowHandler (IntPtr window) {
166                         InstallEventHandler (GetWindowEventTarget (window), EventHandlerDelegate, (uint)WindowEvents.Length, WindowEvents, window, IntPtr.Zero);
167                 }
168
169                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
170                 static extern IntPtr GetApplicationEventTarget ();
171                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
172                 internal static extern IntPtr GetControlEventTarget (IntPtr control);
173                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
174                 internal static extern IntPtr GetWindowEventTarget (IntPtr window);
175
176                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
177                 internal static extern uint GetEventClass (IntPtr eventref);
178                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
179                 static extern uint GetEventKind (IntPtr eventref);
180                 
181                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
182                 static extern int InstallEventHandler (IntPtr window, EventDelegate event_handler, uint count, EventTypeSpec [] types, IntPtr user_data, IntPtr handlerref);
183         }
184 }