File: DemoPrinting.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (137 lines) | stat: -rw-r--r-- 3,609 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Printing
 *
 * GtkPrintOperation offers a simple API to support printing in a cross-platform way.
 */

using System;
using System.IO;
using System.Reflection;
using Gtk;
using Cairo;

namespace GtkDemo
{
	[Demo ("Printing", "DemoPrinting.cs")]
        public class DemoPrinting
        {
		private static double headerHeight = (10*72/25.4);
		private static double headerGap = (3*72/25.4);
		private static int pangoScale = 1024;

		private PrintOperation print;

		private string fileName = "DemoPrinting.cs";
		private double fontSize = 12.0;
		private int linesPerPage;
		private string[] lines;
		private int numLines;
		private int numPages;

                public DemoPrinting ()
                {
			print = new PrintOperation ();
			
			print.BeginPrint += new BeginPrintHandler (OnBeginPrint);
			print.DrawPage += new DrawPageHandler (OnDrawPage);
			print.EndPrint += new EndPrintHandler (OnEndPrint);

			print.Run (PrintOperationAction.PrintDialog, null);
		}

		private void OnBeginPrint (object obj, Gtk.BeginPrintArgs args)
		{
			string contents;
			double height;

			PrintContext context = args.Context;
			height = context.Height;
		
			linesPerPage = (int)Math.Floor(height / fontSize);
			contents = LoadFile("DemoPrinting.cs");

			lines = contents.Split('\n');
			
			numLines = lines.Length;
			numPages = (numLines - 1) / linesPerPage + 1;
			
			print.NPages = numPages;			
		}

		private string LoadFile (string filename)
		{
			Stream file = Assembly.GetExecutingAssembly ().GetManifestResourceStream 
(filename);
                        if (file == null && File.Exists (filename)) {
                                file = File.OpenRead (filename);
                        }
			if (file == null) {
				return "File not found";
			}

			StreamReader sr = new StreamReader (file);
			return sr.ReadToEnd ();
		}

		private void OnDrawPage (object obj, Gtk.DrawPageArgs args)
		{
			PrintContext context = args.Context;

			Cairo.Context cr = context.CairoContext;
			double width = context.Width;

			cr.Rectangle (0, 0, width, headerHeight);
			cr.SetSourceRGB (0.8, 0.8, 0.8);
			cr.FillPreserve ();

			cr.SetSourceRGB (0, 0, 0);
			cr.LineWidth = 1;
			cr.Stroke();

			Pango.Layout layout = context.CreatePangoLayout ();
			
			Pango.FontDescription desc = Pango.FontDescription.FromString ("sans 14");
			layout.FontDescription = desc;
			
			layout.SetText (fileName);
			layout.Width = (int)width;
			layout.Alignment = Pango.Alignment.Center;

			int layoutWidth, layoutHeight;
			layout.GetSize (out layoutWidth, out layoutHeight);
			double textHeight = (double)layoutHeight / (double)pangoScale;

			cr.MoveTo (width/2, (headerHeight - textHeight) / 2);
			Pango.CairoHelper.ShowLayout (cr, layout);

			string pageStr = String.Format ("{0}/{1}", args.PageNr + 1, numPages);
			layout.SetText (pageStr);
			layout.Alignment = Pango.Alignment.Right;

			cr.MoveTo (width - 2, (headerHeight - textHeight) / 2);
			Pango.CairoHelper.ShowLayout (cr, layout);

			layout = null;
			layout = context.CreatePangoLayout ();

			desc = Pango.FontDescription.FromString ("mono");
			desc.Size = (int)(fontSize * pangoScale);
			layout.FontDescription = desc;
			
			cr.MoveTo (0, headerHeight + headerGap);
			int line = args.PageNr * linesPerPage;
			for (int i=0; i < linesPerPage && line < numLines; i++)
			{
				layout.SetText (lines[line]);
				Pango.CairoHelper.ShowLayout (cr, layout);
				cr.RelMoveTo (0, fontSize);
				line++;
			}
			(cr as IDisposable).Dispose ();
			layout = null;
		}

		private void OnEndPrint (object obj, Gtk.EndPrintArgs args)
		{
		}
	}
}