OSDN Git Service

コンパイルエラーを修正した
[fooeditengine/FooEditEngine.git] / Core / MarkerCollection.cs
1 /*
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.
5
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.
8
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/>.
10  */
11 using System;
12 using System.Linq;
13 using System.Collections.Generic;
14
15 namespace FooEditEngine
16 {
17     /// <summary>
18     /// 既定のIDリスト
19     /// </summary>
20     public static class MarkerIDs
21     {
22         /// <summary>
23         /// デフォルトIDを表す
24         /// </summary>
25         public static int Defalut = 0;
26         /// <summary>
27         /// URLを表す
28         /// </summary>
29         public static int URL = 1;
30         /// <summary>
31         /// IMEの変換候補を表す
32         /// </summary>
33         public static int IME = -1;
34     }
35     /// <summary>
36     /// マーカーのタイプを表す列挙体
37     /// </summary>
38     public enum HilightType
39     {
40         /// <summary>
41         /// マーカーとして表示しないことを表す
42         /// </summary>
43         None,
44         /// <summary>
45         /// 選択状態を表す
46         /// </summary>
47         Select,
48         /// <summary>
49         /// URLを表す
50         /// </summary>
51         Url,
52         /// <summary>
53         /// 実線を表す
54         /// </summary>
55         Sold,
56         /// <summary>
57         /// 破線を表す
58         /// </summary>
59         Dash,
60         /// <summary>
61         /// 一点鎖線を表す
62         /// </summary>
63         DashDot,
64         /// <summary>
65         /// 二点鎖線を表す
66         /// </summary>
67         DashDotDot,
68         /// <summary>
69         /// 点線を表す
70         /// </summary>
71         Dot,
72         /// <summary>
73         /// 波線を表す
74         /// </summary>
75         Squiggle,
76     }
77
78     /// <summary>
79     /// マーカー自身を表します
80     /// </summary>
81     public struct Marker : IRange, IEqualityComparer<Marker>
82     {
83         #region IRange メンバー
84
85         /// <summary>
86         /// 開始位置
87         /// </summary>
88         public int start
89         {
90             get;
91             set;
92         }
93
94         /// <summary>
95         /// 長さ
96         /// </summary>
97         public int length
98         {
99             get;
100             set;
101         }
102
103         #endregion
104
105         /// <summary>
106         /// マーカーのタイプ
107         /// </summary>
108         public HilightType hilight;
109
110         /// <summary>
111         /// 色を指定する
112         /// </summary>
113         public Color color;
114
115         /// <summary>
116         /// 線を太くするかどうか
117         /// </summary>
118         public bool isBoldLine;
119
120         /// <summary>
121         /// マーカーを作成します
122         /// </summary>
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)
128         {
129             return new Marker { start = start, length = length, hilight = hilight, color = new Color(), isBoldLine = false};
130         }
131
132         /// <summary>
133         /// マーカーを作成します
134         /// </summary>
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)
142         {
143             return new Marker { start = start, length = length, hilight = hilight ,color = color , isBoldLine = isBoldLine };
144         }
145
146         /// <summary>
147         /// 等しいかどうかを調べます
148         /// </summary>
149         /// <param name="x">比較されるマーカー</param>
150         /// <param name="y">比較するマーカー</param>
151         /// <returns>等しいなら真。そうでなければ偽</returns>
152         public bool Equals(Marker x, Marker y)
153         {
154             return x.hilight == y.hilight && x.length == y.length && x.start == y.start;
155         }
156
157         /// <summary>
158         /// ハッシュを得ます
159         /// </summary>
160         /// <param name="obj">マーカー</param>
161         /// <returns>ハッシュ</returns>
162         public int GetHashCode(Marker obj)
163         {
164             return this.start ^ this.length ^ (int)this.hilight;
165         }
166     }
167
168     /// <summary>
169     /// マーカークラスのコレクションを表します
170     /// </summary>
171     public sealed class MarkerCollection
172     {
173         Dictionary<int, RangeCollection<Marker>> collection = new Dictionary<int, RangeCollection<Marker>>();
174
175         internal MarkerCollection()
176         {
177             this.Updated +=new EventHandler((s,e)=>{});
178         }
179
180         /// <summary>
181         /// 更新されたことを通知します
182         /// </summary>
183         public event EventHandler Updated;
184
185         internal void Add(int id,Marker m)
186         {
187             this.AddImpl(id, m);
188             this.Updated(this, null);
189         }
190
191         void AddImpl(int id, Marker m)
192         {
193             RangeCollection<Marker> markers;
194             if (this.collection.TryGetValue(id, out markers))
195             {
196                 markers.Remove(m.start, m.length);
197                 markers.Add(m);
198             }
199             else
200             {
201                 markers = new RangeCollection<Marker>();
202                 markers.Add(m);
203                 this.collection.Add(id, markers);
204             }
205         }
206
207         internal void AddRange(int id, IEnumerable<Marker> collection)
208         {
209             foreach (Marker m in collection)
210                 this.AddImpl(id, m);
211             this.Updated(this, null);
212         }
213
214         internal void RemoveAll(int id)
215         {
216             RangeCollection<Marker> markers;
217             if (this.collection.TryGetValue(id, out markers))
218             {
219                 markers.Clear();
220             }
221             this.Updated(this, null);
222         }
223
224         internal void RemoveAll(int id,int start, int length)
225         {
226             RangeCollection<Marker> markers;
227             if (this.collection.TryGetValue(id, out markers))
228             {
229                 markers.Remove(start, length);
230             }
231             this.Updated(this, null);
232         }
233
234         internal void RemoveAll(int id, HilightType type)
235         {
236             RangeCollection<Marker> markers;
237             if (this.collection.TryGetValue(id, out markers))
238             {
239                 for (int i = 0; i < markers.Count; i++)
240                 {
241                     if (markers[i].hilight == type)
242                         markers.RemoveAt(i);
243                 }
244             }
245             this.Updated(this, null);
246         }
247
248         internal IEnumerable<int> IDs
249         {
250             get
251             {
252                 return this.collection.Keys;
253             }
254         }
255
256         internal IEnumerable<Marker> Get(int id)
257         {
258             RangeCollection<Marker> markers;
259             if (this.collection.TryGetValue(id, out markers))
260             {
261                 foreach (var m in markers)
262                     yield return m;
263             }
264             yield break;
265         }
266
267         internal IEnumerable<Marker> Get(int id, int index)
268         {
269             RangeCollection<Marker> markers;
270             if (this.collection.TryGetValue(id, out markers))
271             {
272                 foreach (var m in markers.Get(index))
273                     yield return m;
274             }
275             yield break;
276         }
277
278         internal IEnumerable<Marker> Get(int id, int index, int length)
279         {
280             RangeCollection<Marker> markers;
281             if (this.collection.TryGetValue(id, out markers))
282             {
283                 foreach (var m in markers.Get(index, length))
284                     yield return m;
285             }
286             yield break;
287         }
288
289         /// <summary>
290         /// マーカーをすべて削除します
291         /// </summary>
292         /// <param name="id">マーカーID</param>
293         public void Clear(int id)
294         {
295             RangeCollection<Marker> markers;
296             if (this.collection.TryGetValue(id, out markers))
297                 markers.Clear();
298             this.Updated(this, null);
299         }
300
301         /// <summary>
302         /// マーカーをすべて削除します
303         /// </summary>
304         public void Clear()
305         {
306             this.collection.Clear();
307             this.Updated(this, null);
308         }
309
310         internal void UpdateMarkers(int startIndex,int insertLength,int removeLength)
311         {
312             int deltaLength = insertLength - removeLength;
313             foreach (RangeCollection<Marker> markers in this.collection.Values)
314             {
315                 for (int i = 0; i < markers.Count; i++)
316                 {
317                     Marker m = markers[i];
318                     if (m.start + m.length - 1 < startIndex)
319                     {
320                         continue;
321                     }
322                     else
323                     {
324                         m.start += deltaLength;
325                     }
326                     markers[i] = m;
327                 }
328             }
329         }
330
331     }
332 }