]> O.S.I.I.S - jp/crow.git/commitdiff
IEditableTextWidget, cache design FieldInfos for iml
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 25 Mar 2021 11:39:06 +0000 (12:39 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 25 Mar 2021 11:39:06 +0000 (12:39 +0100)
Crow/src/IML/CompilerServices.cs
Crow/src/IML/Instantiator.cs
Crow/src/Interface.cs
Crow/src/Text/IEditableTextWidget.cs [new file with mode: 0644]
Crow/src/Widgets/Label.cs
Crow/src/Widgets/TextBox.cs

index 1bf378a99be930b528e7add990dd4747f1753fda..f47f4fb845e22f06112b12f856baff171ca22070 100644 (file)
@@ -91,6 +91,12 @@ namespace Crow.IML
 
                #if DESIGN_MODE
                internal static MethodInfo miDicStrStrAdd = typeof(Dictionary<string, string>).GetMethod ("set_Item", new Type[] { typeof(string), typeof(string) });
+               internal static FieldInfo fiWidget_design_id = typeof(Widget).GetField("design_id");
+               internal static FieldInfo fiWidget_design_line = typeof(Widget).GetField("design_line");
+               internal static FieldInfo fiWidget_design_column = typeof(Widget).GetField("design_column");
+               internal static FieldInfo fiWidget_design_imlPath = typeof(Widget).GetField("design_imlPath");
+               internal static FieldInfo fiWidget_design_iml_values = typeof(Widget).GetField("design_iml_values");
+
                #endif
 
                #region tree handling methods
index 34f904b7b9cdec51f9cdc8590fa141cf54cdb69d..f43e700d39eb16eba2a24c3ea12b9a2c2403bbcf 100644 (file)
@@ -386,7 +386,7 @@ namespace Crow.IML {
                void emitSetDesignAttribute (IMLContext ctx, string name, string value){
                        //store member value in iml
                        ctx.il.Emit (OpCodes.Ldloc_0);
-                       ctx.il.Emit (OpCodes.Ldfld, typeof(Widget).GetField("design_iml_values"));
+                       ctx.il.Emit (OpCodes.Ldfld, CompilerServices.fiWidget_design_iml_values);
                        ctx.il.Emit (OpCodes.Ldstr, name);
                        if (string.IsNullOrEmpty (value))
                                ctx.il.Emit (OpCodes.Ldnull);
@@ -409,17 +409,17 @@ namespace Crow.IML {
                                IXmlLineInfo li = (IXmlLineInfo)reader;
                                ctx.il.Emit (OpCodes.Ldloc_0);
                                ctx.il.Emit (OpCodes.Ldstr, this.NextDesignID);
-                               ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_id"));
+                               ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_id);
                                ctx.il.Emit (OpCodes.Ldloc_0);
                                ctx.il.Emit (OpCodes.Ldc_I4, ctx.curLine + li.LineNumber);
-                               ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_line"));
+                               ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_line);
                                ctx.il.Emit (OpCodes.Ldloc_0);
                                ctx.il.Emit (OpCodes.Ldc_I4, li.LinePosition);
-                               ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_column"));
+                               ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_column);
                                if (!string.IsNullOrEmpty (sourcePath)) {
                                        ctx.il.Emit (OpCodes.Ldloc_0);
                                        ctx.il.Emit (OpCodes.Ldstr, sourcePath);
-                                       ctx.il.Emit (OpCodes.Stfld, typeof(Widget).GetField("design_imlPath"));
+                                       ctx.il.Emit (OpCodes.Stfld, CompilerServices.fiWidget_design_imlPath);
                                }
 #endif
                                #region Styling and default values loading
index 4cd9eabb219a23fb4eefee281827f147376cb9bd..98df8ceed72e655b45626fe38389ee0ee656cea0 100644 (file)
@@ -1065,11 +1065,11 @@ namespace Crow
                /// </summary>
                public static long TEXT_CURSOR_BLINK_FREQUENCY = 400;
                internal Rectangle? textCursor = null;//last printed cursor, used to clear it.
-               internal bool forceTextCursor = true;//when true, cursor is printed even if blinkingCursor.elapsed is not reached.
+               public bool forceTextCursor = true;//when true, cursor is printed even if blinkingCursor.elapsed is not reached.
                Stopwatch blinkingCursor = Stopwatch.StartNew ();
                void drawTextCursor (Context ctx) {
                        if (forceTextCursor) {
-                               if (FocusedWidget is Label lab) {
+                               if (FocusedWidget is IEditableTextWidget lab) {
                                        if (lab.DrawCursor (ctx, out Rectangle c)) {
                                                if (textCursor != null && c != textCursor.Value)
                                                        RegisterClip (textCursor.Value);
@@ -1084,7 +1084,7 @@ namespace Crow
                                RegisterClip (textCursor.Value);
                                textCursor = null;
                                blinkingCursor.Restart ();
-                       } else if (FocusedWidget is Label lab && lab.SelectionIsEmpty) {
+                       } else if (FocusedWidget is IEditableTextWidget lab) {
                                if (blinkingCursor.ElapsedMilliseconds > TEXT_CURSOR_BLINK_FREQUENCY) {
                                        if (lab.DrawCursor (ctx, out Rectangle c)) {
                                                textCursor = c;
@@ -1752,44 +1752,40 @@ namespace Crow
                #endregion
 
                #region ILayoutable implementation
-               public virtual bool PointIsIn (ref Point m)
-               {
-                       return true;
-               }
+               public virtual bool PointIsIn (ref Point m) => true;
                public void RegisterClip (Rectangle r)
                {
                        clipping.UnionRectangle (r);
                }
-               public bool ArrangeChildren { get { return false; } }
+               public bool ArrangeChildren => false;
                public int LayoutingTries {
                        get { throw new NotImplementedException (); }
                        set { throw new NotImplementedException (); }
                }
                public LayoutingType RegisteredLayoutings {
-                       get { return LayoutingType.None; }
+                       get => LayoutingType.None;
                        set { throw new NotImplementedException (); }
                }
                public void RegisterForLayouting (LayoutingType layoutType) { throw new NotImplementedException (); }
                public bool UpdateLayout (LayoutingType layoutType) { throw new NotImplementedException (); }
-               public Rectangle ContextCoordinates (Rectangle r) { return r; }
-               public Rectangle ScreenCoordinates (Rectangle r) { return r; }
+               public Rectangle ContextCoordinates (Rectangle r) => r;
+               public Rectangle ScreenCoordinates (Rectangle r) => r;
 
                public ILayoutable Parent {
-                       get { return null; }
+                       get => null;
                        set { throw new NotImplementedException (); }
                }
                public ILayoutable LogicalParent {
-                       get { return null; }
+                       get => null;
                        set { throw new NotImplementedException (); }
                }
 
-               public Rectangle ClientRectangle {
-                       get { return clientRectangle; }
-               }
+               public Rectangle ClientRectangle => clientRectangle; 
                public Interface HostContainer {
                        get { return this; }
                }
-               public Rectangle getSlot () { return ClientRectangle; }
+               public Rectangle getSlot () => ClientRectangle;
+               public void ChildrenLayoutingConstraints(ILayoutable layoutable, ref LayoutingType layoutType){ }
                #endregion
        }
 }
diff --git a/Crow/src/Text/IEditableTextWidget.cs b/Crow/src/Text/IEditableTextWidget.cs
new file mode 100644 (file)
index 0000000..20092ac
--- /dev/null
@@ -0,0 +1,22 @@
+using System;
+using Crow.Cairo;
+
+namespace Crow
+{
+       /// <summary>
+       /// Implement this interface to have a blinking cursor for a widget
+       /// with editable text ability.
+       /// </summary>
+       public interface IEditableTextWidget
+       {
+               /// <summary>
+               /// Draw text cursor in widget with screen coordinates. This interface when implemented is called
+               /// automatically by the Interface to make the cursor blink. The blinking frequency is controlled by
+               /// Interface.TEXT_CURSOR_BLINK_FREQUENCY static field.
+               /// </summary>
+               /// <param name="ctx">The master interface context on which to draw the cursor</param>
+               /// <param name="rect">Return a clipping rectangle containing the cursor position to be cleared when needed</param>
+               /// <returns>True if cursor were drawed, false otherwise</returns>
+               bool DrawCursor (Context ctx, out Rectangle rect);              
+       }
+}
\ No newline at end of file
index 9183f7d8376962f15c795c47bf584e1720ca0ad6..9d6af8b8197683fe00209d9b97825131cdc20959 100644 (file)
@@ -14,7 +14,7 @@ namespace Crow
     /// <summary>
     /// Simple label widget possibly multiline but without tabulation handling.
     /// </summary>
-    public class Label : Widget
+    public class Label : Widget, IEditableTextWidget
     {
                #region CTOR
                protected Label () {}
@@ -490,7 +490,11 @@ namespace Crow
                                return null;
                        return cursor;
                }
-               internal virtual bool DrawCursor (Context ctx, out Rectangle rect) {
+               public virtual bool DrawCursor (Context ctx, out Rectangle rect) {
+                       if (CurrentLoc == null || !SelectionIsEmpty) {
+                               rect = default;
+                               return false;
+                       }
                        if (!CurrentLoc.Value.HasVisualX) {
                                ctx.SelectFontFace (Font.Name, Font.Slant, Font.Wheight);
                                ctx.SetFontSize (Font.Size);
index 1f9408968602dd4b6bf3332350ab21a3f67498e6..68bcffcac3cefbf4f5a8ada6b1bada59b83e0a00 100644 (file)
@@ -210,12 +210,6 @@ namespace Crow
             
             return cursor;            
         }
-        /*internal override bool DrawCursor (Context ctx, out Rectangle rect) {            
-            ctx.Translate (-scrollX, -scrollY);
-            bool result = base.DrawCursor (ctx, out rect);
-            ctx.Translate (scrollX, scrollY);
-            return result;
-        }*/
 
         void updateMaxScrolls (LayoutingType layout) {
             Rectangle cb = ClientRectangle;