From dd9b17c495b7ddceeb2ab5cd7d4d1930f9c9b843 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 18 Jul 2018 22:26:00 -0400 Subject: [PATCH] [ListView] allow the source to be null, so that it can be easily set later, fixes #111 --- Example/demo.cs | 1 + Terminal.Gui/Views/ListView.cs | 38 +++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 12 deletions(-) mode change 100644 => 100755 Example/demo.cs diff --git a/Example/demo.cs b/Example/demo.cs old mode 100644 new mode 100755 index d1d562a6e..3d5405c43 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -138,6 +138,7 @@ static class Demo { new Button (10, 19, "Cancel"), progress, new Label (3, 22, "Press F9 (on Unix, ESC+9 is an alias) to activate the menubar") + ); } diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index 57e016b92..078a0cf1f 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -162,8 +162,6 @@ namespace Terminal.Gui { public IListDataSource Source { get => source; set { - if (value == null) - throw new ArgumentNullException ("value"); source = value; top = 0; selected = 0; @@ -178,10 +176,12 @@ namespace Terminal.Gui { public void SetSource (IList source) { if (source == null) - throw new ArgumentNullException (nameof (source)); - Source = MakeWrapper (source); - ((ListWrapper) Source).Container = this; - ((ListWrapper) Source).Driver = Driver; + Source = null; + else { + Source = MakeWrapper (source); + ((ListWrapper)Source).Container = this; + ((ListWrapper)Source).Driver = Driver; + } } bool allowsMarking; @@ -204,6 +204,9 @@ namespace Terminal.Gui { public int TopItem { get => top; set { + if (source == null) + return; + if (top < 0 || top >= source.Count) throw new ArgumentException ("value"); top = value; @@ -218,6 +221,8 @@ namespace Terminal.Gui { public int SelectedItem { get => selected; set { + if (source == null) + return; if (selected < 0 || selected >= source.Count) throw new ArgumentException ("value"); selected = value; @@ -250,12 +255,17 @@ namespace Terminal.Gui { /// IListDataSource object that provides a mechanism to render the data. The number of elements on the collection should not change, if you must change, set the "Source" property to reset the internal settings of the ListView. public ListView (IListDataSource source) : base () { - if (source == null) - throw new ArgumentNullException (nameof (source)); Source = source; CanFocus = true; } + /// + /// Initializes a new instance of the class. You must set the Source property for this to show something. + /// + public ListView () : base () + { + } + /// /// Initializes a new ListView that will display the contents of the object implementing the IList interface with an absolute position. /// @@ -274,8 +284,6 @@ namespace Terminal.Gui { /// IListDataSource object that provides a mechanism to render the data. The number of elements on the collection should not change, if you must change, set the "Source" property to reset the internal settings of the ListView. public ListView (Rect rect, IListDataSource source) : base (rect) { - if (source == null) - throw new ArgumentNullException (nameof (source)); Source = source; CanFocus = true; } @@ -302,8 +310,8 @@ namespace Terminal.Gui { current = newcolor; } - if (item >= source.Count) { - Move(0, row); + if (source == null || item >= source.Count) { + Move(0, row); for (int c = 0; c < f.Width; c++) Driver.AddRune(' '); } else { @@ -324,6 +332,9 @@ namespace Terminal.Gui { /// Keyboard event. public override bool ProcessKey (KeyEvent kb) { + if (source == null) + return base.ProcessKey (kb); + switch (kb.Key) { case Key.CursorUp: case Key.ControlP: @@ -398,6 +409,9 @@ namespace Terminal.Gui { if (!HasFocus) SuperView.SetFocus (this); + if (source == null) + return false; + if (me.Y + top >= source.Count) return true;