OSDN Git Service

コンパイルエラーを修正した
[fooeditengine/FooEditEngine.git] / Core / PrintableView.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
13 namespace FooEditEngine
14 {
15     sealed class PrintableView : ViewBase
16     {
17         public PrintableView(Document doc, IPrintableTextRender r, Padding margin)
18             : base (doc,r,margin)
19         {
20         }
21
22         public string Header
23         {
24             get;
25             set;
26         }
27
28         public string Footer
29         {
30             get;
31             set;
32         }
33
34         public override void Draw(Rectangle updateRect, bool force = false)
35         {
36             if (this.LayoutLines.Count == 0)
37                 return;
38
39             if (this.Hilighter != null)
40                 this.Hilighter.Reset();
41
42             Point pos = this.render.TextArea.TopLeft;
43             pos.X -= Src.X;
44
45             int endRow = Math.Min(this.LayoutLines.Count - 1, Src.Row + this.LineCountOnScreen - 1);
46
47             Size lineNumberSize = new Size(this.render.LineNemberWidth, this.render.TextArea.Height);
48
49             IPrintableTextRender render = (IPrintableTextRender)this.render;
50
51             //ヘッダーを印刷する
52             if (this.Header != null && this.Header != string.Empty)
53             {
54                 this.render.DrawString(this.Header, pos.X, pos.Y, StringAlignment.Center,
55                     new Size(render.TextArea.Width - this.GetRealtiveX(AreaType.TextArea),render.FooterHeight));
56
57                 pos.Y += (int)render.HeaderHeight;
58             }
59
60             //レイアウト行を印刷する
61             for (int i = Src.Row; i <= endRow; i++)
62             {
63                 double lineHeight = this.LayoutLines.GetLayout(i).Height;
64
65                 this.render.DrawOneLine(this.Document, this.LayoutLines,i, pos.X + this.render.TextArea.X, pos.Y);
66
67                 if (this.Document.DrawLineNumber)
68                     this.render.DrawString((i + 1).ToString(), this.PageBound.X + this.GetRealtiveX(AreaType.LineNumberArea), pos.Y, StringAlignment.Right, lineNumberSize);
69
70                 pos.Y += lineHeight;
71             }
72
73             //フッターを印刷する
74             if (this.Footer != null && this.Footer != string.Empty)
75             {
76                 pos.Y = render.TextArea.Bottom;
77                 this.render.DrawString(this.Footer, pos.X, pos.Y, StringAlignment.Center,
78                     new Size(render.TextArea.Width - this.GetRealtiveX(AreaType.TextArea), render.FooterHeight));
79             }
80
81             return;
82         }
83
84         public bool TryPageDown()
85         {
86             return base.TryScroll(this.Src.X, this.Src.Row + this.LineCountOnScreen);
87         }
88
89         protected override void CalculateClipRect()
90         {
91             double x, y, width, height;
92
93             if (this.Document.DrawLineNumber)
94             {
95                 if (this.render.RightToLeft)
96                     x = this.Padding.Left;
97                 else
98                     x = this.render.LineNemberWidth + this.render.emSize.Width + this.Padding.Left + this.LineNumberMargin;
99                 width = this.PageBound.Width - this.render.LineNemberWidth - this.render.emSize.Width - this.Padding.Right - this.Padding.Left - this.LineNumberMargin;
100             }
101             else
102             {
103                 x = this.Padding.Left;
104                 width = this.PageBound.Width  - this.Padding.Right - this.Padding.Left;
105             }
106
107             y = this.Padding.Top;
108             height = this.PageBound.Height - this.Padding.Bottom - this.Padding.Top;
109
110             if (width < 0)
111                 width = 0;
112
113             if (height < 0)
114                 height = 0;
115
116             IPrintableTextRender render = (IPrintableTextRender)this.render;
117
118             if (this.Footer != null && this.Footer != string.Empty)
119                 height -= render.FooterHeight;
120             if (this.Header != null && this.Header != string.Empty)
121                 height -= render.HeaderHeight;
122
123             this.render.TextArea = new Rectangle(x, y, width, height);
124         }
125
126         public override void CalculateLineCountOnScreen()
127         {
128             if (this.LayoutLines.Count == 0)
129                 return;
130
131             double y = 0;
132             int i = this.Src.Row;
133             for (; true; i++)
134             {
135                 int row = i < this.LayoutLines.Count ? i : this.LayoutLines.Count - 1;
136
137                 ITextLayout layout = this.LayoutLines.GetLayout(row);
138
139                 double lineHeight = layout.Height;
140
141                 y += lineHeight;
142
143                 if (y >= this.render.TextArea.Height)
144                     break;
145             }
146             this.LineCountOnScreen = Math.Max(i - this.Src.Row - 1, 0); //ループを抜けると+1されている
147         }
148
149         enum AreaType
150         {
151             LineNumberArea,
152             TextArea
153         }
154
155         double GetRealtiveX(AreaType type)
156         {
157             switch (type)
158             {
159                 case AreaType.LineNumberArea:
160                     if (this.Document.DrawLineNumber == false)
161                         throw new InvalidOperationException();
162                     if (this.render.RightToLeft)
163                         return this.PageBound.TopRight.X - this.render.LineNemberWidth;
164                     else
165                         return this.render.TextArea.X - this.render.LineNemberWidth - this.render.emSize.Width - this.LineNumberMargin;
166                 case AreaType.TextArea:
167                     return this.render.TextArea.X;
168             }
169             throw new ArgumentOutOfRangeException();
170         }
171     }
172 }