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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/CustomListModel.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
// CustomTreeModel.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
using System;
 
27
using System.Collections.Generic;
 
28
using System.Runtime.InteropServices;
 
29
 
 
30
namespace Xwt.GtkBackend
 
31
{
 
32
        public class CustomListModel: GLib.Object, Gtk.TreeModelImplementor
 
33
        {
 
34
                IListDataSource source;
 
35
                Dictionary<int,int> nodeHash = new Dictionary<int,int> ();
 
36
                Dictionary<int,int> handleHash = new Dictionary<int,int> ();
 
37
                Type[] colTypes;
 
38
                int counter = 1;
 
39
                Gtk.TreeModelAdapter adapter;
 
40
                Gtk.Widget parent;
 
41
                
 
42
                public CustomListModel (IntPtr p): base (p)
 
43
                {
 
44
                }
 
45
                
 
46
                public CustomListModel (IListDataSource source, Gtk.Widget w)
 
47
                {
 
48
                        parent = w;
 
49
                        this.source = source;
 
50
                        adapter = new Gtk.TreeModelAdapter (this);
 
51
                        colTypes = source.ColumnTypes;
 
52
                        source.RowChanged += HandleRowChanged;
 
53
                        source.RowDeleted += HandleRowDeleted;
 
54
                        source.RowInserted += HandleRowInserted;
 
55
                        source.RowsReordered += HandleRowsReordered;
 
56
                }
 
57
 
 
58
                void HandleRowsReordered (object sender, ListRowOrderEventArgs e)
 
59
                {
 
60
                        var p = new Gtk.TreePath (new int[] { e.Row });
 
61
                        var it = IterFromNode (e.Row);
 
62
                        adapter.EmitRowsReordered (p, it, e.ChildrenOrder);
 
63
                        parent.QueueResize ();
 
64
                }
 
65
 
 
66
                void HandleRowInserted (object sender, ListRowEventArgs e)
 
67
                {
 
68
                        var p = new Gtk.TreePath (new int[] { e.Row });
 
69
                        var it = IterFromNode (e.Row);
 
70
                        adapter.EmitRowInserted (p, it);
 
71
                        parent.QueueResize ();
 
72
                }
 
73
 
 
74
                void HandleRowDeleted (object sender, ListRowEventArgs e)
 
75
                {
 
76
                        var p = new Gtk.TreePath (new int[] { e.Row });
 
77
                        adapter.EmitRowDeleted (p);
 
78
                        parent.QueueResize ();
 
79
                }
 
80
 
 
81
                void HandleRowChanged (object sender, ListRowEventArgs e)
 
82
                {
 
83
                        var p = new Gtk.TreePath (new int[] { e.Row });
 
84
                        var it = IterFromNode (e.Row);
 
85
                        adapter.EmitRowChanged (p, it);
 
86
                        parent.QueueResize ();
 
87
                }
 
88
                
 
89
                public Gtk.TreeModelAdapter Store {
 
90
                        get { return adapter; }
 
91
                }
 
92
                
 
93
                Gtk.TreeIter IterFromNode (int node)
 
94
                {
 
95
                        int gch;
 
96
                        if (!handleHash.TryGetValue (node, out gch)) {
 
97
                                gch = counter++;
 
98
                                handleHash [node] = gch;
 
99
                                nodeHash [gch] = node;
 
100
                        }
 
101
                        Gtk.TreeIter result = Gtk.TreeIter.Zero;
 
102
                        result.UserData = (IntPtr)gch;
 
103
                        return result;
 
104
                }
 
105
                
 
106
                int NodeFromIter (Gtk.TreeIter iter)
 
107
                {
 
108
                        int node;
 
109
                        int gch = (int)iter.UserData;
 
110
                        nodeHash.TryGetValue (gch, out node);
 
111
                        return node;
 
112
                }
 
113
                
 
114
                #region TreeModelImplementor implementation
 
115
                public GLib.GType GetColumnType (int index)
 
116
                {
 
117
                        return (GLib.GType)colTypes [index];
 
118
                }
 
119
 
 
120
                public bool GetIter (out Gtk.TreeIter iter, Gtk.TreePath path)
 
121
        {
 
122
            iter = Gtk.TreeIter.Zero;
 
123
                        if (path.Indices.Length == 0)
 
124
                                return false;
 
125
                        int row = path.Indices [0];
 
126
                        if (row >= source.RowCount) {
 
127
                                return false;
 
128
                        }
 
129
                        iter = IterFromNode (row);
 
130
                        return true;
 
131
                }
 
132
 
 
133
                public Gtk.TreePath GetPath (Gtk.TreeIter iter)
 
134
                {
 
135
                        int row = NodeFromIter (iter);
 
136
                        return new Gtk.TreePath (new int[] { row });
 
137
                }
 
138
                
 
139
                public void GetValue (Gtk.TreeIter iter, int column, ref GLib.Value value)
 
140
                {
 
141
                        int row = NodeFromIter (iter);
 
142
                        var v = source.GetValue (row, column);
 
143
                        value = v != null ? new GLib.Value (v) : GLib.Value.Empty;
 
144
                }
 
145
 
 
146
                public bool IterNext (ref Gtk.TreeIter iter)
 
147
                {
 
148
                        int row = NodeFromIter (iter);
 
149
                        if (++row < source.RowCount) {
 
150
                                iter = IterFromNode (row);
 
151
                                return true;
 
152
                        } else
 
153
                                return false;
 
154
                }
 
155
 
 
156
                public bool IterChildren (out Gtk.TreeIter iter, Gtk.TreeIter parent)
 
157
        {
 
158
            iter = Gtk.TreeIter.Zero;
 
159
                        return false;
 
160
                }
 
161
 
 
162
                public bool IterHasChild (Gtk.TreeIter iter)
 
163
                {
 
164
                        return false;
 
165
                }
 
166
 
 
167
                public int IterNChildren (Gtk.TreeIter iter)
 
168
                {
 
169
                        if (iter.Equals (Gtk.TreeIter.Zero))
 
170
                                return source.RowCount;
 
171
                        else
 
172
                                return 0;
 
173
                }
 
174
 
 
175
                public bool IterNthChild (out Gtk.TreeIter iter, Gtk.TreeIter parent, int n)
 
176
                {
 
177
                        if (parent.Equals (Gtk.TreeIter.Zero)) {
 
178
                                iter = IterFromNode (n);
 
179
                                return true;
 
180
                        } else {
 
181
                                iter = Gtk.TreeIter.Zero;
 
182
                                return false;
 
183
                        }
 
184
                }
 
185
 
 
186
                public bool IterParent (out Gtk.TreeIter iter, Gtk.TreeIter child)
 
187
                {
 
188
                        iter = Gtk.TreeIter.Zero;
 
189
                        return false;
 
190
                }
 
191
 
 
192
                public void RefNode (Gtk.TreeIter iter)
 
193
                {
 
194
                }
 
195
 
 
196
                public void UnrefNode (Gtk.TreeIter iter)
 
197
                {
 
198
                }
 
199
 
 
200
                public Gtk.TreeModelFlags Flags {
 
201
                        get {
 
202
                                return Gtk.TreeModelFlags.ItersPersist | Gtk.TreeModelFlags.ListOnly;
 
203
                        }
 
204
                }
 
205
 
 
206
                public int NColumns {
 
207
                        get {
 
208
                                return colTypes.Length;
 
209
                        }
 
210
                }
 
211
                #endregion
 
212
        }
 
213
}
 
214