1 /* Printing
2  *
3  * GtkPrintOperation offers a simple API to support printing in a cross-platform way.
4  */
5 
6 using System;
7 using System.IO;
8 using System.Reflection;
9 using Gtk;
10 using Cairo;
11 
12 namespace GtkDemo
13 {
14 	[Demo ("Printing", "DemoPrinting.cs")]
15         public class DemoPrinting
16         {
17 		private static double headerHeight = (10*72/25.4);
18 		private static double headerGap = (3*72/25.4);
19 		private static int pangoScale = 1024;
20 
21 		private PrintOperation print;
22 
23 		private string fileName = "DemoPrinting.cs";
24 		private double fontSize = 12.0;
25 		private int linesPerPage;
26 		private string[] lines;
27 		private int numLines;
28 		private int numPages;
29 
DemoPrinting()30                 public DemoPrinting ()
31                 {
32 			print = new PrintOperation ();
33 
34 			print.BeginPrint += new BeginPrintHandler (OnBeginPrint);
35 			print.DrawPage += new DrawPageHandler (OnDrawPage);
36 			print.EndPrint += new EndPrintHandler (OnEndPrint);
37 
38 			print.Run (PrintOperationAction.PrintDialog, null);
39 		}
40 
OnBeginPrint(object obj, Gtk.BeginPrintArgs args)41 		private void OnBeginPrint (object obj, Gtk.BeginPrintArgs args)
42 		{
43 			string contents;
44 			double height;
45 
46 			PrintContext context = args.Context;
47 			height = context.Height;
48 
49 			linesPerPage = (int)Math.Floor(height / fontSize);
50 			contents = LoadFile("DemoPrinting.cs");
51 
52 			lines = contents.Split('\n');
53 
54 			numLines = lines.Length;
55 			numPages = (numLines - 1) / linesPerPage + 1;
56 
57 			print.NPages = numPages;
58 		}
59 
LoadFile(string filename)60 		private string LoadFile (string filename)
61 		{
62 			Stream file = Assembly.GetExecutingAssembly ().GetManifestResourceStream
63 (filename);
64                         if (file == null && File.Exists (filename)) {
65                                 file = File.OpenRead (filename);
66                         }
67 			if (file == null) {
68 				return "File not found";
69 			}
70 
71 			StreamReader sr = new StreamReader (file);
72 			return sr.ReadToEnd ();
73 		}
74 
OnDrawPage(object obj, Gtk.DrawPageArgs args)75 		private void OnDrawPage (object obj, Gtk.DrawPageArgs args)
76 		{
77 			PrintContext context = args.Context;
78 
79 			Cairo.Context cr = context.CairoContext;
80 			double width = context.Width;
81 
82 			cr.Rectangle (0, 0, width, headerHeight);
83 			cr.SetSourceRGB (0.8, 0.8, 0.8);
84 			cr.FillPreserve ();
85 
86 			cr.SetSourceRGB (0, 0, 0);
87 			cr.LineWidth = 1;
88 			cr.Stroke();
89 
90 			Pango.Layout layout = context.CreatePangoLayout ();
91 
92 			Pango.FontDescription desc = Pango.FontDescription.FromString ("sans 14");
93 			layout.FontDescription = desc;
94 
95 			layout.SetText (fileName);
96 			layout.Width = (int)width;
97 			layout.Alignment = Pango.Alignment.Center;
98 
99 			int layoutWidth, layoutHeight;
100 			layout.GetSize (out layoutWidth, out layoutHeight);
101 			double textHeight = (double)layoutHeight / (double)pangoScale;
102 
103 			cr.MoveTo (width/2, (headerHeight - textHeight) / 2);
104 			Pango.CairoHelper.ShowLayout (cr, layout);
105 
106 			string pageStr = String.Format ("{0}/{1}", args.PageNr + 1, numPages);
107 			layout.SetText (pageStr);
108 			layout.Alignment = Pango.Alignment.Right;
109 
110 			cr.MoveTo (width - 2, (headerHeight - textHeight) / 2);
111 			Pango.CairoHelper.ShowLayout (cr, layout);
112 
113 			layout = null;
114 			layout = context.CreatePangoLayout ();
115 
116 			desc = Pango.FontDescription.FromString ("mono");
117 			desc.Size = (int)(fontSize * pangoScale);
118 			layout.FontDescription = desc;
119 
120 			cr.MoveTo (0, headerHeight + headerGap);
121 			int line = args.PageNr * linesPerPage;
122 			for (int i=0; i < linesPerPage && line < numLines; i++)
123 			{
124 				layout.SetText (lines[line]);
125 				Pango.CairoHelper.ShowLayout (cr, layout);
126 				cr.RelMoveTo (0, fontSize);
127 				line++;
128 			}
129 			(cr as IDisposable).Dispose ();
130 			layout = null;
131 		}
132 
OnEndPrint(object obj, Gtk.EndPrintArgs args)133 		private void OnEndPrint (object obj, Gtk.EndPrintArgs args)
134 		{
135 		}
136 	}
137 }
138