GetValue() public method

public GetValue ( object obj ) : object
obj object
return object
        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();
        }
Ejemplo n.º 2
0
        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());
            }
        }
Ejemplo n.º 3
0
		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 ();
		}