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/>.
12 using System.Threading.Tasks;
13 using System.Printing;
15 using System.Windows.Xps;
16 using Shapes = System.Windows.Shapes;
17 using System.Collections.Generic;
18 using System.Windows.Controls;
19 using System.Windows.Documents;
20 using System.Windows.Documents.Serialization;
21 using System.Windows.Media;
23 namespace FooEditEngine.WPF
28 public sealed class ParseCommandEventArgs
33 public int PageNumber;
35 /// ページ範囲内で許容されている最大の番号
37 public int MaxPageNumber;
41 public string Original;
45 /// <param name="nowPage">印刷中のページ番号</param>
46 /// <param name="maxPage">印刷すべき最大のページ番号</param>
47 /// <param name="org">処理前の文字列</param>
48 public ParseCommandEventArgs(int nowPage,int maxPage,string org)
50 this.PageNumber = nowPage;
51 this.MaxPageNumber = maxPage;
59 /// <param name="sender">送信元のクラス</param>
60 /// <param name="e">イベントデータ</param>
61 /// <returns>処理後の文字列</returns>
62 public delegate string ParseCommandHandler(object sender,ParseCommandEventArgs e);
67 public class FooPrintText
74 this.ParseHF = new ParseCommandHandler((s, e) => { return e.Original; });
98 public System.Windows.Rect PageRect
107 public Document Document
116 public FlowDirection FlowDirection
125 public bool DrawLineNumber
143 public System.Windows.Media.Color Foreground
152 public System.Windows.Media.Color URL
161 public System.Windows.Media.Color Keyword1
170 public System.Windows.Media.Color Keyword2
179 public System.Windows.Media.Color Comment
188 public System.Windows.Media.Color Litral
197 public FontFamily Font
206 public double FontSize
215 public LineBreakMethod LineBreakMethod
224 public int LineBreakCharCount
251 public IHilighter Hilighter
260 public Padding Padding
269 public ParseCommandHandler ParseHF;
274 /// <param name="pd">プリントダイアログ</param>
275 public void Print(PrintDialog pd)
277 if (this.Font == null || this.Document == null)
278 throw new InvalidOperationException();
280 WPFRender render = new WPFRender(this.Font, this.FontSize);
281 render.Foreground = this.Foreground;
282 render.Comment = this.Comment;
283 render.Keyword1 = this.Keyword1;
284 render.Keyword2 = this.Keyword2;
285 render.Literal = this.Litral;
286 render.Url = this.URL;
287 render.RightToLeft = this.FlowDirection == System.Windows.FlowDirection.RightToLeft;
288 render.Printing = true;
289 Document documentSnap = new Document(this.Document);
290 documentSnap.LayoutLines.Render = render;
291 PrintableView view = new PrintableView(documentSnap, render,this.Padding);
292 view.Header = this.Header;
293 view.Footer = this.Footer;
294 view.PageBound = this.PageRect;
295 view.Hilighter = this.Hilighter;
296 documentSnap.LineBreak = this.LineBreakMethod;
297 documentSnap.LineBreakCharCount = this.LineBreakCharCount;
298 documentSnap.DrawLineNumber = this.DrawLineNumber;
299 documentSnap.UrlMark = this.MarkURL;
300 view.PerfomLayouts();
304 FixedDocument fd = new FixedDocument();
305 fd.DocumentPaginator.PageSize = this.PageRect.Size;
313 if (this.EndPage != -1 && currentPage > this.EndPage)
316 if (this.StartPage == -1 || currentPage >= this.StartPage)
318 PageContent pc = new PageContent();
320 FixedPage fp = new FixedPage();
321 fp.Width = this.PageRect.Width;
322 fp.Height = this.PageRect.Height;
326 view.Header = this.ParseHF(this, new ParseCommandEventArgs(currentPage, this.EndPage, this.Header));
327 view.Footer = this.ParseHF(this, new ParseCommandEventArgs(currentPage, this.EndPage, this.Footer));
329 DrawingVisual dv = new DrawingVisual();
331 using (DrawingContext dc = dv.RenderOpen())
333 render.SetDrawingContext(dc);
334 view.Draw(view.PageBound);
337 VisualHost host = new VisualHost();
340 fp.Children.Add(host);
344 result = view.TryPageDown();
348 pd.PrintDocument(fd.DocumentPaginator,"");
350 catch (PrintingCanceledException)
360 class VisualHost : FrameworkElement
362 private List<Visual> fVisuals;
366 fVisuals = new List<Visual>();
369 protected override Visual GetVisualChild(int index)
371 return fVisuals[index];
374 protected override int VisualChildrenCount
376 get { return fVisuals.Count; }
379 public void AddVisual(Visual visual)
381 fVisuals.Add(visual);
382 base.AddVisualChild(visual);
385 public void RemoveVisual(Visual visual)
387 fVisuals.Remove(visual);
388 base.RemoveVisualChild(visual);