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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/ListViewBackend.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
// ListViewBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Eric Maupin <ermau@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
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Linq;
 
30
using System.Windows;
 
31
using System.Windows.Controls;
 
32
using Xwt.Engine;
 
33
using Xwt.WPFBackend.Utilities;
 
34
using SWC = System.Windows.Controls;
 
35
using Xwt.Backends;
 
36
 
 
37
namespace Xwt.WPFBackend
 
38
{
 
39
        public class ListViewBackend
 
40
                : WidgetBackend, IListViewBackend
 
41
        {
 
42
                public ListViewBackend()
 
43
                {
 
44
                        ListView = new ExListView();
 
45
                        ListView.View = this.view;
 
46
                }
 
47
                
 
48
                public ScrollPolicy VerticalScrollPolicy {
 
49
                        get { return ScrollViewer.GetVerticalScrollBarVisibility (this.ListView).ToXwtScrollPolicy (); }
 
50
                        set { ScrollViewer.SetVerticalScrollBarVisibility (ListView, value.ToWpfScrollBarVisibility ()); }
 
51
                }
 
52
 
 
53
                public ScrollPolicy HorizontalScrollPolicy {
 
54
                        get { return ScrollViewer.GetHorizontalScrollBarVisibility (this.ListView).ToXwtScrollPolicy (); }
 
55
                        set { ScrollViewer.SetHorizontalScrollBarVisibility (ListView, value.ToWpfScrollBarVisibility ()); }
 
56
                }
 
57
 
 
58
                private bool borderVisible = true;
 
59
                public bool BorderVisible
 
60
                {
 
61
                        get { return this.borderVisible; }
 
62
                        set
 
63
                        {
 
64
                                if (this.borderVisible == value)
 
65
                                        return;
 
66
 
 
67
                                if (value)
 
68
                                        ListView.ClearValue (Control.BorderBrushProperty);
 
69
                                else
 
70
                                        ListView.BorderBrush = null;
 
71
 
 
72
                                this.borderVisible = value;
 
73
                        }
 
74
                }
 
75
 
 
76
                public bool HeadersVisible {
 
77
                        get { return this.headersVisible; }
 
78
                        set {
 
79
                                this.headersVisible = value;
 
80
                                if (value) {
 
81
                                    if (this.view.ColumnHeaderContainerStyle != null)
 
82
                                                this.view.ColumnHeaderContainerStyle.Setters.Remove (HideHeadersSetter);
 
83
                                } else {
 
84
                                        if (this.view.ColumnHeaderContainerStyle == null)
 
85
                                                this.view.ColumnHeaderContainerStyle = new Style();
 
86
 
 
87
                                        this.view.ColumnHeaderContainerStyle.Setters.Add (HideHeadersSetter);
 
88
                                }
 
89
                        }
 
90
                }
 
91
 
 
92
                public int[] SelectedRows {
 
93
                        get { return ListView.SelectedItems.Cast<object>().Select (ListView.Items.IndexOf).ToArray (); }
 
94
                }
 
95
 
 
96
                public object AddColumn (ListViewColumn col)
 
97
                {
 
98
                        var column = new GridViewColumn ();
 
99
                        column.CellTemplate = new DataTemplate { VisualTree = CellUtil.CreateBoundColumnTemplate (col.Views) };
 
100
                        if (col.HeaderView != null)
 
101
                                column.HeaderTemplate = new DataTemplate { VisualTree = CellUtil.CreateBoundCellRenderer (col.HeaderView) };
 
102
                        else
 
103
                                column.Header = col.Title;
 
104
 
 
105
                        this.view.Columns.Add (column);
 
106
 
 
107
                        return column;
 
108
                }
 
109
 
 
110
                public void RemoveColumn (ListViewColumn col, object handle)
 
111
                {
 
112
                        this.view.Columns.Remove ((GridViewColumn) handle);
 
113
                }
 
114
 
 
115
                public void UpdateColumn (ListViewColumn col, object handle, ListViewColumnChange change)
 
116
                {
 
117
                        var column = (GridViewColumn) handle;
 
118
                        column.CellTemplate = new DataTemplate { VisualTree = CellUtil.CreateBoundColumnTemplate (col.Views) };
 
119
                        if (col.HeaderView != null)
 
120
                                column.HeaderTemplate = new DataTemplate { VisualTree = CellUtil.CreateBoundCellRenderer (col.HeaderView) };
 
121
                        else
 
122
                                column.Header = col.Title;
 
123
                }
 
124
 
 
125
                public void SetSelectionMode (SelectionMode mode)
 
126
                {
 
127
                        switch (mode) {
 
128
                        case SelectionMode.Single:
 
129
                                ListView.SelectionMode = SWC.SelectionMode.Single;
 
130
                                break;
 
131
 
 
132
                        case SelectionMode.Multiple:
 
133
                                ListView.SelectionMode = SWC.SelectionMode.Extended;
 
134
                                break;
 
135
                        }
 
136
                }
 
137
 
 
138
                public void SelectAll ()
 
139
                {
 
140
                        ListView.SelectAll();
 
141
                }
 
142
 
 
143
                public void UnselectAll ()
 
144
                {
 
145
                        ListView.UnselectAll();
 
146
                }
 
147
 
 
148
                public void SetSource (IListDataSource source, IBackend sourceBackend)
 
149
                {
 
150
                        var dataSource = sourceBackend as ListDataSource;
 
151
                        if (dataSource != null)
 
152
                                ListView.ItemsSource = dataSource;
 
153
                        else
 
154
                                ListView.ItemsSource = new ListSourceNotifyWrapper (source);
 
155
                }
 
156
 
 
157
                public void SelectRow (int pos)
 
158
                {
 
159
                        object item = ListView.Items [pos];
 
160
                        if (ListView.SelectionMode == System.Windows.Controls.SelectionMode.Single)
 
161
                                ListView.SelectedItem = item;
 
162
                        else
 
163
                                ListView.SelectedItems.Add (item);
 
164
                }
 
165
 
 
166
                public void UnselectRow (int pos)
 
167
                {
 
168
                        object item = ListView.Items [pos];
 
169
                        if (ListView.SelectionMode == System.Windows.Controls.SelectionMode.Extended)
 
170
                                ListView.SelectedItems.Remove (item);
 
171
                        else if (ListView.SelectedItem == item)
 
172
                                ListView.SelectedItem = null;
 
173
                }
 
174
 
 
175
                public override void EnableEvent(object eventId)
 
176
                {
 
177
                        base.EnableEvent (eventId);
 
178
                        if (eventId is TableViewEvent) {
 
179
                                switch ((TableViewEvent)eventId) {
 
180
                                case TableViewEvent.SelectionChanged:
 
181
                                        ListView.SelectionChanged += OnSelectionChanged;
 
182
                                        break;
 
183
                                }
 
184
                        }
 
185
                }
 
186
 
 
187
                public override void DisableEvent (object eventId)
 
188
                {
 
189
                        base.DisableEvent (eventId);
 
190
                        if (eventId is TableViewEvent) {
 
191
                                switch ((TableViewEvent)eventId) {
 
192
                                case TableViewEvent.SelectionChanged:
 
193
                                        ListView.SelectionChanged -= OnSelectionChanged;
 
194
                                        break;
 
195
                                }
 
196
                        }
 
197
                }
 
198
 
 
199
                private void OnSelectionChanged (object sender, SelectionChangedEventArgs e)
 
200
                {
 
201
                        Toolkit.Invoke (ListViewEventSink.OnSelectionChanged);
 
202
                }
 
203
 
 
204
                private bool headersVisible;
 
205
                private readonly GridView view = new GridView();
 
206
 
 
207
                protected ExListView ListView {
 
208
                        get { return (ExListView) Widget; }
 
209
                        set { Widget = value; }
 
210
                }
 
211
 
 
212
                protected IListViewEventSink ListViewEventSink {
 
213
                        get { return (IListViewEventSink) EventSink; }
 
214
                }
 
215
 
 
216
                private static readonly Setter HideHeadersSetter = new Setter (UIElement.VisibilityProperty, Visibility.Collapsed);
 
217
        }
 
218
}