]> O.S.I.I.S - jp/crow.git/commitdiff
paged ListBox
authorjpbruyere <jp.bruyere@hotmail.com>
Mon, 8 Feb 2016 10:54:30 +0000 (11:54 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Mon, 8 Feb 2016 10:54:30 +0000 (11:54 +0100)
Crow.csproj
Templates/ListBox.goml
Tests/GOLIBTests.cs
Tests/Interfaces/testColorList.crow
src/GraphicObjects/GraphicObject.cs
src/GraphicObjects/ListBox.cs
src/GraphicObjects/Scroller.cs
src/ScrollingEventArgs.cs [new file with mode: 0644]

index f44744924c62363b40f161aea4efc2e3f90e9505..fbb6ca242a161c08831bdfc17854ee069f2081e2 100644 (file)
     <Compile Include="src\Gradient.cs" />
     <Compile Include="src\Fill\Fill.cs" />
     <Compile Include="src\LayoutingEventArgs.cs" />
+    <Compile Include="src\ScrollingEventArgs.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
index ce05400bf0d2315d9782351fd20e6b82165a900e..0a21be0f940b5e8265640fa1420bac8ff0061d9a 100755 (executable)
@@ -5,6 +5,7 @@
 <Border BorderWidth="1" Margin="1" MinimumSize="20;20" Height="{../TemplatedHeight}" Width="{../TemplatedWidth}" >
        <Scroller  Name="scroller1" Margin="1" VerticalScrolling="true"
                Height="{../../TemplatedHeight}" Width="{../../TemplatedWidth}">
-               <VerticalStack Height="-1" Width="{../../../TemplatedWidth}" Name="List" Margin="0" VerticalAlignment="Top"/>
+               <VerticalStack LayoutChanged="../../../_list_LayoutChanged"
+                       Height="-1" Width="{../../../TemplatedWidth}" Name="List" Margin="0" VerticalAlignment="Top"/>
        </Scroller>
 </Border>
\ No newline at end of file
index 2b2aabd6d7facce5f3dbb40b2a9decd30d8fe0e8..f6c333946a69cbfe20dfc3ce97f3eaacb35ed642 100644 (file)
@@ -37,10 +37,10 @@ namespace test
                int frameCpt = 0;
                int idx = 0;
                string[] testFiles = {
+                       "testColorList.crow",
                        "0.crow",
                        "testExpandable.goml",
                        "4.crow",
-                       "testColorList.crow",
                        "5.crow",
                        "testSpinner.goml",
                        "testScrollbar.goml",
index a39547f3a37595fa74b38e39cc88b955ccba8a31..fc5e023d8b1486e59fcc57d79cab60a695109178 100755 (executable)
@@ -8,9 +8,11 @@
                                <Border BorderWidth="1" Height="{../TemplatedHeight}" Width="{../TemplatedWidth}">
                                        <HorizontalStack Margin="1" Height="{../../TemplatedHeight}" Width="{../../TemplatedWidth}" >
                                                <Scroller Name="scroller1" Height="{../../../TemplatedHeight}" Width="{../../../TemplatedWidth}" 
-                                                               Margin="2" VerticalScrolling="true" ScrollY="{../scrollbar1.Value}">
+                                                               Margin="2" VerticalScrolling="true" ScrollY="{../scrollbar1.Value}"
+                                                               ValueChanged="../../../_scroller_ValueChanged">
                                                        <VerticalStack Width="{../../../../TemplatedWidth}" Height="-1" MinimumSize="100;100"
-                                                               Name="List" Margin="0" VerticalAlignment="Top" />
+                                                               Name="List" Margin="0" VerticalAlignment="Top"
+                                                               LayoutChanged="../../../../_list_LayoutChanged"/>
                                                </Scroller>
                                                <ScrollBar Name="scrollbar1" Value="{../scroller1.ScrollY}" 
                                                        Maximum="{../scroller1.MaximumScroll}" Orientation="Vertical" 
index d75676595b9ecd9d703b2d56d101d7d8dfef8b34..43b9a7251f8fca89f48513805a17a107eaaa0425 100644 (file)
@@ -21,7 +21,7 @@ using System.Reflection.Emit;
 
 namespace Crow
 {              
-       public class GraphicObject : IXmlSerializable, ILayoutable, IValueChange
+       public class GraphicObject : IXmlSerializable, ILayoutable, IValueChange, ICloneable
        {
                #if DEBUG_LAYOUTING
                internal static ulong currentUid = 0;
@@ -462,7 +462,7 @@ namespace Crow
                }
 
                public virtual GraphicObject FindByName(string nameToFind){
-                       return nameToFind == _name ? this : null;
+                       return string.Equals(nameToFind, _name, StringComparison.Ordinal) ? this : null;
                }
                public virtual bool Contains(GraphicObject goToFind){
                        return false;
@@ -1381,5 +1381,30 @@ namespace Crow
                }
                #endregion
 
+               #region ICloneable implementation
+               public object Clone ()
+               {
+                       Type type = this.GetType ();
+                       GraphicObject result = (GraphicObject)Activator.CreateInstance (type);
+
+                       foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
+                               if (pi.GetSetMethod () == null)
+                                       continue;
+
+                               if (pi.GetCustomAttribute<XmlIgnoreAttribute> () != null)
+                                       continue;
+
+//                             object[] att = pi.GetCustomAttributes (false);
+//                             foreach (object o in att) {
+//                                     XmlIgnoreAttribute xia = o as XmlIgnoreAttribute;
+//                                     if (xia != null)
+//                                             continue;
+//                             }
+
+                               pi.SetValue(result, pi.GetValue(this));
+                       }
+                       return result;
+               }
+               #endregion
        }
 }
