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
namespace Xwt.GtkBackend
32
public class TreeViewBackend: TableViewBackend, ITreeViewBackend
34
Gtk.TreePath autoExpandPath;
37
protected override void OnSetDragTarget (Gtk.TargetEntry[] table, Gdk.DragAction actions)
39
base.OnSetDragTarget (table, actions);
40
Widget.EnableModelDragDest (table, actions);
43
protected override void OnSetDragSource (Gdk.ModifierType modifierType, Gtk.TargetEntry[] table, Gdk.DragAction actions)
45
base.OnSetDragSource (modifierType, table, actions);
46
Widget.EnableModelDragSource (modifierType, table, actions);
49
protected override void OnSetDragStatus (Gdk.DragContext context, int x, int y, uint time, Gdk.DragAction action)
51
base.OnSetDragStatus (context, x, y, time, action);
53
// We are overriding the TreeView methods for handling drag & drop, so we need
54
// to manually highlight the selected row
56
Gtk.TreeViewDropPosition tpos;
58
if (!Widget.GetDestRowAtPos (x, y, out path, out tpos))
61
if (expandTimer == 0 || !object.Equals (autoExpandPath, path)) {
63
GLib.Source.Remove (expandTimer);
65
expandTimer = GLib.Timeout.Add (600, delegate {
66
Widget.ExpandRow (path, false);
70
autoExpandPath = path;
73
if (path != null && action != 0)
74
Widget.SetDragDestRow (path, tpos);
76
Widget.SetDragDestRow (null, 0);
79
protected override void Dispose (bool disposing)
82
GLib.Source.Remove (expandTimer);
83
base.Dispose (disposing);
86
public void SetSource (ITreeDataSource source, IBackend sourceBackend)
88
TreeStoreBackend b = sourceBackend as TreeStoreBackend;
90
CustomTreeModel model = new CustomTreeModel (source);
91
Widget.Model = model.Store;
93
Widget.Model = b.Store;
96
public TreePosition[] SelectedRows {
98
var rows = Widget.Selection.GetSelectedRows ();
99
IterPos[] sel = new IterPos [rows.Length];
100
for (int i = 0; i < rows.Length; i++) {
102
Widget.Model.GetIter (out it, rows[i]);
103
sel[i] = new IterPos (-1, it);
109
public void SelectRow (TreePosition pos)
111
Widget.Selection.SelectIter (((IterPos)pos).Iter);
114
public void UnselectRow (TreePosition pos)
116
Widget.Selection.UnselectIter (((IterPos)pos).Iter);
119
public bool IsRowSelected (TreePosition pos)
121
return Widget.Selection.IterIsSelected (((IterPos)pos).Iter);
124
public bool IsRowExpanded (TreePosition pos)
126
return Widget.GetRowExpanded (Widget.Model.GetPath (((IterPos)pos).Iter));
129
public void ExpandRow (TreePosition pos, bool expandedChildren)
131
Widget.ExpandRow (Widget.Model.GetPath (((IterPos)pos).Iter), expandedChildren);
134
public void CollapseRow (TreePosition pos)
136
Widget.CollapseRow (Widget.Model.GetPath (((IterPos)pos).Iter));
139
public void ScrollToRow (TreePosition pos)
141
Widget.ScrollToCell (Widget.Model.GetPath (((IterPos)pos).Iter), Widget.Columns[0], false, 0, 0);
144
public void ExpandToRow (TreePosition pos)
146
Widget.ExpandToPath (Widget.Model.GetPath (((IterPos)pos).Iter));
149
public bool HeadersVisible {
151
return Widget.HeadersVisible;
154
Widget.HeadersVisible = value;
158
public bool GetDropTargetRow (double x, double y, out RowDropPosition pos, out TreePosition nodePosition)
160
Gtk.TreeViewDropPosition tpos;
162
if (!Widget.GetDestRowAtPos ((int)x, (int)y, out path, out tpos)) {
163
pos = RowDropPosition.Into;
169
Widget.Model.GetIter (out it, path);
170
nodePosition = new IterPos (-1, it);
172
case Gtk.TreeViewDropPosition.After: pos = RowDropPosition.After; break;
173
case Gtk.TreeViewDropPosition.Before: pos = RowDropPosition.Before; break;
174
default: pos = RowDropPosition.Into; break;