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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/TableViewBackend.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
// TableViewBackend.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
 
 
27
using System;
 
28
using MonoMac.AppKit;
 
29
using Xwt.Backends;
 
30
using System.Collections.Generic;
 
31
using MonoMac.Foundation;
 
32
using Xwt.Engine;
 
33
 
 
34
namespace Xwt.Mac
 
35
{
 
36
        public abstract class TableViewBackend<T,S>: ViewBackend<NSScrollView,S>, ICellSource where T:NSTableView where S:ITableViewEventSink
 
37
        {
 
38
                List<NSTableColumn> cols = new List<NSTableColumn> ();
 
39
                protected NSTableView Table;
 
40
                ScrollView scroll;
 
41
                NSObject selChangeObserver;
 
42
                
 
43
                public TableViewBackend ()
 
44
                {
 
45
                }
 
46
                
 
47
                public override void Initialize ()
 
48
                {
 
49
                        Table = CreateView ();
 
50
                        scroll = new ScrollView ();
 
51
                        scroll.DocumentView = Table;
 
52
                        ViewObject = scroll;
 
53
                        Table.SizeToFit ();
 
54
                        Widget.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
 
55
                        Widget.AutoresizesSubviews = true;
 
56
                }
 
57
                
 
58
                public ScrollPolicy VerticalScrollPolicy {
 
59
                        get {
 
60
                                if (scroll.AutohidesScrollers && scroll.HasVerticalScroller)
 
61
                                        return ScrollPolicy.Automatic;
 
62
                                else if (scroll.HasVerticalScroller)
 
63
                                        return ScrollPolicy.Always;
 
64
                                else
 
65
                                        return ScrollPolicy.Never;
 
66
                        }
 
67
                        set {
 
68
                                switch (value) {
 
69
                                case ScrollPolicy.Automatic:
 
70
                                        scroll.AutohidesScrollers = true;
 
71
                                        scroll.HasVerticalScroller = true;
 
72
                                        break;
 
73
                                case ScrollPolicy.Always:
 
74
                                        scroll.AutohidesScrollers = false;
 
75
                                        scroll.HasVerticalScroller = true;
 
76
                                        break;
 
77
                                case ScrollPolicy.Never:
 
78
                                        scroll.HasVerticalScroller = false;
 
79
                                        break;
 
80
                                }
 
81
                        }
 
82
                }
 
83
 
 
84
                public ScrollPolicy HorizontalScrollPolicy {
 
85
                        get {
 
86
                                if (scroll.AutohidesScrollers && scroll.HasHorizontalScroller)
 
87
                                        return ScrollPolicy.Automatic;
 
88
                                else if (scroll.HasHorizontalScroller)
 
89
                                        return ScrollPolicy.Always;
 
90
                                else
 
91
                                        return ScrollPolicy.Never;
 
92
                        }
 
93
                        set {
 
94
                                switch (value) {
 
95
                                case ScrollPolicy.Automatic:
 
96
                                        scroll.AutohidesScrollers = true;
 
97
                                        scroll.HasHorizontalScroller = true;
 
98
                                        break;
 
99
                                case ScrollPolicy.Always:
 
100
                                        scroll.AutohidesScrollers = false;
 
101
                                        scroll.HasHorizontalScroller = true;
 
102
                                        break;
 
103
                                case ScrollPolicy.Never:
 
104
                                        scroll.HasHorizontalScroller = false;
 
105
                                        break;
 
106
                                }
 
107
                        }
 
108
                }
 
109
                
 
110
                protected override Size GetNaturalSize ()
 
111
                {
 
112
                        return EventSink.GetDefaultNaturalSize ();
 
113
                }
 
114
                
 
115
                protected abstract NSTableView CreateView ();
 
116
                protected abstract string SelectionChangeEventName { get; }
 
117
                
 
118
                public override void EnableEvent (object eventId)
 
119
                {
 
120
                        base.EnableEvent (eventId);
 
121
                        if (eventId is TableViewEvent) {
 
122
                                switch ((TableViewEvent)eventId) {
 
123
                                case TableViewEvent.SelectionChanged:
 
124
                                        selChangeObserver = NSNotificationCenter.DefaultCenter.AddObserver (new NSString (SelectionChangeEventName), HandleTreeSelectionDidChange, Table);
 
125
                                        break;
 
126
                                }
 
127
                        }
 
128
                }
 
129
                
 
130
                public override void DisableEvent (object eventId)
 
131
                {
 
132
                        base.DisableEvent (eventId);
 
133
                        if (eventId is TableViewEvent) {
 
134
                                switch ((TableViewEvent)eventId) {
 
135
                                case TableViewEvent.SelectionChanged:
 
136
                                        if (selChangeObserver != null)
 
137
                                                NSNotificationCenter.DefaultCenter.RemoveObserver (selChangeObserver);
 
138
                                        break;
 
139
                                }
 
140
                        }
 
141
                }
 
142
 
 
143
                void HandleTreeSelectionDidChange (NSNotification notif)
 
144
                {
 
145
                        Toolkit.Invoke (delegate {
 
146
                                EventSink.OnSelectionChanged ();
 
147
                        });
 
148
                }
 
149
                
 
150
                public void SetSelectionMode (SelectionMode mode)
 
151
                {
 
152
                        Table.AllowsMultipleSelection = mode == SelectionMode.Multiple;
 
153
                }
 
154
 
 
155
                public virtual object AddColumn (ListViewColumn col)
 
156
                {
 
157
                        var tcol = new NSTableColumn ();
 
158
                        cols.Add (tcol);
 
159
                        var c = CellUtil.CreateCell (this, col.Views);
 
160
                        tcol.DataCell = c;
 
161
                        Table.AddColumn (tcol);
 
162
                        var hc = new NSTableHeaderCell ();
 
163
                        hc.Title = col.Title;
 
164
                        tcol.HeaderCell = hc;
 
165
                        return tcol;
 
166
                }
 
167
                
 
168
                public void RemoveColumn (ListViewColumn col, object handle)
 
169
                {
 
170
                        Table.RemoveColumn ((NSTableColumn)handle);
 
171
                }
 
172
 
 
173
                public void UpdateColumn (ListViewColumn col, object handle, ListViewColumnChange change)
 
174
                {
 
175
                        NSTableColumn tcol = (NSTableColumn) handle;
 
176
                        tcol.DataCell = CellUtil.CreateCell (this, col.Views);
 
177
                }
 
178
 
 
179
                public void SelectAll ()
 
180
                {
 
181
                        Table.SelectAll (null);
 
182
                }
 
183
 
 
184
                public void UnselectAll ()
 
185
                {
 
186
                        Table.DeselectAll (null);
 
187
                }
 
188
                
 
189
                public abstract object GetValue (object pos, int nField);
 
190
                
 
191
                float ICellSource.RowHeight {
 
192
                        get { return Table.RowHeight; }
 
193
                        set { Table.RowHeight = value; }
 
194
                }
 
195
                
 
196
                public bool BorderVisible {
 
197
                        get { return scroll.BorderType == NSBorderType.BezelBorder;}
 
198
                        set {
 
199
                                scroll.BorderType = value ? NSBorderType.BezelBorder : NSBorderType.NoBorder;
 
200
                        }
 
201
                }
 
202
 
 
203
                public bool HeadersVisible {
 
204
                        get {
 
205
                                return Table.HeaderView != null;
 
206
                        }
 
207
                        set {
 
208
                                if (value) {
 
209
                                        if (Table.HeaderView == null)
 
210
                                                Table.HeaderView = new NSTableHeaderView ();
 
211
                                } else {
 
212
                                        Table.HeaderView = null;
 
213
                                }
 
214
                        }
 
215
                }
 
216
        }
 
217
        
 
218
        class ScrollView: NSScrollView, IViewObject
 
219
        {
 
220
                public Widget Frontend { get; set; }
 
221
                public NSView View {
 
222
                        get { return this; }
 
223
                }
 
224
        }
 
225
}
 
226