5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
29
using System.Collections.ObjectModel;
33
// This implementation is a workaround to a Windows GTK# bug,
34
// which crashes when using custom model implementations.
35
// I'll switch to the other version when the bug is fixed
37
public sealed class ItemCollection: Collection<Object>
40
DataField<string> labelField = new DataField<string> ();
41
DataField<object> dataField = new DataField<object> ();
48
internal ItemCollection ()
50
store = new ListStore (labelField, dataField);
53
public void Add (object item, string label)
55
Add (new ItemWithLabel () { Item = item, Label = label });
58
public void Insert (int index, object item, string label)
60
Insert (index, new ItemWithLabel () { Item = item, Label = label });
63
protected override void InsertItem (int index, object item)
65
if (item is ItemWithLabel) {
66
var itl = (ItemWithLabel) item;
67
base.InsertItem (index, itl.Item);
68
store.InsertRowBefore (index);
69
store.SetValue (index, labelField, itl.Label);
70
store.SetValue (index, dataField, itl.Item);
72
base.InsertItem (index, item);
73
store.InsertRowBefore (index);
74
store.SetValue (index, labelField, item.ToString ());
75
store.SetValue (index, dataField, item);
79
protected override void RemoveItem (int index)
81
base.RemoveItem (index);
82
store.RemoveRow (index);
85
protected override void SetItem (int index, object item)
87
base.SetItem (index, item);
88
store.SetValue (index, dataField, item);
91
protected override void ClearItems ()
94
while (store.RowCount > 0)
98
internal IListDataSource DataSource {
103
/* public sealed class ItemCollection: Collection<Object>, IListDataSource
107
internal ItemCollection ()
111
public event EventHandler<ListRowEventArgs> RowInserted;
112
public event EventHandler<ListRowEventArgs> RowDeleted;
113
public event EventHandler<ListRowEventArgs> RowChanged;
114
public event EventHandler<ListRowOrderEventArgs> RowsReordered;
116
class ItemWithLabel {
121
public void Add (object item, string label)
123
Add (new ItemWithLabel () { Item = item, Label = label });
126
public void Insert (int index, object item, string label)
128
Insert (index, new ItemWithLabel () { Item = item, Label = label });
131
void InitLabelList (int index)
134
labels = new List<string> ();
135
for (int n=labels.Count - 1; n < index; n++)
139
protected override void InsertItem (int index, object item)
141
if (item is ItemWithLabel) {
142
var itl = (ItemWithLabel) item;
143
InitLabelList (index - 1);
144
labels.Insert (index, itl.Label);
147
else if (labels != null && index < labels.Count)
148
labels.Insert (index, null);
150
base.InsertItem (index, item);
151
if (RowInserted != null)
152
RowInserted (this, new ListRowEventArgs (index));
155
protected override void RemoveItem (int index)
157
if (labels != null && index < labels.Count)
158
labels.RemoveAt (index);
159
base.RemoveItem (index);
160
if (RowDeleted != null)
161
RowDeleted (this, new ListRowEventArgs (index));
164
protected override void SetItem (int index, object item)
166
base.SetItem (index, item);
167
if (RowChanged != null)
168
RowChanged (this, new ListRowEventArgs (index));
171
protected override void ClearItems ()
177
for (int n=count - 1; n >= 0; n--) {
178
if (RowDeleted != null)
179
RowDeleted (this, new ListRowEventArgs (n));
183
internal IListDataSource DataSource {
187
#region IListViewSource implementation
188
object IListDataSource.GetValue (int row, int column)
191
throw new InvalidOperationException ("Not data for column " + column);
192
if (labels != null && row < labels.Count)
198
void IListDataSource.SetValue (int row, int column, object value)
201
throw new InvalidOperationException ("Not data for column " + column);
202
if (labels != null && row < labels.Count)
203
labels [row] = (string)value;
208
int IListDataSource.RowCount {
214
Type[] IListDataSource.ColumnTypes {
216
return new Type[] { typeof(string) };