From: Jean-Philippe Bruyère Date: Sat, 10 Mar 2018 05:03:25 +0000 (+0100) Subject: getStremFromPath made instanced for overriding in designiface, crowide wip X-Git-Tag: v0.9.5-beta~153^2~11 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=523a04de5252ae089e52eeaf9c8d83a5e0676731;p=jp%2Fcrow.git getStremFromPath made instanced for overriding in designiface, crowide wip --- diff --git a/Tests/OpenGL/Shader.cs b/Tests/OpenGL/Shader.cs index f91a982c..1253ff72 100644 --- a/Tests/OpenGL/Shader.cs +++ b/Tests/OpenGL/Shader.cs @@ -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 (); diff --git a/Tests/OpenGL/Texture.cs b/Tests/OpenGL/Texture.cs index ce24808b..08215ada 100644 --- a/Tests/OpenGL/Texture.cs +++ b/Tests/OpenGL/Texture.cs @@ -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; diff --git a/src/BmpPicture.cs b/src/BmpPicture.cs index b8271615..e3af9c79 100644 --- a/src/BmpPicture.cs +++ b/src/BmpPicture.cs @@ -55,7 +55,7 @@ namespace Crow /// load the image for rendering from the path given as argument /// /// image path, may be embedded - 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); } diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index 12a020ff..e315f761 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -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 /// /// create delegate helper /// - 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) { diff --git a/src/ExtensionsMethods.cs b/src/ExtensionsMethods.cs index d7448874..cb662c51 100644 --- a/src/ExtensionsMethods.cs +++ b/src/ExtensionsMethods.cs @@ -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); diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index c7491330..5b6438c2 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -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: diff --git a/src/GraphicObjects/Group.cs b/src/GraphicObjects/Group.cs index 43f08198..9cedec81 100644 --- a/src/GraphicObjects/Group.cs +++ b/src/GraphicObjects/Group.cs @@ -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) diff --git a/src/GraphicObjects/Image.cs b/src/GraphicObjects/Image.cs index d6392626..c59abca8 100644 --- a/src/GraphicObjects/Image.cs +++ b/src/GraphicObjects/Image.cs @@ -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; diff --git a/src/GraphicObjects/TabItem.cs b/src/GraphicObjects/TabItem.cs index 584c180e..473fc313 100644 --- a/src/GraphicObjects/TabItem.cs +++ b/src/GraphicObjects/TabItem.cs @@ -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 { diff --git a/src/IML/IMLContext.cs b/src/IML/IMLContext.cs index b11dfa69..aa4dee2f 100644 --- a/src/IML/IMLContext.cs +++ b/src/IML/IMLContext.cs @@ -183,23 +183,48 @@ namespace Crow.IML /// Bd. /// passed as arg to prevent refetching it for the 3rd time 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 diff --git a/src/Instantiator.cs b/src/Instantiator.cs index 49d4f533..3eab819a 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -85,7 +85,7 @@ namespace Crow.IML /// /// Initializes a new instance of the Instantiator class. /// - public Instantiator (Interface _iface, string path) : this (_iface, Interface.GetStreamFromPath(path), path) { + public Instantiator (Interface _iface, string path) : this (_iface, _iface.GetStreamFromPath(path), path) { } /// @@ -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)) { diff --git a/src/Interface.cs b/src/Interface.cs index f17ac895..00a952b0 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -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 /// A file or resource stream /// This could be a normal file path, or an embedded ressource ID /// Resource ID's must be prefixed with '#' character - 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; diff --git a/src/Picture.cs b/src/Picture.cs index fe7007c7..3784780b 100644 --- a/src/Picture.cs +++ b/src/Picture.cs @@ -87,7 +87,7 @@ namespace Crow /// image path, may be embedded 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 /// /// picture stream - public abstract void Load(string path); + public abstract void Load(Interface iface, string path); #endregion /// @@ -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; } diff --git a/src/SvgPicture.cs b/src/SvgPicture.cs index ce7fdedf..f676a497 100644 --- a/src/SvgPicture.cs +++ b/src/SvgPicture.cs @@ -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); diff --git a/src/XCursor.cs b/src/XCursor.cs index 627e2afd..1cca510e 100644 --- a/src/XCursor.cs +++ b/src/XCursor.cs @@ -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)