static IntPtr RegisterProperty(GType type, string name, string nick, string blurb, uint property_id, GType property_type, bool can_read, bool can_write) { IntPtr declaring_class = type.GetClassPtr(); ParamSpec pspec = new ParamSpec(name, nick, blurb, property_type, can_read, can_write); g_object_class_install_property(declaring_class, property_id, pspec.Handle); return(pspec.Handle); }
static void AddProperties(GType gtype, System.Type t, bool register_instance_prop, ref bool handlers_overridden) { if (register_instance_prop) { IntPtr declaring_class = gtype.GetClassPtr(); ParamSpec pspec = new ParamSpec("gtk-sharp-managed-instance", "", "", GType.Pointer, ParamFlags.Writable | ParamFlags.ConstructOnly); g_object_class_install_property(declaring_class, idx, pspec.Handle); idx++; } foreach (PropertyInfo pinfo in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { foreach (object attr in pinfo.GetCustomAttributes(typeof(PropertyAttribute), false)) { if (pinfo.GetIndexParameters().Length > 0) { throw(new InvalidOperationException(String.Format("GLib.RegisterPropertyAttribute cannot be applied to property {0} of type {1} because the property expects one or more indexed parameters", pinfo.Name, t.FullName))); } if (!handlers_overridden) { IntPtr class_ptr = gtype.GetClassPtr(); GObjectClass gobject_class = (GObjectClass)Marshal.PtrToStructure(class_ptr, typeof(GObjectClass)); gobject_class.get_prop_cb = GetPropertyHandler; gobject_class.set_prop_cb = SetPropertyHandler; Marshal.StructureToPtr(gobject_class, class_ptr, false); handlers_overridden = true; } PropertyAttribute property_attr = attr as PropertyAttribute; try { IntPtr param_spec = RegisterProperty(gtype, property_attr.Name, property_attr.Nickname, property_attr.Blurb, idx, (GType)pinfo.PropertyType, pinfo.CanRead, pinfo.CanWrite); Type type = (Type)gtype; Dictionary <IntPtr, PropertyInfo> gtype_properties; if (!Properties.TryGetValue(type, out gtype_properties)) { gtype_properties = new Dictionary <IntPtr, PropertyInfo> (); Properties [type] = gtype_properties; } gtype_properties.Add(param_spec, pinfo); idx++; } catch (ArgumentException) { throw new InvalidOperationException(String.Format("GLib.PropertyAttribute cannot be applied to property {0} of type {1} because the return type of the property is not supported", pinfo.Name, t.FullName)); } } } }
void InitForProperty(GType gtype, string name) { IntPtr p_name = Marshaller.StringToPtrGStrdup(name); IntPtr spec_ptr = g_object_class_find_property(gtype.GetClassPtr(), p_name); Marshaller.Free(p_name); if (spec_ptr == IntPtr.Zero) { throw new Exception(String.Format("No property with name '{0}' in type '{1}'", name, gtype.ToString())); } ParamSpec spec = new ParamSpec(spec_ptr); g_value_init(ref this, spec.ValueType.Val); }
protected internal static GType RegisterGType(System.Type t) { GType gtype = GType.RegisterGObjectType(t); bool is_first_subclass = gtype.GetBaseType() == gtype.GetThresholdType(); if (is_first_subclass) { IntPtr class_ptr = gtype.GetClassPtr(); GObjectClass gobject_class = (GObjectClass)Marshal.PtrToStructure(class_ptr, typeof(GObjectClass)); gobject_class.constructor_cb = ConstructorHandler; gobject_class.get_prop_cb = GetPropertyHandler; gobject_class.set_prop_cb = SetPropertyHandler; Marshal.StructureToPtr(gobject_class, class_ptr, false); } AddProperties(gtype, t, is_first_subclass); ConnectDefaultHandlers(gtype, t); InvokeTypeInitializers(gtype, t); AddInterfaces(gtype, t); return(gtype); }
public static N BaseOverride <N>(this AbiStruct class_abi, GType gtype, string fieldname) where N : Delegate { N unmanaged = null; unsafe { IntPtr raw_ptr = IntPtr.Zero; while (raw_ptr == IntPtr.Zero && GType.IsManaged(gtype)) { gtype = gtype.GetThresholdType(); var abi_ptr = (IntPtr *)(((long)gtype.GetClassPtr()) + (long)class_abi.GetFieldOffset(fieldname)); raw_ptr = *abi_ptr; } if (raw_ptr != IntPtr.Zero) { unmanaged = Marshal.GetDelegateForFunctionPointer <N>(raw_ptr); } } return(unmanaged); }
static GstMiniObjectClass GetClassStruct(GLib.GType gtype, bool use_cache) { if (class_structs == null) { class_structs = new Hashtable(); } if (use_cache && class_structs.Contains(gtype)) { return((GstMiniObjectClass)class_structs [gtype]); } else { IntPtr class_ptr = gtype.GetClassPtr(); GstMiniObjectClass class_struct = (GstMiniObjectClass)Marshal.PtrToStructure(class_ptr, typeof(GstMiniObjectClass)); if (use_cache) { class_structs.Add(gtype, class_struct); } return(class_struct); } }
private void OverrideHandlers(bool ctor, bool properties) { OverrideHandlers(gtype.GetClassPtr(), ctor, properties); }
static IntPtr RegisterProperty(GType type, string name, string nick, string blurb, uint property_id, GType property_type, bool can_read, bool can_write) { IntPtr declaring_class = type.GetClassPtr (); ParamSpec pspec = new ParamSpec (name, nick, blurb, property_type, can_read, can_write); g_object_class_install_property (declaring_class, property_id, pspec.Handle); return pspec.Handle; }
static void AddProperties(GType gtype, System.Type t, bool register_instance_prop, ref bool handlers_overridden) { if (register_instance_prop) { IntPtr declaring_class = gtype.GetClassPtr (); ParamSpec pspec = new ParamSpec ("gtk-sharp-managed-instance", "", "", GType.Pointer, ParamFlags.Writable | ParamFlags.ConstructOnly); g_object_class_install_property (declaring_class, idx, pspec.Handle); idx++; } foreach (PropertyInfo pinfo in t.GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { foreach (object attr in pinfo.GetCustomAttributes (typeof (PropertyAttribute), false)) { if(pinfo.GetIndexParameters().Length > 0) throw(new InvalidOperationException(String.Format("GLib.RegisterPropertyAttribute cannot be applied to property {0} of type {1} because the property expects one or more indexed parameters", pinfo.Name, t.FullName))); if (!handlers_overridden) { IntPtr class_ptr = gtype.GetClassPtr (); GObjectClass gobject_class = (GObjectClass) Marshal.PtrToStructure (class_ptr, typeof (GObjectClass)); gobject_class.get_prop_cb = GetPropertyHandler; gobject_class.set_prop_cb = SetPropertyHandler; Marshal.StructureToPtr (gobject_class, class_ptr, false); handlers_overridden = true; } PropertyAttribute property_attr = attr as PropertyAttribute; try { IntPtr param_spec = RegisterProperty (gtype, property_attr.Name, property_attr.Nickname, property_attr.Blurb, idx, (GType) pinfo.PropertyType, pinfo.CanRead, pinfo.CanWrite); Properties.Add (param_spec, pinfo); idx++; } catch (ArgumentException) { throw new InvalidOperationException (String.Format ("GLib.PropertyAttribute cannot be applied to property {0} of type {1} because the return type of the property is not supported", pinfo.Name, t.FullName)); } } } }
static void AddInterfaces(GType gtype, Type t, ref bool handlers_overridden) { foreach (Type iface in t.GetInterfaces ()) { if (!iface.IsDefined (typeof (GInterfaceAttribute), true)) continue; GInterfaceAttribute attr = iface.GetCustomAttributes (typeof (GInterfaceAttribute), false) [0] as GInterfaceAttribute; GInterfaceAdapter adapter = Activator.CreateInstance (attr.AdapterType, null) as GInterfaceAdapter; if (!handlers_overridden) { IntPtr class_ptr = gtype.GetClassPtr (); GObjectClass gobject_class = (GObjectClass) Marshal.PtrToStructure (class_ptr, typeof (GObjectClass)); gobject_class.get_prop_cb = GetPropertyHandler; gobject_class.set_prop_cb = SetPropertyHandler; Marshal.StructureToPtr (gobject_class, class_ptr, false); handlers_overridden = true; } if (!iface.IsAssignableFrom (t.BaseType)) { GInterfaceInfo info = adapter.Info; info.Data = gtype.GetClassPtr (); //FIXME: overiding prop is done inside the init of interface adapter // not sure that it is the good solution but // it is the only one I found without exception or loop g_type_add_interface_static (gtype.Val, adapter.GType.Val, ref info); } foreach (PropertyInfo p in iface.GetProperties ()) { PropertyAttribute[] attrs = p.GetCustomAttributes (typeof (PropertyAttribute), true) as PropertyAttribute []; if (attrs.Length == 0) continue; PropertyAttribute property_attr = attrs [0]; PropertyInfo declared_prop = t.GetProperty (p.Name, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); if (declared_prop == null) continue; IntPtr param_spec = FindInterfaceProperty (adapter.GType, property_attr.Name); Properties [param_spec] = declared_prop; } } }
void InitForProperty (GType gtype, string name) { IntPtr p_name = Marshaller.StringToPtrGStrdup (name); IntPtr spec_ptr = g_object_class_find_property (gtype.GetClassPtr (), p_name); Marshaller.Free (p_name); if (spec_ptr == IntPtr.Zero) throw new Exception (String.Format ("No property with name '{0}' in type '{1}'", name, gtype.ToString())); ParamSpec spec = new ParamSpec (spec_ptr); g_value_init (ref this, spec.ValueType.Val); }
static void OverrideClassStruct(GLib.GType gtype, GstMiniObjectClass class_struct) { IntPtr class_ptr = gtype.GetClassPtr(); Marshal.StructureToPtr(class_struct, class_ptr, false); }