[futurebasic] Help with Printing Concept

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2011 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 1 Jul 2011 07:18:53 -0400
Sofia asked:

>I am VERY new to API programming, and was wondering if anyone could 
>help me understand the printing concept (from PMCreateSession to 
>PMSessionEndDocument) with a simple procedural example. Perhaps just 
>to get a line or two (or even a bitmap) printed on paper.

Sofia,

Welcome to the world of FB!

Maybe the following simplified demo will be of assistance.

Ken


/*
   Simple CoreGraphics Printing Demo

      Assumes a default U.S. letter-size page

   Ken Shmidheiser
   July 1, 2011

*/

// Load the FB headers we need for this demo
include "Tlbx Printing.incl"
include "Tlbx CoreGraphics.incl"

// Define a couple CoreGraphics helper functions for easy drawing
toolbox fn PMSessionBeginCGDocument( PMPrintSession printSession, 
PMPrintSettings printSettings, PMPageFormat pageFormat ) = OSStatus
toolbox fn PMSessionGetCGGraphicsContext ( PMPrintSession 
printSession, CGContextRef *context ) = OSStatus

// Dimension program variables
dim as PMPrintSession    session
dim as PMPageFormat      pageFormat
dim as PMPrintSettings   printSettings
dim as OSStatus          stat
dim as CGContextRef      cg : cg = 0
dim as CGRect            letterPageRect
dim as Boolean           accepted

// Create a print session and page format for this session
stat = fn PMCreateSession( @session )
stat = fn PMCreatePageFormat( @pageFormat )

// This code assumes a default U.S. letter-size page
stat = fn PMSessionDefaultPageFormat( session, pageFormat )
stat = fn PMCreatePrintSettings( @printSettings )
stat = fn PMSessionDefaultPrintSettings( session, printSettings )

// Present the user with a print dialog
stat = fn PMSessionPrintDialog( session, printSettings, pageFormat, @accepted )
long if ( accepted  == _true )

// Give the the user something to print, in this case some simple graphics

// Prepare the print job to accept a CoreGraphics drawing
stat = fn PMSessionBeginCGDocument( session, printSettings, pageFormat )

// Open a page to begin drawing on it
stat = fn PMSessionBeginPage( session, pageFormat, #0 )
stat = fn PMSessionGetCGGraphicsContext( session, @cg  )

// Set background of the page to solid white
CGContextSetAlpha( cg, 1.0 )
CGContextSetRGBFillColor( cg, 1.0, 1.0, 1.0, 1.0 )

/*
Create a inset rectangle to define
the margins of a U.S. letter-size page.
This is just to give a visual clue of the
page bounds for this demo.
*/
letterPageRect = fn CGRectMake( 16, 15, 584, 765 )
CGContextFillRect( cg, letterPageRect )

// Stroke the page margin rect with black
CGContextSetRGBFillColor( cg, 0.0, 0.0, 0.0, 1.0 )
CGContextStrokeRect( cg, letterPageRect )

// Set up solid yellow fill color for a simple circle
CGContextSetRGBFillColor( cg, 1.0, 1.0, 0.0, 1.0 )

// Set red as color to stoke the yellow circle
CGContextSetRGBStrokeColor( cg, 1.0, 0.0, 0.0, 1.0 )

// Draw the yellow circle at the center of the page
CGContextFillEllipseInRect( cg, fn CGRectMake( 200, 300, 200, 200 ) )

// Stoke the yellow circle with a red boarder
CGContextStrokeEllipseInRect( cg, fn CGRectMake( 200, 300, 200, 200 ) )

// Drawing over, empty the drawing trash
CGContextFlush( cg )

// Done with this page...
stat = fn PMSessionEndPage( session )

//...so send it to the printer
stat = fn PMSessionEndDocument( session )

// Printing is over, so empty the printing trash
stat = fn PMRelease( printSettings)
stat = fn PMRelease( pageFormat)
stat = fn PMRelease( session)

xelse
// Just in case the user cancels the print dialog
stop "Print job canceled"
end if