~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/ItemCollection.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ItemCollection.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Collections.ObjectModel;
 
30
 
 
31
namespace Xwt
 
32
{
 
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
 
36
        
 
37
        public sealed class ItemCollection: Collection<Object>
 
38
        {
 
39
                ListStore store;
 
40
                DataField<string> labelField = new DataField<string> ();
 
41
                DataField<object> dataField = new DataField<object> ();
 
42
                
 
43
                class ItemWithLabel {
 
44
                        public object Item;
 
45
                        public string Label;
 
46
                }
 
47
                
 
48
                internal ItemCollection ()
 
49
                {
 
50
                        store = new ListStore (labelField, dataField);
 
51
                }
 
52
                
 
53
                public void Add (object item, string label)
 
54
                {
 
55
                        Add (new ItemWithLabel () { Item = item, Label = label });
 
56
                }
 
57
 
 
58
                public void Insert (int index, object item, string label)
 
59
                {
 
60
                        Insert (index, new ItemWithLabel () { Item = item, Label = label });
 
61
                }
 
62
                
 
63
                protected override void InsertItem (int index, object item)
 
64
                {
 
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);
 
71
                        } else {
 
72
                                base.InsertItem (index, item);
 
73
                                store.InsertRowBefore (index);
 
74
                                store.SetValue (index, labelField, item.ToString ());
 
75
                                store.SetValue (index, dataField, item);
 
76
                        }
 
77
                }
 
78
                
 
79
                protected override void RemoveItem (int index)
 
80
                {
 
81
                        base.RemoveItem (index);
 
82
                        store.RemoveRow (index);
 
83
                }
 
84
                
 
85
                protected override void SetItem (int index, object item)
 
86
                {
 
87
                        base.SetItem (index, item);
 
88
                        store.SetValue (index, dataField, item);
 
89
                }
 
90
                
 
91
                protected override void ClearItems ()
 
92
                {
 
93
                        base.ClearItems ();
 
94
                        while (store.RowCount > 0)
 
95
                                store.RemoveRow (0);
 
96
                }
 
97
 
 
98
                internal IListDataSource DataSource {
 
99
                        get { return store; }
 
100
                }
 
101
        }
 
102
        
 
103
/*      public sealed class ItemCollection: Collection<Object>, IListDataSource
 
104
        {
 
105
                List<string> labels;
 
106
                
 
107
                internal ItemCollection ()
 
108
                {
 
109
                }
 
110
                
 
111
                public event EventHandler<ListRowEventArgs> RowInserted;
 
112
                public event EventHandler<ListRowEventArgs> RowDeleted;
 
113
                public event EventHandler<ListRowEventArgs> RowChanged;
 
114
                public event EventHandler<ListRowOrderEventArgs> RowsReordered;
 
115
                
 
116
                class ItemWithLabel {
 
117
                        public object Item;
 
118
                        public string Label;
 
119
                }
 
120
                
 
121
                public void Add (object item, string label)
 
122
                {
 
123
                        Add (new ItemWithLabel () { Item = item, Label = label });
 
124
                }
 
125
 
 
126
                public void Insert (int index, object item, string label)
 
127
                {
 
128
                        Insert (index, new ItemWithLabel () { Item = item, Label = label });
 
129
                }
 
130
                
 
131
                void InitLabelList (int index)
 
132
                {
 
133
                        if (labels == null)
 
134
                                labels = new List<string> ();
 
135
                        for (int n=labels.Count - 1; n < index; n++)
 
136
                                labels.Add (null);
 
137
                }
 
138
 
 
139
                protected override void InsertItem (int index, object item)
 
140
                {
 
141
                        if (item is ItemWithLabel) {
 
142
                                var itl = (ItemWithLabel) item;
 
143
                                InitLabelList (index - 1);
 
144
                                labels.Insert (index, itl.Label);
 
145
                                item = itl.Item;
 
146
                        }
 
147
                        else if (labels != null && index < labels.Count)
 
148
                                labels.Insert (index, null);
 
149
                        
 
150
                        base.InsertItem (index, item);
 
151
                        if (RowInserted != null)
 
152
                                RowInserted (this, new ListRowEventArgs (index));
 
153
                }
 
154
                
 
155
                protected override void RemoveItem (int index)
 
156
                {
 
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));
 
162
                }
 
163
                
 
164
                protected override void SetItem (int index, object item)
 
165
                {
 
166
                        base.SetItem (index, item);
 
167
                        if (RowChanged != null)
 
168
                                RowChanged (this, new ListRowEventArgs (index));
 
169
                }
 
170
                
 
171
                protected override void ClearItems ()
 
172
                {
 
173
                        if (labels != null)
 
174
                                labels.Clear ();
 
175
                        int count = Count;
 
176
                        base.ClearItems ();
 
177
                        for (int n=count - 1; n >= 0; n--) {
 
178
                                if (RowDeleted != null)
 
179
                                        RowDeleted (this, new ListRowEventArgs (n));
 
180
                        }
 
181
                }
 
182
 
 
183
                internal IListDataSource DataSource {
 
184
                        get { return this; }
 
185
                }
 
186
                
 
187
                #region IListViewSource implementation
 
188
                object IListDataSource.GetValue (int row, int column)
 
189
                {
 
190
                        if (column != 0)
 
191
                                throw new InvalidOperationException ("Not data for column " + column);
 
192
                        if (labels != null && row < labels.Count)
 
193
                                return labels[row];
 
194
                        else
 
195
                                return this [row];
 
196
                }
 
197
 
 
198
                void IListDataSource.SetValue (int row, int column, object value)
 
199
                {
 
200
                        if (column != 0)
 
201
                                throw new InvalidOperationException ("Not data for column " + column);
 
202
                        if (labels != null && row < labels.Count)
 
203
                                labels [row] = (string)value;
 
204
                        else
 
205
                                this [row] = value;
 
206
                }
 
207
 
 
208
                int IListDataSource.RowCount {
 
209
                        get {
 
210
                                return Count;
 
211
                        }
 
212
                }
 
213
 
 
214
                Type[] IListDataSource.ColumnTypes {
 
215
                        get {
 
216
                                return new Type[] { typeof(string) };
 
217
                        }
 
218
                }
 
219
                #endregion
 
220
        }*/
 
221
}
 
222