From f92c105ada5520e84f4bbecc23ce9dc52895934e Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Tue, 9 Feb 2016 18:35:33 +0100 Subject: [PATCH] Simpler Combo, derived from ListBox with a popper as template root --- Crow.csproj | 1 - Templates/ComboBox.goml | 32 ++- Templates/ComboBoxOverlay.goml | 7 - Tests/Interfaces/testCombobox.goml | 4 +- src/GraphicObjects/ComboBox.cs | 333 +---------------------------- src/GraphicObjects/ListBox.cs | 7 +- src/GraphicObjects/Popper.cs | 3 +- 7 files changed, 33 insertions(+), 354 deletions(-) delete mode 100755 Templates/ComboBoxOverlay.goml diff --git a/Crow.csproj b/Crow.csproj index 5d2ad868..31d5cd1b 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -215,7 +215,6 @@ - diff --git a/Templates/ComboBox.goml b/Templates/ComboBox.goml index a4272d61..59381172 100755 --- a/Templates/ComboBox.goml +++ b/Templates/ComboBox.goml @@ -1,10 +1,24 @@  - - - - \ No newline at end of file + + + + + + + + diff --git a/Templates/ComboBoxOverlay.goml b/Templates/ComboBoxOverlay.goml deleted file mode 100755 index 53782ac3..00000000 --- a/Templates/ComboBoxOverlay.goml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/Tests/Interfaces/testCombobox.goml b/Tests/Interfaces/testCombobox.goml index 2f5e69e5..7fe9118c 100755 --- a/Tests/Interfaces/testCombobox.goml +++ b/Tests/Interfaces/testCombobox.goml @@ -1,2 +1,4 @@  - \ No newline at end of file + + + \ No newline at end of file diff --git a/src/GraphicObjects/ComboBox.cs b/src/GraphicObjects/ComboBox.cs index c071cb5e..f337a2f4 100644 --- a/src/GraphicObjects/ComboBox.cs +++ b/src/GraphicObjects/ComboBox.cs @@ -21,341 +21,10 @@ using System.Threading; namespace Crow { [DefaultTemplate("#Crow.Templates.ComboBox.goml")] - [DefaultOverlayTemplate("#Crow.Templates.ComboBoxOverlay.goml")] - public class ComboBox : TemplatedContainer + public class ComboBox : ListBox { #region CTOR public ComboBox() : base(){ } #endregion - - bool _isPopped; - string caption; - GraphicObject _overlay; - Group _list; - IList data; - int _selectedIndex; - object _selectedItem; - string _itemTemplate; - string _overlayTemplate; - - public event EventHandler Pop; - public event EventHandler Unpop; - public event EventHandler SelectedItemChanged; - - #region implemented abstract members of TemplatedControl - protected override void loadTemplate (GraphicObject template) - { - base.loadTemplate (template); - loadOverlayTemplate (null); - } - public override GraphicObject Content { - get { - throw new NotImplementedException (); - } - set { - throw new NotImplementedException (); - } - } - #endregion - - protected virtual void loadOverlayTemplate(GraphicObject overlayTemplate) - { - if (overlayTemplate == null) { - DefaultOverlayTemplate dt = (DefaultOverlayTemplate)this.GetType ().GetCustomAttributes (typeof(DefaultOverlayTemplate), true).FirstOrDefault (); - Overlay = Interface.Load (dt.Path); - Overlay.ResolveBindings (); - } else - Overlay = overlayTemplate; - _list = Overlay.FindByName ("List") as Group; - } - - [XmlAttributeAttribute][DefaultValue("#Crow.Templates.ItemTemplate.goml")] - public string ItemTemplate { - get { return _itemTemplate; } - set { - //TODO:reload list with new template? - _itemTemplate = value; - } - } - [XmlAttributeAttribute][DefaultValue("#Crow.Templates.ComboBoxOverlay.goml")] - public string OverlayTemplate { - get { return _overlayTemplate; } - set { - //TODO:reload list with new template? - _overlayTemplate = value; - - Overlay = Interface.Load (_overlayTemplate); - Overlay.ResolveBindings (); - _list = Overlay.FindByName ("List") as Group; - } - } - [XmlAttributeAttribute][DefaultValue(-1)] - public int SelectedIndex{ - get { return _selectedIndex; } - set { - //store value event if data is null, because in xml parsing selindex is always - //before data, so it's impossible without that trick to set a default index in goml - _selectedIndex = value; - - if (data == null) - return; - - if (_selectedIndex > data.Count - 1 || _selectedIndex < 0) - throw new Exception ("Combobox SelectedIndex out of range"); - - - _selectedItem = data [_selectedIndex]; - NotifyValueChanged ("SelectedIndex", SelectedIndex); - SelectedItemChanged.Raise (this, new SelectionChangeEventArgs(_selectedItem)); - - if (SelectedItem == null) - Caption = ""; - else - Caption = _selectedItem.ToString (); - } - } - public object SelectedItem{ - set { - if (_selectedItem == value) - return; - - _selectedItem = value; - _selectedIndex = data.IndexOf (_selectedItem); - NotifyValueChanged ("SelectedIndex", _selectedIndex); - SelectedItemChanged.Raise (this, new SelectionChangeEventArgs(_selectedItem)); - - if (SelectedItem == null) - Caption = ""; - else - Caption = _selectedItem.ToString (); - } - - get { return _selectedItem; } - } - [XmlAttributeAttribute][DefaultValue(null)] - public IList Data { - get { - return data; - } - set { - data = value; - if (_list == null) - return; - - foreach (GraphicObject c in _list.Children) { - c.ClearBinding (); - } - _list.Children.Clear (); - _list.registerForGraphicUpdate (); - if (data == null) - return; - if (SelectedIndex < 0) - return; - - //force raise of changes - SelectedIndex = SelectedIndex; - - pendingChildrenAddition = new Queue (); - threadedLoadingFinished = false; - - Thread t = new Thread (loadingThread); - t.Start (); - t.Join (); - - } - } - - internal void CheckPendingChildrenAddition() - { - if (pendingChildrenAddition == null) - return; - lock (pendingChildrenAddition) { - if (!threadedLoadingFinished && pendingChildrenAddition.Count < 50) - return; - while (pendingChildrenAddition.Count > 0) { - _list.AddChild (pendingChildrenAddition.Dequeue ()); - } - } - } - - volatile Queue pendingChildrenAddition; - volatile bool threadedLoadingFinished = false; - - void loadingThread() - { - #if DEBUG_LOAD_TIME - Stopwatch loadingTime = new Stopwatch (); - loadingTime.Start (); - #endif - - MemoryStream ms = new MemoryStream (); - lock (ItemTemplate) { - using (Stream stream = Interface.GetStreamFromPath (ItemTemplate)) - stream.CopyTo (ms); - } - - Type t = Interface.GetTopContainerOfGOMLStream (ms); - - foreach (var item in data) { - ms.Seek(0,SeekOrigin.Begin); - GraphicObject g = Interface.Load (ms, t); - g.DataSource = item; - g.MouseClick += itemClick; - - lock (pendingChildrenAddition) { - pendingChildrenAddition.Enqueue (g); - } - } - - ms.Dispose (); - - threadedLoadingFinished = true; - - #if DEBUG_LOAD_TIME - loadingTime.Stop (); - Debug.WriteLine("Listbox {2} Loading: {0} ticks \t, {1} ms", - loadingTime.ElapsedTicks, - loadingTime.ElapsedMilliseconds, this.ToString()); - #endif - } - public GraphicObject Overlay { - get { return _overlay; } - set { - if (_overlay != null) { - _overlay.LayoutChanged -= _overlay_LayoutChanged; - _overlay.MouseLeave -= _overlay_MouseLeave; - _overlay.LogicalParent = null; - } - - _overlay = value; - - if (_overlay == null) - return; - - _overlay.LogicalParent = this; - _overlay.Focusable = true; - _overlay.LayoutChanged += _overlay_LayoutChanged; - _overlay.MouseLeave += _overlay_MouseLeave; - } - } - [XmlAttributeAttribute()][DefaultValue("Combobox")] - public string Caption { - get { return caption; } - set { - if (caption == value) - return; - caption = value; - NotifyValueChanged ("Caption", caption); - } - } - [XmlAttributeAttribute()][DefaultValue(false)] - public bool IsPopped - { - get { return _isPopped; } - set - { - _isPopped = value; - - if (_isPopped) { - onPop (this, null); - NotifyValueChanged ("SvgSub", "expanded"); - return; - } - - onUnpop (this, null); - NotifyValueChanged ("SvgSub", "collapsed"); - } - } - - void itemClick(object sender, OpenTK.Input.MouseButtonEventArgs e){ - object datasource = (sender as GraphicObject).DataSource; - SelectedItem = datasource; - IsPopped = false; - //Debug.WriteLine ((sender as GraphicObject).DataSource); - } - void _overlay_MouseLeave (object sender, MouseMoveEventArgs e) - { - IsPopped = false; - } - void _overlay_LayoutChanged (object sender, LayoutingEventArgs e) - { - ILayoutable tc = Overlay.Parent as ILayoutable; - if (tc == null) - return; - Rectangle r = this.ScreenCoordinates (this.Slot); - if (e.LayoutType == LayoutingType.Width) { - if (Overlay.Slot.Width < tc.ClientRectangle.Width) { - if (r.Left + Overlay.Slot.Width > tc.ClientRectangle.Right) - Overlay.Left = tc.ClientRectangle.Right - Overlay.Slot.Width; - else - Overlay.Left = r.Left; - }else - Overlay.Left = 0; - }else if (e.LayoutType == LayoutingType.Height) { - if (Overlay.Slot.Height < tc.ClientRectangle.Height) { - if (r.Bottom + Overlay.Slot.Height > tc.ClientRectangle.Bottom) - Overlay.Top = r.Top - Overlay.Slot.Height; - else - Overlay.Top = r.Bottom; - }else - Overlay.Top = 0; - } - } - - [XmlAttributeAttribute()][DefaultValue(true)]//overiden to get default to true - public override bool Focusable - { - get { return base.Focusable; } - set { base.Focusable = value; } - } - - - public virtual void onPop(object sender, EventArgs e) - { - IGOLibHost tc = HostContainer; - if (tc == null) - return; - if (Overlay != null) { - Overlay.Visible = true; - if (Overlay.Parent == null) - tc.AddWidget (Overlay); - (tc as OpenTKGameWindow).PutOnTop (Overlay); - } - Pop.Raise (this, e); - } - public virtual void onUnpop(object sender, EventArgs e) - { - IGOLibHost tc = HostContainer; - if (tc == null) - return; - Overlay.Visible = false; - Unpop.Raise (this, e); - } - - public override void onMouseClick (object sender, MouseButtonEventArgs e) - { - IsPopped = !IsPopped; - base.onMouseClick (sender, e); - } - - public override void ClearBinding () - { - //ensure popped window is cleared - if (Overlay != null) { - if (Overlay.Parent != null) { - IGOLibHost tc = Overlay.Parent as IGOLibHost; - if (tc != null) - tc.DeleteWidget (Overlay); - } - } - base.ClearBinding (); - - } - public override void ResolveBindings () - { - base.ResolveBindings (); - if (Overlay != null) - Overlay.ResolveBindings (); - } } } diff --git a/src/GraphicObjects/ListBox.cs b/src/GraphicObjects/ListBox.cs index c90593cb..8cfffe49 100644 --- a/src/GraphicObjects/ListBox.cs +++ b/src/GraphicObjects/ListBox.cs @@ -90,7 +90,7 @@ namespace Crow } } public object SelectedItem{ - get { return data == null ? null : data[_selectedIndex]; } + get { return data == null ? "none" : data[_selectedIndex]; } } [XmlAttributeAttribute]//[DefaultValue(null)] public IList Data { @@ -157,7 +157,7 @@ namespace Crow loadingTime.ElapsedMilliseconds, this.ToString()); #endif } - void _scroller_ValueChanged (object sender, ValueChangeEventArgs e) + protected void _scroller_ValueChanged (object sender, ValueChangeEventArgs e) { if (_gsList == null) return; @@ -177,7 +177,7 @@ namespace Crow } } } - void _list_LayoutChanged (object sender, LayoutingEventArgs e) + protected void _list_LayoutChanged (object sender, LayoutingEventArgs e) { if (_gsList == null) return; @@ -211,6 +211,7 @@ namespace Crow SelectedItemChanged.Raise (sender, new SelectionChangeEventArgs((sender as GraphicObject).DataSource)); NotifyValueChanged ("SelectedItem", (sender as GraphicObject).DataSource); //Debug.WriteLine ((sender as GraphicObject).DataSource); + } #region IXmlSerializable diff --git a/src/GraphicObjects/Popper.cs b/src/GraphicObjects/Popper.cs index e0b821a8..171e54e4 100644 --- a/src/GraphicObjects/Popper.cs +++ b/src/GraphicObjects/Popper.cs @@ -26,6 +26,7 @@ namespace Crow { } #endregion + bool _isPopped; string caption; string image; @@ -52,7 +53,7 @@ namespace Crow } } - void _content_LayoutChanged (object sender, LayoutingEventArgs e) + protected void _content_LayoutChanged (object sender, LayoutingEventArgs e) { ILayoutable tc = Content.Parent as ILayoutable; if (tc == null) -- 2.47.3