[f-spot/cleanup-backend: 5/23] Base Selection implementation of IconView on Hyena.Collections.Selection



commit e71b430f63b4083b5c969f8d61cdfbbaa64c8ee4
Author: Mike Gemünde <mike gemuende de>
Date:   Thu Jul 15 14:39:08 2010 +0200

    Base Selection implementation of IconView on Hyena.Collections.Selection
    
    This might break some things. However, the further transition should remove
    it completely.

 src/Widgets/IconView.cs |  213 ++++++++++-------------------------------------
 1 files changed, 46 insertions(+), 167 deletions(-)
---
diff --git a/src/Widgets/IconView.cs b/src/Widgets/IconView.cs
index fa92e69..d1c4cfd 100644
--- a/src/Widgets/IconView.cs
+++ b/src/Widgets/IconView.cs
@@ -14,10 +14,13 @@ using Gdk;
 using System;
 using System.Reflection;
 using System.Collections;
+using System.Collections.Generic;
 using System.IO;
 using FSpot.Utils;
 using FSpot.Platform;
 
+using Hyena.Collections;
+
 namespace FSpot.Widgets
 {
     public class StartDragArgs {
@@ -249,16 +252,15 @@ namespace FSpot.Widgets
 			collection.Changed += HandleChanged;
 			collection.ItemsChanged += HandleItemsChanged;
 
-			selection.DetailedChanged += HandleSelectionChanged;
+			selection.Changed += HandleSelectionChanged;
+            selection.FocusChanged +=  delegate {
+                QueueDraw ();
+            };;
 		}
 
-		private void HandleSelectionChanged (FSpot.IBrowsableCollection collection, int [] ids)
+		private void HandleSelectionChanged (FSpot.IBrowsableCollection collection)
 		{
-			if (ids == null)
-				QueueDraw ();
-			else
-				foreach (int id in ids)
-					InvalidateCell (id);
+			QueueDraw ();
 		}
 
 		private void HandleChanged (FSpot.IBrowsableCollection sender)
@@ -299,9 +301,8 @@ namespace FSpot.Widgets
 		// FIXME right now a selection change triggers a complete view redraw
 		// This should be optimized away by directly notifyiing the view of changed
 		// indexes rather than having the view connect to the collection.Changed event.
-		public class SelectionCollection : IBrowsableCollection {
+		public class SelectionCollection : Hyena.Collections.Selection, IBrowsableCollection {
 			IBrowsableCollection parent;
-			Hashtable selected_cells;
 			BitArray bit_array;
 			int [] selection;
 			IBrowsableItem [] items;
@@ -309,45 +310,28 @@ namespace FSpot.Widgets
 
 			public SelectionCollection (IBrowsableCollection collection)
 			{
-				this.selected_cells = new Hashtable ();
 				this.parent = collection;
 				this.bit_array = new BitArray (this.parent.Count);
 				this.parent.Changed += HandleParentChanged;
 				this.parent.ItemsChanged += HandleParentItemsChanged;
-			}
+
+                base.Changed += delegate {
+                    if (this.Changed != null)
+                        this.Changed (this);
+                };
+            }
 
 			private void HandleParentChanged (IBrowsableCollection collection)
 			{
 				IBrowsableItem [] local = old;
-				selected_cells.Clear ();
-				bit_array = new BitArray (parent.Count);
-				ClearCached ();
-
-				if (old != null) {
-					int i = 0;
-
-					for (i = 0; i < local.Length; i++) {
-						int parent_index = parent.IndexOf (local [i]);
-						if (parent_index >= 0)
-							this.Add (parent_index, false);
-					}
-				}
-
-				// Call the directly so that we don't reset old immediately this way the old selection
-				// set isn't actually lost until we change it.
-				if (this.Changed != null)
-					Changed (this);
-
-				if (this.DetailedChanged != null)
-					DetailedChanged (this, null);
-
+                Clear ();
 			}
 
 			public void MarkChanged (int item, IBrowsableItemChanges changes)
 			{
 				// Forward the change event up to our parent
 				// we'll fire the event when the parent calls us back
-				parent.MarkChanged ((int) selected_cells [item], changes);
+				//parent.MarkChanged ((int) selected_cells [item], changes);
 			}
 
 			private void HandleParentItemsChanged (IBrowsableCollection collection, BrowsableEventArgs args)
@@ -379,17 +363,8 @@ namespace FSpot.Widgets
 
 			public int [] Ids {
 				get {
-					if (selection != null)
-						return selection;
-
-					selection = new int [selected_cells.Count];
-
-					int i = 0;
-					foreach (int cell in selected_cells.Values)
-						selection [i ++] = cell;
-
-					Array.Sort (selection);
-					return selection;
+                    List<int> indices = new List<int> (this);
+                    return indices.ToArray ();
 				}
 			}
 
@@ -402,105 +377,56 @@ namespace FSpot.Widgets
 
 			public IBrowsableItem [] Items {
 				get {
-					if (items != null)
-						return items;
+                    List<IBrowsableItem> items = new List<IBrowsableItem> ();
 
-					int [] ids = this.Ids;
-					items = new IBrowsableItem [ids.Length];
-					for (int i = 0; i < items.Length; i++) {
-						items [i] = parent [ids[i]];
-					}
-					return items;
-				}
-			}
-
-			public void Clear ()
-			{
-				Clear (true);
-			}
+                    foreach (int index in this) {
+                        items.Add (parent [index]);
+                    }
 
-			public void Clear (bool update)
-			{
-				int [] ids = Ids;
-				selected_cells.Clear ();
-				bit_array.SetAll (false);
-				SignalChange (ids);
+                    return items.ToArray ();
+				}
 			}
 
 			public void Add (IBrowsableItem item)
 			{
-				if (this.Contains (item))
-					return;
-
-				int index = parent.IndexOf (item);
-				this.Add (index);
-			}
-
-			public int Count {
-				get {
-					return selected_cells.Count;
-				}
+				Add (parent.IndexOf (item), true);
 			}
 
 			public bool Contains (IBrowsableItem item)
 			{
-				return selected_cells.ContainsKey (item);
-			}
-
-			public bool Contains (int num)
-			{
-				if (num < 0 || num > parent.Count)
-					return false;
-
-				return this.Contains (parent [num]);
+                return Contains (parent.IndexOf (item));
 			}
 
 			public void Add (int num)
 			{
-				this.Add (num, true);
+                Add (num, true);
 			}
 
 			public void Add (int num, bool notify)
 			{
-				if (num == -1)
-					return;
+                bit_array.Set (num, true);
 
-				if (this.Contains (num))
-					return;
-
-				IBrowsableItem item = parent [num];
-				selected_cells [item] = num;
-				bit_array.Set (num, true);
+                if (notify)
+                    Select (num);
+                else
+                    QuietSelect (num);
 
-				if (notify)
-					SignalChange (new int [] {num});
+                FocusedIndex = num;
 			}
 
 			public void Add (int start, int end)
 			{
-				if (start == -1 || end == -1)
-					return;
-
-				int current = Math.Min (start, end);
-				int final = Math.Max (start, end);
-				int count = final - current + 1;
-				int [] ids = new int [count];
-
-				for (int i = 0; i < count; i++) {
-					this.Add (current, false);
-					ids [i] = current;
-					current++;
-				}
-
-				SignalChange (ids);
+                SelectRange (start, end);
 			}
 
 			public void Remove (int cell, bool notify)
 			{
-				IBrowsableItem item = parent [cell];
-				if (item != null)
-					this.Remove (item, notify);
+                if (notify)
+                    Unselect (cell);
+                else
+                    QuietUnselect (cell);
 
+                FocusedIndex = cell;
 			}
 
 			public void Remove (IBrowsableItem item)
@@ -515,35 +441,13 @@ namespace FSpot.Widgets
 
 			private void Remove (IBrowsableItem item, bool notify)
 			{
-				if (item == null)
-					return;
-
-				int parent_index = (int) selected_cells [item];
-				selected_cells.Remove (item);
-				bit_array.Set (parent_index, false);
-
-				if (notify)
-					SignalChange (new int [] {parent_index});
+                Remove (parent.IndexOf (item), true);
 			}
 
 			// Remove a range, except the start entry
 			public void Remove (int start, int end)
 			{
-				if (start == -1 || end == -1)
-					return;
-
-				int current = Math.Min (start + 1, end);
-				int final = Math.Max (start - 1, end);
-				int count = final - current + 1;
-				int [] ids = new int [count];
-
-				for (int i = 0; i < count; i++) {
-					this.Remove (current, false);
-					ids [i] = current;
-					current++;
-				}
-
-				SignalChange (ids);
+                UnselectRange (start, end);
 			}
 
 			public int IndexOf (int parent_index)
@@ -556,8 +460,7 @@ namespace FSpot.Widgets
 				if (!this.Contains (item))
 					return -1;
 
-				int parent_index = (int) selected_cells [item];
-				return System.Array.IndexOf (Ids, parent_index);
+				return System.Array.IndexOf (Ids, parent.IndexOf (item));
 			}
 
 			private void ToggleCell (int cell_num, bool notify)
