OSDN Git Service

互換性のないメソッドをいつか廃止した
[fooeditengine/FooEditEngine.git] / Windows / FooEditEngine / FooPrintText.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.Drawing;
13 using System.Drawing.Printing;
14
15 namespace FooEditEngine.Windows
16 {
17     /// <summary>
18     /// イベントデータ
19     /// </summary>
20     public class ParseCommandEventArgs
21     {
22         /// <summary>
23         /// ページ番号
24         /// </summary>
25         public int PageNumber;
26         /// <summary>
27         /// プリンターの設定
28         /// </summary>
29         public PrinterSettings PrinterSetting;
30         /// <summary>
31         /// 処理前の文字列
32         /// </summary>
33         public string Original;
34         /// <summary>
35         /// コンストラクター
36         /// </summary>
37         /// <param name="nowPage">印刷中のページ番号</param>
38         /// <param name="setting">プリンターの設定</param>
39         /// <param name="org">処理前の文字列</param>
40         public ParseCommandEventArgs(int nowPage,PrinterSettings setting,string org)
41         {
42             this.PageNumber = nowPage;
43             this.PrinterSetting = setting;
44             this.Original = org;
45         }
46     }
47
48     /// <summary>
49     /// コマンド処理用デリゲート
50     /// </summary>
51     /// <param name="sender">送信元のクラス</param>
52     /// <param name="e">イベントデータ</param>
53     /// <returns>処理後の文字列</returns>
54     public delegate string ParseCommandHandler(object sender,ParseCommandEventArgs e);
55
56     /// <summary>
57     /// 印刷用のクラス
58     /// </summary>
59     public class FooPrintText
60     {
61         PrintableView view;
62         PrintableTextRender render;
63         int PageNumber;
64
65         /// <summary>
66         /// コンストラクター
67         /// </summary>
68         public FooPrintText()
69         {
70             this.PrintDocument = new PrintDocument();
71             this.PrintDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
72             this.PrintDocument.EndPrint += new PrintEventHandler(PrintDocument_EndPrint);
73             this.ParseHF = new ParseCommandHandler((s, e) => { return e.Original; });
74         }
75
76         /// <summary>
77         /// 対象となるドキュメント
78         /// </summary>
79         public Document Document
80         {
81             get;
82             set;
83         }
84
85         /// <summary>
86         /// プリントドキュメント
87         /// </summary>
88         public PrintDocument PrintDocument
89         {
90             get;
91             private set;
92         }
93
94         /// <summary>
95         /// 右から左に表示するなら真
96         /// </summary>
97         public bool RightToLeft
98         {
99             get;
100             set;
101         }
102
103         /// <summary>
104         /// 行番号を表示するかどうか
105         /// </summary>
106         public bool DrawLineNumber
107         {
108             get;
109             set;
110         }
111
112         /// <summary>
113         /// 印刷に使用するフォント
114         /// </summary>
115         public Font Font
116         {
117             get;
118             set;
119         }
120
121         /// <summary>
122         /// 折り返しの方法を指定する
123         /// </summary>
124         public LineBreakMethod LineBreakMethod
125         {
126             get;
127             set;
128         }
129
130         /// <summary>
131         /// 折り返した時の文字数を指定する
132         /// </summary>
133         public int LineBreakCharCount
134         {
135             get;
136             set;
137         }
138
139         /// <summary>
140         /// ヘッダー
141         /// </summary>
142         public string Header
143         {
144             get;
145             set;
146         }
147
148         /// <summary>
149         /// フッター
150         /// </summary>
151         public string Footer
152         {
153             get;
154             set;
155         }
156
157         /// <summary>
158         /// 余白
159         /// </summary>
160         public Padding Padding
161         {
162             get;
163             set;
164         }
165
166         /// <summary>
167         /// 前景色
168         /// </summary>
169         public System.Drawing.Color Foreground
170         {
171             get;
172             set;
173         }
174
175         /// <summary>
176         /// ヘッダーやフッターを処理する
177         /// </summary>
178         public ParseCommandHandler ParseHF;
179
180         void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
181         {
182             if (this.Font == null || this.Document == null)
183                 throw new InvalidOperationException();
184
185             if (view == null)
186             {
187                 this.render = new PrintableTextRender(this.Font, e.Graphics);
188                 this.render.Foreground = this.Foreground;
189                 this.render.RightToLeft = this.RightToLeft;
190                 Document documentSnap = new Document(this.Document);
191                 documentSnap.LayoutLines.Render = render;
192                 this.view = new PrintableView(documentSnap, this.render,this.Padding);
193                 this.view.PageBound = e.MarginBounds;
194                 this.PageNumber = 1;
195                 documentSnap.LineBreak = this.LineBreakMethod;
196                 documentSnap.LineBreakCharCount = this.LineBreakCharCount;
197                 documentSnap.DrawLineNumber = this.DrawLineNumber;
198                 documentSnap.UrlMark = this.Document.UrlMark;
199                 documentSnap.PerformLayout(false);
200             }
201
202             if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages)
203             {
204                 for (; this.PageNumber < e.PageSettings.PrinterSettings.FromPage; this.PageNumber++)
205                 {
206                     if (this.view.TryPageDown())
207                         return;
208                 }
209             }
210
211             this.view.Header = this.ParseHF(this, new ParseCommandEventArgs(this.PageNumber, e.PageSettings.PrinterSettings,this.Header));
212             this.view.Footer = this.ParseHF(this, new ParseCommandEventArgs(this.PageNumber, e.PageSettings.PrinterSettings, this.Footer));
213
214             this.render.BeginDraw(e.Graphics);
215
216             this.view.Draw(e.MarginBounds);
217
218             e.HasMorePages = !this.view.TryPageDown();
219
220             this.render.EndDraw();
221
222             this.PageNumber++;
223
224             if (e.HasMorePages && e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && this.PageNumber > e.PageSettings.PrinterSettings.ToPage)
225                 e.HasMorePages = false;
226         }
227
228         void PrintDocument_EndPrint(object sender, PrintEventArgs e)
229         {
230             this.view.Dispose();
231             this.view = null;
232             this.render = null;
233         }
234
235     }
236 }