/// <summary>this is an entry point for all objects handlers</summary> /// <param name="objectHashCode"> hash code of the object</param> /// <param name="eventName"> event name</param> /// <param name="parameters">event parameters</param> /// <returns></returns> public static void handleDotNetEvent(int objectHashCode, String eventName, Object[] parameters) { Object invokedObject = DNManager.getInstance().DNObjectEventsCollection.getObject(objectHashCode); GuiMgControl mgControl = null; bool raiseRuntimeEvent = true; Manager.ContextIDGuard contextIDGuard = null; // If Object is a control and it is 'our' control (not created dynamically, decided by .Tag) if (invokedObject is Control && ((Control)invokedObject).Tag != null) { ControlsMap controlsMap = ControlsMap.getInstance(); MapData mapData = controlsMap.getControlMapData((Control)invokedObject); if (mapData == null) { return; } mgControl = mapData.getControl(); contextIDGuard = new Manager.ContextIDGuard(Manager.GetContextID(mgControl)); //first execute default magic handling of event EventType type = DNControlEvents.getStandardEvent(eventName); if (type != EventType.NONE && parameters.Length == 2) { DefaultHandler.getInstance().handleEvent(type, invokedObject, (EventArgs)parameters[1]); } if (eventName == "KeyDown") //QCR #736734 KeyDown is handled from Filter.cs on WM_KEYDOWN - do not duplicate the event { raiseRuntimeEvent = false; } else if (eventName == "Disposed") { // a Disposed event for a control can't be handled as a runtime-event after the task/form are closed raiseRuntimeEvent = false; } else { // raise .NET runtime event only if hooked from the application (i.e. have a handler in the task) List <String> applicationHookedDNeventsNames = ((TagData)((Control)invokedObject).Tag).ApplicationHookedDNeventsNames; if (applicationHookedDNeventsNames == null || !applicationHookedDNeventsNames.Contains(eventName)) { raiseRuntimeEvent = false; } } } // raise .NET event if (raiseRuntimeEvent) { Events.OnDotNetEvent(invokedObject, mgControl, eventName, parameters); } if (contextIDGuard != null) { contextIDGuard.Dispose(); } }
private void handlePredefinedEvent(string eventName, object sender, EventArgs e) { GuiMgControl guiMgCtrl = null; Debug.Assert(sender is Control && DNControlEvents.isPredefinedEvent(eventName)); ControlsMap controlsMap = ControlsMap.getInstance(); MapData mapData = controlsMap.getMapData(sender); if (mapData != null) { guiMgCtrl = mapData.getControl(); } Events.OnDotNetEvent(sender, guiMgCtrl, eventName, new[] { sender, e }); }
/// <summary> adds events for control</summary> /// <param name="control"></param> /// <param name="dnEventsNames">comma-delimited string of events that should be raised for the object</param> internal void addHandler(Control control, List <String> dnEventsNames) { DNObjectEventsCollection.ObjectEvents objectEvents = DNManager.getInstance().DNObjectEventsCollection.checkAndCreateObjectEvents(control); foreach (string item in dnEventsNames) { ReflectionServices.addHandler(item, control, objectEvents, true); } ICollection <String> standartEvents = DNControlEvents.getStandardEventsNames(); foreach (string item in standartEvents) { if (!dnEventsNames.Contains(item)) { ReflectionServices.addHandler(item, control, objectEvents, false); } } }
/// <summary>add handler for the .NET object: /// create a dynamic delegate according to the given event type /// add method body to the delegate /// in the method body call DefaultDotNetHandler which will handle the event</summary> /// <param name="eventName"> name of the event</param> /// <param name="objectToHook"> object to hook</param> /// <param name="objectEvents"> once registered, events are added to this collection</param> /// <param name="reportErrors"> indicates whether to log errors while hooking event. There are some /// default events that are hooked for each .NET control (see DNControlEvents._standardControlEvents). /// Errors occurring while hooking these events should not be reported.</param> /// TODO handle static events internal static void addHandler(String eventName, Object objectToHook, DNObjectEventsCollection.ObjectEvents objectEvents, bool reportErrors) { //subscribe for events only once , check if handler already added if (objectEvents.delegateExist(eventName)) { return; } Type type = objectToHook.GetType(); // Get an EventInfo representing the event, and get the type of delegate that handles the event. EventInfo eventInfo = type.GetEvent(eventName); if (eventInfo != null) { // Get the "add" accessor of the event and invoke it late-bound, passing in the delegate instance. // This is equivalent to using the += operator in C#, or AddHandler in Visual Basic. // The instance on which the "add" accessor is invoked is the form; // the arguments must be passed as an array. MethodInfo addHandler = eventInfo.GetAddMethod(); Delegate eventHandler = null; try { #if !PocketPC eventHandler = createDynamicDelegate(objectToHook.GetHashCode(), eventInfo); #else if (objectToHook is Control) { eventHandler = DNControlEvents.getPredefinedEvent(eventName); } #endif try { addHandler.Invoke(objectToHook, new Object[] { eventHandler }); } catch (Exception e) { if (e is TargetInvocationException && e.InnerException != null) { throw e.InnerException; } else { throw e; } } objectEvents.add(eventName, eventHandler); } catch (Exception exception) { if (reportErrors) { DNException dnException = Manager.GetCurrentRuntimeContext().DNException; dnException.set(exception); } } } else { if (reportErrors) { Events.WriteExceptionToLog(String.Format("Event type \"{0}\" not supported", eventName)); } } }