index 3822275e3ecaa91ed8ae0d8a9d6feb6b21de3b56..607f6316fe76e6e08febf072610a7806e310d159 100644 (file)
@@ -40,6 +40,7 @@ namespace Crow
                #endregion
 
                Group _list;
+               GenericStack _gsList;
                IList data;
                int _selectedIndex;
                string _itemTemplate;
@@ -51,21 +52,42 @@ namespace Crow
                {
                        base.loadTemplate (template);
                        _list = this.child.FindByName ("List") as Group;
+                       _gsList = _list as GenericStack;
                }
+
+
                #endregion
 
                [XmlAttributeAttribute][DefaultValue("#Crow.Templates.ItemTemplate.goml")]
                public string ItemTemplate {
                        get { return _itemTemplate; }
                        set { 
+                               if (value == _itemTemplate)
+                                       return;
+
+                               _itemTemplate = value;
+
+                               if (templateStream != null) {
+                                       templateStream.Dispose ();
+                                       templateStream = null;
+                               }
+
                                //TODO:reload list with new template?
-                               _itemTemplate = value; 
+                               NotifyValueChanged("ItemTemplate", _itemTemplate);
                        }
                }
                [XmlAttributeAttribute][DefaultValue(-1)]
                public int SelectedIndex{
                        get { return _selectedIndex; }
-                       set { _selectedIndex = value; }
+                       set { 
+                               if (value == _selectedIndex)
+                                       return;
+                               
+                               _selectedIndex = value; 
+
+                               NotifyValueChanged ("SelectedIndex", _selectedIndex);
+                               NotifyValueChanged ("SelectedItem", SelectedItem);
+                       }
                }
                public object SelectedItem{
                        get { return data == null ? null : data[_selectedIndex]; }
@@ -76,44 +98,112 @@ namespace Crow
                                return data;
                        }
                        set {
+                               if (value == data)
+                                       return;
                                
                                data = value;
 
+                               NotifyValueChanged ("Data", data);
+
                                _list.ClearChildren ();
 
                                if (data == null)
                                        return;
-                               
-                               #if DEBUG_LOAD_TIME
-                               Stopwatch loadingTime = new Stopwatch ();
-                               loadingTime.Start ();
-                               #endif
 
-                               MemoryStream ms = new MemoryStream ();
+                               loadPage (1);
+                       }
+               }
+               int itemPerPage = 40;
+               MemoryStream templateStream = null;
+               Type templateBaseType = null;
+
+               void loadPage(int pageNum)
+               {
+                       #if DEBUG_LOAD_TIME
+                       Stopwatch loadingTime = new Stopwatch ();
+                       loadingTime.Start ();
+                       #endif
+
+                       if (templateStream == null) {
+                               templateStream = new MemoryStream ();
                                lock (ItemTemplate) {
                                        using (Stream stream = Interface.GetStreamFromPath (ItemTemplate))
-                                               stream.CopyTo (ms);
+                                               stream.CopyTo (templateStream);
                                }
+                               templateBaseType = Interface.GetTopContainerOfGOMLStream (templateStream);
+                       }
 
-                               Type t = Interface.GetTopContainerOfGOMLStream (ms);
+                       Group page = _list.Clone () as Group;
+                       page.Name = "page" + pageNum;
 
-                               foreach (var item in data) {
-                                       ms.Seek(0,SeekOrigin.Begin);
-                                       GraphicObject g = Interface.Load (ms, t);
-                                       g.MouseClick += itemClick;
-                                       _list.AddChild (g);
-                                       g.DataSource = item;
-                               }
 
-                               ms.Dispose ();                  
+                       for (int i = (pageNum - 1) * itemPerPage; i < pageNum * itemPerPage; i++) {
+                               if (i >= data.Count)
+                                       break;
+                               templateStream.Seek(0,SeekOrigin.Begin);
+                               GraphicObject g = Interface.Load (templateStream, templateBaseType);
+                               g.MouseClick += itemClick;
+                               page.AddChild (g);
+                               g.DataSource = data [i];
+                               //g.LogicalParent = this;
+                       }
+
+                       _list.AddChild (page);
+
+                       #if DEBUG_LOAD_TIME
+                       loadingTime.Stop ();
+                       Debug.WriteLine("Listbox {2} Loading: {0} ticks \t, {1} ms",
+                       loadingTime.ElapsedTicks,
+                       loadingTime.ElapsedMilliseconds, this.ToString());
+                       #endif
+               }
+               void _scroller_ValueChanged (object sender, ValueChangeEventArgs e)
+               {
+                       if (_gsList == null)
+                               return;
+
+                       if (_gsList.Orientation == Orientation.Horizontal) {
+                       } else {
+                               if (!string.Equals (e.MemberName, "ScrollY"))
+                                       return;
+
+                               double scroll = (double)e.NewValue;
+                               int pageHeight = (int)Math.Ceiling((double)_gsList.getSlot().Height / (double)data.Count * (double)itemPerPage);
 
-                               #if DEBUG_LOAD_TIME
-                               loadingTime.Stop ();
-                               Debug.WriteLine("Listbox {2} Loading: {0} ticks \t, {1} ms",
-                               loadingTime.ElapsedTicks,
-                               loadingTime.ElapsedMilliseconds, this.ToString());
-                               #endif
+                               int pagePtr = (int)Math.Ceiling(scroll / (double)pageHeight);
 
+                               for (int i = _gsList.Children.Count+1; i <= pagePtr+1; i++) {
+                                       loadPage (i);                                   
+                               }
+                       }
+               }
+               void _list_LayoutChanged (object sender, LayoutingEventArgs e)
+               {
+                       if (_gsList == null)
+                               return;
+
+                       GenericStack page1 = _list.FindByName ("page1") as GenericStack;
+                       if (page1 == null)
+                               return;
+
+                       if (_gsList.Orientation == Orientation.Horizontal) {
+                               if (e.LayoutType != LayoutingType.Width)
+                                       return;
+                               int tmpWidth = (int)Math.Ceiling ((double)page1.Slot.Width / (double)itemPerPage * (double)data.Count);
+                               if (_gsList.Slot.Width == tmpWidth)
+                                       return;
+                               _gsList.Slot.Width = tmpWidth;
+                               _gsList.OnLayoutChanges (LayoutingType.Width);
+                               _gsList.LastSlots.Width = _gsList.Slot.Width;
+                       } else {
+                               if (e.LayoutType != LayoutingType.Height)
+                                       return;
+                               int tmpHeight = (int)Math.Ceiling ((double)page1.Slot.Height / (double)itemPerPage * (double)data.Count);
+                               if (_gsList.Slot.Height == tmpHeight)
+                                       return;
+                               _gsList.Slot.Height = tmpHeight;
+                               _gsList.OnLayoutChanges (LayoutingType.Height);
+                               _gsList.LastSlots.Height = _gsList.Slot.Height;
                        }
                }
 
index 5188baf88388367df2b282f80099b873ce0c702e..3ac8d66c9edb995f82fdd250ff29d6c137ba35a2 100644 (file)
@@ -20,7 +20,7 @@ namespace Crow
                double _scrollY = 0.0;
                int scrollSpeed;
 
-
+               public event EventHandler<ScrollingEventArgs> Scrolled;
 
                #region public properties
                [XmlAttributeAttribute][DefaultValue(false)]
diff --git a/src/ScrollingEventArgs.cs b/src/ScrollingEventArgs.cs
new file mode 100644 (file)
index 0000000..7e04eeb
--- /dev/null
@@ -0,0 +1,15 @@
+using System;
+
+namespace Crow
+{
+       public class ScrollingEventArgs: EventArgs
+       {
+               public Orientation  Direction;
+
+               public ScrollingEventArgs (Orientation  _direction) : base()
+               {
+                       Direction = _direction;
+               }
+       }
+}
+