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
30
using System.Collections.Generic;
31
using MonoMac.Foundation;
36
public abstract class TableViewBackend<T,S>: ViewBackend<NSScrollView,S>, ICellSource where T:NSTableView where S:ITableViewEventSink
38
List<NSTableColumn> cols = new List<NSTableColumn> ();
39
protected NSTableView Table;
41
NSObject selChangeObserver;
43
public TableViewBackend ()
47
public override void Initialize ()
49
Table = CreateView ();
50
scroll = new ScrollView ();
51
scroll.DocumentView = Table;
54
Widget.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
55
Widget.AutoresizesSubviews = true;
58
public ScrollPolicy VerticalScrollPolicy {
60
if (scroll.AutohidesScrollers && scroll.HasVerticalScroller)
61
return ScrollPolicy.Automatic;
62
else if (scroll.HasVerticalScroller)
63
return ScrollPolicy.Always;
65
return ScrollPolicy.Never;
69
case ScrollPolicy.Automatic:
70
scroll.AutohidesScrollers = true;
71
scroll.HasVerticalScroller = true;
73
case ScrollPolicy.Always:
74
scroll.AutohidesScrollers = false;
75
scroll.HasVerticalScroller = true;
77
case ScrollPolicy.Never:
78
scroll.HasVerticalScroller = false;
84
public ScrollPolicy HorizontalScrollPolicy {
86
if (scroll.AutohidesScrollers && scroll.HasHorizontalScroller)
87
return ScrollPolicy.Automatic;
88
else if (scroll.HasHorizontalScroller)
89
return ScrollPolicy.Always;
91
return ScrollPolicy.Never;
95
case ScrollPolicy.Automatic:
96
scroll.AutohidesScrollers = true;
97
scroll.HasHorizontalScroller = true;
99
case ScrollPolicy.Always:
100
scroll.AutohidesScrollers = false;
101
scroll.HasHorizontalScroller = true;
103
case ScrollPolicy.Never:
104
scroll.HasHorizontalScroller = false;
110
protected override Size GetNaturalSize ()
112
return EventSink.GetDefaultNaturalSize ();
115
protected abstract NSTableView CreateView ();
116
protected abstract string SelectionChangeEventName { get; }
118
public override void EnableEvent (object eventId)
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);
130
public override void DisableEvent (object eventId)
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);
143
void HandleTreeSelectionDidChange (NSNotification notif)
145
Toolkit.Invoke (delegate {
146
EventSink.OnSelectionChanged ();
150
public void SetSelectionMode (SelectionMode mode)
152
Table.AllowsMultipleSelection = mode == SelectionMode.Multiple;
155
public virtual object AddColumn (ListViewColumn col)
157
var tcol = new NSTableColumn ();
159
var c = CellUtil.CreateCell (this, col.Views);
161
Table.AddColumn (tcol);
162
var hc = new NSTableHeaderCell ();
163
hc.Title = col.Title;
164
tcol.HeaderCell = hc;
168
public void RemoveColumn (ListViewColumn col, object handle)
170
Table.RemoveColumn ((NSTableColumn)handle);
173
public void UpdateColumn (ListViewColumn col, object handle, ListViewColumnChange change)
175
NSTableColumn tcol = (NSTableColumn) handle;
176
tcol.DataCell = CellUtil.CreateCell (this, col.Views);
179
public void SelectAll ()
181
Table.SelectAll (null);
184
public void UnselectAll ()
186
Table.DeselectAll (null);
189
public abstract object GetValue (object pos, int nField);
191
float ICellSource.RowHeight {
192
get { return Table.RowHeight; }
193
set { Table.RowHeight = value; }
196
public bool BorderVisible {
197
get { return scroll.BorderType == NSBorderType.BezelBorder;}
199
scroll.BorderType = value ? NSBorderType.BezelBorder : NSBorderType.NoBorder;
203
public bool HeadersVisible {
205
return Table.HeaderView != null;
209
if (Table.HeaderView == null)
210
Table.HeaderView = new NSTableHeaderView ();
212
Table.HeaderView = null;
218
class ScrollView: NSScrollView, IViewObject
220
public Widget Frontend { get; set; }