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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/TreeViewBackend.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
// TreeViewBackend.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 Xwt.Backends;
 
29
 
 
30
namespace Xwt.GtkBackend
 
31
{
 
32
        public class TreeViewBackend: TableViewBackend, ITreeViewBackend
 
33
        {
 
34
                Gtk.TreePath autoExpandPath;
 
35
                uint expandTimer;
 
36
                
 
37
                protected override void OnSetDragTarget (Gtk.TargetEntry[] table, Gdk.DragAction actions)
 
38
                {
 
39
                        base.OnSetDragTarget (table, actions);
 
40
                        Widget.EnableModelDragDest (table, actions);
 
41
                }
 
42
                
 
43
                protected override void OnSetDragSource (Gdk.ModifierType modifierType, Gtk.TargetEntry[] table, Gdk.DragAction actions)
 
44
                {
 
45
                        base.OnSetDragSource (modifierType, table, actions);
 
46
                        Widget.EnableModelDragSource (modifierType, table, actions);
 
47
                }
 
48
                
 
49
                protected override void OnSetDragStatus (Gdk.DragContext context, int x, int y, uint time, Gdk.DragAction action)
 
50
                {
 
51
                        base.OnSetDragStatus (context, x, y, time, action);
 
52
                        
 
53
                        // We are overriding the TreeView methods for handling drag & drop, so we need
 
54
                        // to manually highlight the selected row
 
55
                        
 
56
                        Gtk.TreeViewDropPosition tpos;
 
57
                        Gtk.TreePath path;
 
58
                        if (!Widget.GetDestRowAtPos (x, y, out path, out tpos))
 
59
                                path = null;
 
60
                        
 
61
                        if (expandTimer == 0 || !object.Equals (autoExpandPath, path)) {
 
62
                                if (expandTimer != 0)
 
63
                                        GLib.Source.Remove (expandTimer);
 
64
                                if (path != null) {
 
65
                                        expandTimer = GLib.Timeout.Add (600, delegate {
 
66
                                                Widget.ExpandRow (path, false);
 
67
                                                return false;
 
68
                                        });
 
69
                                }
 
70
                                autoExpandPath = path;
 
71
                        }
 
72
                        
 
73
                        if (path != null && action != 0)
 
74
                                Widget.SetDragDestRow (path, tpos);
 
75
                        else
 
76
                                Widget.SetDragDestRow (null, 0);
 
77
                }
 
78
                
 
79
                protected override void Dispose (bool disposing)
 
80
                {
 
81
                        if (expandTimer != 0)
 
82
                                GLib.Source.Remove (expandTimer);
 
83
                        base.Dispose (disposing);
 
84
                }
 
85
                
 
86
                public void SetSource (ITreeDataSource source, IBackend sourceBackend)
 
87
                {
 
88
                        TreeStoreBackend b = sourceBackend as TreeStoreBackend;
 
89
                        if (b == null) {
 
90
                                CustomTreeModel model = new CustomTreeModel (source);
 
91
                                Widget.Model = model.Store;
 
92
                        } else
 
93
                                Widget.Model = b.Store;
 
94
                }
 
95
 
 
96
                public TreePosition[] SelectedRows {
 
97
                        get {
 
98
                                var rows = Widget.Selection.GetSelectedRows ();
 
99
                                IterPos[] sel = new IterPos [rows.Length];
 
100
                                for (int i = 0; i < rows.Length; i++) {
 
101
                                        Gtk.TreeIter it;
 
102
                                        Widget.Model.GetIter (out it, rows[i]);
 
103
                                        sel[i] = new IterPos (-1, it);
 
104
                                }
 
105
                                return sel;
 
106
                        }
 
107
                }
 
108
                
 
109
                public void SelectRow (TreePosition pos)
 
110
                {
 
111
                        Widget.Selection.SelectIter (((IterPos)pos).Iter);
 
112
                }
 
113
                
 
114
                public void UnselectRow (TreePosition pos)
 
115
                {
 
116
                        Widget.Selection.UnselectIter (((IterPos)pos).Iter);
 
117
                }
 
118
                
 
119
                public bool IsRowSelected (TreePosition pos)
 
120
                {
 
121
                        return Widget.Selection.IterIsSelected (((IterPos)pos).Iter);
 
122
                }
 
123
                
 
124
                public bool IsRowExpanded (TreePosition pos)
 
125
                {
 
126
                        return Widget.GetRowExpanded (Widget.Model.GetPath (((IterPos)pos).Iter));
 
127
                }
 
128
                
 
129
                public void ExpandRow (TreePosition pos, bool expandedChildren)
 
130
                {
 
131
                        Widget.ExpandRow (Widget.Model.GetPath (((IterPos)pos).Iter), expandedChildren);
 
132
                }
 
133
                
 
134
                public void CollapseRow (TreePosition pos)
 
135
                {
 
136
                        Widget.CollapseRow (Widget.Model.GetPath (((IterPos)pos).Iter));
 
137
                }
 
138
                
 
139
                public void ScrollToRow (TreePosition pos)
 
140
                {
 
141
                        Widget.ScrollToCell (Widget.Model.GetPath (((IterPos)pos).Iter), Widget.Columns[0], false, 0, 0);
 
142
                }
 
143
                
 
144
                public void ExpandToRow (TreePosition pos)
 
145
                {
 
146
                        Widget.ExpandToPath (Widget.Model.GetPath (((IterPos)pos).Iter));
 
147
                }
 
148
                
 
149
                public bool HeadersVisible {
 
150
                        get {
 
151
                                return Widget.HeadersVisible;
 
152
                        }
 
153
                        set {
 
154
                                Widget.HeadersVisible = value;
 
155
                        }
 
156
                }
 
157
                
 
158
                public bool GetDropTargetRow (double x, double y, out RowDropPosition pos, out TreePosition nodePosition)
 
159
                {
 
160
                        Gtk.TreeViewDropPosition tpos;
 
161
                        Gtk.TreePath path;
 
162
                        if (!Widget.GetDestRowAtPos ((int)x, (int)y, out path, out tpos)) {
 
163
                                pos = RowDropPosition.Into;
 
164
                                nodePosition = null;
 
165
                                return false;
 
166
                        }
 
167
                        
 
168
                        Gtk.TreeIter it;
 
169
                        Widget.Model.GetIter (out it, path);
 
170
                        nodePosition = new IterPos (-1, it);
 
171
                        switch (tpos) {
 
172
                        case Gtk.TreeViewDropPosition.After: pos = RowDropPosition.After; break;
 
173
                        case Gtk.TreeViewDropPosition.Before: pos = RowDropPosition.Before; break;
 
174
                        default: pos = RowDropPosition.Into; break;
 
175
                        }
 
176
                        return true;
 
177
                }
 
178
        }
 
179
}
 
180