Merge pull request #29 from bl8/ginterface-fixes

GInterface properties fixes
This commit is contained in:
Mike Kestner 2011-12-04 17:52:35 -08:00
commit 63e9993fcf
3 changed files with 63 additions and 24 deletions

View File

@ -151,13 +151,14 @@ namespace GLib {
}
}
// Key: The pointer to the ParamSpec of the property
// Value: The corresponding PropertyInfo object
static Dictionary<IntPtr, PropertyInfo> properties;
static Dictionary<IntPtr, PropertyInfo> Properties {
// Key: The Type for the set of properties
// Value->SubKey: The pointer to the ParamSpec of the property
// Value->SubValue: The corresponding PropertyInfo object
static Dictionary<Type, Dictionary<IntPtr, PropertyInfo>> properties;
static Dictionary<Type, Dictionary<IntPtr, PropertyInfo>> Properties {
get {
if (properties == null)
properties = new Dictionary<IntPtr, PropertyInfo> ();
properties = new Dictionary<Type, Dictionary<IntPtr, PropertyInfo>> ();
return properties;
}
}
@ -288,7 +289,7 @@ namespace GLib {
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++;
idx++;
}
foreach (PropertyInfo pinfo in t.GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) {
@ -307,7 +308,13 @@ namespace GLib {
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);
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));
@ -321,11 +328,18 @@ namespace GLib {
static void GetPropertyCallback (IntPtr handle, uint property_id, ref GLib.Value value, IntPtr param_spec)
{
if (!Properties.ContainsKey (param_spec))
GLib.Object obj = GLib.Object.GetObject (handle, false);
var type = (Type)obj.LookupGType ();
Dictionary<IntPtr, PropertyInfo> props;
if (!Properties.TryGetValue (type, out props))
return;
GLib.Object obj = GLib.Object.GetObject (handle, false);
value.Val = Properties [param_spec].GetValue (obj, new object [0]);
PropertyInfo prop;
if (!props.TryGetValue (param_spec, out prop))
return;
value.Val = prop.GetValue (obj, new object [0]);
}
static GetPropertyDelegate get_property_handler;
@ -352,11 +366,18 @@ namespace GLib {
o.Raw = handle;
}
}
if (!Properties.ContainsKey (param_spec))
GLib.Object obj = GLib.Object.GetObject (handle, false);
var type = (Type)obj.LookupGType ();
Dictionary<IntPtr, PropertyInfo> props;
if (!Properties.TryGetValue (type, out props))
return;
GLib.Object obj = GLib.Object.GetObject (handle, false);
Properties [param_spec].SetValue (obj, value.Val, new object [0]);
PropertyInfo prop;
if (!props.TryGetValue (param_spec, out prop))
return;
prop.SetValue (obj, value.Val, new object [0]);
}
static SetPropertyDelegate set_property_handler;
@ -401,12 +422,17 @@ namespace GLib {
if (attrs.Length == 0)
continue;
PropertyAttribute property_attr = attrs [0];
PropertyInfo declared_prop = t.GetProperty (p.Name, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
PropertyInfo declared_prop = t.GetProperty (p.Name, BindingFlags.Public | BindingFlags.Instance);
if (declared_prop == null)
continue;
IntPtr param_spec = FindInterfaceProperty (adapter.GType, property_attr.Name);
Properties [param_spec] = declared_prop;
Dictionary<IntPtr, PropertyInfo> props;
if (!Properties.TryGetValue (t, out props)) {
props = new Dictionary<IntPtr, PropertyInfo> ();
Properties [t] = props;
}
props [param_spec] = declared_prop;
}
}
}

View File

@ -116,7 +116,7 @@ namespace GLib {
}
}
public string ToString ()
public override string ToString ()
{
GParamSpec spec = (GParamSpec) Marshal.PtrToStructure (Handle, typeof (GParamSpec));
GType valtype= new GType (spec.value_type);

View File

@ -9,14 +9,25 @@ class CustomScrollableWidgetTest {
Window win = new Window ("Custom Scrollable Widget Test");
win.DeleteEvent += new DeleteEventHandler (OnQuit);
VPaned paned = new VPaned ();
ScrolledWindow scroll = new ScrolledWindow ();
scroll.HscrollbarPolicy = PolicyType.Automatic;
scroll.VscrollbarPolicy = PolicyType.Automatic;
CustomScrollableWidget cw = new CustomScrollableWidget ("This one label that is repeated");
var cw = new DerivedScrollableWidget<string> ("This one label that is repeated");
scroll.Add (cw);
paned.Pack1 (scroll, true, false);
scroll = new ScrolledWindow ();
scroll.HscrollbarPolicy = PolicyType.Automatic;
scroll.VscrollbarPolicy = PolicyType.Automatic;
win.Add (scroll);
var cw2 = new DerivedScrollableWidget<object> ("Another label that is repeated");
scroll.Add (cw2);
paned.Pack2 (scroll, true, false);
win.Add (paned);
win.DefaultWidth = 200;
win.DefaultHeight = 200;
win.ShowAll ();
@ -36,7 +47,13 @@ abstract class CustomBase : Widget
{ }
}
class CustomScrollableWidget : CustomBase, ScrollableImplementor {
class DerivedScrollableWidget<T> : CustomScrollableWidget<T>
{
public DerivedScrollableWidget (string label) : base (label)
{ }
}
class CustomScrollableWidget<T> : CustomBase, ScrollableImplementor {
private int num_rows = 20;
private string label;
private Pango.Layout layout;
@ -166,8 +183,6 @@ class CustomScrollableWidget : CustomBase, ScrollableImplementor {
if (hadjustment.Value + hadjustment.PageSize > hadjustment.Upper) {
hadjustment.Value = hadjustment.Upper - hadjustment.PageSize;
}
Console.WriteLine (String.Format ("H adj={0} upper={1} PageSize={2} PageInc={3}",
HadjustmentValue, hadjustment.Upper, hadjustment.PageSize, hadjustment.PageIncrement));
hadjustment.Change ();
}
@ -177,8 +192,6 @@ class CustomScrollableWidget : CustomBase, ScrollableImplementor {
if (vadjustment.Value + vadjustment.PageSize > vadjustment.Upper) {
vadjustment.Value = vadjustment.Upper - vadjustment.PageSize;
}
Console.WriteLine (String.Format ("V adj={0} upper={1} PageSize={2} PageInc={3}",
VadjustmentValue, vadjustment.Upper, vadjustment.PageSize, vadjustment.PageIncrement));
vadjustment.Change ();
}
}