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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt/Xwt/ComboBox.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
// ComboBox.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
using System;
 
27
using Xwt.Backends;
 
28
using Xwt.Engine;
 
29
 
 
30
namespace Xwt
 
31
{
 
32
        public class ComboBox: Widget
 
33
        {
 
34
                CellViewCollection views;
 
35
                IListDataSource source;
 
36
                ItemCollection itemCollection;
 
37
                
 
38
                protected new class WidgetBackendHost: Widget.WidgetBackendHost, IComboBoxEventSink, ICellContainer
 
39
                {
 
40
                        public void NotifyCellChanged ()
 
41
                        {
 
42
                                ((ComboBox)Parent).OnCellChanged ();
 
43
                        }
 
44
                        
 
45
                        public void OnSelectionChanged ()
 
46
                        {
 
47
                                ((ComboBox)Parent).OnSelectionChanged (EventArgs.Empty);
 
48
                        }
 
49
                        
 
50
                        public bool RowIsSeparator (int rowIndex)
 
51
                        {
 
52
                                return ((ComboBox)Parent).RowIsSeparator (rowIndex);
 
53
                        }
 
54
                        
 
55
                        public override Size GetDefaultNaturalSize ()
 
56
                        {
 
57
                                return Xwt.Backends.DefaultNaturalSizes.ComboBox;
 
58
                        }
 
59
                }
 
60
                
 
61
                IComboBoxBackend Backend {
 
62
                        get { return (IComboBoxBackend) BackendHost.Backend; }
 
63
                }
 
64
                
 
65
                public ComboBox ()
 
66
                {
 
67
                        views = new CellViewCollection ((ICellContainer)BackendHost);
 
68
                }
 
69
                
 
70
                protected override BackendHost CreateBackendHost ()
 
71
                {
 
72
                        return new WidgetBackendHost ();
 
73
                }
 
74
                
 
75
                public CellViewCollection Views {
 
76
                        get { return views; }
 
77
                }
 
78
                
 
79
                public ItemCollection Items {
 
80
                        get {
 
81
                                if (itemCollection == null) {
 
82
                                        itemCollection = new ItemCollection ();
 
83
                                        ItemsSource = itemCollection.DataSource;
 
84
                                } else {
 
85
                                        if (ItemsSource != itemCollection.DataSource)
 
86
                                                throw new InvalidOperationException ("The Items collection can't be used when a custom DataSource is set");
 
87
                                }
 
88
                                return itemCollection;
 
89
                        }
 
90
                }
 
91
                
 
92
                public IListDataSource ItemsSource {
 
93
                        get { return source; }
 
94
                        set {
 
95
                                if (source != null) {
 
96
                                        source.RowChanged -= HandleModelChanged;
 
97
                                        source.RowDeleted -= HandleModelChanged;
 
98
                                        source.RowInserted -= HandleModelChanged;
 
99
                                        source.RowsReordered -= HandleModelChanged;
 
100
                                }
 
101
                                
 
102
                                source = value;
 
103
                                Backend.SetSource (source, source is IFrontend ? (IBackend) WidgetRegistry.GetBackend (source) : null);
 
104
                                
 
105
                                if (source != null) {
 
106
                                        source.RowChanged += HandleModelChanged;
 
107
                                        source.RowDeleted += HandleModelChanged;
 
108
                                        source.RowInserted += HandleModelChanged;
 
109
                                        source.RowsReordered += HandleModelChanged;
 
110
                                }
 
111
                        }
 
112
                }
 
113
 
 
114
                void HandleModelChanged (object sender, ListRowEventArgs e)
 
115
                {
 
116
                        OnPreferredSizeChanged ();
 
117
                }
 
118
                
 
119
                public int SelectedIndex {
 
120
                        get { return Backend.SelectedRow; }
 
121
                        set { Backend.SelectedRow = value; }
 
122
                }
 
123
                
 
124
                public object SelectedItem {
 
125
                        get {
 
126
                                if (Backend.SelectedRow == -1)
 
127
                                        return null;
 
128
                                return Items [Backend.SelectedRow];
 
129
                        }
 
130
                        set {
 
131
                                SelectedIndex = Items.IndexOf (value);
 
132
                        }
 
133
                }
 
134
                
 
135
                public string SelectedText {
 
136
                        get {
 
137
                                if (Backend.SelectedRow == -1)
 
138
                                        return null;
 
139
                                return (string)Items.DataSource.GetValue (Backend.SelectedRow, 0);
 
140
                        }
 
141
                        set {
 
142
                                SelectedIndex = Items.IndexOf (value);
 
143
                        }
 
144
                }
 
145
                
 
146
                public Func<int,bool> RowSeparatorCheck {
 
147
                        get; set;
 
148
                }
 
149
                
 
150
                void OnCellChanged ()
 
151
                {
 
152
                        Backend.SetViews (views);
 
153
                }
 
154
                
 
155
                EventHandler selectionChanged;
 
156
                
 
157
                public event EventHandler SelectionChanged {
 
158
                        add {
 
159
                                BackendHost.OnBeforeEventAdd (ComboBoxEvent.SelectionChanged, selectionChanged);
 
160
                                selectionChanged += value;
 
161
                        }
 
162
                        remove {
 
163
                                selectionChanged -= value;
 
164
                                BackendHost.OnAfterEventRemove (ComboBoxEvent.SelectionChanged, selectionChanged);
 
165
                        }
 
166
                }
 
167
                
 
168
                protected virtual void OnSelectionChanged (EventArgs args)
 
169
                {
 
170
                        if (selectionChanged != null)
 
171
                                selectionChanged (this, args);
 
172
                }
 
173
                
 
174
                protected virtual bool RowIsSeparator (int rowIndex)
 
175
                {
 
176
                        if (RowSeparatorCheck != null)
 
177
                                return RowSeparatorCheck (rowIndex);
 
178
                        if (itemCollection != null && itemCollection.DataSource == source)
 
179
                                return Items [rowIndex] is ItemSeparator;
 
180
                        else
 
181
                                return false;
 
182
                }
 
183
        }
 
184
}