TweetFollow Us on Twitter

Sep 99 Getting Started

Volume Number: 15 (1999)
Issue Number: 9
Column Tag: Getting Started

More Printing

by Dan Parks Sydow

How a Mac program opens and prints a document

In last month's Getting Started article you were introduced to printing. In that article we described the basics of the Printing Manager and the basics of getting an application to print. In that article's PrintPict example program you saw how a program could load PICT resource data to memory and then print the resulting picture. While that example served the purpose of demonstrating how to send data to a printer, it didn't provide an entirely realistic environment in which to implement printing. This month we'll remedy that. Here we'll dig a little deeper into printing techniques to see how a program provides the user with the ability to open any PICT file (one of the most common formats for Macintosh picture files), display the file's picture in a window, and then print that picture. This month's PrintDoc example is a menu-driven program that provides you with all the code you need to easily add a document-printing feature to your own program.

Printing Basics Recap

Last month you saw how to write a program that displays the two standard printing dialog boxes found in most Mac programs. Figures 1 and 2 provide examples of the Printing Style dialog box and the Printing Job dialog box. Recall that the exact look of these dialog boxes is dependent on the printer connected to the user's Mac.

Figure 1. A typical Printing Style dialog box.

Figure 2. A typical Printing Job dialog box.

To display the Printing Style dialog box your program calls the Printing Manager function PrStlDialog(). To display the Printing Job dialog box your program makes a call to the Printing Manager function PrJobDialog(). Before calling either function your program must declare a THPrint variable and call NewHandle() or NewHandleClear() in order to create a new print record and to receive a handle to that record. A call to PrintDefault() then fills the record with default values. User-entered values originating in either the Printing Style or Printing Job dialog boxes can alter some or all of the values in this record.

THPrint		printRecord;
Boolean		doPrint;
printRecord = (THPrint)NewHandleClear( sizeof (TPrint) );
PrintDefault( printRecord );
PrStlDialog( printRecord );
doPrint = PrJobDialog( printRecord );

For printing a document, the display of the Printing Style and Printing Job dialog boxes are important, user-friendly steps. However, printing can be accomplished without calls to PrStlDialog() and PrJobDialog(). Printing can't take place, though, without making calls to a few other very important Printing Manager functions.

A call to PrOpen() prepares the current printer resource file (the one associated with the printer the user has selected from the Chooser) for use. PrOpenDoc() initializes a printing graphics port-the port to which all subsequent QuickDraw commands are to be sent. PrOpenPage()begins the definition of a single page to be printed.

PrClosePage() signals the end of the current page. PrCloseDoc() closes a printing graphics port. PrClose() closes the Printing Manager and the printer resource file.

TPPrPort		printerPort;
printerPort = PrOpenDoc( printRecord, nil, nil );
PrOpenPage( printerPort, nil );
// QuickDraw calls that define what's to be printed
PrClosePage( printerPort );  
PrCloseDoc( printerPort );

Printing a Document

When last month's example program is run, the Printing Style and Printing Job dialog boxes mysteriously appear and a picture from an unknown (to the user) source prints. No window opens, so to the user, there's no apparent origin of the graphics that get printed. A more realistic use of printing would be for an application to bring up the Printing Style and Printing Job dialog boxes only when requested by the user. When the user opts to print, then the contents of the frontmost window should be sent to the printer.

To implement a useful printing scheme, begin by creating a print record. Use a global variable to keep track of the record. Call PrintDefault() to fill the record with default values.

THPrint	gPrintRecord;
gPrintRecord = (THPrint)NewHandleClear( sizeof(TPrint) );
PrintDefault( gPrintRecord );

