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

MonodocDataSources.cs - github.com/xamarin/macdoc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3dd6235baf82e584f9a1330036073bb595e8b06a (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
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Linq;
using Monodoc;
using AppKit;
using Foundation;
using WebKit;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CoreGraphics;

namespace macdoc
{
	class LinkPageVisit : PageVisit {
		MyDocument document;
		string url;
		
		public LinkPageVisit (MyDocument document, string url)
		{
			this.document = document;
			this.url = url;
		}
		
		public override void Go ()
		{
			document.LoadUrl (url, true, null, false);
		}
	}
	
	// 
	// The OutlineView works by passing NSObject tokens around for its data.
	// We conveniently have a Node structure from MonoDoc that we can use
	// so we just wrap that in this WrapNode class
	//
	public class WrapNode : NSObject {
		public WrapNode (Node n) { Node = n; }
		public Node Node { get; set; }
		public static Node FromObject (NSObject obj)
		{
			return (obj as WrapNode).Node;
		}
	}
	
	//
	// Data source for rendering the tree 
	//
	public class DocTreeDataSource : NSOutlineViewDataSource {
		RootTree Root = AppDelegate.Root;
			
		// We need to keep all objects that we have ever handed out to the Outline View around
		Dictionary<Node,WrapNode> nodeToWrapper;
		
		static Node GetNode (NSObject obj)
		{
			return WrapNode.FromObject (obj);
		}
		
		public DocTreeDataSource (MyDocument parent)
		{
			nodeToWrapper = parent.nodeToWrapper;
		}
		
		public override NSObject GetChild (NSOutlineView outlineView, nint index, NSObject item)
		{
			WrapNode wrap;
			Node n = (Node) (item == null ? Root.RootNode : (Node) GetNode (item)).ChildNodes [(int)index];

			if (nodeToWrapper.ContainsKey (n))
				return nodeToWrapper [n];
			wrap = new WrapNode (n);
			nodeToWrapper [n] = wrap;
			return wrap;
		}
		
		public override bool ItemExpandable (NSOutlineView outlineView, NSObject item)
		{
			if (item == null)
				return true;
			return GetNode (item).ChildNodes.Count > 0;
		}
		
		public override nint GetChildrenCount (NSOutlineView outlineView, NSObject item)
		{
			if (item == null)
				return Root.RootNode.ChildNodes.Count;
			return GetNode (item).ChildNodes.Count;
		}
		
		public override NSObject GetObjectValue (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
		{
			if (item == null)
				return new NSString ("Root");
			
			return new NSString (GetNode (item).Caption);
		}
	}
	
	//
	// Data source for rendering the index
	//
	public class IndexDataSource : NSTableViewSource {
		IndexSearcher searcher;
		
		public IndexDataSource (IndexSearcher searcher)
		{
			this.searcher = searcher;
		}
		
		public override nint GetRowCount (NSTableView tableView)
		{
			if (searcher.Index == null)
				return 0;
			return searcher.Index.Rows;
		}
		
		public override NSObject GetObjectValue (NSTableView tableView, NSTableColumn tableColumn, nint row)
		{
			return new NSString (searcher.Index.GetValue ((int)row));
		}
	}
	
	public class ResultDataSource : NSTableViewSource
	{
		struct ResultDataEntry
		{
			// value is element is a section, null if it's a section child
			public string SectionName { get; set; }
			public Result ResultSet { get; set; }
			public int Index { get; set; }
		}
		
		// Dict key is section name, value is a sorted list of section element (result it comes from and the index in it) with key being the url (to avoid duplicates)
		Dictionary<string, SortedList<string, Tuple<Result, int>>> sections = new Dictionary<string, SortedList<string, Tuple<Result, int>>> ();
		List<ResultDataEntry> data = new List<ResultDataEntry> ();
		
		NSTextFieldCell normalCell;
		NSTableHeaderCell headerCell;
		
		public ResultDataSource ()
		{
			normalCell = new NSTextFieldCell ();
			
			headerCell = new NSTableHeaderCell ();
			headerCell.LineBreakMode = NSLineBreakMode.TruncatingMiddle;
			headerCell.FocusRingType = NSFocusRingType.None;
			headerCell.Editable = false;
			headerCell.Selectable = false;
		}
		
		public void AddResultSet (Result result)
		{
			for (int i = 0; i < result.Count; i++) {
				string fullTitle = result.GetFullTitle (i);
				string section = string.IsNullOrWhiteSpace (fullTitle) ? "Other" : fullTitle.Split (':')[0];
				SortedList<string, Tuple<Result, int>> sectionContent;
				var newItem = Tuple.Create (result, i);
				var url = result.GetUrl (i);
				
				if (!sections.TryGetValue (section, out sectionContent))
					sections[section] = new SortedList<string, Tuple<Result, int>> () { { url, newItem } };
				else
					sectionContent[url] = newItem;
			}
			// Flatten everything back to a list
			data.Clear ();
			foreach (var kvp in sections) {
				data.Add (new ResultDataEntry { SectionName = kvp.Key });
				foreach (var item in kvp.Value)
					data.Add (new ResultDataEntry { ResultSet = item.Value.Item1, Index = item.Value.Item2 });
			}
		}
		
		public void ClearResultSet ()
		{
			sections.Clear ();
		}
		
		public override nint GetRowCount (NSTableView tableView)
		{
			return data.Count;
		}
		
		public override NSCell GetCell (NSTableView tableView, NSTableColumn tableColumn, nint row)
		{
			if (tableView == null)
				return null;
			var resultEntry = data[(int)row];
			return !string.IsNullOrEmpty (resultEntry.SectionName) ? headerCell : normalCell;
		}
		
		public string GetResultUrl (int row)
		{
			if (data == null || row >= data.Count || row < 0)
				return null;
			
			var resultEntry = data[row];
			return resultEntry.ResultSet == null ? null : resultEntry.ResultSet.GetUrl (resultEntry.Index);
		}
		
		public override NSObject GetObjectValue (NSTableView tableView, NSTableColumn tableColumn, nint row)
		{
			var resultEntry = data[(int)row];
			return new NSString (!string.IsNullOrEmpty (resultEntry.SectionName) ? resultEntry.SectionName : resultEntry.ResultSet.GetTitle (resultEntry.Index));
		}
		
		public override bool ShouldSelectRow (NSTableView tableView, nint row)
		{
			// If it's a section, do not select
			return string.IsNullOrEmpty (data[(int)row].SectionName);
		}
		
		// Keep the search term in memory so that heavy search can check if its result are still fresh enough
		public string LatestSearchTerm { get; set; }
	}
}