]> O.S.I.I.S - jp/crow.git/commitdiff
getStremFromPath made instanced for overriding in designiface, crowide wip
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 10 Mar 2018 05:03:25 +0000 (06:03 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 10 Mar 2018 05:03:25 +0000 (06:03 +0100)
15 files changed:
Tests/OpenGL/Shader.cs
Tests/OpenGL/Texture.cs
src/BmpPicture.cs
src/CompilerServices/CompilerServices.cs
src/ExtensionsMethods.cs
src/GraphicObjects/GraphicObject.cs
src/GraphicObjects/Group.cs
src/GraphicObjects/Image.cs
src/GraphicObjects/TabItem.cs
src/IML/IMLContext.cs
src/Instantiator.cs
src/Interface.cs
src/Picture.cs
src/SvgPicture.cs
src/XCursor.cs

index f91a982c764a61ee5cdada1a7d9ae8da36fc99ba..1253ff72b6804affb0aca58401bb581455c18459 100644 (file)
@@ -269,7 +269,7 @@ namespace Crow
                        Stream s;
 
                        if (!string.IsNullOrEmpty (VertSourcePath)) {
-                               s = Crow.Interface.GetStreamFromPath (VertSourcePath);
+                               s = Crow.Interface.StaticGetStreamFromPath (VertSourcePath);
                                if (s != null) {
                                        using (StreamReader sr = new StreamReader (s)) {
                                                vertSource = sr.ReadToEnd ();
@@ -278,7 +278,7 @@ namespace Crow
                        }
 
                        if (!string.IsNullOrEmpty (FragSourcePath)) {
-                               s = Crow.Interface.GetStreamFromPath (FragSourcePath);
+                               s = Crow.Interface.StaticGetStreamFromPath (FragSourcePath);
                                if (s != null) {
                                        using (StreamReader sr = new StreamReader (s)) {
                                                fragSource = sr.ReadToEnd ();
@@ -287,7 +287,7 @@ namespace Crow
                        }
 
                        if (!string.IsNullOrEmpty (GeomSourcePath)) {
-                               s = Crow.Interface.GetStreamFromPath (GeomSourcePath);
+                               s = Crow.Interface.StaticGetStreamFromPath (GeomSourcePath);
                                if (s != null) {
                                        using (StreamReader sr = new StreamReader (s)) {
                                                geomSource = sr.ReadToEnd ();
index ce24808b4daab8e55a852ea890946052190c7a99..08215ada2ed21fd0ed514dd024f97d1ec25fc6ac 100644 (file)
@@ -37,7 +37,7 @@ namespace Crow
                        
                public Texture(string _mapPath, bool flipY = true)
         {
-                       using (Stream s = Interface.GetStreamFromPath (_mapPath)) {
+                       using (Stream s = Interface.StaticGetStreamFromPath (_mapPath)) {
 
                                try {
                                        Map = _mapPath;
index b827161514f4ee9b877f31a8d7ab8f2eac568a98..e3af9c79781761a5a6abb6fdffce3788b3a17470 100644 (file)
@@ -55,7 +55,7 @@ namespace Crow
                /// load the image for rendering from the path given as argument
                /// </summary>
                /// <param name="path">image path, may be embedded</param>
-               public override void Load (string path)
+               public override void Load (Interface iface, string path)
                {
                        Path = path;
                        if (sharedResources.ContainsKey (path)) {
@@ -64,11 +64,8 @@ namespace Crow
                                Dimensions = sp.Dims;
                                return;
                        }
-                       using (Stream stream = Interface.GetStreamFromPath (path)) {
-                               using (MemoryStream ms = new MemoryStream ()) {
-                                       stream.CopyTo (ms);
-                                       loadBitmap (new System.Drawing.Bitmap (ms));    
-                               }
+                       using (Stream stream = iface.GetStreamFromPath (path)) {                                
+                               loadBitmap (new System.Drawing.Bitmap (stream));        
                        }
                        sharedResources [path] = new sharedPicture (image, Dimensions);
                }
index 12a020ffa73c9d1f8b24a96a67764768f7f0678e..e315f7616d951cfa8f0c267186ca559f3280a1ca 100644 (file)
@@ -276,7 +276,11 @@ namespace Crow.IML
 
                #region Reflexion helpers
                static MemberInfo getMemberInfoWithReflexion(object instance, string member){
-                       return instance.GetType ().GetMember (member)?.FirstOrDefault();
+                       Type t = instance.GetType();
+                       MemberInfo mi = t.GetMember (member)?.FirstOrDefault();
+                       if (mi == null)
+                               mi = CompilerServices.SearchExtMethod (t, member);
+                       return mi;
                }
                static MethodInfo getMethodInfoWithReflexion(object instance, string method){
                        return instance.GetType ().GetMethod (method, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
@@ -344,12 +348,20 @@ namespace Crow.IML
                                        PropertyInfo pi = mi as PropertyInfo;
                                        tmp = pi.GetValue (instance);
                                        dstType = pi.PropertyType;
-                               }
-                               if (mi.MemberType == MemberTypes.Field) {
+                               }else if (mi.MemberType == MemberTypes.Field) {
                                        FieldInfo fi = mi as FieldInfo;
                                        tmp = fi.GetValue (instance);
                                        dstType = fi.FieldType;
+                               }else if (mi.MemberType == MemberTypes.Method) {
+                                       MethodInfo gi = mi as MethodInfo;
+                                       if (gi.IsStatic)
+                                               tmp = gi.Invoke(null, new object[] {instance});
+                                       else
+                                               tmp = gi.Invoke(instance, null);
+                                       dstType = gi.ReturnType;
                                }
+
+
                                if (tmp != null)
                                        return tmp;
                                if (dstType == typeof(string) || dstType == CompilerServices.TObject)//TODO:object should be allowed to return null and not ""
@@ -693,7 +705,7 @@ namespace Crow.IML
                /// <summary>
                /// create delegate helper
                /// </summary>
-               static Delegate createDel(Type eventType, object instance, string method){
+               static Delegate createDel(object instance, Type eventType, string method){
                        Type t = instance.GetType ();
                        MethodInfo mi = t.GetMethod (method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        if (mi == null) {
index d74488743e617abda11acbd17345ca990f4d366d..cb662c51dc2aaebd9f6781896422867c5b28584e 100644 (file)
@@ -144,7 +144,12 @@ namespace Crow
                                handler(sender, e);
                        }
                }
-
+               public static byte[] GetBytes(this string str)
+               {
+                       byte[] bytes = new byte[str.Length * sizeof(char)];
+                       System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
+                       return bytes;
+               }
                public static bool IsWhiteSpaceOrNewLine (this char c)
                {
                        return c == '\t' || c == '\r' || c == '\n' || char.IsWhiteSpace (c);
index c74913300a43c2c8313647676ec43eef5672660a..5b6438c2168846fbd6b307dd4234f6edf67380e3 100644 (file)
@@ -1404,11 +1404,11 @@ namespace Crow
                        IFace.currentLQI.NewSlot = Slot;
                        Debug.WriteLine ("\t\t{0} => {1}",LastSlots,Slot);
                        #endif
-                       #if DESIGN_MODE
-                       if (IFace.GetType().Name == "DesignInterface"){
-                               Debug.WriteLine ("\t\t{2}: {0} => {1}",LastSlots,Slot,this.name);
-                       }
-                       #endif
+//                     #if DESIGN_MODE
+//                     if (IFace.GetType().Name == "DesignInterface"){
+//                             Debug.WriteLine ("\t\t{2}: {0} => {1}",LastSlots,Slot,this.name);
+//                     }
+//                     #endif
 
                        switch (layoutType) {
                        case LayoutingType.Width:
index 43f0819844b8d676a31aace5f2f61bab15a5ff58..9cedec81e766d1909bf897186e1573aa01475f53 100644 (file)
@@ -167,6 +167,17 @@ namespace Crow
                        this.RegisterForLayouting (LayoutingType.Sizing);
                        ChildrenCleared.Raise (this, new EventArgs ());
                }
+               public override void OnDataSourceChanged (object sender, DataSourceChangeEventArgs e)
+               {
+                       base.OnDataSourceChanged (this, e);
+
+                       childrenRWLock.EnterReadLock ();
+                       foreach (GraphicObject g in Children) {
+                               if (g.localDataSourceIsNull & g.localLogicalParentIsNull)
+                                       g.OnDataSourceChanged (g, e);   
+                       }
+                       childrenRWLock.ExitReadLock ();
+               }
 
                public void putWidgetOnTop(GraphicObject w)
                {
@@ -194,18 +205,7 @@ namespace Crow
                }
 
                #region GraphicObject overrides
-               public override void OnDataSourceChanged (object sender, DataSourceChangeEventArgs e)
-               {
-                       base.OnDataSourceChanged (this, e);
 
-                       childrenRWLock.EnterReadLock ();
-
-                       foreach (GraphicObject g in children)
-                               if (g.localDataSourceIsNull & g.localLogicalParentIsNull)
-                                       g.OnDataSourceChanged (g, e);
-                       
-                       childrenRWLock.ExitReadLock ();
-               }
                public override GraphicObject FindByName (string nameToFind)
                {
                        if (Name == nameToFind)
index d6392626c795c2390f9a31376d071f8f6c7dd994..c59abca8417279b924b7475afc01c08f362a9a24 100644 (file)
@@ -169,11 +169,11 @@ namespace Crow
                {
                        Picture pic;
                        if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture))
-                               pic = new SvgPicture ();
+                               pic = new SvgPicture (path);
                        else
-                               pic = new BmpPicture ();
+                               pic = new BmpPicture (path);
 
-                       pic.Load (path);
+                       pic.Load (IFace, path);
                        pic.Scaled = scaled;
                        pic.KeepProportions = keepProps;
 
index 584c180e53838ee8f2bfb9e07a79604750b5fc59..473fc313e4c11dd59c0fb37313a914e7745ca218 100644 (file)
@@ -90,7 +90,7 @@ namespace Crow
                        }
                }
                        
-               [XmlAttributeAttribute][DefaultValue(0)]
+               [DefaultValue(0)]
                public int TabOffset {
                        get { return tabOffset; }
                        set {
@@ -109,7 +109,7 @@ namespace Crow
                public Measure TabWidth {
                        get { return tview == null ? Measure.Fit : tview.TabWidth; }
                }
-               [XmlAttributeAttribute][DefaultValue(false)]
+               [DefaultValue(false)]
                public virtual bool IsSelected {
                        get { return isSelected; }
                        set {
index b11dfa69dd36a4667dfbc066819111a5c0f55e96..aa4dee2f768f6bdd0b9d9e4590ee715e964fdc0e 100644 (file)
@@ -183,23 +183,48 @@ namespace Crow.IML
                /// <param name="bd">Bd.</param>
                /// <param name="evt">passed as arg to prevent refetching it for the 3rd time</param>
                public void emitHandlerMethodAddition(EventBinding bd){
+
                        //fetch source instance with address for handler addition (as 1st arg of handler.add)
                        il.Emit (OpCodes.Ldloc_0);//push root
                        CompilerServices.emitGetInstance (il, bd.SourceNA);
 
+                       il.Emit (OpCodes.Ldloc_0);
+                       CompilerServices.emitGetInstance (il, bd.TargetNA);
+
+                       string[] membs = bd.TargetMember.Split ('.');
+                       for (int i = 0; i < membs.Length - 1; i++) {
+                               il.Emit (OpCodes.Dup);
+                               il.Emit (OpCodes.Ldstr, membs[i]);
+                               il.Emit (OpCodes.Call, CompilerServices.miGetMembIinfoWithRefx);
+                               il.Emit (OpCodes.Call, CompilerServices.miGetValWithRefx);
+                       }
+
                        //load handlerType of sourceEvent to create handler delegate (1st arg)
                        il.Emit (OpCodes.Ldtoken, bd.SourceEvent.EventHandlerType);
                        il.Emit (OpCodes.Call, CompilerServices.miGetTypeFromHandle);
-                       //load target the where the method is defined (2nd arg)
-                       il.Emit (OpCodes.Ldloc_0);
-                       CompilerServices.emitGetInstance (il, bd.TargetNA);
                        //load methodInfo (3rd arg)
-                       il.Emit (OpCodes.Ldstr, bd.TargetMember);
-
+                       il.Emit (OpCodes.Ldstr, membs[membs.Length-1]);
                        il.Emit (OpCodes.Callvirt, CompilerServices.miCreateDel);
-
                        il.Emit (OpCodes.Callvirt, bd.SourceEvent.AddMethod);//call add event
                }
-
+//             public void emitHandlerMethodAddition(EventBinding bd){
+//                     //fetch source instance with address for handler addition (as 1st arg of handler.add)
+//                     il.Emit (OpCodes.Ldloc_0);//push root
+//                     CompilerServices.emitGetInstance (il, bd.SourceNA);
+//
+//                     //load handlerType of sourceEvent to create handler delegate (1st arg)
+//                     il.Emit (OpCodes.Ldtoken, bd.SourceEvent.EventHandlerType);
+//                     il.Emit (OpCodes.Call, CompilerServices.miGetTypeFromHandle);
+//                     //load target the where the method is defined (2nd arg)
+//                     il.Emit (OpCodes.Ldloc_0);
+//                     CompilerServices.emitGetInstance (il, bd.TargetNA);
+//                     //load methodInfo (3rd arg)
+//                     il.Emit (OpCodes.Ldstr, bd.TargetMember);
+//
+//                     il.Emit (OpCodes.Callvirt, CompilerServices.miCreateDel);
+//
+//                     il.Emit (OpCodes.Callvirt, bd.SourceEvent.AddMethod);//call add event
+//             }
+//
        }
 }
\ No newline at end of file
index 49d4f5334c37beb2b8f0d1910484abf4b2575d6e..3eab819a417f665e03a622176fd8ad52457dfac2 100644 (file)
@@ -85,7 +85,7 @@ namespace Crow.IML
                /// <summary>
                /// Initializes a new instance of the Instantiator class.
                /// </summary>
-               public Instantiator (Interface _iface, string path) : this (_iface, Interface.GetStreamFromPath(path), path) {
+               public Instantiator (Interface _iface, string path) : this (_iface, _iface.GetStreamFromPath(path), path) {
                        
                }
                /// <summary>
@@ -335,7 +335,7 @@ namespace Crow.IML
                                                if (iface.Instantiators.ContainsKey (itemTemplatePath)) {
                                                        itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "" });
                                                } else {
-                                                       using (Stream stream = Interface.GetStreamFromPath (itemTemplatePath)) {
+                                                       using (Stream stream = iface.GetStreamFromPath (itemTemplatePath)) {
                                                                //itemtemplate files may have multiple root nodes
                                                                XmlReaderSettings itrSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
                                                                using (XmlReader itr = XmlReader.Create (stream, itrSettings)) {                                                                        
index f17ac89515b54581b08b7eb32b385dd72490b1b2..00a952b0e440392d2d15cd0b840f197241c4ef67 100644 (file)
@@ -88,8 +88,6 @@ namespace Crow
                                }
                        }
 
-                       loadCursors ();
-
                        FontRenderingOptions = new FontOptions ();
                        FontRenderingOptions.Antialias = Antialias.Subpixel;
                        FontRenderingOptions.HintMetrics = HintMetrics.On;
@@ -103,6 +101,7 @@ namespace Crow
 
                public void Init () {
                        CurrentInterface = this;
+                       loadCursors ();
                        loadStyling ();
                        findAvailableTemplates ();
                        initTooltip ();
@@ -271,17 +270,17 @@ namespace Crow
 
                        }
                }
-               static void loadCursors(){
+               void loadCursors(){
                        //Load cursors
-                       XCursor.Cross = XCursorFile.Load("#Crow.Images.Icons.Cursors.cross").Cursors[0];
-                       XCursor.Default = XCursorFile.Load("#Crow.Images.Icons.Cursors.arrow").Cursors[0];
-                       XCursor.NW = XCursorFile.Load("#Crow.Images.Icons.Cursors.top_left_corner").Cursors[0];
-                       XCursor.NE = XCursorFile.Load("#Crow.Images.Icons.Cursors.top_right_corner").Cursors[0];
-                       XCursor.SW = XCursorFile.Load("#Crow.Images.Icons.Cursors.bottom_left_corner").Cursors[0];
-                       XCursor.SE = XCursorFile.Load("#Crow.Images.Icons.Cursors.bottom_right_corner").Cursors[0];
-                       XCursor.H = XCursorFile.Load("#Crow.Images.Icons.Cursors.sb_h_double_arrow").Cursors[0];
-                       XCursor.V = XCursorFile.Load("#Crow.Images.Icons.Cursors.sb_v_double_arrow").Cursors[0];
-                       XCursor.Text = XCursorFile.Load("#Crow.Images.Icons.Cursors.ibeam").Cursors[0];
+                       XCursor.Cross = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.cross").Cursors[0];
+                       XCursor.Default = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.arrow").Cursors[0];
+                       XCursor.NW = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.top_left_corner").Cursors[0];
+                       XCursor.NE = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.top_right_corner").Cursors[0];
+                       XCursor.SW = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.bottom_left_corner").Cursors[0];
+                       XCursor.SE = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.bottom_right_corner").Cursors[0];
+                       XCursor.H = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.sb_h_double_arrow").Cursors[0];
+                       XCursor.V = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.sb_v_double_arrow").Cursors[0];
+                       XCursor.Text = XCursorFile.Load(this, "#Crow.Images.Icons.Cursors.ibeam").Cursors[0];
                }
                #endregion
 
@@ -329,7 +328,28 @@ namespace Crow
                /// <returns>A file or resource stream</returns>
                /// <param name="path">This could be a normal file path, or an embedded ressource ID
                /// Resource ID's must be prefixed with '#' character</param>
-               public static Stream GetStreamFromPath (string path)
+               public virtual Stream GetStreamFromPath (string path)
+               {
+                       Stream stream = null;
+
+                       if (path.StartsWith ("#")) {
+                               string resId = path.Substring (1);
+                               //try/catch added to prevent nunit error
+                               try {
+                                       stream = System.Reflection.Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
+                               } catch{}
+                               if (stream == null)//try to find ressource in Crow assembly
+                                       stream = System.Reflection.Assembly.GetExecutingAssembly ().GetManifestResourceStream (resId);
+                               if (stream == null)
+                                       throw new Exception ("Resource not found: " + path);
+                       } else {
+                               if (!File.Exists (path))
+                                       throw new FileNotFoundException ("File not found: ", path);
+                               stream = new FileStream (path, FileMode.Open, FileAccess.Read);
+                       }
+                       return stream;
+               }
+               public static Stream StaticGetStreamFromPath (string path)
                {
                        Stream stream = null;
 
index fe7007c79d43a9d18bde12be95c2f66e32e2357e..3784780b812dbcae8def3491e482634dc238fc46 100644 (file)
@@ -87,7 +87,7 @@ namespace Crow
                /// <param name="path">image path, may be embedded</param>
                public Picture (string path)
                {
-                       Load (path);
+                       Path = path;
                }
                #endregion
 
@@ -96,7 +96,7 @@ namespace Crow
                /// load the image for rendering from the stream given as argument
                /// </summary>
                /// <param name="stream">picture stream</param>
-               public abstract void Load(string path);
+               public abstract void Load(Interface iface, string path);
                #endregion
 
                /// <summary>
@@ -117,11 +117,9 @@ namespace Crow
                        Picture _pic = null;
 
                        if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) 
-                               _pic = new SvgPicture ();
+                               _pic = new SvgPicture (path);
                        else 
-                               _pic = new BmpPicture ();
-
-                       _pic.Load (path);                       
+                               _pic = new BmpPicture (path);
 
                        return _pic;
                }
@@ -139,11 +137,9 @@ namespace Crow
                        Picture _pic = null;
 
                        if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) 
-                               _pic = new SvgPicture ();
+                               _pic = new SvgPicture (path);
                        else 
-                               _pic = new BmpPicture ();
-
-                       _pic.Load (path);                       
+                               _pic = new BmpPicture (path);
 
                        return _pic;
                }
index ce7fdedfc5a16237c7c44d0aabfcc41a7900d693..f676a4975d3796be9b280a4cca519415df4695eb 100644 (file)
@@ -51,7 +51,7 @@ namespace Crow
                {}
                #endregion
 
-               public override void Load (string path)
+               public override void Load (Interface iface, string path)
                {
                        Path = path;
                        if (sharedResources.ContainsKey (path)) {
@@ -60,7 +60,7 @@ namespace Crow
                                Dimensions = sp.Dims;
                                return;
                        }
-                       using (Stream stream = Interface.GetStreamFromPath (path)) {
+                       using (Stream stream = iface.GetStreamFromPath (path)) {
                                using (MemoryStream ms = new MemoryStream ()) {
                                        stream.CopyTo (ms);
 
index 627e2afdd473aa1cd70bc0580537b3a99fc3672c..1cca510e61beb62f3b1d38fa9db8ac00b7b0e813 100644 (file)
@@ -86,9 +86,9 @@ namespace Crow
                        return tmp;
                }
 
-               public static XCursorFile Load(string path)
+               public static XCursorFile Load(Interface iface, string path)
                {
-                       return loadFromStream (Interface.GetStreamFromPath (path));
+                       return loadFromStream (iface.GetStreamFromPath (path));
                }
 
                static XCursor imageLoad(BinaryReader sr)