The attach code works perfect in ubuntu 12.04, but in windows I get the following error:

Marshaling clicked signal
Exception in Gtk# callback delegate
Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocatio n. ---> System.Runtime.InteropServices.SEHException: External component has thrown an exception. at Gtk.PrintOperation.gtk_print_operation_run(IntPtr raw, Int32 action, IntPtr parent, IntPtr& er
ror)
   at Gtk.PrintOperation.Run(PrintOperationAction action, Window parent)
at Workbench.PrintingDialog.Print(PrintOperationAction action, String FileName) at Workbench.PrintingDialog.PrintDialogHandleClicked(Object sender, EventArgs e)
   --- End of inner exception stack trace ---

I not have a clue about this.

May be will be better use system.drawing.printing?

Regards

--
Jesus Eguiluz
Ingeniero Electrónico.
Investigación y Desarrollo
Andes Electrónica Ltda.
+56 2 2347-8780
www.andeselec.com

using System;

namespace Workbench
{
	public class PrintingDialog
	{
		string PrintText;
		System.Collections.Generic.List<int> page_breaks;
		Pango.Layout layout;
		Gtk.Window win = new Gtk.Window(Gtk.WindowType.Toplevel);
		public PrintingDialog()
		{

		}
		private Gtk.PrintOperationResult Print(Gtk.PrintOperationAction action, string FileName){
			Gtk.PaperSize[] PapersSizes =  Gtk.PaperSize.GetPaperSizes(false);
			foreach(Gtk.PaperSize pz in PapersSizes){
				Console.WriteLine(pz.Name);
			}
			Gtk.PaperSize PaperSize= new Gtk.PaperSize("na_letter");
			Gtk.PageSetup PageSetup = new Gtk.PageSetup();
			PageSetup.PaperSize=PaperSize;
			Gtk.PrintOperation PrintOperation = new Gtk.PrintOperation();
			PrintOperation.DefaultPageSetup=PageSetup;
			PrintOperation.Unit = Gtk.Unit.Mm;
			PrintOperation.BeginPrint+= PrintOperationHandleBeginPrint;
			PrintOperation.DrawPage+= PrintOperationHandleDrawPage;
			if(action == Gtk.PrintOperationAction.Export){
				PrintOperation.ExportFilename=FileName;
			}
			return PrintOperation.Run(action, win);
		}

		void PrintOperationHandleDrawPage (object o, Gtk.DrawPageArgs args)
		{
			int start=0;
			int end=0;
			if(args.PageNr==0){
				start=0;
			}else{
				start =  page_breaks[args.PageNr-1];
			}
			try{
				end=page_breaks[args.PageNr];
			}catch{
				end=layout.LineCount;
			}
			Cairo.Context cr = args.Context.CairoContext;
			cr.SetSourceRGB(0,0,0);
			int i=0;
			double star_pos=0;
			Pango.LayoutLine line;
			double BaseLine=0;
			Pango.LayoutIter iter=layout.Iter;
			Pango.Rectangle ink_rec;
			Pango.Rectangle logical_rec;
			while(true){
				if(i>=start){
					line=iter.Line;
					iter.GetLineExtents(out ink_rec,out logical_rec);
					BaseLine=iter.Baseline;
					if(i==start){
						star_pos=logical_rec.Y/Pango.Scale.PangoScale;
					}
					cr.MoveTo(logical_rec.X/Pango.Scale.PangoScale,(BaseLine/Pango.Scale.PangoScale)-star_pos);
					Pango.CairoHelper.ShowLayoutLine(cr,line);
				}
				i++;
				bool ToNextLine = iter.NextLine();
				if(!((i<end) && ToNextLine)){
					break;
				}
			}
			((IDisposable)cr).Dispose();
		}

		void PrintOperationHandleBeginPrint (object o, Gtk.BeginPrintArgs args)
		{
			double width = args.Context.Width;
			double height= args.Context.Height;
			layout=args.Context.CreatePangoLayout();
			layout.FontDescription = new Pango.FontDescription();
			layout.Width = (int)(width*Pango.Scale.PangoScale);
			layout.SetText(PrintText);
			int NumLines = layout.LineCount;
			double page_height=0;
			System.Collections.Generic.List<int> page_breaks = new System.Collections.Generic.List<int>();
			for(int line=0;line<NumLines;line++){
				Pango.LayoutLine PangoLayoutLine = layout.GetLine(line);
				Pango.Rectangle ink_rect = new Pango.Rectangle();
				Pango.Rectangle logical_rect = new Pango.Rectangle();
				PangoLayoutLine.GetExtents(ref ink_rect,ref logical_rect);
				int x = logical_rect.X;
				int y = logical_rect.Y;
				int lwith = logical_rect.Width;
				int lheigth = logical_rect.Height;
				double line_height = lheigth/Pango.Scale.PangoScale;
				page_height+=line_height;
				if((page_height+line_height)>height){
					page_breaks.Add(line);
					page_height=0;
					page_height+=line_height;
				}
				((Gtk.PrintOperation)o).NPages=page_breaks.Count+1;
				this.page_breaks = page_breaks;
			}


		}
		public void Run(){
			win.DeleteEvent+= (o, args) => {win.Dispose();};
			Gtk.HBox hbox = new Gtk.HBox(false,0);
			Gtk.VBox vbox = new Gtk.VBox(false,0);

			Gtk.FileChooserButton ButtonOpen = new Gtk.FileChooserButton("Open File",Gtk.FileChooserAction.Open);
			ButtonOpen.SelectionChanged+= HandleSelectionChanged;

			Gtk.Button PrintPreview = new Gtk.Button("Previsualizacion");
			PrintPreview.Clicked+= PrintPreviewHandleClicked;

			Gtk.Button PrintNow = new Gtk.Button("Imprimir Ahora");
			PrintNow.Clicked+= PrintNowHandleClicked;

			Gtk.Button PrintPdf = new Gtk.Button("A PDF");
			PrintPdf.Clicked+= PrintPdfHandleClicked;

			Gtk.Button PrintDialog = new Gtk.Button("Print Dialog");
			PrintDialog.Clicked+= PrintDialogHandleClicked;

			hbox.PackStart(PrintDialog,true,true,5);
			hbox.PackStart(PrintNow,true,true,5);
			hbox.PackStart(PrintPdf,true,true,5);
			hbox.PackStart(PrintPreview,true,true,5);
			vbox.PackStart(ButtonOpen,false,true,5);
			vbox.PackStart(hbox,false,true,5);
			win.Add(vbox);
			win.ShowAll();
		}

		void PrintDialogHandleClicked (object sender, EventArgs e)
		{
			Print(Gtk.PrintOperationAction.PrintDialog,"");
		}

		void PrintPdfHandleClicked (object sender, EventArgs e)
		{
			Print(Gtk.PrintOperationAction.Export,"pdf.pdf");
		}

		void PrintNowHandleClicked (object sender, EventArgs e)
		{
			Print(Gtk.PrintOperationAction.Print,"");
		}

		void PrintPreviewHandleClicked (object sender, EventArgs e)
		{
			Print(Gtk.PrintOperationAction.Preview,"");
		}

		void HandleSelectionChanged (object sender, EventArgs e)
		{
			PrintText = System.IO.File.ReadAllText(((Gtk.FileChooserButton)sender).Filename);
		}
	}
}

_______________________________________________
Gtk-sharp-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Reply via email to