]> O.S.I.I.S - jp/crow.git/commitdiff
Table widget columns resize with mouse
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 25 Mar 2021 14:53:10 +0000 (15:53 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 25 Mar 2021 14:53:10 +0000 (15:53 +0100)
Crow/src/2d/Measure.cs
Crow/src/Widgets/Label.cs
Crow/src/Widgets/TableRow.cs

index 18ab62ce8a0bed1afb34e1455bbcb368488c3a81..b5f475f5d8d1c05225741096cf2ff1d6b9638cf5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2013-2020  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+// Copyright (c) 2013-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
 //
 // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
 
@@ -13,7 +13,7 @@ namespace Crow
        /// <summary>
        /// Measure class allow proportional sizes as well as stretched and fit on content.
        /// </summary>
-       public struct Measure
+       public struct Measure : IEquatable<Measure>
        {
                /// <summary>
                /// Integer value of the measure
@@ -52,38 +52,20 @@ namespace Crow
                /// </summary>
                public bool IsRelativeToParent { get { return Value >= 0 && Units == Unit.Percent; }}
                #region Operators
-               public static implicit operator int(Measure m){
-                       return m.Value;
-               }
-               public static implicit operator Measure(int i){
-                       return new Measure(i);
-               }
-               public static implicit operator string(Measure m){
-                       return m.ToString();
-               }
-               public static implicit operator Measure(string s){
-                       return Measure.Parse(s);
-               }
+               public static implicit operator int(Measure m) => m.Value;
+               public static implicit operator Measure(int i) => new Measure(i);               
+               public static implicit operator string(Measure m) => m.ToString();              
+               public static implicit operator Measure(string s) => Measure.Parse(s);
 
-               public static bool operator ==(Measure m1, Measure m2){
-                       return m1.Value == m2.Value && m1.Units == m2.Units;
-               }
-               public static bool operator !=(Measure m1, Measure m2){
-                       return !(m1.Value == m2.Value && m1.Units == m2.Units);
-               }
+               public static bool operator ==(Measure m1, Measure m2) => m1.Equals (m2);
+               public static bool operator !=(Measure m1, Measure m2) => !m1.Equals (m2);
                #endregion
 
+               public bool Equals(Measure other) => Value == other.Value && Units == other.Units;
+
                #region Object overrides
-               public override int GetHashCode ()
-               {
-                       return Value.GetHashCode ();
-               }
-               public override bool Equals (object obj)
-               {
-                       return (obj == null || obj.GetType() != typeof(Measure)) ?
-                               false :
-                               this == (Measure)obj;
-               }
+               public override int GetHashCode () => HashCode.Combine (Value, Units);
+               public override bool Equals (object obj) => obj is Measure m ? Equals (m) : false;
                public override string ToString ()
                {
                        return Units == Unit.Inherit ? "Inherit" :
index 9d6af8b8197683fe00209d9b97825131cdc20959..f99908fb4869c5d723900e489686133b1d96065d 100644 (file)
@@ -675,7 +675,7 @@ namespace Crow
                {
                        base.onMouseMove (sender, e);
 
-                       updateHoverLocation (e.Position - ScreenCoordinates (Slot).TopLeft - ClientRectangle.TopLeft);
+                       updateHoverLocation (ScreenPointToLocal (e.Position));
 
                        if (HasFocus && IFace.IsDown (MouseButton.Left)) {
                                CurrentLoc = hoverLoc;                          
index 2754771833442ff026072c30f5973d1354f32526..a251b6f99a8de4a36a745717d0760ff292d76c12 100644 (file)
@@ -1,9 +1,11 @@
-using System.Linq;
-// Copyright (c) 2019-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+// Copyright (c) 2019-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
 //
 // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
 using System;
 using System.ComponentModel;
+using System.Linq;
+using Crow.Cairo;
+using Glfw;
 
 namespace Crow
 {
@@ -142,5 +144,46 @@ namespace Crow
 
                        base.OnChildLayoutChanges (sender, arg);
                }*/
+               int splitIndex = -1;            
+               const int minColumnSize = 10;
+               public override void onMouseMove(object sender, MouseMoveEventArgs e)
+               {                       
+                       if (Spacing > 0 && Table != null && Table.Children.Count > 0) {
+                               Point m = ScreenPointToLocal (e.Position);
+                               if (IFace.IsDown (Glfw.MouseButton.Left) && splitIndex >= 0) {
+                                       TableRow firstRow = Table.Children[0] as TableRow;
+                                       Rectangle cb = ClientRectangle;
+                                       int splitPos = (int)(0.5 * Spacing + m.X);                                      
+                                       if (splitPos > firstRow.Children[splitIndex].Slot.Left + minColumnSize && splitPos < firstRow.Children[splitIndex+1].Slot.Right - minColumnSize) {
+                                               Table.Columns[splitIndex+1].Width = firstRow.Children[splitIndex+1].Slot.Right - splitPos;
+                                               splitPos -= Spacing;
+                                               Table.Columns[splitIndex].Width = splitPos - firstRow.Children[splitIndex].Slot.Left;
+                                               Table.RegisterForLayouting (LayoutingType.Width);
+                                       }
+                                       //Console.WriteLine ($"left:{firstRow.Children[splitIndex].Slot.Left} right:{firstRow.Children[splitIndex+1].Slot.Right} cb.X:{cb.X} splitPos:{splitPos} m:{m}");                               
+                               } else {
+                                       splitIndex = -1;                                        
+                                       for (int i = 0; i < Children.Count - 1; i++)
+                                       {
+                                               Rectangle r = Children[i].Slot;
+                                               if (m.X >= r.Right) {
+                                                       r = Children[i+1].Slot;
+                                                       if (m.X <= r.Left && Table.Columns.Count - 1 > i ) {
+                                                               Console.WriteLine ($"Set cursor Table row on mouse move. {m}");
+                                                               IFace.MouseCursor = MouseCursor.sb_h_double_arrow;
+                                                               splitIndex = i;
+                                                               e.Handled = true;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                                       if (splitIndex < 0) {
+                                               IFace.MouseCursor = MouseCursor.top_left_arrow;
+                                               Console.WriteLine ($"RESet cursor Table row on mouse move. {m}");
+                                       }
+                               }
+                       }
+                       base.onMouseMove(sender, e);
+               }
        }
 }