In the above code, the call to the Printing Manager function PrintDefault() is nested between calls to PrOpen() and PrClose(). Last month's example called PrOpen() near the start of the program and PrClose() near the end. That worked fine for our short, simple program. For a more sophisticated program, it's wiser to open and close the printing resource file at each use. Recall that when an application launches, its resource fork is opened by the system. When PrOpen() is called, the printer resource file is opened. At that point, the program has two resource files open. When making certain Toolbox calls, this has to be kept in mind (as it's possible for two resource forks to each include a resource of the same type and ID). If your program closes the printing resource after using it, you won't have to be concerned with issues such as which resource file is considered the current file.

Your printing-ready program should include a Page Setup item in the File menu. Handling a Page Setup choice involves little more than calling PrStlDialog(). A File menu-handling routine would include a case section similar to this one:

case iPageSetup:
	PrStlDialog( gPrintRecord );

Your print-capable program will of course include a Print item in the File menu. Handling a Print choice begins with a determination of the front window. A call to the Toolbox routine FrontWindow() takes care of that task. Next, a call to PrJobDialog() displays the Printing Job dialog box. If from this dialog box the user cancels printing, there's nothing your program needs to do. If the user instead clicks the Print button, you'll invoke an application-defined function that carries out the printing.

WindowPtr	window;
Boolean		doPrint;
case iPrint:
	window = FrontWindow();
	doPrint = PrJobDialog( gPrintRecord );
	if ( doPrint == true )
		DoPrintChoice( window );

Regardless of the contents of the window, your print-handling routine should look similar to the DoPrintChoice() function shown below. That's because this function does the busy work of getting ready for printing and cleaning up after printing. What actually gets printed is determined in the application-defined DrawToPort() routine.

void		DoPrintChoice( WindowPtr window )
	TPPrPort		printerPort; 
	printerPort = PrOpenDoc( gPrintRecord, nil, nil );    
	PrOpenPage( printerPort, nil );
	DrawToPort( window );
	PrClosePage( printerPort );  
	PrCloseDoc( printerPort );


This month's program is PrintDoc. When you run PrintDoc, you see a menu bar that holds the File menu pictured in Figure 3. Choosing the Open item displays the standard Open dialog box pictured in Figure 4. From this dialog box you can select and open any picture file (any file of type PICT) you have available.

Figure 3. The File menu from the PrintDoc program.

Figure 4. Choosing a picture file to open.

After selecting a file, PrintDoc opens it, loads the picture data to memory, and displays the resulting picture in its own window. Figure 5 shows that PrintDoc sizes the window to match the size of the picture. A window can be dragged and closed. PrintDoc allows for any number of windows to be open (memory permitting). When one or more windows are on the screen, the program enables the File menu items that are initially disabled. As shown in Figure 3, these are the Close, Page Setup, and Print items. Closing all windows again disables those same items.

Figure 5. A typical picture displayed in the PrintDoc program.

Choosing Page Setup from the File menu displays the Printing Style dialog box. Choosing Print from the File menu displays the Printing Job dialog box. If you click the Print button, the program prints the picture that's in the frontmost window.

Because the program doesn't handle any type of editing, all of the items in the Edit menu are disabled.

Creating the PrintDoc Resources

Start the project by creating a new folder named PrintDoc in your CodeWarrior development folder. Start up ResEdit, then create a new resource file named PrintDoc.rsrc. Make sure to specify the PrintDoc folder as the resource file's destination. The resource file will hold resources of the types shown in Figure 6.

Figure 6. The PrintDoc resources.

Figure 6 shows the three MENU resources. After creating these resources, create a single MBAR resource that includes MENU IDs 128, 129, and 130.

The one ALRT and one DITL resource are used to define the program's error-handling alert. The only other resource needed is a single WIND. This resource serves as the window that's to display a picture. Because the program resizes the window to match the size of the picture that's to be displayed, the WIND boundaries you specify are unimportant.

Creating the PrintDoc Project

Launch CodeWarrior and choose New Project from the File menu. As usual, choose the MacOS:C_C++:MacOS Toolbox:MacOS Toolbox Multi-Target project stationary for the new project. You already have a project folder created, so uncheck the Create Folder check box before clicking the OK button. Name the project PrintDoc.mcp, and make sure the project's destination is the PrintDoc folder.

Add the PrintDoc.rsrc resource file to the project. Remove the SillyBalls.rsrc file. Because the project doesn't make use of any ANSI libraries, you can go ahead and remove the ANSI Libraries folder from the project window if you want.

Create a new source code window by choosing New from the File menu.. Save the window, giving it the name PrintDoc.c. Choose Add Window from the Project menu to add this empty file to the project. Now remove the SillyBalls.c placeholder file from the project window. You're all ready to type in the source code.

To save yourself the work involved in typing all the source code, visit MacTech's ftp site at <>. There you'll find the PrintDoc source code file available for downloading.

Walking Through the Source Code

PrintDoc begins with a number of constants. Most of the constants define menu-related resources and items. Exceptions are kSleep and kMoveToFront, which are used in calls to WaitNextEvent() and GetNewCWindow(), respectively.

/********************* constants *********************/
#define		kWINDResID			128
#define		kMBARResID			128
#define		kALRTResID			128
#define		kSleep					7
#define		kMoveToFront		(WindowPtr)-1L
#define		mApple					128
#define		iAbout					8
#define		mFile					129
#define		iOpen					1
#define		iClose					3
#define		iPageSetup			5
#define		iPrint					6
#define		iQuit					8
#define		mEdit					130
#define		iUndo					1
#define		iCut						3
#define		iCopy					4
#define		iPaste					5
#define		iClear					6

PrintDoc declares four global variables. The THPrint variable gPrintRecord is the print record that holds information about the currently selected printer. The MenuHandle variables gFileMenuHandle and gEditMenuHandle are used in the enabling and disabling of items in the menus referenced by these handles. The Boolean variable gDone is used to signal the end of the program's execution.

/****************** global variables *****************/
Boolean			gDone;
THPrint			gPrintRecord;
MenuHandle		gFileMenuHandle;
MenuHandle		gEditMenuHandle;

Next come the program's function prototypes.

/********************* functions *********************/
void				ToolBoxInit( void );
void				MenuBarInit( void );
void				PrintingInit( void );
void				OpenPictureDisplayWindow( void );
								PicHandle	LoadPictureFromPICTFile( void );
void				DoPrintChoice( WindowPtr window );
void				DrawToPort( WindowPtr window );
void				EventLoop( void );
void				DoEvent( EventRecord *eventPtr );
void				HandleMouseDown( EventRecord *eventPtr );
void				AdjustMenus( void );
void				HandleMenuChoice( long menuChoice );
void				HandleAppleChoice( short item );
void				HandleFileChoice( short item );
void				DoError( Str255 errorString );

The main() function does nothing remarkable - it initializes the Toolbox and the Printing Manager, sets up the program's menu bar, and then enters an event loop.

/********************** main *************************/
void		main( void )

As expected, ToolBoxInit() is identical to previous versions.

/******************** ToolBoxInit ********************/
void		ToolBoxInit( void )
	InitGraf( &qd.thePort );
	InitDialogs( nil );

The application-defined function PrintingInit() takes care of the little bit of work necessary to initialize the Printing Manager. As mentioned earlier, in a non-trivial program it's wise to open and close the printing resource file at each use. That's what we do here in PrintingInit().

/******************* PrintingInit ********************/
void PrintingInit( void )
  gPrintRecord = (THPrint)NewHandleClear( sizeof(TPrint) );
  PrintDefault( gPrintRecord );

MenuBarInit() is similar to previous versions. Here in PrintDoc, though, we use a couple of global variables to obtain handles to the File and Edit menus. Later (in AdjustMenus()) we'll use these handles to enable and disable items in each of these two menus.

/******************** MenuBarInit ********************/
void		MenuBarInit( void )
	Handle				menuBar;
	MenuHandle		menu;
	menuBar = GetNewMBar( kMBARResID );
	SetMenuBar( menuBar );
	menu = GetMenuHandle( mApple );
	AppendResMenu( menu, 'DRVR' );
	gFileMenuHandle = GetMenuHandle( mFile );  
	gEditMenuHandle = GetMenuHandle( mEdit );  

When the user chooses Open from the File menu, OpenPictureDisplayWindow() is called to display the standard Open dialog box, open the user-selected file, load that file's data to memory, and display the resulting data in a window. OpenPictureDisplayWindow() divvies up this work a bit by calling another application-defined function, LoadPictureFromPICTFile() (discussed ahead), to handle the tasks of displaying the Open dialog box and loading the picture to memory.

/************** OpenPictureDisplayWindow *************/
void		OpenPictureDisplayWindow( void )
	PicHandle	pict = nil;
	Rect				pictRect;
	short			width;
	short			height;
	WindowPtr	window;
	pict = LoadPictureFromPICTFile(); 
	if ( pict == nil )
		DoError( "\pError loading picture" );

LoadPictureFromPICTFile() returns a handle to the memory that holds the picture from the just-opened PICT file. OpenPictureDisplayWindow() saves this handle in a variable named pict, and uses the picFrame field of the Picture record referenced by this handle to get the size of the picture. Then it opens a window and calls the Toolbox routine SetWindowPic(). When SetWindowPic() is passed a pointer to a window and a handle to a picture, it establishes a link between the two. It does this by adding the picture handle to the windowPic field of the window's WindowRecord. Once that is done, updating the window is no longer a concern of the programmer. Instead, the Window Manager redraws the picture any time the window needs updating. Assigning a picture to the windowPic field of a window isn't the solution for all picture-displaying purposes, but when it is appropriate, it's a good trick to use!

	pictRect = (**pict).picFrame;
	width = pictRect.right - pictRect.left;
	height = pictRect.bottom -;
	window = GetNewCWindow( kWINDResID, nil, (WindowPtr)-1L );
	SetWindowPic( window, pict );

OpenPictureDisplayWindow() ends by making the newly opened window's port the current port, resizing the window so that it is the same size as the picture, and then showing the window.

	SetPort( window );
	SizeWindow( window, width, height, true );
	ShowWindow( window );

You just saw that OpenPictureDisplayWindow() relies on LoadPictureFromPICTFile() to take care of the work of loading a picture to memory. LoadPictureFromPICTFile() begins with a number of local variable declarations, and then a call to StandardGetFile().

/*************** LoadPictureFromPICTFile *************/
PicHandle	LoadPictureFromPICTFile( void )
	SFTypeList					typeList = { 'PICT', 0, 0, 0 };
	StandardFileReply		reply;
	short							pictRefNum = 0;
	long								fileLength;
	Size								pictSize;
	Handle							tempHandle = nil;
	PicHandle					pict = nil;
	StandardGetFile( nil, 1, typeList, &reply );

The standard Open dialog box is also referred to as the Standard Get dialog box, and it's brought to the screen by way of a call to the Toolbox function StandardGetFile(). The first three parameters are used to tell the File Manager which types of files to display in the dialog box display list. StandardGetFile() can easily display four different types of files. If your application is capable of opening more than four different types of files, you'll need to pass a pointer to a filter function as the first parameter. Since the PrintDoc program only works with one file type (PICT files), we pass nil here. The second parameter specifies each file type. You can fill the SFTypeList when you declare it, as done here. Surround the type in single quotes, and for an unused type simply supply a 0. The last parameter is a pointer to a variable of type StandardFileReply. When the user clicks on the Open or Cancel button in the Open dialog box, the File Manager fills the members of the StandardFileReply structure. This structure has a number of members, but only the sfGood and sfFile members are generally of importance. By examining the value of sfGood, your program can determine if the user clicked on the Open button (sfGood will be true) or the Cancel button (sfGood will be false). If the user cancels, then no file is to be opened, no picture data is to go to memory, and LoadPictureFromPICTFile() can end by returning nil (rather than a valid picture handle) to OpenPictureDisplayWindow().

	if ( reply.sfGood == false )
		return ( nil );

If the user doesn't cancel, then a file is to be opened. The sfFile member of the StandardFileReply structure filled in by the call to StandardGetFile() holds a file system specification, or FSSpec, of the file to open. A call to the Toolbox function FSpOpenDF() opens the data fork of the file named in the first parameter. The second parameter holds a file-access permission level (the constant fsRdPerm means read-only permission), while the third parameter is a pointer to a variable of type short. After FSpOpenDF() successfully opens the file referenced by the FSSpec named in the first parameter, the routine fills in the last parameter with a file reference number by which your program can reference the now opened file.

	FSpOpenDF( &reply.sfFile, fsRdPerm, &pictRefNum );

Now that a picture file is open, it's time to work on getting the file's data into memory. The Toolbox function GetEOF() is used to get the size (in bytes) of the contents of a file. SetFPos() is used to move the file mark-the position maker used to keep track of the current position to read from or write to-to a particular byte location in a file. All PICT files have a 512-byte header that is used in different ways by different applications. These first 512 bytes hold data unrelated to the picture data, so you'll want to move the file mark past them before starting to read the picture data. The fsFromStart constant used in the call to SetFPos() tells the function to count from the start of the file.

	GetEOF( pictRefNum, &fileLength );
	SetFPos( pictRefNum, fsFromStart, 512 );

The total length of the file was found by GetEOF() and is held in the variable fileLength. The actual size of the picture is this total size minus the 512-byte header.

	pictSize = fileLength - 512;

At this point, the file is open and all of the information necessary to read its contents has been obtained. Before beginning the read, we make a call to NewHandleClear() to allocate an area of memory the size of the picture, and to obtain a handle to this area. The Toolbox routine FSRead() is then used to read in the file data. Pass this function the reference number of the file to read from, a pointer to the number of bytes to read, and a pointer to a buffer to read to. Because this third parameter needs to be a pointer to a buffer, it's necessary to dereference the handle once. Since a handle is relocatable, take the precaution of locking it during the moving of data from the open file to the buffer memory.

	tempHandle = NewHandleClear( pictSize );
	HLock( tempHandle );
		FSRead( pictRefNum, &pictSize, *tempHandle );
	HUnlock( tempHandle );

After the call to FSRead() is complete, tempHandle references the picture data in memory. Now it's a simple matter to typecast this generic handle to a PicHandle. At this point the application has a PicHandle that can be used as any other picture handle is used. For instance, it can be used in a call to DrawPicture(). Alternately, it can be stored in a window's windowPic field. After LoadPictureFromPICTFile() returns the PicHandle to OpenPictureDisplayWindow(), that routine does in fact use the handle to store the picture in the window's windowPic field.

	pict = ( PicHandle )tempHandle;
	return ( pict );

The Print item in the File menu is enabled when a window is open. As you'll see ahead, choosing Print causes the PrintDoc program to determine which window is frontmost (because the program allows for multiple windows) and then passes a pointer to that window to the DoPrintChoice() routine. This routine's code should look quite familiar to you - it consists mostly of calls to the Printing Manager functions that make printing happen.

A call to PrOpenDoc() initializes a printing graphics port and tells the program to route subsequent QuickDraw calls to this port (the printer) rather than to a port associated with a window. A call to PrOpenPage() is used to begin printing a single page of a document. Exactly what it is that gets printed is found in the application-defined function DrawToPort(). After printing is complete (after DrawToPort() executes), a call to PrClosePage() signals the end of the current page. A call to PrCloseDoc() closes the printing graphics port.

/******************** DoPrintChoice ******************/
void		DoPrintChoice( WindowPtr window )
	TPPrPort		printerPort;
	printerPort = PrOpenDoc( gPrintRecord, nil, nil );
	PrOpenPage( printerPort, nil );
	DrawToPort( window );
	PrClosePage( printerPort );  
	PrCloseDoc( printerPort );

DrawToPort() includes the QuickDraw calls that define what gets printed to a single page. If the current port was a window's graphics port, then drawing would take place in that window. Because a printing graphics port is current when DrawToPort() is called, the QuickDraw calls instead "draw" to the printer. In the PrintDoc program it's the window's picture (which is referenced by the PicHandle assigned to the window's windowPic field) that is to be printed. Earlier we used the Toolbox routine GetWindowPic() to retrieve that same handle. The coordinates of the picture are obtained by examining the picFrame field of the Picture structure referenced by the PicHandle, and then a call to the Toolbox function DrawPicture() sends the picture to the printer.

