5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2012 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.ComponentModel;
34
/// A list of selectable items
36
public class ListBox: Widget
38
CellViewCollection views;
39
IListDataSource source;
40
ItemCollection itemCollection;
43
protected new class WidgetBackendHost: Widget.WidgetBackendHost, IListBoxEventSink, ICellContainer
45
public void NotifyCellChanged ()
47
((ListBox)Parent).OnCellChanged ();
50
public void OnSelectionChanged ()
52
((ListBox)Parent).OnSelectionChanged (EventArgs.Empty);
55
public override Size GetDefaultNaturalSize ()
57
return Xwt.Backends.DefaultNaturalSizes.ComboBox;
61
IListBoxBackend Backend {
62
get { return (IListBoxBackend) BackendHost.Backend; }
67
views = new CellViewCollection ((ICellContainer)BackendHost);
70
protected override BackendHost CreateBackendHost ()
72
return new WidgetBackendHost ();
76
/// Views to be used to display the data of the items
78
public CellViewCollection Views {
83
/// Items shown in the list
86
/// The Items collection can only be used when no custom DataSource is set.
88
public ItemCollection Items {
90
if (itemCollection == null) {
91
itemCollection = new ItemCollection ();
92
DataSource = itemCollection.DataSource;
94
if (DataSource != itemCollection.DataSource)
95
throw new InvalidOperationException ("The Items collection can't be used when a custom DataSource is set");
97
return itemCollection;
102
/// Gets or sets the data source from which to get the data of the items
108
/// Then a DataSource is set, the Items collection can't be used.
110
public IListDataSource DataSource {
111
get { return source; }
113
if (source != null) {
114
source.RowChanged -= HandleModelChanged;
115
source.RowDeleted -= HandleModelChanged;
116
source.RowInserted -= HandleModelChanged;
117
source.RowsReordered -= HandleModelChanged;
121
Backend.SetSource (source, source is IFrontend ? (IBackend) WidgetRegistry.GetBackend (source) : null);
123
if (source != null) {
124
source.RowChanged += HandleModelChanged;
125
source.RowDeleted += HandleModelChanged;
126
source.RowInserted += HandleModelChanged;
127
source.RowsReordered += HandleModelChanged;
133
/// Gets or sets the vertical scroll policy.
136
/// The vertical scroll policy.
138
public ScrollPolicy VerticalScrollPolicy {
139
get { return Backend.VerticalScrollPolicy; }
140
set { Backend.VerticalScrollPolicy = value; }
144
/// Gets or sets the horizontal scroll policy.
147
/// The horizontal scroll policy.
149
public ScrollPolicy HorizontalScrollPolicy {
150
get { return Backend.HorizontalScrollPolicy; }
151
set { Backend.HorizontalScrollPolicy = value; }
155
/// Gets or sets the selection mode.
158
/// The selection mode.
160
public SelectionMode SelectionMode {
166
Backend.SetSelectionMode (mode);
171
/// Gets the selected row.
174
/// The selected row.
176
public int SelectedRow {
178
var items = SelectedRows;
179
if (items.Length == 0)
186
public object SelectedItem {
188
if (SelectedRow == -1)
190
return Items [SelectedRow];
193
if (SelectionMode == Xwt.SelectionMode.Multiple)
195
var i = Items.IndexOf (value);
204
/// Gets the selected rows.
207
/// The selected rows.
209
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
210
public int[] SelectedRows {
212
return Backend.SelectedRows;
219
/// <param name='row'>
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.
226
public void SelectRow (int row)
228
Backend.SelectRow (row);
234
/// <param name='row'>
237
public void UnselectRow (int row)
239
Backend.UnselectRow (row);
245
public void SelectAll ()
247
Backend.SelectAll ();
251
/// Clears the selection
253
public void UnselectAll ()
255
Backend.UnselectAll ();
258
void HandleModelChanged (object sender, ListRowEventArgs e)
260
OnPreferredSizeChanged ();
263
void OnCellChanged ()
265
Backend.SetViews (views);
268
EventHandler selectionChanged;
271
/// Occurs when selection changes
273
public event EventHandler SelectionChanged {
275
BackendHost.OnBeforeEventAdd (ListBoxEvent.SelectionChanged, selectionChanged);
276
selectionChanged += value;
279
selectionChanged -= value;
280
BackendHost.OnAfterEventRemove (ListBoxEvent.SelectionChanged, selectionChanged);
285
/// Raises the selection changed event.
287
/// <param name='args'>
290
protected virtual void OnSelectionChanged (EventArgs args)
292
if (selectionChanged != null)
293
selectionChanged (this, args);