OSDN Git Service

自動補完を行うかどうか指定できるようにした
[fooeditengine/FooEditEngine.git] / Core / ITextRender.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.Collections.Generic;
13
14 namespace FooEditEngine
15 {
16     /// <summary>
17     /// 点を表す構造体
18     /// </summary>
19     public struct Point
20     {
21         /// <summary>
22         /// X座標
23         /// </summary>
24         public double X;
25         /// <summary>
26         /// Y座標
27         /// </summary>
28         public double Y;
29         /// <summary>
30         /// コンストラクター
31         /// </summary>
32         /// <param name="x">X座標</param>
33         /// <param name="y">Y座標</param>
34         public Point(double x, double y)
35         {
36             this.X = x;
37             this.Y = y;
38         }
39         /// <summary>
40         /// 比較演算子を実装します
41         /// </summary>
42         /// <param name="a">比較される方</param>
43         /// <param name="b">比較対象</param>
44         /// <returns>条件を満たすなら真</returns>
45         public static bool operator ==(Point a, Point b)
46         {
47             return a.Equals(b);
48         }
49
50         /// <summary>
51         /// 比較演算子を実装します
52         /// </summary>
53         /// <param name="a">比較される方</param>
54         /// <param name="b">比較対象</param>
55         /// <returns>条件を満たすなら真</returns>
56         public static bool operator !=(Point a, Point b)
57         {
58             return !a.Equals(b);
59         }
60
61         /// <summary>
62         /// 一致するかどうか
63         /// </summary>
64         /// <param name="o">比較対象</param>
65         /// <returns>一致するなら真</returns>
66         public override bool Equals(object o)
67         {
68             Point b = (Point)o;
69             return this.X == b.X && this.Y == b.Y;
70         }
71
72         /// <summary>
73         /// ハッシュを返す
74         /// </summary>
75         /// <returns>ハッシュを返す</returns>
76         public override int GetHashCode()
77         {
78             int result = this.X.GetHashCode();
79             result ^= this.Y.GetHashCode();
80             return result;
81         }
82
83         /// <summary>
84         /// 一定の倍率で拡大する
85         /// </summary>
86         /// <param name="scale">倍率</param>
87         /// <returns></returns>
88         public Point Scale(double scale)
89         {
90             this.X *= scale;
91             this.Y *= scale;
92             return this;
93         }
94
95         /// <summary>
96         /// 一定方向に移動する
97         /// </summary>
98         /// <param name="x_offset">移動量X</param>
99         /// <param name="y_offset">移動量Y</param>
100         /// <returns></returns>
101         public Point Offset(double x_offset, double y_offset)
102         {
103             this.X += x_offset;
104             this.Y += y_offset;
105             return this;
106         }
107 #if WINFORM
108         public static implicit operator Point(System.Drawing.Point p)
109         {
110             return new Point(p.X, p.Y);
111         }
112         public static implicit operator System.Drawing.Point(Point p)
113         {
114             return new System.Drawing.Point((int)p.X, (int)p.Y);
115         }
116         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
117         {
118             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
119         }
120 #endif
121 #if WPF
122         public static implicit operator Point(System.Windows.Point p)
123         {
124             return new Point(p.X, p.Y);
125         }
126         public static implicit operator System.Windows.Point(Point p)
127         {
128             return new System.Windows.Point(p.X, p.Y);
129         }
130         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
131         {
132             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
133         }
134 #endif
135 #if METRO || WINDOWS_UWP
136         public static implicit operator Point(Windows.Foundation.Point p)
137         {
138             return new Point(p.X, p.Y);
139         }
140         public static implicit operator Windows.Foundation.Point(Point p)
141         {
142             return new Windows.Foundation.Point(p.X, p.Y);
143         }
144         public static implicit operator SharpDX.Mathematics.Interop.RawVector2(Point p)
145         {
146             return new SharpDX.Mathematics.Interop.RawVector2((float)p.X, (float)p.Y);
147         }
148 #endif
149     }
150     struct Size
151     {
152         public double Width;
153         public double Height;
154         public Size(double width, double height)
155         {
156             this.Width = width;
157             this.Height = height;
158         }
159
160         /// <summary>
161         /// 比較演算子を実装します
162         /// </summary>
163         /// <param name="a">比較される方</param>
164         /// <param name="b">比較対象</param>
165         /// <returns>条件を満たすなら真</returns>
166         public static bool operator ==(Size a, Size b)
167         {
168             return a.Equals(b);
169         }
170
171         /// <summary>
172         /// 比較演算子を実装します
173         /// </summary>
174         /// <param name="a">比較される方</param>
175         /// <param name="b">比較対象</param>
176         /// <returns>条件を満たすなら真</returns>
177         public static bool operator !=(Size a, Size b)
178         {
179             return !a.Equals(b);
180         }
181
182         /// <summary>
183         /// 一致するかどうか
184         /// </summary>
185         /// <param name="o">比較対象</param>
186         /// <returns>一致するなら真</returns>
187         public override bool Equals(object o)
188         {
189             Size b = (Size)o;
190             return this.Width == b.Width && this.Height == b.Height;
191         }
192
193         /// <summary>
194         /// ハッシュを返す
195         /// </summary>
196         /// <returns>ハッシュを返す</returns>
197         public override int GetHashCode()
198         {
199             int result = this.Height.GetHashCode();
200             result ^= this.Width.GetHashCode();
201             return result;
202         }
203 #if WINFORM
204         public static implicit operator Size(System.Drawing.Size p)
205         {
206             return new Size(p.Width, p.Height);
207         }
208         public static implicit operator System.Drawing.Size(Size p)
209         {
210             return new System.Drawing.Size((int)p.Width, (int)p.Height);
211         }
212 #endif
213 #if WPF
214         public static implicit operator Size(System.Windows.Size p)
215         {
216             return new Size(p.Width, p.Height);
217         }
218         public static implicit operator System.Windows.Size(Size p)
219         {
220             return new System.Windows.Size(p.Width, p.Height);
221         }
222 #endif
223 #if METRO || WINDOWS_UWP
224         public static implicit operator Size(Windows.Foundation.Size p)
225         {
226             return new Size(p.Width, p.Height);
227         }
228         public static implicit operator Windows.Foundation.Size(Size p)
229         {
230             return new Windows.Foundation.Size(p.Width, p.Height);
231         }
232 #endif
233     }
234     struct Rectangle
235     {
236         public Point Location;
237         public Size Size;
238         public Point TopLeft
239         {
240             get { return this.Location; }
241         }
242         public Point TopRight
243         {
244             get { return new Point(this.Right, this.Location.Y); }
245         }
246         public Point BottomLeft
247         {
248             get { return new Point(this.Location.X, this.Bottom); }
249         }
250         public Point BottomRight
251         {
252             get { return new Point(this.Right, this.Bottom); }
253         }
254         public double Right
255         {
256             get { return this.X + this.Width; }
257         }
258         public double Bottom
259         {
260             get { return this.Y + this.Height; }
261         }
262         public double Height
263         {
264             get { return this.Size.Height; }
265             set { this.Size.Height = value; }
266         }
267         public double Width
268         {
269             get { return this.Size.Width; }
270             set { this.Size.Width = value; }
271         }
272         public double X
273         {
274             get { return this.Location.X; }
275         }
276         public double Y
277         {
278             get { return this.Location.Y; }
279         }
280         public Rectangle(double x, double y, double width, double height)
281         {
282             this.Location = new Point(x, y);
283             this.Size = new Size(width, height);
284         }
285         public Rectangle(Point leftTop, Point bottomRight)
286         {
287             this.Location = leftTop;
288             this.Size = new Size(bottomRight.X - leftTop.X, bottomRight.Y - leftTop.Y);
289         }
290
291         /// <summary>
292         /// どの領域も指さないことを表す
293         /// </summary>
294         public static Rectangle Empty = new Rectangle(0, 0, 0, 0);
295
296         /// <summary>
297         /// 任意の点が領域内にあるなら真を返す
298         /// </summary>
299         /// <param name="p"></param>
300         /// <returns></returns>
301         public bool IsHit(Point p)
302         {
303             if (p.X >= this.TopLeft.X &&
304                 p.Y >= this.TopLeft.Y &&
305                 p.X <= this.BottomRight.X &&
306                 p.Y <= this.BottomRight.Y)
307                 return true;
308             return false;
309         }
310
311         /// <summary>
312         /// 比較演算子を実装します
313         /// </summary>
314         /// <param name="a">比較される方</param>
315         /// <param name="b">比較対象</param>
316         /// <returns>条件を満たすなら真</returns>
317         public static bool operator ==(Rectangle a, Rectangle b)
318         {
319             return a.Equals(b);
320         }
321
322         /// <summary>
323         /// 比較演算子を実装します
324         /// </summary>
325         /// <param name="a">比較される方</param>
326         /// <param name="b">比較対象</param>
327         /// <returns>条件を満たすなら真</returns>
328         public static bool operator !=(Rectangle a, Rectangle b)
329         {
330             return !a.Equals(b);
331         }
332
333         /// <summary>
334         /// 一致するかどうか
335         /// </summary>
336         /// <param name="o">比較対象</param>
337         /// <returns>一致するなら真</returns>
338         public override bool Equals(object o)
339         {
340             Rectangle b = (Rectangle)o;
341             return this.Location.Equals(b.Location) && this.Size.Equals(b.Size);
342         }
343
344         /// <summary>
345         /// ハッシュを返す
346         /// </summary>
347         /// <returns>ハッシュを返す</returns>
348         public override int GetHashCode()
349         {
350             int result = this.Location.GetHashCode();
351             result ^= this.Size.GetHashCode();
352             return result;
353         }
354 #if WINFORM
355         public static implicit operator Rectangle(System.Drawing.Rectangle p)
356         {
357             return new Rectangle(p.X,p.Y,p.Width,p.Height);
358         }
359         public static implicit operator System.Drawing.Rectangle(Rectangle p)
360         {
361             return new System.Drawing.Rectangle((int)p.X, (int)p.Y, (int)p.Width, (int)p.Height);
362         }
363         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
364         {
365             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
366         }
367 #endif
368 #if WPF
369         public static implicit operator Rectangle(System.Windows.Rect p)
370         {
371             return new Rectangle(p.X,p.Y,p.Width,p.Height);
372         }
373         public static implicit operator System.Windows.Rect(Rectangle p)
374         {
375             return new System.Windows.Rect(p.X, p.Y, p.Width, p.Height);
376         }
377         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
378         {
379             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
380         }
381 #endif
382 #if METRO || WINDOWS_UWP
383         public static implicit operator Rectangle(Windows.Foundation.Rect p)
384         {
385             return new Rectangle(p.X, p.Y, p.Width, p.Height);
386         }
387         public static implicit operator Windows.Foundation.Rect(Rectangle p)
388         {
389             return new Windows.Foundation.Rect(p.X, p.Y, p.Width, p.Height);
390         }
391
392         public static implicit operator SharpDX.Mathematics.Interop.RawRectangleF(Rectangle p)
393         {
394             return new SharpDX.Mathematics.Interop.RawRectangleF((float)p.X, (float)p.Y, (float)p.BottomRight.X, (float)p.BottomRight.Y);
395         }
396 #endif
397     }
398     /// <summary>
399     /// 色構造体
400     /// </summary>
401     public struct Color: IEqualityComparer<Color>
402     {
403         /// <summary>
404         /// アルファ成分
405         /// </summary>
406         public byte A;
407         /// <summary>
408         /// 赤成分
409         /// </summary>
410         public byte R;
411         /// <summary>
412         /// 緑成分
413         /// </summary>
414         public byte G;
415         /// <summary>
416         /// 青成分
417         /// </summary>
418         public byte B;
419
420         /// <summary>
421         /// コンストラクター
422         /// </summary>
423         /// <param name="a">A成分</param>
424         /// <param name="r">R成分</param>
425         /// <param name="g">G成分</param>
426         /// <param name="b">B成分</param>
427         public Color(byte a = 255, byte r = 0, byte g = 0, byte b = 0)
428         {
429             this.A = a;
430             this.R = r;
431             this.B = b;
432             this.G = g;
433         }
434
435         /// <summary>
436         /// 等しいかどうかを調べます
437         /// </summary>
438         /// <param name="x">比較される方</param>
439         /// <param name="y">比較する方</param>
440         /// <returns>等しいなら真。そうでなければ偽</returns>
441         public bool Equals(Color x, Color y)
442         {
443             return x.A == y.A && x.R == y.R && x.G == y.G && x.B == y.B;
444         }
445
446         /// <summary>
447         /// ハッシュを得ます
448         /// </summary>
449         /// <param name="obj">Colorオブジェクト</param>
450         /// <returns>ハッシュ</returns>
451         public int GetHashCode(Color obj)
452         {
453             return this.A ^ this.R ^ this.B ^ this.G;
454         }
455
456         /// <summary>
457         /// 一致するかどうか
458         /// </summary>
459         /// <param name="o">比較対象</param>
460         /// <returns>一致するなら真</returns>
461         public override bool Equals(object o)
462         {
463             Color b = (Color)o;
464             return this.Equals(this,b);
465         }
466
467         /// <summary>
468         /// ハッシュを返す
469         /// </summary>
470         /// <returns>ハッシュを返す</returns>
471         public override int GetHashCode()
472         {
473             return this.GetHashCode(this);
474         }
475     }
476     enum AlignDirection
477     {
478         Forward,
479         Back,
480     }
481     enum ResourceType
482     {
483         Font,
484         Brush,
485         Antialias,
486         InlineChar,
487     }
488     enum FillRectType
489     {
490         OverwriteCaret,
491         InsertCaret,
492         InsertPoint,
493         LineMarker,
494         UpdateArea,
495     }
496     enum StringColorType
497     {
498         Forground,
499         LineNumber,
500     }
501     class ChangedRenderRsourceEventArgs : EventArgs
502     {
503         public ResourceType type;
504         public ChangedRenderRsourceEventArgs(ResourceType type)
505         {
506             this.type = type;
507         }
508     }
509     delegate void ChangedRenderResourceEventHandler(object sender, ChangedRenderRsourceEventArgs e);
510     interface ITextRender
511     {
512         /// <summary>
513         /// 右から左に表示するなら真
514         /// </summary>
515         bool RightToLeft { get; set; }
516
517         /// <summary>
518         /// ドキュメントを表示する領域
519         /// </summary>
520         Rectangle TextArea { get; set; }
521
522         /// <summary>
523         /// 行番号の幅
524         /// </summary>
525         double LineNemberWidth { get; }
526
527         /// <summary>
528         /// タブの文字数
529         /// </summary>
530         int TabWidthChar { get; set; }
531
532         /// <summary>
533         /// 全角スペースを表示するかどうか
534         /// </summary>
535         bool ShowFullSpace { get; set; }
536
537         /// <summary>
538         /// 半角スペースを表示するかどうか
539         /// </summary>
540         bool ShowHalfSpace { get; set; }
541
542         /// <summary>
543         /// TABを表示するかどうか
544         /// </summary>
545         bool ShowTab { get; set; }
546
547         /// <summary>
548         /// 改行を表示するかどうか
549         /// </summary>
550         bool ShowLineBreak { get; set; }
551
552         /// <summary>
553         /// 1文字当たりの高さと幅
554         /// </summary>
555         Size emSize { get; }
556
557         /// <summary>
558         /// 保持しているリソースに変化があったことを通知する
559         /// </summary>
560         event ChangedRenderResourceEventHandler ChangedRenderResource;
561
562         /// <summary>
563         /// RightToLeftの値が変わったことを通知する
564         /// </summary>
565         event EventHandler ChangedRightToLeft;
566
567         /// <summary>
568         /// 文字列を表示する
569         /// </summary>
570         /// <param name="str">文字列</param>
571         /// <param name="x">x座標</param>
572         /// <param name="y">y座標</param>
573         /// <param name="align">書式方向</param>
574         /// <param name="layoutRect">レイアウト領域</param>
575         /// <param name="colorType">色</param>
576         void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect,StringColorType colorType = StringColorType.Forground);
577
578         /// <summary>
579         /// 行を表示する
580         /// </summary>
581         /// <param name="lti">LineToIndexオブジェクト</param>
582         /// <param name="row">行</param>
583         /// <param name="x">行の左上を表すX座標</param>
584         /// <param name="y">行の左上を表すY座標</param>
585         /// <param name="SelectRanges">選択領域を保持しているコレクション。選択領域の開始位置は行の先頭を0とする相対位置としてください(位置が-1の場合表示されません)</param>
586         void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges);
587
588         /// <summary>
589         /// 行を折り返す
590         /// </summary>
591         /// <param name="doc">ドキュメント</param>
592         /// <param name="layoutLineCollection">レイアウトライン</param>
593         /// <param name="startIndex">開始インデックス</param>
594         /// <param name="endIndex">終了インデックス</param>
595         /// <param name="wrapwidth">折り返しの幅</param>
596         /// <returns>行のリスト</returns>
597         List<LineToIndexTableData> BreakLine(Document doc,LineToIndexTable layoutLineCollection, int startIndex, int endIndex, double wrapwidth);
598
599         /// <summary>
600         /// レイアウトを生成する
601         /// </summary>
602         /// <param name="str">文字列</param>
603         /// <returns>ITextLayoutオブジェクト</returns>
604         /// <param name="syntaxCollection">ハイライト関連の情報を保持しているコレクション</param>
605         /// <param name="MarkerRanges">マーカーを保持しているコレクション。マーカーの開始位置は行の先頭を0とする相対位置としてください(位置が-1の場合表示しないこと)</param>
606         ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges);
607
608         /// <summary>
609         /// グリッパーを描く
610         /// </summary>
611         /// <param name="p">中心点</param>
612         /// <param name="radius">半径</param>
613         void DrawGripper(Point p, double radius);
614
615         /// <summary>
616         /// クリッピングを開始します
617         /// </summary>
618         /// <param name="rect">クリッピングする範囲</param>
619         void BeginClipRect(Rectangle rect);
620
621         /// <summary>
622         /// クリッピングを終了します
623         /// </summary>
624         void EndClipRect();
625     }
626     interface IEditorRender : ITextRender
627     {
628         /// <summary>
629         /// フォールティングエリアの幅
630         /// </summary>
631         double FoldingWidth { get; }
632
633         /// <summary>
634         /// キャッシュされたビットマップを描写する
635         /// </summary>
636         /// <param name="rect">描く領域</param>
637         void DrawCachedBitmap(Rectangle rect);
638
639         /// <summary>
640         /// 線を描く
641         /// </summary>
642         /// <param name="from">開始座標</param>
643         /// <param name="to">修了座標</param>
644         void DrawLine(Point from, Point to);
645
646         /// <summary>
647         /// 描写したものをキャッシュする
648         /// </summary>
649         void CacheContent();
650
651         /// <summary>
652         /// キャッシュが存在するなら真を返し、そうでないなら偽を返す
653         /// </summary>
654         bool IsVaildCache();
655
656         /// <summary>
657         /// 四角形を描く
658         /// </summary>
659         /// <param name="rect"></param>
660         /// <param name="type"></param>
661         void FillRectangle(Rectangle rect,FillRectType type);
662
663         /// <summary>
664         /// ツリーに使用するマークを描く
665         /// </summary>
666         /// <param name="expand">展開済みなら真</param>
667         /// <param name="x">x座標</param>
668         /// <param name="y">y座標</param>
669         void DrawFoldingMark(bool expand, double x, double y);
670
671         /// <summary>
672         /// 背景を塗りつぶす
673         /// </summary>
674         /// <param name="rect">塗りつぶすべき領域</param>
675         void FillBackground(Rectangle rect);
676     }
677
678     enum StringAlignment
679     {
680         Left,
681         Center,
682         Right,
683     }
684     interface IPrintableTextRender : ITextRender
685     {
686         /// <summary>
687         /// ヘッダーの高さ
688         /// </summary>
689         float HeaderHeight { get; }
690
691         /// <summary>
692         /// フッターの高さ
693         /// </summary>
694         float FooterHeight { get; }
695     }
696 }