/********************** DrawToPort *******************/
void DrawToPort( WindowPtr window )
	PicHandle	pict;
	Rect				pictRect;
	pict = GetWindowPic( window );
	pictRect = (**pict).picFrame;
	DrawPicture( pict, &pictRect );

Now it's on to the code that's common to most Mac applications. PrintDoc is event driven-so much of the remaining code (which is event-handling) should look familiar to you. EventLoop() is called from main(). Its purpose is to pull the next available event from the event queue and pass it to DoEvent().

/********************* EventLoop *********************/
void		EventLoop( void )
	EventRecord		event;
	gDone = false;
	while ( gDone == false )
		if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
			DoEvent( &event );

Like EventLoop(), DoEvent() offers no surprises. The purpose of DoEvent() is to handle a single event.

/********************** DoEvent **********************/
void	 DoEvent( EventRecord *eventPtr )
	char		theChar;
	switch ( eventPtr->what )
		case mouseDown: 
			HandleMouseDown( eventPtr );
		case keyDown:
		case autoKey:
			theChar = eventPtr->message & charCodeMask;
			if ( (eventPtr->modifiers & cmdKey) != 0 ) 
				HandleMenuChoice( MenuKey( theChar ) );
		case updateEvt:
			BeginUpdate( (WindowPtr)(eventPtr->message) );
			EndUpdate( (WindowPtr)(eventPtr->message) );

