[comp.sys.mac.programmer] Basic Printing - Help

coxr@piccolo.ecn.purdue.edu (Richard L Cox) (06/12/91)

 Hello all, I am just beginning to develop and one thing which is very
important to writing applications is printing.  I have read The C
Programming Primer and Inside and Can't make this simple call to the
printer and use quickdraw commands.  Here is what I have (using Think C
4.0.5)


#include <PrintMgr.h>
#include <MacTypes.h>
#include <QuickDraw.h>
#include <PrintTraps.h>

#define NIL					0L
#define REMOVE_ALL_EVENTS	0

#define HORIZONTAL_PIXEL	10
#define VERTICAL_PIXEL		220

/******************* Main *******************/

int main() 
{
	ToolBoxInit();
	PrintStuff();	
}

/************** Tool Box Init ***************/
	
ToolBoxInit()

{
	InitGraf( &thePort );
	InitFonts();
	FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs(NIL);
	InitCursor();
}


/****************** WindowInit ****************/

PrintStuff()
{
	TPPrPort	myPrPort;
	THPrint		prRecHdl;

	PrOpen();
	PrintDefault(prRecHdl);
	myPrPort = PrOpenDoc( prRecHdl, NIL, NIL);
	PrOpenPage(myPrPort,NIL);
	
		/* QuickDraw Commands */
		MoveTo( HORIZONTAL_PIXEL, VERTICAL_PIXEL );
		TextFont( 14 );
		TextFace( bold );
		TextSize( 18 );
		DrawString("\p Grateful Dead");
	
	PrClosePage(myPrPort);
	PrCloseDoc(myPrPort);
	PrClose();
	
}
	
aTdHvAaNnKcSe

-Rich

Internet: coxr@ecn.purdue.edu

aep@world.std.com (Andrew E Page) (06/12/91)

In article <coxr.676674459@piccolo.ecn.purdue.edu> coxr@piccolo.ecn.purdue.edu (Richard L Cox) writes:
>
> Hello all, I am just beginning to develop and one thing which is very
>important to writing applications is printing.  I have read The C
>Programming Primer and Inside and Can't make this simple call to the
>printer and use quickdraw commands.  Here is what I have (using Think C
>4.0.5)
>
>
>PrintStuff()
>{
>	TPPrPort	myPrPort;
>	THPrint		prRecHdl;
>
>	PrOpen();
>	PrintDefault(prRecHdl);
*       ^^^^^^^^^^^^^^^^^^^^^^    
*  
* YOu may have a problem here.  You never allocated prRecHdl.  So in short
*   you just passed a piece of garbage to a routine that NEEDS a valid handle.
*   What you need to do is this...
*
*      prRecHdl = (THPrint)NewHandle(sizeof(TPrint)) ;
*
*
*      Do this BEFORE calling PrintDefautl, which gives you the default fields
* for the printer selected with the Chooser.
*
>	myPrPort = PrOpenDoc( prRecHdl, NIL, NIL);
>	PrOpenPage(myPrPort,NIL);
>	
>		/* QuickDraw Commands */
>		MoveTo( HORIZONTAL_PIXEL, VERTICAL_PIXEL );
>		TextFont( 14 );
>		TextFace( bold );
>		TextSize( 18 );
>		DrawString("\p Grateful Dead");
*                              ^^^^^^^^^^^^^^^
*  How's about Wilson Phillips instead?
*
*
>	
>	PrClosePage(myPrPort);
>	PrCloseDoc(myPrPort);
>	PrClose();
>	
>}


-- 
Andrew E. Page (Warrior Poet)   |   Decision and Effort The Archer and Arrow
Concepts Enginerring            |     The difference between what we are
Macintosh and DSP Technology    |           and what we want to be.

casseres@apple.com (David Casseres) (06/12/91)

In article <coxr.676674459@piccolo.ecn.purdue.edu>, coxr@piccolo.ecn.purdue.edu (Richard L Cox) writes:
> 
> PrintStuff()
> {
> 	TPPrPort	myPrPort;
> 	THPrint		prRecHdl;
> 
> 	PrOpen();
> 	PrintDefault(prRecHdl);

There's your problem.  You must allocate storage for the handle before
passing it to PrintDefault.  Otherwise, it's a garbage value which points
to something somewhere, which in turn points to some memory somewhere,
which PrintDefault will trash by filling it with default print record
values.  Results are, as they say, unpredictable.

David Casseres