Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ListViewBackend.cs « Xwt.GtkBackend « Xwt.Gtk - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06e3e3f265b8747c680a3aea5e4a125f7c0376c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// 
// ListViewBackend.cs
//  
// Author:
//       Lluis Sanchez <lluis@xamarin.com>
// 
// Copyright (c) 2011 Xamarin Inc
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Xwt.Backends;

namespace Xwt.GtkBackend
{
	public class ListViewBackend: TableViewBackend, IListViewBackend
	{
		bool showBorder;
		
		protected new IListViewEventSink EventSink {
			get { return (IListViewEventSink)base.EventSink; }
		}
		
		public override void EnableEvent (object eventId)
		{
			base.EnableEvent (eventId);
			if (eventId is ListViewEvent) {
				if (((ListViewEvent)eventId) == ListViewEvent.RowActivated)
					Widget.RowActivated += HandleRowActivated;
			}
		}
		
		public override void DisableEvent (object eventId)
		{
			base.DisableEvent (eventId);
			if (eventId is ListViewEvent) {
				if (((ListViewEvent)eventId) == ListViewEvent.RowActivated)
					Widget.RowActivated -= HandleRowActivated;
			}
		}
		
		void HandleRowActivated (object o, Gtk.RowActivatedArgs args)
		{
			ApplicationContext.InvokeUserCode (delegate {
				EventSink.OnRowActivated (args.Path.Indices[0]);
			});
		}

		public void SetSource (IListDataSource source, IBackend sourceBackend)
		{
			ListStoreBackend b = sourceBackend as ListStoreBackend;
			if (b == null) {
				CustomListModel model = new CustomListModel (source, Widget);
				Widget.Model = model.Store;
			} else
				Widget.Model = b.Store;
		}

		public void SelectRow (int row)
		{
			Gtk.TreeIter it;
			if (!Widget.Model.IterNthChild (out it, row))
				return;
			Widget.Selection.SelectIter (it);
		}

		public void UnselectRow (int row)
		{
			Gtk.TreeIter it;
			if (!Widget.Model.IterNthChild (out it, row))
				return;
			Widget.Selection.UnselectIter (it);
		}

		public void ScrollToRow (int row)
		{
			Gtk.TreeIter it;
			if (!Widget.Model.IterNthChild (out it, row))
				return;
			ScrollToRow (it);
		}

		public void StartEditingCell (int row, CellView cell)
		{
			var col = GetCellColumn (cell);
			if (col != null)
				Widget.SetCursor (new Gtk.TreePath (new [] { row }), col, true);
		}

		public int[] SelectedRows {
			get {
				var sel = Widget.Selection.GetSelectedRows ();
				int[] res = new int [sel.Length];
				for (int n=0; n<sel.Length; n++)
					res [n] = sel [n].Indices[0];
				return res;
			}
		}

		public int FocusedRow {
			get {
				Gtk.TreePath path;
				Gtk.TreeViewColumn column;
				Widget.GetCursor (out path, out column);
				if (path == null)
					return -1;
				return path.Indices [0];
			}
			set {
				Gtk.TreePath path = new Gtk.TreePath (new [] { value >= 0 ? value : int.MaxValue });
				Widget.SetCursor (path, null, false);
			}
		}

		public int CurrentEventRow {
			get;
			internal set;
		}

		public bool BorderVisible {
			get {
				return ScrolledWindow.ShadowType == Gtk.ShadowType.In;
			}
			set {
				showBorder = value;
				UpdateBorder ();
			}
		}
		
		void UpdateBorder ()
		{
			var shadowType = showBorder ? Gtk.ShadowType.In : Gtk.ShadowType.None;
			if (ScrolledWindow.Child is Gtk.Viewport)
				((Gtk.Viewport) ScrolledWindow.Child).ShadowType = shadowType;
			else
				ScrolledWindow.ShadowType = shadowType;
		}
		
		public bool HeadersVisible {
			get {
				return Widget.HeadersVisible;
			}
			set {
				Widget.HeadersVisible = value;
			}
		}

		public int GetRowAtPosition (Point p)
		{
			Gtk.TreePath path = GetPathAtPosition (p);
			if (path != null)
				return path.Indices [0];
			return -1;
		}

		public Rectangle GetCellBounds (int row, CellView cell, bool includeMargin)
		{
			var col = GetCellColumn (cell);
			var cr = GetCellRenderer (cell);
			Gtk.TreePath path = new Gtk.TreePath (new [] { row });

			Gtk.TreeIter iter;
			if (!Widget.Model.GetIterFromString (out iter, path.ToString ()))
				return Rectangle.Zero;

			if (includeMargin)
				return ((ICellRendererTarget)this).GetCellBackgroundBounds (col, cr, iter);
			else
				return ((ICellRendererTarget)this).GetCellBounds (col, cr, iter);
		}

		public Rectangle GetRowBounds (int row, bool includeMargin)
		{
			Gtk.TreePath path = new Gtk.TreePath (new [] { row });
			Gtk.TreeIter iter;
			if (!Widget.Model.GetIterFromString (out iter, path.ToString ()))
				return Rectangle.Zero;

			if (includeMargin)
				return GetRowBackgroundBounds (iter);
			else
				return GetRowBounds (iter);
		}

		public override void SetCurrentEventRow (string path)
		{
			if (path.Contains (":")) {
				path = path.Split (':') [0];
			}
			CurrentEventRow = int.Parse (path);
		}
	}
}