If an event is a click of the mouse, then HandleMouseDown() is called to take care of the event. This version of HandleMouseDown() differs from previous versions only in the handling of a mouse click that occurs while the cursor is over the menu bar. For such a situation the inMenuBar case section includes a call to a new application-defined function - AdjustMenus(). As you'll see just ahead, AdjustMenus() disables and enables menu items as appropriate for the current state of the program. By calling AdjustMenus() at each occurrence of a mouse click in the menu bar, we're assured of always having the menu items set to the correct state. When a user clicks on the menu bar to perhaps reveal a menu, we care about the state of the menu items. At no other time in the running of the program do we worry about menu item states.

/****************** HandleMouseDown ******************/
void		HandleMouseDown( EventRecord *eventPtr )
	WindowPtr	window;
	short			thePart;
	long				menuChoice;
	thePart = FindWindow( eventPtr->where, &window );
	switch ( thePart )
		case inMenuBar:
			menuChoice = MenuSelect( eventPtr->where );
			HandleMenuChoice( menuChoice );
		case inSysWindow : 
			SystemClick( eventPtr, window );
		case inContent:
			SelectWindow( window );
		case inDrag : 
			DragWindow( window, eventPtr->where,
		case inGoAway:
			if ( TrackGoAway( window, eventPtr->where ) )
				DisposeWindow( window );

The adjusting of the state of a menu item (enabled or disabled) based on the current state of a program has been a topic we've largely ignored - until now. In a real-world application all menu items don't pertain to all situations. For instance, if no window is open, then a Close menu item certainly shouldn't be enabled - there's nothing to close. If a window is open in our PrintDoc program, then the Close, Page Setup, and Print items should be active. If no windows are open, then these same items of course do not apply - and should be dimmed. The AdjustMenus() function looks to see if a window is open (via a call to FrontWindow()), then makes calls to either DisableItem() or EnableItem() to set the File menu items to the proper state. AdjustMenus() also makes a call to DisableItem() with a second parameter of 0 to disable all of the Edit menu items (pass a second parameter of 1 to enable all items). If the PrintDoc program included editing, then we'd have to come up with a scheme for selectively enabling and disabling items from the Edit menu.

/******************** AdjustMenus ********************/
void		AdjustMenus( void )
	WindowPtr	window;
	DisableItem( gEditMenuHandle, 0 );
	window = FrontWindow();
	if ( window == nil )
		DisableItem( gFileMenuHandle, iClose );
		DisableItem( gFileMenuHandle, iPageSetup );
		DisableItem( gFileMenuHandle, iPrint );
		EnableItem( gFileMenuHandle, iClose );
		EnableItem( gFileMenuHandle, iPageSetup );
		EnableItem( gFileMenuHandle, iPrint );

HandleMenuChoice() and HandleAppleChoice() are both similar to prior versions.

/****************** HandleMenuChoice *****************/
void		HandleMenuChoice( long menuChoice )
	short	menu;
	short	item;
	if ( menuChoice != 0 )
		menu = HiWord( menuChoice );
		item = LoWord( menuChoice );
		switch ( menu )
			case mApple:
				HandleAppleChoice( item );
			case mFile:
				HandleFileChoice( item );
		HiliteMenu( 0 );
/***************** HandleAppleChoice *****************/
void		HandleAppleChoice( short item )
	MenuHandle		appleMenu;
	Str255				accName;
	short				accNumber;
	switch ( item )
		case iAbout:
			SysBeep( 10 );
			appleMenu = GetMenuHandle( mApple );
			GetMenuItemText( appleMenu, item, accName );
			accNumber = OpenDeskAcc( accName );

When the user chooses an item from the File menu, the chain of function calls goes like this: EventLoop(), DoEvent(), HandleMouseDown(), HandleMenuChoice(), HandleFileChoice(). What HandleFileChoice() does is based on the item selected from the File menu. An Open choice results in a call to OpenPictureDisplayWindow(). You've already seen that this function displays the Open dialog box, opens a picture file, and displays the file's picture in its own window. A Close choice closes the frontmost window. A Page Setup choice displays the Printing Style dialog box. A Print choice determines the frontmost window, displays the Printing Job dialog box, and then calls DoPrintChoice() to send the contents of the frontmost window to the printer. Choosing Quit of course quits the program.

/****************** HandleFileChoice *****************/
void		HandleFileChoice( short item )
	WindowPtr	window;
	Boolean		doPrint;
	switch ( item )
		case iOpen:
		case iClose:
			window = FrontWindow();
			DisposeWindow( window );
		case iPageSetup:
			PrStlDialog( gPrintRecord );
		case iPrint:
			window = FrontWindow();
			doPrint = PrJobDialog( gPrintRecord );
			if ( doPrint == true )
				DoPrintChoice( window );
		case iQuit:
			gDone = true;

DoError() is unchanged from prior versions. A call to this function results in the posting of an alert that holds an error message. After the alert is dismissed the program ends.

/********************** DoError **********************/
void		DoError( Str255 errorString )
	ParamText( errorString, "\p", "\p", "\p" );
	StopAlert( kALRTResID, nil );

Running PrintDoc

Run PrintDoc by selecting Run from CodeWarrior's Project menu. After compiling the code and building a program, CodeWarrior runs the program. Begin by choosing Open from the File menu in order to open a new window that displays a picture (you'll need to have at least one PICT file somewhere on your Mac's drive or drives). Then choose Page Setup from the File menu to bring up the Printing Style dialog box to make any desired changes to the characteristics (such as page orientation or scaling) of the printing that's to take place. Now choose Print from the File menu to print the contents of the frontmost window. Open and print as many windows as you want. When you're satisfied that PrintDoc performs as expected, choose Quit from the File menu to end the program.

Till Next Month...

Note the PrintDoc program's application-defined DrawToPort() function contains no printing-specific code. Instead, it's similar to a window updating function. In fact, we could use DrawToPort() to update a window. That's no accident - your own program should define a single routine that defines what gets drawn to a window and defines what gets sent to a printer. This one routine can then be called in response to the need for window updating and in response to a user's request to print the contents of the window. Your program will determine where the code from DrawToPort() gets sent depending on which port is current (a window's graphics port or the printer graphics port). With the window updating/printing code isolated, PrintDoc makes it easy to experiment with changing the content of a window, and thus to experiment with what gets sent to the printer. Of course, you won't be able to simply change the QuickDraw calls in DrawToPort() - you'll also need to change what initially gets displayed in a window. Our PrintDoc program assumes a window always displays the contents of a PICT file - your program will most likely display something else.

The PrintDoc program enhances last month's PrintPict program to provide you with the code necessary to add printing capabilities to your own program. Spend the coming weeks refining your own application's printing power. Spend some time looking over the Imaging With QuickDraw volume of Inside Macintosh to read up on how a program allows for multiple page printing. Then visit next month's Getting Started column for information on a new topic...


Community Search:
MacTech Search:

Software Updates via MacUpdate

WhatRoute 2.0.18 - Geographically trace...
WhatRoute is designed to find the names of all the routers an IP packet passes through on its way from your Mac to a destination host. It also measures the round-trip time from your Mac to the router... Read more
Posterino 3.3.5 - Create posters, collag...
Posterino offers enhanced customization and flexibility including a variety of new, stylish templates featuring grids of identical or odd-sized image boxes. You can customize the size and shape of... Read more
Skim 1.4.28 - PDF reader and note-taker...
Skim is a PDF reader and note-taker for OS X. It is designed to help you read and annotate scientific papers in PDF, but is also great for viewing any PDF file. Skim includes many features and has a... Read more
Apple macOS Sierra 10.12.4 - The latest...
With Apple macOS Sierra, Siri makes its debut on Mac, with new features designed just for the desktop. Your Mac works with iCloud and your Apple devices in smart new ways, and intelligent... Read more
Apple Numbers 4.1 - Apple's spreads...
With Apple Numbers, sophisticated spreadsheets are just the start. The whole sheet is your canvas. Just add dramatic interactive charts, tables, and images that paint a revealing picture of your data... Read more
Xcode 8.3 - Integrated development envir...
Xcode includes everything developers need to create great applications for Mac, iPhone, iPad, and Apple Watch. Xcode provides developers a unified workflow for user interface design, coding, testing... Read more
Dropbox 22.4.24 - Cloud backup and synch...
Dropbox is an application that creates a special Finder folder that automatically syncs online and between your computers. It allows you to both backup files and keep them up-to-date between systems... Read more
Merlin Project 4.2.0 - Project managemen...
Merlin Project is the leading professional project management software for OS X. If you plan complex projects on your Mac, you won’t get far with a simple list of tasks. Good planning raises... Read more
Postbox 5.0.12 - Powerful and flexible e...
Postbox is a new email application that helps you organize your work life and get stuff done. It has all the elegance and simplicity of Apple Mail, but with more power and flexibility to manage even... Read more
Apple Pages 6.1 - Apple's word proc...
Apple Pages is a powerful word processor that gives you everything you need to create documents that look beautiful. And read beautifully. It lets you work seamlessly between Mac and iOS devices, and... Read more

Hearthstone celebrates the upcoming Jour...
Hearthstone gets a new expansion, Journey to Un'Goro, in a little over a week, and they'll be welcoming the Year of the Mammoth, the next season, at the same time. There's a lot to be excited about, so Blizzard is celebrating in kind. Players will... | Read more »
4 smart and stylish puzzle games like Ty...
TypeShift launched a little over a week ago, offering some puzzling new challenges for word nerds equipped with an iOS device. Created by Zach Gage, the mind behind Spelltower, TypeShift boasts, like its predecessor, a sleak design and some very... | Read more »
The best deals on the App Store this wee...
Deals, deals, deals. We're all about a good bargain here on 148Apps, and luckily this was another fine week in App Store discounts. There's a big board game sale happening right now, and a few fine indies are still discounted through the weekend.... | Read more »
The best new games we played this week
It's been quite the week, but now that all of that business is out of the way, it's time to hunker down with some of the excellent games that were released over the past few days. There's a fair few to help you relax in your down time or if you're... | Read more »
Orphan Black: The Game (Games)
Orphan Black: The Game 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: Dive into a dark and twisted puzzle-adventure that retells the pivotal events of Orphan Black. | Read more »
The Elder Scrolls: Legends is now availa...
| Read more »
Ticket to Earth beginner's guide: H...
Robot Circus launched Ticket to Earth as part of the App Store's indie games event last week. If you're not quite digging the space operatics Mass Effect: Andromeda is serving up, you'll be pleased to know that there's a surprising alternative on... | Read more »
Leap to victory in Nexx Studios new plat...
You’re always a hop, skip, and a jump away from a fiery death in Temple Jump, a new platformer-cum-endless runner from Nexx Studio. It’s out now on both iOS and Android if you’re an adventurer seeking treasure in a crumbling, pixel-laden temple. | Read more »
Failbetter Games details changes coming...
Sunless Sea, Failbetter Games' dark and gloomy sea explorer, sets sail for the iPad tomorrow. Ahead of the game's launch, Failbetter took to Twitter to discuss what will be different in the mobile version of the game. Many of the changes make... | Read more »
Splish, splash! The Pokémon GO Water Fes...
Niantic is back with a new festival for dedicated Pokémon GO collectors. The Water Festival officially kicks off today at 1 P.M. PDT and runs through March 29. Magikarp, Squirtle, Totodile, and their assorted evolved forms will be appearing at... | Read more »

Price Scanner via

Apple refurbished Apple Watches available sta...
Apple is now offering Certified Refurbished Series 1 and Series 2 Apple Watches for 14-16% off MSRP, starting at $229. An Apple one-year warranty is included with each watch. Shipping is free: Series... Read more
9-inch 32GB Space Gray iPad Pro on sale for $...
B&H Photo has the 9.7″ 32GB Space Gray Apple iPad Pro on sale for $549 for a limited time. Shipping is free, and B&H charges NY sales tax only. Their price is $50 off MSRP. Read more
13-inch MacBook Airs on sale for $100-$150 of...
B&H Photo has 13″ MacBook Airs on sale for up to $150 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 13″ 1.6GHz/128GB MacBook Air (MMGF2LL/A): $899 $100 off MSRP - 13″ 1.... Read more
13-inch MacBook Airs, Apple refurbished, in s...
Apple has Certified Refurbished 2016 13″ MacBook Airs available starting at $849. An Apple one-year warranty is included with each MacBook, and shipping is free: - 13″ 1.6GHz/8GB/128GB MacBook Air: $... Read more
12-inch Retina MacBooks on sale for $1199, sa...
B&H has 12″ 1.1GHz Retina MacBooks on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - 12″ 1.1GHz Space Gray Retina MacBook: $1199 $100 off MSRP - 12″ 1.1GHz... Read more
Save up to $260 with Apple refurbished 12-inc...
Apple has Certified Refurbished 2016 12″ Retina MacBooks available for $200-$260 off MSRP. Apple will include a standard one-year warranty with each MacBook, and shipping is free. The following... Read more
13-inch 2.7GHz Retina MacBook Pro on sale for...
B&H Photo has the 2015 13″ 2.7GHz/128GB Retina Apple MacBook Pro on sale for $170 off MSRP. Shipping is free, and B&H charges NY tax only: - 13″ 2.7GHz/128GB Retina MacBook Pro (MF839LL/A): $... Read more
15-inch 2.2GHz Retina MacBook Pro on sale for...
B&H Photo has the 2015 15″ 2.2GHz Retina MacBook Pro (MJLQ2LL/A) on sale for $1799.99 including free shipping plus NY sales tax only. Their price is $200 off MSRP. Read more
Save up to $160 with Apple refurbished 9-inch...
Apple has Certified Refurbished 9″ and 12″ Apple iPad Pros available for up to $160 off the cost of new iPads. An Apple one-year warranty is included with each model, and shipping is free: - 32GB 9″... Read more
Apple Chip Foundry TSMC To Begin A11 System-o...
Digitimes’ Steve Shen is reporting today that according to the Chinese-language Economic Daily News (EDN), chipmaker and major Apple supplier foundery Taiwan Semiconductor Manufacturing Company (TSMC... Read more

Jobs Board

*Apple* Mobile Master - Best Buy (United Sta...
**493168BR** **Job Title:** Apple Mobile Master **Location Number:** 000827-Denton-Store **Job Description:** **What does a Best Buy Apple Mobile Master do?** At Read more
Fulltime aan de slag als shopmanager in een h...
Ben jij helemaal gek van Apple -producten en vind je het helemaal super om fulltime shopmanager te zijn in een jonge en hippe elektronicazaak? Wil jij werken in Read more
*Apple* Mobile Master - Best Buy (United Sta...
**492889BR** **Job Title:** Apple Mobile Master **Location Number:** 000886-Norwalk-Store **Job Description:** **What does a Best Buy Apple Mobile Master do?** Read more
*Apple* Mobile Master - Best Buy (United Sta...
**492472BR** **Job Title:** Apple Mobile Master **Location Number:** 000470-Seattle-Store **Job Description:** **What does a Best Buy Apple Mobile Master do?** Read more
*Apple* Mobile Master - Best Buy (United Sta...
**492562BR** **Job Title:** Apple Mobile Master **Location Number:** 000853-Jackson-Store **Job Description:** **What does a Best Buy Apple Mobile Master do?** Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.