0

How do I print in monomac? This is as far as I can get, but I can't seem to get a reference to the graphics context in the NSView. If I add a control to the PrintDoc that's fine, but I want to draw.

//Print Function
void Print(){
    PrintDoc NewDoc = new PrintDoc ();
    NewDoc.SetFrameSize(new SizeF(600,1000));
    NSPrintOperation P = NSPrintOperation.FromView (NewDoc);
    P.RunOperation ();
}

//NSView to be printed
class PrintDoc:NSView
{
    public PrintDoc ()
    {
    }
    public override void DrawRect (System.Drawing.RectangleF dirtyRect)
    {
        //NSPrintOperation.CurrentOperation.Context !! this is null
        //NSGraphicsContext.CurrentContext !! this hangs
    }
}
Gordon Truslove
  • 724
  • 2
  • 10
  • 19

1 Answers1

0

I've managed to get it working by getting the context manually, instead of using NSGraphicsContext.CurrentContext:

https://github.com/picoe/Eto/blob/feature/printing/Source/Eto.Platform.Mac/Forms/Printing/PrintDocumentHandler.cs#L39

Snippet:

static IntPtr selCurrentContext = Selector.GetHandle ("currentContext");
static IntPtr classNSGraphicsContext = Class.GetHandle ("NSGraphicsContext");

public override void DrawRect (System.Drawing.RectangleF dirtyRect)
{
    var operation = NSPrintOperation.CurrentOperation;

    var context = new NSGraphicsContext(Messaging.IntPtr_objc_msgSend (classNSGraphicsContext, selCurrentContext));
    // this causes monomac to hang for some reason:
    //var context = NSGraphicsContext.CurrentContext;
}
Curtis
  • 1,552
  • 15
  • 20