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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/ListBox.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
// ListBox.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 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
using System;
 
27
using Xwt.Backends;
 
28
using System.ComponentModel;
 
29
using Xwt.Engine;
 
30
 
 
31
namespace Xwt
 
32
{
 
33
        /// <summary>
 
34
        /// A list of selectable items
 
35
        /// </summary>
 
36
        public class ListBox: Widget
 
37
        {
 
38
                CellViewCollection views;
 
39
                IListDataSource source;
 
40
                ItemCollection itemCollection;
 
41
                SelectionMode mode;
 
42
                
 
43
                protected new class WidgetBackendHost: Widget.WidgetBackendHost, IListBoxEventSink, ICellContainer
 
44
                {
 
45
                        public void NotifyCellChanged ()
 
46
                        {
 
47
                                ((ListBox)Parent).OnCellChanged ();
 
48
                        }
 
49
                        
 
50
                        public void OnSelectionChanged ()
 
51
                        {
 
52
                                ((ListBox)Parent).OnSelectionChanged (EventArgs.Empty);
 
53
                        }
 
54
                        
 
55
                        public override Size GetDefaultNaturalSize ()
 
56
                        {
 
57
                                return Xwt.Backends.DefaultNaturalSizes.ComboBox;
 
58
                        }
 
59
                }
 
60
                
 
61
                IListBoxBackend Backend {
 
62
                        get { return (IListBoxBackend) BackendHost.Backend; }
 
63
                }
 
64
                
 
65
                public ListBox ()
 
66
                {
 
67
                        views = new CellViewCollection ((ICellContainer)BackendHost);
 
68
                }
 
69
                
 
70
                protected override BackendHost CreateBackendHost ()
 
71
                {
 
72
                        return new WidgetBackendHost ();
 
73
                }
 
74
                
 
75
                /// <summary>
 
76
                /// Views to be used to display the data of the items
 
77
                /// </summary>
 
78
                public CellViewCollection Views {
 
79
                        get { return views; }
 
80
                }
 
81
 
 
82
                /// <summary>
 
83
                /// Items shown in the list
 
84
                /// </summary>
 
85
                /// <remarks>
 
86
                /// The Items collection can only be used when no custom DataSource is set.
 
87
                /// </remarks>
 
88
                public ItemCollection Items {
 
89
                        get {
 
90
                                if (itemCollection == null) {
 
91
                                        itemCollection = new ItemCollection ();
 
92
                                        DataSource = itemCollection.DataSource;
 
93
                                } else {
 
94
                                        if (DataSource != itemCollection.DataSource)
 
95
                                                throw new InvalidOperationException ("The Items collection can't be used when a custom DataSource is set");
 
96
                                }
 
97
                                return itemCollection;
 
98
                        }
 
99
                }
 
100
 
 
101
                /// <summary>
 
102
                /// Gets or sets the data source from which to get the data of the items
 
103
                /// </summary>
 
104
                /// <value>
 
105
                /// The data source.
 
106
                /// </value>
 
107
                /// <remarks>
 
108
                /// Then a DataSource is set, the Items collection can't be used.
 
109
                /// </remarks>
 
110
                public IListDataSource DataSource {
 
111
                        get { return source; }
 
112
                        set {
 
113
                                if (source != null) {
 
114
                                        source.RowChanged -= HandleModelChanged;
 
115
                                        source.RowDeleted -= HandleModelChanged;
 
116
                                        source.RowInserted -= HandleModelChanged;
 
117
                                        source.RowsReordered -= HandleModelChanged;
 
118
                                }
 
119
                                
 
120
                                source = value;
 
121
                                Backend.SetSource (source, source is IFrontend ? (IBackend) WidgetRegistry.GetBackend (source) : null);
 
122
                                
 
123
                                if (source != null) {
 
124
                                        source.RowChanged += HandleModelChanged;
 
125
                                        source.RowDeleted += HandleModelChanged;
 
126
                                        source.RowInserted += HandleModelChanged;
 
127
                                        source.RowsReordered += HandleModelChanged;
 
128
                                }
 
129
                        }
 
130
                }
 
131
                
 
132
                /// <summary>
 
133
                /// Gets or sets the vertical scroll policy.
 
134
                /// </summary>
 
135
                /// <value>
 
136
                /// The vertical scroll policy.
 
137
                /// </value>
 
138
                public ScrollPolicy VerticalScrollPolicy {
 
139
                        get { return Backend.VerticalScrollPolicy; }
 
140
                        set { Backend.VerticalScrollPolicy = value; }
 
141
                }
 
142
                
 
143
                /// <summary>
 
144
                /// Gets or sets the horizontal scroll policy.
 
145
                /// </summary>
 
146
                /// <value>
 
147
                /// The horizontal scroll policy.
 
148
                /// </value>
 
149
                public ScrollPolicy HorizontalScrollPolicy {
 
150
                        get { return Backend.HorizontalScrollPolicy; }
 
151
                        set { Backend.HorizontalScrollPolicy = value; }
 
152
                }
 
153
                
 
154
                /// <summary>
 
155
                /// Gets or sets the selection mode.
 
156
                /// </summary>
 
157
                /// <value>
 
158
                /// The selection mode.
 
159
                /// </value>
 
160
                public SelectionMode SelectionMode {
 
161
                        get {
 
162
                                return mode;
 
163
                        }
 
164
                        set {
 
165
                                mode = value;
 
166
                                Backend.SetSelectionMode (mode);
 
167
                        }
 
168
                }
 
169
                
 
170
                /// <summary>
 
171
                /// Gets the selected row.
 
172
                /// </summary>
 
173
                /// <value>
 
174
                /// The selected row.
 
175
                /// </value>
 
176
                public int SelectedRow {
 
177
                        get {
 
178
                                var items = SelectedRows;
 
179
                                if (items.Length == 0)
 
180
                                        return -1;
 
181
                                else
 
182
                                        return items [0];
 
183
                        }
 
184
                }
 
185
                
 
186
                public object SelectedItem {
 
187
                        get {
 
188
                                if (SelectedRow == -1)
 
189
                                        return null;
 
190
                                return Items [SelectedRow];
 
191
                        }
 
192
                        set {
 
193
                                if (SelectionMode == Xwt.SelectionMode.Multiple)
 
194
                                        UnselectAll ();
 
195
                                var i = Items.IndexOf (value);
 
196
                                if (i != -1)
 
197
                                        SelectRow (i);
 
198
                                else
 
199
                                        UnselectAll ();
 
200
                        }
 
201
                }
 
202
                
 
203
                /// <summary>
 
204
                /// Gets the selected rows.
 
205
                /// </summary>
 
206
                /// <value>
 
207
                /// The selected rows.
 
208
                /// </value>
 
209
                [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
 
210
                public int[] SelectedRows {
 
211
                        get {
 
212
                                return Backend.SelectedRows;
 
213
                        }
 
214
                }
 
215
                
 
216
                /// <summary>
 
217
                /// Selects a row.
 
218
                /// </summary>
 
219
                /// <param name='row'>
 
220
                /// a row.
 
221
                /// </param>
 
222
                /// <remarks>
 
223
                /// In single selection mode, the row will be selected and the previously selected row will be deselected.
 
224
                /// In multiple selection mode, the row will be added to the set of selected rows.
 
225
                /// </remarks>
 
226
                public void SelectRow (int row)
 
227
                {
 
228
                        Backend.SelectRow (row);
 
229
                }
 
230
                
 
231
                /// <summary>
 
232
                /// Unselects a row.
 
233
                /// </summary>
 
234
                /// <param name='row'>
 
235
                /// A row
 
236
                /// </param>
 
237
                public void UnselectRow (int row)
 
238
                {
 
239
                        Backend.UnselectRow (row);
 
240
                }
 
241
                
 
242
                /// <summary>
 
243
                /// Selects all rows
 
244
                /// </summary>
 
245
                public void SelectAll ()
 
246
                {
 
247
                        Backend.SelectAll ();
 
248
                }
 
249
                
 
250
                /// <summary>
 
251
                /// Clears the selection
 
252
                /// </summary>
 
253
                public void UnselectAll ()
 
254
                {
 
255
                        Backend.UnselectAll ();
 
256
                }
 
257
                
 
258
                void HandleModelChanged (object sender, ListRowEventArgs e)
 
259
                {
 
260
                        OnPreferredSizeChanged ();
 
261
                }
 
262
                
 
263
                void OnCellChanged ()
 
264
                {
 
265
                        Backend.SetViews (views);
 
266
                }
 
267
                
 
268
                EventHandler selectionChanged;
 
269
                
 
270
                /// <summary>
 
271
                /// Occurs when selection changes
 
272
                /// </summary>
 
273
                public event EventHandler SelectionChanged {
 
274
                        add {
 
275
                                BackendHost.OnBeforeEventAdd (ListBoxEvent.SelectionChanged, selectionChanged);
 
276
                                selectionChanged += value;
 
277
                        }
 
278
                        remove {
 
279
                                selectionChanged -= value;
 
280
                                BackendHost.OnAfterEventRemove (ListBoxEvent.SelectionChanged, selectionChanged);
 
281
                        }
 
282
                }
 
283
                
 
284
                /// <summary>
 
285
                /// Raises the selection changed event.
 
286
                /// </summary>
 
287
                /// <param name='args'>
 
288
                /// Arguments.
 
289
                /// </param>
 
290
                protected virtual void OnSelectionChanged (EventArgs args)
 
291
                {
 
292
                        if (selectionChanged != null)
 
293
                                selectionChanged (this, args);
 
294
                }
 
295
        }
 
296
}
 
297