Ejemplo n.º 1
0
 private void BindEmbeddedItem()
 {
     if (_HeaderHeight <= 0)
     {
         _HeaderHeight = HeaderEndRect().Height;
     }
     using (Graphics graphics = CreateGraphics())
     {
         foreach (EmbeddedItem embeddedItem2 in _EmbeddedItems)
         {
             EmbeddedItem embeddedItem = embeddedItem2;
             Rectangle    bounds       = embeddedItem.SubItem.Bounds;
             if (bounds.Y > _HeaderHeight - bounds.Height && bounds.Y > 0 && bounds.Y < base.ClientRectangle.Height)
             {
                 embeddedItem.EmbeddedControl.Visible = true;
                 int num = Convert.ToInt32(graphics.MeasureString(embeddedItem.EmbeddedControl.Text, embeddedItem.SubItem.Font).Width) + 2 * _cpadding;
                 if (bounds.X <= 10 && num >= base.Columns[0].Width)
                 {
                     num = base.Columns[0].Width - 2 * _cpadding;
                 }
                 embeddedItem.EmbeddedControl.Bounds = new Rectangle(bounds.X + _cpadding, bounds.Y + _cpadding, num, bounds.Height - 2 * _cpadding);
             }
             else
             {
                 embeddedItem.EmbeddedControl.Visible = false;
             }
         }
     }
 }
Ejemplo n.º 2
0
 public void ClearEmbeddedItems()
 {
     foreach (EmbeddedItem embeddedItem2 in _EmbeddedItems)
     {
         EmbeddedItem embeddedItem = embeddedItem2;
         embeddedItem.EmbeddedControl.Visible = false;
         embeddedItem.EmbeddedControl.Dispose();
     }
     _EmbeddedItems.Clear();
 }
Ejemplo n.º 3
0
        public void AddControlToSubItem(Control control, ListViewItem.ListViewSubItem item, int itemIndex)
        {
            base.Controls.Add(control);
            EmbeddedItem embeddedItem = default(EmbeddedItem);

            embeddedItem.EmbeddedControl = control;
            embeddedItem.SubItem         = item;
            embeddedItem.ItemIndex       = itemIndex;
            _EmbeddedItems.Add(embeddedItem);
        }
Ejemplo n.º 4
0
        private void ProjectMethods(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string                   typeName            = type.FullName;
            BindingFlags             defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
            IEnumerable <MethodInfo> methods             = type.GetMethods(defaultBindingFlags)
                                                           .Where(ReflectionHelpers.IsFullyFledgedMethod);
            IEnumerable <IGrouping <string, MethodInfo> > methodGroups = methods.GroupBy(m => m.Name);

            foreach (IGrouping <string, MethodInfo> methodGroup in methodGroups)
            {
                string       methodName       = methodGroup.Key;
                MethodInfo[] methodCandidates = methodGroup.ToArray();

                JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        CreateAndSetTypeError(string.Format(
                                                  Strings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                        return(JsValue.Undefined);
                    }

                    object[] processedArgs = GetHostItemMemberArguments(args);

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        CreateAndSetReferenceError(string.Format(
                                                       Strings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        return(JsValue.Undefined);
                    }

                    ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitMethod.GetParameters());

                    object result;

                    try
                    {
                        result = bestFitMethod.Invoke(obj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as WrapperException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  string.Format(Strings.Runtime_HostObjectMethodInvocationFailed, methodName,
                                                                exception.Message)
                                                                :
                                                  string.Format(Strings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName,
                                                                exception.Message)
                            ;
                            errorValue = JsErrorHelpers.CreateError(errorMessage);
                        }
                        JsErrorHelpers.SetException(errorValue);

                        return(JsValue.Undefined);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                nativeFunctions.Add(nativeFunction);

                JsValue methodValue = JsValue.CreateFunction(nativeFunction);
                typeValue.SetProperty(methodName, methodValue, true);
            }
        }
Ejemplo n.º 5
0
        private void ProjectProperties(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags);

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                JsValue descriptorValue = JsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", JsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(JsValue.Undefined);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(obj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertyGettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertyGettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsErrorHelpers.SetException(errorValue);

                            return(JsValue.Undefined);
                        }

                        JsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    nativeFunctions.Add(nativeGetFunction);

                    JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(JsValue.Undefined);
                        }

                        object value = MapToHostType(args[1]);
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(obj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertySettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertySettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsErrorHelpers.SetException(errorValue);

                            return(JsValue.Undefined);
                        }

                        return(JsValue.Undefined);
                    };
                    nativeFunctions.Add(nativeSetFunction);

                    JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                typeValue.DefineProperty(propertyName, descriptorValue);
            }
        }
