protected virtual void GeneratePropertySet(GeneratorContext ctx, CodeExpression var, PropertyDescriptor prop) { object oval = prop.GetValue(Wrapped); if (oval == null || (prop.HasDefault && prop.IsDefaultValue(oval))) { return; } CodeExpression val = ctx.GenerateValue(oval, prop.RuntimePropertyType, prop.Translatable && prop.IsTranslated(Wrapped)); CodeExpression cprop; TypedPropertyDescriptor tprop = prop as TypedPropertyDescriptor; if (tprop == null || tprop.GladeProperty == prop) { cprop = new CodePropertyReferenceExpression(var, prop.Name); } else { cprop = new CodePropertyReferenceExpression(var, tprop.GladeProperty.Name); cprop = new CodePropertyReferenceExpression(cprop, prop.Name); } ctx.Statements.Add(new CodeAssignStatement(cprop, val)); }
static public void GetProps(ObjectWrapper wrapper, XmlElement parent_elem) { ClassDescriptor klass = wrapper.ClassDescriptor; foreach (ItemGroup group in klass.ItemGroups) { foreach (ItemDescriptor item in group) { TypedPropertyDescriptor prop = item as TypedPropertyDescriptor; if (prop == null) { continue; } prop = prop.GladeProperty; if (prop.GladeName == null) { continue; } if (!prop.VisibleFor(wrapper.Wrapped)) { continue; } string val = PropToString(wrapper, prop); if (val == null) { continue; } XmlElement prop_elem = parent_elem.OwnerDocument.CreateElement("property"); prop_elem.SetAttribute("name", prop.GladeName); if (val.Length > 0) { prop_elem.InnerText = val; } if (prop.Translatable && prop.IsTranslated(wrapper.Wrapped)) { prop_elem.SetAttribute("translatable", "yes"); if (prop.TranslationContext(wrapper.Wrapped) != null) { prop_elem.SetAttribute("context", "yes"); prop_elem.InnerText = prop.TranslationContext(wrapper.Wrapped) + "|" + prop_elem.InnerText; } if (prop.TranslationComment(wrapper.Wrapped) != null) { prop_elem.SetAttribute("comments", prop.TranslationComment(wrapper.Wrapped)); } } parent_elem.AppendChild(prop_elem); } } }
static void SetProperties(TypedClassDescriptor klass, Gtk.Widget widget, string[] propNames, GLib.Value[] propVals) { for (int n = 0; n < propNames.Length; n++) { foreach (ItemGroup grp in klass.ItemGroups) { foreach (ItemDescriptor it in grp) { if (it is TypedPropertyDescriptor) { TypedPropertyDescriptor prop = (TypedPropertyDescriptor)it; if (prop.GladeName == propNames[n]) { prop.SetValue(widget, propVals[n].Val); } } } } } }
internal void LoadDefaultValues() { // This is a hack because there is no managed way of getting // the default value of a GObject property. // This method creates an dummy instance of this class and // gets the values for their properties. Those values are // considered the default if (defaultValuesLoaded) { return; } defaultValuesLoaded = true; object ob = NewInstance(null, false); foreach (ItemGroup group in ItemGroups) { foreach (ItemDescriptor item in group) { TypedPropertyDescriptor prop = item as TypedPropertyDescriptor; if (prop == null) { continue; } if (!prop.HasDefault) { prop.SetDefault(null); } else { object val = prop.GetValue(ob); prop.SetDefault(val); } } } ObjectWrapper ww = ObjectWrapper.Lookup(ob); ww.Dispose(); }
static void ExtractProperties(TypedClassDescriptor klass, XmlElement elem, out Hashtable rawProps, out Hashtable overrideProps) { rawProps = new Hashtable(); overrideProps = new Hashtable(); foreach (ItemGroup group in klass.ItemGroups) { foreach (ItemDescriptor item in group) { TypedPropertyDescriptor prop = item as TypedPropertyDescriptor; if (prop == null) { continue; } prop = prop.GladeProperty; if (prop.GladeName == null) { continue; } XmlNode prop_node = elem.SelectSingleNode("property[@name='" + prop.GladeName + "']"); if (prop_node == null) { continue; } if (prop.GladeOverride) { overrideProps[prop] = prop_node; } else { rawProps[prop] = prop_node; } } } }
static string PropToString(ObjectWrapper wrapper, TypedPropertyDescriptor prop) { object value; if (!prop.GladeOverride) { Stetic.Wrapper.Container.ContainerChild ccwrap = wrapper as Stetic.Wrapper.Container.ContainerChild; GLib.Value gval; if (ccwrap != null) { Gtk.Container.ContainerChild cc = (Gtk.Container.ContainerChild)ccwrap.Wrapped; gval = new GLib.Value((GLib.GType)prop.PropertyType); gtk_container_child_get_property(cc.Parent.Handle, cc.Child.Handle, prop.GladeName, ref gval); } else { Gtk.Widget widget = wrapper.Wrapped as Gtk.Widget; gval = new GLib.Value(widget, prop.GladeName); g_object_get_property(widget.Handle, prop.GladeName, ref gval); } value = gval.Val; } else { value = prop.GetValue(wrapper.Wrapped); } if (value == null) { return(null); } // If the property has its default value, we don't need to write it if (prop.HasDefault && prop.ParamSpec.IsDefaultValue(value)) { return(null); } if (value is Gtk.Adjustment) { Gtk.Adjustment adj = value as Gtk.Adjustment; return(String.Format("{0:G} {1:G} {2:G} {3:G} {4:G} {5:G}", adj.Value, adj.Lower, adj.Upper, adj.StepIncrement, adj.PageIncrement, adj.PageSize)); } else if (value is Enum && prop.ParamSpec != null) { IntPtr klass = g_type_class_ref(((GLib.GType)prop.PropertyType).Val); if (prop.PropertyType.IsDefined(typeof(FlagsAttribute), false)) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); uint val = (uint)System.Convert.ChangeType(value, typeof(uint)); while (val != 0) { IntPtr flags_value = g_flags_get_first_value(klass, val); if (flags_value == IntPtr.Zero) { break; } IntPtr fval = Marshal.ReadIntPtr(flags_value); val &= ~(uint)fval; IntPtr name = Marshal.ReadIntPtr(flags_value, Marshal.SizeOf(typeof(IntPtr))); if (name != IntPtr.Zero) { if (sb.Length != 0) { sb.Append('|'); } sb.Append(GLib.Marshaller.Utf8PtrToString(name)); } } g_type_class_unref(klass); return(sb.ToString()); } else { int val = (int)System.Convert.ChangeType(value, typeof(int)); IntPtr enum_value = g_enum_get_value(klass, val); g_type_class_unref(klass); IntPtr name = Marshal.ReadIntPtr(enum_value, Marshal.SizeOf(typeof(IntPtr))); return(GLib.Marshaller.Utf8PtrToString(name)); } } else if (value is bool) { return((bool)value ? "True" : "False"); } else { return(value.ToString()); } }
public TypedPropertyDescriptor(XmlElement elem, ItemGroup group, TypedClassDescriptor klass) : base(elem, group, klass) { this.klass = klass; string propertyName = elem.GetAttribute("name"); int dot = propertyName.IndexOf('.'); if (dot != -1) { // Sub-property (eg, "Alignment.Value") memberInfo = FindProperty(klass.WrapperType, klass.WrappedType, propertyName.Substring(0, dot)); isWrapperProperty = memberInfo.DeclaringType.IsSubclassOf(typeof(ObjectWrapper)); gladeProperty = new TypedPropertyDescriptor(isWrapperProperty ? klass.WrapperType : klass.WrappedType, memberInfo.Name); propertyInfo = FindProperty(memberInfo.PropertyType, propertyName.Substring(dot + 1)); } else { // Basic simple property propertyInfo = FindProperty(klass.WrapperType, klass.WrappedType, propertyName); isWrapperProperty = propertyInfo.DeclaringType.IsSubclassOf(typeof(ObjectWrapper)); } // Wrapper properties that override widgets properties (using the same name) // must be considered runtime properties (will be available at run-time). if (!isWrapperProperty || klass.WrappedType.GetProperty(propertyName) != null) { isRuntimeProperty = true; } if (!IsInternal && propertyInfo.PropertyType.IsEnum && Registry.LookupEnum(propertyInfo.PropertyType.FullName) == null) { throw new ArgumentException("No EnumDescriptor for " + propertyInfo.PropertyType.FullName + "(" + klass.WrappedType.FullName + "." + propertyName + ")"); } pspec = FindPSpec(propertyInfo); if (isWrapperProperty && pspec == null) { PropertyInfo pinfo = klass.WrappedType.GetProperty(propertyInfo.Name, flags); if (pinfo != null) { pspec = FindPSpec(pinfo); } } if (pspec != null) { // This information will be overridden by what's specified in the xml file description = pspec.Blurb; minimum = pspec.Minimum; maximum = pspec.Maximum; label = propertyName; if (!elem.HasAttribute("ignore-default")) { hasDefault = Type.GetTypeCode(PropertyType) != TypeCode.Object || PropertyType.IsEnum; } } else { label = propertyInfo.Name; gladeOverride = true; } string typeName = elem.GetAttribute("editor"); if (typeName.Length > 0) { editorType = Registry.GetType(typeName, false); } // Look for a default value attribute object[] ats = propertyInfo.GetCustomAttributes(typeof(DefaultValueAttribute), true); if (ats.Length > 0) { DefaultValueAttribute at = (DefaultValueAttribute)ats [0]; defaultValue = at.Value; } // Load default data Load(elem); }
static string PropToString (ObjectWrapper wrapper, TypedPropertyDescriptor prop) { object value; if (!prop.GladeOverride) { Stetic.Wrapper.Container.ContainerChild ccwrap = wrapper as Stetic.Wrapper.Container.ContainerChild; GLib.Value gval; if (ccwrap != null) { Gtk.Container.ContainerChild cc = (Gtk.Container.ContainerChild)ccwrap.Wrapped; gval = new GLib.Value ((GLib.GType) prop.PropertyType); gtk_container_child_get_property (cc.Parent.Handle, cc.Child.Handle, prop.GladeName, ref gval); } else { Gtk.Widget widget = wrapper.Wrapped as Gtk.Widget; gval = new GLib.Value (widget, prop.GladeName); g_object_get_property (widget.Handle, prop.GladeName, ref gval); } value = gval.Val; } else value = prop.GetValue (wrapper.Wrapped); if (value == null) return null; // If the property has its default value, we don't need to write it if (prop.HasDefault && prop.ParamSpec.IsDefaultValue (value)) return null; if (value is Gtk.Adjustment) { Gtk.Adjustment adj = value as Gtk.Adjustment; return String.Format ("{0:G} {1:G} {2:G} {3:G} {4:G} {5:G}", adj.Value, adj.Lower, adj.Upper, adj.StepIncrement, adj.PageIncrement, adj.PageSize); } else if (value is Enum && prop.ParamSpec != null) { IntPtr klass = g_type_class_ref (((GLib.GType)prop.PropertyType).Val); if (prop.PropertyType.IsDefined (typeof (FlagsAttribute), false)) { System.Text.StringBuilder sb = new System.Text.StringBuilder (); uint val = (uint)System.Convert.ChangeType (value, typeof (uint)); while (val != 0) { IntPtr flags_value = g_flags_get_first_value (klass, val); if (flags_value == IntPtr.Zero) break; IntPtr fval = Marshal.ReadIntPtr (flags_value); val &= ~(uint)fval; IntPtr name = Marshal.ReadIntPtr (flags_value, Marshal.SizeOf (typeof (IntPtr))); if (name != IntPtr.Zero) { if (sb.Length != 0) sb.Append ('|'); sb.Append (GLib.Marshaller.Utf8PtrToString (name)); } } g_type_class_unref (klass); return sb.ToString (); } else { int val = (int)System.Convert.ChangeType (value, typeof (int)); IntPtr enum_value = g_enum_get_value (klass, val); g_type_class_unref (klass); IntPtr name = Marshal.ReadIntPtr (enum_value, Marshal.SizeOf (typeof (IntPtr))); return GLib.Marshaller.Utf8PtrToString (name); } } else if (value is bool) return (bool)value ? "True" : "False"; else return value.ToString (); }
public TypedPropertyDescriptor (XmlElement elem, ItemGroup group, TypedClassDescriptor klass) : base (elem, group, klass) { this.klass = klass; string propertyName = elem.GetAttribute ("name"); int dot = propertyName.IndexOf ('.'); if (dot != -1) { // Sub-property (eg, "Alignment.Value") memberInfo = FindProperty (klass.WrapperType, klass.WrappedType, propertyName.Substring (0, dot)); isWrapperProperty = memberInfo.DeclaringType.IsSubclassOf (typeof (ObjectWrapper)); gladeProperty = new TypedPropertyDescriptor (isWrapperProperty ? klass.WrapperType : klass.WrappedType, memberInfo.Name); propertyInfo = FindProperty (memberInfo.PropertyType, propertyName.Substring (dot + 1)); } else { // Basic simple property propertyInfo = FindProperty (klass.WrapperType, klass.WrappedType, propertyName); isWrapperProperty = propertyInfo.DeclaringType.IsSubclassOf (typeof (ObjectWrapper)); } // Wrapper properties that override widgets properties (using the same name) // must be considered runtime properties (will be available at run-time). if (!isWrapperProperty || klass.WrappedType.GetProperty (propertyName) != null) isRuntimeProperty = true; if (!IsInternal && propertyInfo.PropertyType.IsEnum && Registry.LookupEnum (propertyInfo.PropertyType.FullName) == null) throw new ArgumentException ("No EnumDescriptor for " + propertyInfo.PropertyType.FullName + "(" + klass.WrappedType.FullName + "." + propertyName + ")"); pspec = FindPSpec (propertyInfo); if (isWrapperProperty && pspec == null) { PropertyInfo pinfo = klass.WrappedType.GetProperty (propertyInfo.Name, flags); if (pinfo != null) pspec = FindPSpec (pinfo); } if (pspec != null) { // This information will be overridden by what's specified in the xml file description = pspec.Blurb; minimum = pspec.Minimum; maximum = pspec.Maximum; label = propertyName; if (!elem.HasAttribute ("ignore-default")) hasDefault = Type.GetTypeCode (PropertyType) != TypeCode.Object || PropertyType.IsEnum; } else { label = propertyInfo.Name; gladeOverride = true; } string typeName = elem.GetAttribute ("editor"); if (typeName.Length > 0) editorType = Registry.GetType (typeName, false); // Look for a default value attribute object[] ats = propertyInfo.GetCustomAttributes (typeof(DefaultValueAttribute), true); if (ats.Length > 0) { DefaultValueAttribute at = (DefaultValueAttribute) ats [0]; defaultValue = at.Value; } // Load default data Load (elem); }