2 * Copyright (C) 2013 FooProject
\r
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
\r
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
\r
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
\r
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
\r
13 using System.Collections.Generic;
\r
15 namespace FooEditEngine
\r
20 public static class MarkerIDs
\r
25 public static int Defalut = 0;
\r
29 public static int URL = 1;
\r
34 public enum HilightType
\r
37 /// マーカーとして表示しないことを表す
\r
77 public struct Marker : IRange, IEqualityComparer<Marker>
\r
104 public HilightType hilight;
\r
109 public Color color;
\r
114 /// <param name="start">開始インデックス</param>
\r
115 /// <param name="length">長さ</param>
\r
116 /// <param name="hilight">タイプ</param>
\r
117 /// <returns>マーカー</returns>
\r
118 public static Marker Create(int start, int length, HilightType hilight)
\r
120 return new Marker { start = start, length = length, hilight = hilight, color = new Color()};
\r
126 /// <param name="start">開始インデックス</param>
\r
127 /// <param name="length">長さ</param>
\r
128 /// <param name="hilight">タイプ</param>
\r
129 /// <param name="color">色</param>
\r
130 /// <returns>マーカー</returns>
\r
131 public static Marker Create(int start, int length, HilightType hilight,Color color)
\r
133 return new Marker { start = start, length = length, hilight = hilight ,color = color};
\r
139 /// <param name="x">比較されるマーカー</param>
\r
140 /// <param name="y">比較するマーカー</param>
\r
141 /// <returns>等しいなら真。そうでなければ偽</returns>
\r
142 public bool Equals(Marker x, Marker y)
\r
144 return x.hilight == y.hilight && x.length == y.length && x.start == y.start;
\r
150 /// <param name="obj">マーカー</param>
\r
151 /// <returns>ハッシュ</returns>
\r
152 public int GetHashCode(Marker obj)
\r
154 return this.start ^ this.length ^ (int)this.hilight;
\r
159 /// マーカークラスのコレクションを表します
\r
161 public sealed class MarkerCollection
\r
163 Dictionary<int, RangeCollection<Marker>> collection = new Dictionary<int, RangeCollection<Marker>>();
\r
165 internal MarkerCollection(Document doc)
\r
167 doc.UpdateCalledAlways += new DocumentUpdateEventHandler(doc_Update);
\r
168 this.Updated +=new EventHandler((s,e)=>{});
\r
174 public event EventHandler Updated;
\r
176 internal void Add(int id,Marker m)
\r
178 this.AddImpl(id, m);
\r
179 this.Updated(this, null);
\r
182 void AddImpl(int id, Marker m)
\r
184 RangeCollection<Marker> markers;
\r
185 if (this.collection.TryGetValue(id, out markers))
\r
187 markers.Remove(m.start, m.length);
\r
192 markers = new RangeCollection<Marker>();
\r
194 this.collection.Add(id, markers);
\r
198 internal void AddRange(int id, IEnumerable<Marker> collection)
\r
200 foreach (Marker m in collection)
\r
201 this.AddImpl(id, m);
\r
202 this.Updated(this, null);
\r
205 internal void RemoveAll(int id,int start, int length)
\r
207 RangeCollection<Marker> markers;
\r
208 if (this.collection.TryGetValue(id, out markers))
\r
210 markers.Remove(start, length);
\r
212 this.Updated(this, null);
\r
215 internal void RemoveAll(int id, HilightType type)
\r
217 RangeCollection<Marker> markers;
\r
218 if (this.collection.TryGetValue(id, out markers))
\r
220 for (int i = 0; i < markers.Count; i++)
\r
222 if (markers[i].hilight == type)
\r
223 markers.RemoveAt(i);
\r
226 this.Updated(this, null);
\r
229 internal IEnumerable<int> IDs
\r
233 return this.collection.Keys;
\r
237 internal IEnumerable<Marker> Get(int id)
\r
239 RangeCollection<Marker> markers;
\r
240 if (this.collection.TryGetValue(id, out markers))
\r
242 foreach (var m in markers)
\r
248 internal IEnumerable<Marker> Get(int id, int index)
\r
250 RangeCollection<Marker> markers;
\r
251 if (this.collection.TryGetValue(id, out markers))
\r
253 foreach (var m in markers.Get(index))
\r
259 internal IEnumerable<Marker> Get(int id, int index, int length)
\r
261 RangeCollection<Marker> markers;
\r
262 if (this.collection.TryGetValue(id, out markers))
\r
264 foreach (var m in markers.Get(index, length))
\r
273 /// <param name="id">マーカーID</param>
\r
274 public void Clear(int id)
\r
276 RangeCollection<Marker> markers;
\r
277 if (this.collection.TryGetValue(id, out markers))
\r
279 this.Updated(this, null);
\r
285 public void Clear()
\r
287 this.collection.Clear();
\r
288 this.Updated(this, null);
\r
291 void doc_Update(object sender, DocumentUpdateEventArgs e)
\r
293 int deltaLength = 0;
\r
296 case UpdateType.Replace:
\r
297 deltaLength = e.insertLength - e.removeLength;
\r
298 foreach (RangeCollection<Marker> markers in this.collection.Values)
\r
300 for (int i = 0; i < markers.Count; i++)
\r
302 Marker m = markers[i];
\r
303 if (m.start + m.length - 1 < e.startIndex)
\r
309 m.start += deltaLength;
\r