Ejemplo n.º 6
0
        private void ProjectFields(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            bool    instance  = externalItem.IsInstance;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            FieldInfo[] fields = type.GetFields(defaultBindingFlags);

            foreach (FieldInfo field in fields)
            {
                string fieldName = field.Name;

                JsValue descriptorValue = JsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", JsValue.True, true);

                JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateTypeError(
                            string.Format(Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    object result;

                    try
                    {
                        result = field.GetValue(obj);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(Strings.Runtime_HostObjectFieldGettingFailed, fieldName, e.Message)
                                                        :
                                              string.Format(Strings.Runtime_HostTypeFieldGettingFailed, fieldName, typeName, e.Message)
                        ;

                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateError(errorMessage);
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                nativeFunctions.Add(nativeGetFunction);

                JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
                descriptorValue.SetProperty("get", getMethodValue, true);

                JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateTypeError(
                            string.Format(Strings.Runtime_InvalidThisContextForHostObjectField, fieldName));
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    object value = MapToHostType(args[1]);
                    ReflectionHelpers.FixFieldValueType(ref value, field);

                    try
                    {
                        field.SetValue(obj, value);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(Strings.Runtime_HostObjectFieldSettingFailed, fieldName, e.Message)
                                                        :
                                              string.Format(Strings.Runtime_HostTypeFieldSettingFailed, fieldName, typeName, e.Message)
                        ;

                        JsValue undefinedValue = JsValue.Undefined;
                        JsValue errorValue     = JsErrorHelpers.CreateError(errorMessage);
                        JsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    return(JsValue.Undefined);
                };
                nativeFunctions.Add(nativeSetFunction);

                JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
                descriptorValue.SetProperty("set", setMethodValue, true);

                typeValue.DefineProperty(fieldName, descriptorValue);
            }
        }
Ejemplo n.º 7
0
        private void ProjectMethods(EmbeddedItem externalItem, EmbeddingObjectOptions options)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
            var          methods             = type.GetMethods(defaultBindingFlags).Select(options.ExtendInfo)
                                               .Where((m) => m.IsMapped);
            var methodGroups = methods.GroupBy(m => m.Name);

            foreach (var methodGroup in methodGroups)
            {
                string       methodName       = methodGroup.Key;
                MethodInfo[] methodCandidates = methodGroup.Select(m => m.Info).ToArray();

                JsValue nativeFunction(JsValue callee, bool isConstructCall, JsValue[] args, ushort argCount, IntPtr callbackData)
                {
                    if (instance && obj == null)
                    {
                        CreateAndSetError($"Invalid context while calling method '{methodName}'.");
                        return(JsValue.Undefined);
                    }

                    if (!SelectAndProcessFunction(methodCandidates, args, argCount, out MethodInfo bestSelection, out object[] processedArgs))
                    {
                        CreateAndSetError($"Suitable method '{methodName}' was not found.");
                        return(JsValue.Undefined);
                    }

                    object result;

                    try
                    {
                        result = bestSelection.Invoke(obj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as JsException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  $"Host method '{methodName}' invocation error: {exception.Message}"
                                :
                                                  $"Host static type '{typeName}' method '{methodName}' invocation error: {exception.Message}"
                            ;
                            errorValue = JsValue.CreateError(JsValue.FromString(errorMessage));
                        }
                        JsContext.SetException(errorValue);

                        return(JsValue.Undefined);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                }

                nativeFunctions.Add(nativeFunction);

                JsValue methodValue = JsValue.CreateNamedFunction(methodName, nativeFunction);
                typeValue.SetProperty(methodName, methodValue, true);
            }
        }
Ejemplo n.º 8
0
        private void ProjectProperties(EmbeddedItem externalItem, EmbeddingObjectOptions options)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags).Where(options.IsMapped).ToArray();

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                JsValue descriptorValue = JsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", JsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    JsValue nativeGetFunction(JsValue callee, bool isConstructCall, JsValue[] args, ushort argCount, IntPtr callbackData)
                    {
                        if (instance && obj == null)
                        {
                            CreateAndSetError($"Invalid context for '{propertyName}' property.");
                            return(JsValue.Undefined);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(obj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as JsException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      $"Property '{propertyName}' get operation failed: {exception.Message}"
                                    :
                                                      $"Property '{propertyName}' of static type '{typeName}' get operation failed: {exception.Message}"
                                ;
                                errorValue = JsValue.CreateError(JsValue.FromString(errorMessage));
                            }
                            JsContext.SetException(errorValue);

                            return(JsValue.Undefined);
                        }

                        JsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    }

                    nativeFunctions.Add(nativeGetFunction);

                    JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    JsValue nativeSetFunction(JsValue callee, bool isConstructCall, JsValue[] args, ushort argCount, IntPtr callbackData)
                    {
                        JsValue undefinedValue = JsValue.Undefined;

                        if (instance && obj == null)
                        {
                            CreateAndSetError($"Invalid context for '{propertyName}' property.");
                            return(undefinedValue);
                        }

                        object value = MapToHostType(args[1]);

                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(obj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as JsException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      $"Host object property '{propertyName}' setting failed: {exception.Message}"
                                    :
                                                      $"Host type '{typeName}' property '{propertyName}' setting failed: {exception.Message}"
                                ;
                                errorValue = JsValue.CreateError(JsValue.FromString(errorMessage));
                            }
                            JsContext.SetException(errorValue);

                            return(undefinedValue);
                        }

                        return(undefinedValue);
                    }

                    nativeFunctions.Add(nativeSetFunction);

                    JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                typeValue.DefineProperty(propertyName, descriptorValue);
            }
        }
