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
32
public class ComboBox: Widget
34
CellViewCollection views;
35
IListDataSource source;
36
ItemCollection itemCollection;
38
protected new class WidgetBackendHost: Widget.WidgetBackendHost, IComboBoxEventSink, ICellContainer
40
public void NotifyCellChanged ()
42
((ComboBox)Parent).OnCellChanged ();
45
public void OnSelectionChanged ()
47
((ComboBox)Parent).OnSelectionChanged (EventArgs.Empty);
50
public bool RowIsSeparator (int rowIndex)
52
return ((ComboBox)Parent).RowIsSeparator (rowIndex);
55
public override Size GetDefaultNaturalSize ()
57
return Xwt.Backends.DefaultNaturalSizes.ComboBox;
61
IComboBoxBackend Backend {
62
get { return (IComboBoxBackend) BackendHost.Backend; }
67
views = new CellViewCollection ((ICellContainer)BackendHost);
70
protected override BackendHost CreateBackendHost ()
72
return new WidgetBackendHost ();
75
public CellViewCollection Views {
79
public ItemCollection Items {
81
if (itemCollection == null) {
82
itemCollection = new ItemCollection ();
83
ItemsSource = itemCollection.DataSource;
85
if (ItemsSource != itemCollection.DataSource)
86
throw new InvalidOperationException ("The Items collection can't be used when a custom DataSource is set");
88
return itemCollection;
92
public IListDataSource ItemsSource {
93
get { return source; }
96
source.RowChanged -= HandleModelChanged;
97
source.RowDeleted -= HandleModelChanged;
98
source.RowInserted -= HandleModelChanged;
99
source.RowsReordered -= HandleModelChanged;
103
Backend.SetSource (source, source is IFrontend ? (IBackend) WidgetRegistry.GetBackend (source) : null);
105
if (source != null) {
106
source.RowChanged += HandleModelChanged;
107
source.RowDeleted += HandleModelChanged;
108
source.RowInserted += HandleModelChanged;
109
source.RowsReordered += HandleModelChanged;
114
void HandleModelChanged (object sender, ListRowEventArgs e)
116
OnPreferredSizeChanged ();
119
public int SelectedIndex {
120
get { return Backend.SelectedRow; }
121
set { Backend.SelectedRow = value; }
124
public object SelectedItem {
126
if (Backend.SelectedRow == -1)
128
return Items [Backend.SelectedRow];
131
SelectedIndex = Items.IndexOf (value);
135
public string SelectedText {
137
if (Backend.SelectedRow == -1)
139
return (string)Items.DataSource.GetValue (Backend.SelectedRow, 0);
142
SelectedIndex = Items.IndexOf (value);
146
public Func<int,bool> RowSeparatorCheck {
150
void OnCellChanged ()
152
Backend.SetViews (views);
155
EventHandler selectionChanged;
157
public event EventHandler SelectionChanged {
159
BackendHost.OnBeforeEventAdd (ComboBoxEvent.SelectionChanged, selectionChanged);
160
selectionChanged += value;
163
selectionChanged -= value;
164
BackendHost.OnAfterEventRemove (ComboBoxEvent.SelectionChanged, selectionChanged);
168
protected virtual void OnSelectionChanged (EventArgs args)
170
if (selectionChanged != null)
171
selectionChanged (this, args);
174
protected virtual bool RowIsSeparator (int rowIndex)
176
if (RowSeparatorCheck != null)
177
return RowSeparatorCheck (rowIndex);
178
if (itemCollection != null && itemCollection.DataSource == source)
179
return Items [rowIndex] is ItemSeparator;