[Gtk-sharp-list] printing multiple pages with Gtk#

Jestin Stoffel jestin.stoffel at gmail.com
Sat Mar 22 23:02:40 EDT 2008

About a month ago there was some discussion about printing in gtk-sharp,
and I have recently had a need to give it a try.  One of my problems is
that I want to print the content of a widget that will most likely have
multiple pages.  To set the number of pages, I need to reference my
Gtk.PrintOperation object from my BeginPrintHandler (as the Gtk
documentation suggests).  The problem is, that my object is out of
scope.  Here's an example:

protected virtual void OnPrintButtonClicked (object sender,
System.EventArgs e)
	Gtk.PrintOperation Op = new PrintOperation();

	Op.Unit = Gtk.Unit.Pixel;
	Op.BeginPrint += new BeginPrintHandler(OnBeginPrint);
	Op.DrawPage += new DrawPageHandler(OnDrawPage);
	Res = Op.Run(Gtk.PrintOperationAction.PrintDialog, this);

public void OnBeginPrint(object sender, Gtk.BeginPrintArgs Args)
	int nSize = SomeSizeCalculationFunction();
	int nPages = nSize / (int)Args.Context.Height;
	if(nSize % Args.Context.Height > 0)
	Op.NPages = nPages; // OUT OF SCOPE

This can be easily solved by making Op global, but isn't there a way to
include Op in the Gtk.BeginPrintArgs object?  Right now the only thing
it contains is the PrintContext, which is also needed, but is only one
factor in the page count calculation.

This isn't a problem in C, because the parameters passed to a BeginPrint
signal handler include the PrintOperation pointer.  I just thought that
maybe the C# handler should match.


More information about the Gtk-sharp-list mailing list