Ejemplo n.º 9
0
        private void ProjectFields(EmbeddedItem externalItem, EmbeddingObjectOptions options)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            bool    instance  = externalItem.IsInstance;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            FieldInfo[] fields = type.GetFields(defaultBindingFlags).Where(options.IsMapped).ToArray();

            foreach (FieldInfo field in fields)
            {
                string fieldName = field.Name;

                JsValue descriptorValue = JsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", JsValue.True, true);

                JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        CreateAndSetError($"Context error while invoking getter '{fieldName}'.");
                        return(JsValue.Undefined);
                    }
                    object result;

                    try
                    {
                        result = field.GetValue(obj);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as JsException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  $"Error ocured while reading field '{fieldName}': {exception.Message}"
                                                                :
                                                  $"Erorr ocured while reading static field '{fieldName}' from type '{typeName}': {exception.Message}"
                            ;
                            errorValue = JsValue.CreateError(JsValue.FromString(errorMessage));
                        }
                        JsContext.SetException(errorValue);

                        return(JsValue.Undefined);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                nativeFunctions.Add(nativeGetFunction);

                JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
                descriptorValue.SetProperty("get", getMethodValue, true);

                JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    if (instance && obj == null)
                    {
                        CreateAndSetError($"Invalid context got host object field {fieldName}.");
                        return(JsValue.Undefined);
                    }

                    object value = MapToHostType(args[1]);
                    ReflectionHelpers.FixFieldValueType(ref value, field);

                    try
                    {
                        field.SetValue(obj, value);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as JsException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  $"Failed to set value for hosts object field '{fieldName}': {exception.Message}"
                                                                :
                                                  $"Failed to set value for static type '{typeName}' field '{fieldName}': {exception.Message}"
                            ;
                            errorValue = JsValue.CreateError(JsValue.FromString(errorMessage));
                        }
                        JsContext.SetException(errorValue);

                        return(JsValue.Undefined);
                    }

                    return(JsValue.Undefined);
                };
                nativeFunctions.Add(nativeSetFunction);

                JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
                descriptorValue.SetProperty("set", setMethodValue, true);

                typeValue.DefineProperty(fieldName, descriptorValue);
            }
        }