using System; using System.Collections; using System.Reflection; using System.Reflection.Emit; using System.Runtime.InteropServices; using System.ComponentModel; using XCircuit.COM; namespace XCircuit.Proxy { public class EventProxy { private object dispatch_obj; public EventProxy(object obj) { Console.WriteLine("Creating event proxy: {0}", obj); this.dispatch_obj = obj; } public void internal_handler(object sender, EventArgs args) { Console.WriteLine("Firing event: from={0}, to={1}, args={0}", sender, dispatch_obj, args); Manager.fire_event(ObjectProxy.unbox(sender), dispatch_obj, ObjectProxy.unbox(args)); } private static ModuleBuilder moduleBuilder = null; private static Hashtable typeDict = new Hashtable(); public static Type GetEventProxyType(Type eventHandlerType) { object val = typeDict[eventHandlerType]; if (val == null) { val = typeDict[eventHandlerType] = BuildEventProxyType(eventHandlerType); } return (Type)val; } public static Type BuildEventProxyType(Type eventHandlerType) { if (moduleBuilder == null) { AssemblyName aname = new AssemblyName(); aname.Name = "XCircuitInternal"; aname.Version = new Version("1.0.0.0"); AssemblyBuilder bld = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.Run); moduleBuilder = bld.DefineDynamicModule("XCircuitInternal"); } MethodInfo handlerInfo = eventHandlerType.GetMethod("Invoke"); ParameterInfo[] paramInfo = handlerInfo.GetParameters(); Type[] argTypes = new Type[paramInfo.Length]; for (int i=0; i"); Console.WriteLine("Invoking: {0}, {1}, {2}", name, flags, args); args = box(args); object result; if ((flags & (BindingFlags.SetProperty | BindingFlags.SetField)) != 0 && dotnet_obj != null && GetObjectType().GetEvent(name) != null) { Console.WriteLine("Define new event handler for [{0}]::{1}", dotnet_obj, name); EventInfo e_info = GetObjectType().GetEvent(name); Type e_type = EventProxy.GetEventProxyType(e_info.EventHandlerType); EventProxy e_proxy = (EventProxy)Activator.CreateInstance(e_type, new object[1] {args[0]}); Delegate handler = Delegate.CreateDelegate(e_info.EventHandlerType, e_proxy, "Handler"); e_info.AddEventHandler(dotnet_obj, handler); result = null; } else { result = GetObjectType().InvokeMember( name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | flags, null, dotnet_obj, args); } Console.WriteLine("Result: {0}", result); return unbox(result); } catch (Exception e) { Console.WriteLine("Exception: {0}", e); throw e; } } public int GetMemberID(string name) { Console.WriteLine("Looking for object ID: " + name); for (int i=0; i> 8, (cval & 0x00ff0000) >> 16); } set { int cval = (value.R | (value.G << 8) | (value.B << 16)); xc_object.set_color(cval); } } public void Select() { xc_object.select(true); } } public class Point { protected IXCPoint xc_point = null; internal Point(IXCPoint obj) { this.xc_point = obj; } public short X { get { return xc_point.x; } set { xc_point.x = value; } } public short Y { get { return xc_point.y; } set { xc_point.y = value; } } } public enum LineStyle { Solid = 0, Dashed, Dotted, None }; public abstract class LineObject : ObjectBase { protected LineObject() : base() { } protected LineObject(IXCLineObject obj) : base(obj) { } private static string[] style_strings = { "solid", "dashed", "dotted", "none" }; public LineStyle Style { set { ((IXCLineObject)xc_object).border = style_strings[(int)value]; } get { return (LineStyle)Array.IndexOf(style_strings, ((IXCLineObject)xc_object).border); } } public bool Closed { set { ((IXCLineObject)xc_object).closed = value; } get { return ((IXCLineObject)xc_object).closed; } } public float LineWidth { set { ((IXCLineObject)xc_object).width = value; } get { return ((IXCLineObject)xc_object).width; } } } public class Arc : LineObject { public Arc(int cx, int cy, int r, float start, float span) : base() { this.xc_object = XCircuit.Application.self().arc(cx, cy, r, start, start+span); } public Arc(int cx, int cy, int r) : this(cx, cy, r, 0.0f, 360.0f) { } public Point Center { get { return new Point(((IXCArc)xc_object).center); } } public short XRadius { get { return ((IXCArc)xc_object).rx; } set { ((IXCArc)xc_object).rx = value; } } public short YRadius { get { return ((IXCArc)xc_object).ry; } set { ((IXCArc)xc_object).ry = value; } } public short Radius { set { XRadius = YRadius = value; } } } public class Polygon : LineObject { public class PointCollection { private Polygon owner = null; internal PointCollection(Polygon owner) { this.owner = owner; } public Point this[int index] { get { return new Point(((IXCPolygon)owner.xc_object).get_point(index)); } } public void Add(int x, int y) { ((IXCPolygon)owner.xc_object).append(x, y); } } protected PointCollection points; public Polygon(object[] coords) : this() { this.xc_object = XCircuit.Application.self().polygon(coords); } protected Polygon() : base() { this.points = new PointCollection(this); } public PointCollection Points { get { return points; } } } public class Box : Polygon { public Box(int x, int y, int width, int height) : base() { this.xc_object = XCircuit.Application.self().box(x, y, width, height); } } }