@@ -580,8 +483,6 @@ namespace FSpot.Widgets
 					ToggleCell (i, false);
 					changed_cell[i] = i;
 				}
-
-				SignalChange (changed_cell);
 			}
 
 			public void SelectRect (int start_row, int end_row, int start_line, int end_line, int cells_per_row)
@@ -607,28 +508,6 @@ namespace FSpot.Widgets
 
 			public event IBrowsableCollectionChangedHandler Changed;
 			public event IBrowsableCollectionItemsChangedHandler ItemsChanged;
-
-			public delegate void DetailedCollectionChanged (IBrowsableCollection collection, int [] ids);
-			public event DetailedCollectionChanged DetailedChanged;
-
-			private void ClearCached ()
-			{
-				selection = null;
-				items = null;
-			}
-
-			public void SignalChange (int [] ids)
-			{
-				ClearCached ();
-				old = this.Items;
-
-
-				if (Changed != null)
-					Changed (this);
-
-				if (DetailedChanged!= null)
-					DetailedChanged (this, ids);
-			}
 		}
 
 		// Updating.
@@ -1573,8 +1452,8 @@ namespace FSpot.Widgets
 			for (int i = 0; i < selection_changed.Length; i++)
 				if (selection_changed.Get(i))
 					changed.Add (i);
-			if (selection_changed.Length != 0)
-				selection.SignalChange (changed.ToArray());
+			//if (selection_changed.Length != 0)
+			//	selection.SignalChange (changed.ToArray());
 
 			// redraw selection box
 			if (BinWindow != null) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]