2 * Copyright (C) 2013 FooProject
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
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
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/>.
13 using System.Collections.Generic;
15 namespace FooEditEngine
20 public static class MarkerIDs
25 public static int Defalut = 0;
29 public static int URL = 1;
33 public static int IME = -1;
38 public enum HilightType
81 public struct Marker : IRange, IEqualityComparer<Marker>
108 public HilightType hilight;
118 public bool isBoldLine;
123 /// <param name="start">開始インデックス</param>
124 /// <param name="length">長さ</param>
125 /// <param name="hilight">タイプ</param>
126 /// <returns>マーカー</returns>
127 public static Marker Create(int start, int length, HilightType hilight)
129 return new Marker { start = start, length = length, hilight = hilight, color = new Color(), isBoldLine = false};
135 /// <param name="start">開始インデックス</param>
136 /// <param name="length">長さ</param>
137 /// <param name="hilight">タイプ</param>
138 /// <param name="color">色</param>
139 /// <param name="isBoldLine">線を太くするかどうか</param>
140 /// <returns>マーカー</returns>
141 public static Marker Create(int start, int length, HilightType hilight,Color color,bool isBoldLine = false)
143 return new Marker { start = start, length = length, hilight = hilight ,color = color , isBoldLine = isBoldLine };
149 /// <param name="x">比較されるマーカー</param>
150 /// <param name="y">比較するマーカー</param>
151 /// <returns>等しいなら真。そうでなければ偽</returns>
152 public bool Equals(Marker x, Marker y)
154 return x.hilight == y.hilight && x.length == y.length && x.start == y.start;
160 /// <param name="obj">マーカー</param>
161 /// <returns>ハッシュ</returns>
162 public int GetHashCode(Marker obj)
164 return this.start ^ this.length ^ (int)this.hilight;
169 /// マーカークラスのコレクションを表します
171 public sealed class MarkerCollection
173 Dictionary<int, RangeCollection<Marker>> collection = new Dictionary<int, RangeCollection<Marker>>();
175 internal MarkerCollection()
177 this.Updated +=new EventHandler((s,e)=>{});
183 public event EventHandler Updated;
185 internal void Add(int id,Marker m)
188 this.Updated(this, null);
191 void AddImpl(int id, Marker m)
193 RangeCollection<Marker> markers;
194 if (this.collection.TryGetValue(id, out markers))
196 markers.Remove(m.start, m.length);
201 markers = new RangeCollection<Marker>();
203 this.collection.Add(id, markers);
207 internal void AddRange(int id, IEnumerable<Marker> collection)
209 foreach (Marker m in collection)
211 this.Updated(this, null);
214 internal void RemoveAll(int id)
216 RangeCollection<Marker> markers;
217 if (this.collection.TryGetValue(id, out markers))
221 this.Updated(this, null);
224 internal void RemoveAll(int id,int start, int length)
226 RangeCollection<Marker> markers;
227 if (this.collection.TryGetValue(id, out markers))
229 markers.Remove(start, length);
231 this.Updated(this, null);
234 internal void RemoveAll(int id, HilightType type)
236 RangeCollection<Marker> markers;
237 if (this.collection.TryGetValue(id, out markers))
239 for (int i = 0; i < markers.Count; i++)
241 if (markers[i].hilight == type)
245 this.Updated(this, null);
248 internal IEnumerable<int> IDs
252 return this.collection.Keys;
256 internal IEnumerable<Marker> Get(int id)
258 RangeCollection<Marker> markers;
259 if (this.collection.TryGetValue(id, out markers))
261 foreach (var m in markers)
267 internal IEnumerable<Marker> Get(int id, int index)
269 RangeCollection<Marker> markers;
270 if (this.collection.TryGetValue(id, out markers))
272 foreach (var m in markers.Get(index))
278 internal IEnumerable<Marker> Get(int id, int index, int length)
280 RangeCollection<Marker> markers;
281 if (this.collection.TryGetValue(id, out markers))
283 foreach (var m in markers.Get(index, length))
292 /// <param name="id">マーカーID</param>
293 public void Clear(int id)
295 RangeCollection<Marker> markers;
296 if (this.collection.TryGetValue(id, out markers))
298 this.Updated(this, null);
306 this.collection.Clear();
307 this.Updated(this, null);
310 internal void UpdateMarkers(int startIndex,int insertLength,int removeLength)
312 int deltaLength = insertLength - removeLength;
313 foreach (RangeCollection<Marker> markers in this.collection.Values)
315 for (int i = 0; i < markers.Count; i++)
317 Marker m = markers[i];
318 if (m.start + m.length - 1 < startIndex)
324 m.start += deltaLength;