Xojo Developer Conference
25/27th April 2018 in Denver.
MBS Xojo Conference
6/7th September 2018 in Munich, Germany.

Platforms to show: All Mac Windows Linux Cross-Platform

/Cocoa/RTF Printing
Function: You find this example project in your Plugins Download as a Xojo (Real Studio) project file within the examples folder: /Cocoa/RTF Printing
Project "RTF Printing.rbp"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control TextArea1 Inherits TextArea
ControlInstance TextArea1 Inherits TextArea
End Control
Control TextArea2 Inherits TextArea
ControlInstance TextArea2 Inherits TextArea
End Control
Control TextArea3 Inherits TextArea
ControlInstance TextArea3 Inherits TextArea
End Control
Control TextArea4 Inherits TextArea
ControlInstance TextArea4 Inherits TextArea
End Control
Control PrintButton Inherits PushButton
ControlInstance PrintButton Inherits PushButton
EventHandler Sub Action() dim p as new PrinterSetup dim g as Graphics = OpenPrinterDialog(p) if g = nil then Return 'g.ForeColor = &c0000FF 'g.DrawRect 0, 0, g.Width, g.Height // get current Cocoa graphics context dim o as new NSGraphicsMBS // now get the one for printing dim CGContextHandle as integer = g.Handle(g.HandleTypeCGContextRef) System.DebugLog "CGContextHandle: "+str(CGContextHandle) dim c as CGContextMBS = CGContextMBS.contextWithCGContext(CGContextHandle) System.DebugLog "graphicsContextWithCGContext..." // this will also make it the current one dim n as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContext(c) System.DebugLog "get textStorage..." dim n1 as NSAttributedStringMBS = TextArea1.NSTextViewMBS.textStorage dim n2 as NSAttributedStringMBS = TextArea2.NSTextViewMBS.textStorage dim n3 as NSAttributedStringMBS = TextArea3.NSTextViewMBS.textStorage dim n4 as NSAttributedStringMBS = TextArea4.NSTextViewMBS.textStorage dim w as integer = (p.PageWidth - 100)/2 dim h as integer = (p.Pageheight - 100)/2 dim x as integer = 50 dim y as integer = 50 System.DebugLog "w: "+str(w) System.DebugLog "h: "+str(h) System.DebugLog "x: "+str(x) System.DebugLog "y: "+str(y) dim r1 as new NSRectMBS(x , y+h, w, h) dim r2 as new NSRectMBS(x+w, y+h, w, h) dim r3 as new NSRectMBS(x , y , w, h) dim r4 as new NSRectMBS(x+w, y , w, h) n.drawInRect(n1, r1) n.drawInRect(n2, r2) n.drawInRect(n3, r3) n.drawInRect(n4, r4) System.DebugLog "Flush" n.flushGraphics System.DebugLog "cleanup" n = nil c = nil // now restore the graphics context we had before! o.setCurrentContext g = nil End EventHandler
End Control
Control PrintButton1 Inherits PushButton
ControlInstance PrintButton1 Inherits PushButton
EventHandler Sub Action() dim p as new PrinterSetup dim g as Graphics = OpenPrinterDialog(p) if g = nil then Return // now get the one for printing dim CGContextHandle as integer = g.Handle(g.HandleTypeCGContextRef) System.DebugLog "CGContextHandle: "+str(CGContextHandle) dim c as CGContextMBS = CGContextMBS.contextWithCGContext(CGContextHandle) 'g.ForeColor = &c0000FF 'g.DrawRect 0, 0, g.Width, g.Height System.DebugLog "get textStorage..." dim n1 as NSTextViewMBS = TextArea1.NSTextViewMBS dim n2 as NSTextViewMBS = TextArea2.NSTextViewMBS dim n3 as NSTextViewMBS = TextArea3.NSTextViewMBS dim n4 as NSTextViewMBS = TextArea4.NSTextViewMBS dim w as integer = (p.PageWidth - 100)/2 dim h as integer = (p.Pageheight - 100)/2 dim x as integer = 50 dim y as integer = 50 System.DebugLog "w: "+str(w) System.DebugLog "h: "+str(h) System.DebugLog "x: "+str(x) System.DebugLog "y: "+str(y) // proportional dim faktor as Double = min( h / TextArea1.Height, w / TextArea1.Width) // Calculate new size dim dw as double = TextArea1.Width * faktor dim dh as double = TextArea1.Height * faktor dim p1 as MemoryBlock = n1.dataWithPDFInsideRect(n1.bounds) dim p2 as MemoryBlock = n2.dataWithPDFInsideRect(n2.bounds) dim p3 as MemoryBlock = n3.dataWithPDFInsideRect(n3.bounds) dim p4 as MemoryBlock = n4.dataWithPDFInsideRect(n4.bounds) dim q1 as CGPDFDocumentMBS = CGPDFDocumentMBS.CreateWithData(p1) dim q2 as CGPDFDocumentMBS = CGPDFDocumentMBS.CreateWithData(p2) dim q3 as CGPDFDocumentMBS = CGPDFDocumentMBS.CreateWithData(p3) dim q4 as CGPDFDocumentMBS = CGPDFDocumentMBS.CreateWithData(p4) dim r1 as new CGRectMBS(x , y+h +h-dh, dw, dh) dim r2 as new CGRectMBS(x+w, y+h +h-dh, dw, dh) dim r3 as new CGRectMBS(x , y +h-dh, dw, dh) dim r4 as new CGRectMBS(x+w, y +h-dh, dw, dh) c.DrawCGPDFDocument(q1, r1, 1) c.DrawCGPDFDocument(q2, r2, 1) c.DrawCGPDFDocument(q3, r3, 1) c.DrawCGPDFDocument(q4, r4, 1) // debug rectangles 'c.SetRGBStrokeColor(1,0,0) 'c.SetRGBFillColor(0,0,0,0) 'c.frameRect(r1.Origin.x, r1.Origin.y, r1.Size.Width, r1.Size.Height) 'c.frameRect(r2.Origin.x, r2.Origin.y, r2.Size.Width, r2.Size.Height) 'c.frameRect(r3.Origin.x, r3.Origin.y, r3.Size.Width, r3.Size.Height) 'c.frameRect(r4.Origin.x, r4.Origin.y, r4.Size.Width, r4.Size.Height) System.DebugLog "Flush" c.Flush System.DebugLog "cleanup" c = nil g = nil End EventHandler
End Control
EventHandler Sub Open() // show RTF formatted TextArea1.RTFDataMBS = TextArea1.Text TextArea2.RTFDataMBS = TextArea2.Text TextArea3.RTFDataMBS = TextArea3.Text TextArea4.RTFDataMBS = TextArea4.Text End EventHandler
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

Feedback, Comments & Corrections




Links
MBS Xojo Chart Plugins