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

Apple GarageBand 10.1.4 - Complete recor...
The new GarageBand is a whole music creation studio right inside your Mac -- complete with keyboard, synths, orchestral and percussion instruments, presets for guitar and voice, an entirely... Read more
Alfred 3.2.1 - Quick launcher for apps a...
Alfred is an award-winning productivity application for OS X. Alfred saves you time when you search for files online or on your Mac. Be more productive with hotkeys, keywords, and file actions at... Read more
Adobe Lightroom 6.8 - Import, develop, a...
Adobe Lightroom is available as part of Adobe Creative Cloud for as little as $9.99/month bundled with Photoshop CC as part of the photography package. Lightroom 6 is also available for purchase as a... Read more
Backblaze - Online backup serv...
Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $5 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more
OmniPlan Pro 3.6 - Professional-grade pr...
With OmniPlan Pro, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success... Read more
OmniPlan 3.6 - Robust project management...
With OmniPlan, you can create logical, manageable project plans with Gantt charts, schedules, summaries, milestones, and critical paths. Break down the tasks needed to make your project a success,... Read more
Little Snitch 3.7.1 - Alerts you about o...
Little Snitch gives you control over your private outgoing data. Track background activity As soon as your computer connects to the Internet, applications often have permission to send any... Read more
iMazing 2.1.3 - Complete iOS device mana...
iMazing (was DiskAid) is the ultimate iOS device manager with capabilities far beyond what iTunes offers. With iMazing and your iOS device (iPhone, iPad, or iPod), you can: Copy music to and from... Read more
AppDelete 4.3.1 - $7.99
AppDelete is an uninstaller that will remove not only applications but also widgets, preference panes, plugins, and screensavers along with their associated files. Without AppDelete these associated... Read more
FileZilla - Fast and reliable F...
FileZilla (ported from Windows) is a fast and reliable FTP client and server with lots of useful features and an intuitive interface. Version Bug Fixes and Minor Changes Speed up icon... Read more

Latest Forum Discussions

See All

Galaxy on Fire 3 and four other fantasti...
Galaxy on Fire 3 - Manticore brings the series back for another round of daring space battles. It's familiar territory for folks who are familiar with the franchise. If you've beaten the game and are looking to broaden your horizons, might we... | Read more »
The best apps for your holiday gift exch...
What's that, you say? You still haven't started your holiday shopping? Don't beat yourself up over it -- a lot of people have been putting it off, too. It's become easier and easier to procrastinate gift shopping thanks to a number of apps that... | Read more »
Toca Hair Salon 3 (Education)
Toca Hair Salon 3 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: | Read more »
Winter comes to Darkwood as Seekers Note...
MyTona, based in the chilly Siberian city of Yakutsk, has brought a little festive fun to its hidden object game Seekers Notes: Hidden Mystery. The Christmas update introduces some new inhabitants to players, and with them a chance to win plenty of... | Read more »
Bully: Anniversary Edition (Games)
Bully: Anniversary Edition 1.03.1 Device: iOS Universal Category: Games Price: $6.99, Version: 1.03.1 (iTunes) Description: *** PLEASE NOTE: This game is officially supported on the following devices: iPhone 5 and newer, iPod Touch... | Read more »
PINE GROVE 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: A pine grove where there are no footsteps of people due to continuous missing cases. The case is still unsolved and nothing has... | Read more »
Niantic teases new Pokémon announcement...
After rumors started swirling yesterday, it turns out there is an official Pokémon GO update on its way. We’ll find out what’s in store for us and our growing Pokémon collections tomorrow during the Starbucks event, but Niantic will be revealing... | Read more »
3 reasons why Nicki Minaj: The Empire is...
Nicki Minaj is as business-savvy as she is musically talented and she’s proved that by launching her own game. Designed by Glu, purveyors of other fine celebrity games like cult favorite Kim Kardashian: Hollywood, Nicki Minaj: The Empire launched... | Read more »
Clash of Clans is getting its own animat...
Riding on its unending wave of fame and success, Clash of Clans is getting an animated web series based on its Clash-A-Rama animated shorts.As opposed to the current shorts' 60 second run time, the new and improved Clash-A-Rama will be comprised of... | Read more »
Leaks hint at Pokémon GO and Starbucks C...
Leaked images from a hub for Starbucks employees suggests that a big Pokémon GO event with the coffee giant could begin this very week. The images appeared on Reddit and hint at some exciting new things to come for Niantic's smash hit game. | Read more »

Price Scanner via

12-inch Retina MacBooks, Apple refurbished, n...
Apple has restocked a full line of Certified Refurbished 2016 12″ Retina MacBooks, now available for $200-$260 off MSRP. Refurbished 2015 models are available starting at $929. Apple will include a... Read more
Holiday sale: 12-inch Retina MacBook for $100...
B&H has 12″ Retina MacBooks on sale for $100 off MSRP as part of their Holiday sale. Shipping is free, and B&H charges NY sales tax only: - 12″ 1.1GHz Space Gray Retina MacBook: $1199 $100... Read more
Apple refurbished 13-inch MacBook Airs availa...
Apple has Certified Refurbished 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: $849 $... Read more
Apple refurbished iMacs available for up to $...
Apple has Certified Refurbished 2015 21″ & 27″ iMacs available for up to $350 off MSRP. Apple’s one-year warranty is standard, and shipping is free. The following models are available: - 21″ 3.... Read more
Apple’s Education discount saves up to $300 o...
Purchase a new Mac or iPad using Apple’s Education Store and take up to $300 off MSRP. All teachers, students, and staff of any educational institution qualify for the discount. Shipping is free: -... Read more
Back in stock: Apple refurbished Mac minis fr...
Apple has Certified Refurbished Mac minis available starting at $419. Apple’s one-year warranty is included with each mini, and shipping is free: - 1.4GHz Mac mini: $419 $80 off MSRP - 2.6GHz Mac... Read more
Twenty-Five Years Of Apple Laptops – A person...
Among many other things, the often tumultuous 16th year of the new century marked the 25th anniversary of Apple laptop computers, not counting the optimistically named 16-pound Mac Portable of 1989.... Read more
Landlordy iOS App Adds Support For Appliances...
Riga, Latvia based E-protect SIA is releasing major update (version 1.8) to its Landlordy app for managing rental business financials on the go. Landlordy is iPhone and iPad app designed for self-... Read more
Holiday sale, Apple iMacs for up to $200 off...
B&H Photo has 21″ and 27″ Apple iMacs on sale for up to $200 off MSRP including free shipping plus NY sales tax only: - 27″ 3.3GHz iMac 5K: $2099 $200 off MSRP - 27″ 3.2GHz/1TB Fusion iMac 5K: $... Read more
Holiday sale: Mac minis for $50 to $100 off M...
B&H Photo has Mac minis on sale for up to $100 off MSRP free shipping plus NY sales tax only: - 1.4GHz Mac mini: $449 $50 off MSRP - 2.6GHz Mac mini: $629 $70 off MSRP - 2.8GHz Mac mini: $899 $... Read more

Jobs Board

*Apple* Retail - Multiple Positions - Apple,...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Retail - Multiple Positions- Trumbul...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions- Philade...
Job Description: Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Retail - Multiple Positions- San Ant...
Job Description:SalesSpecialist - Retail Customer Service and SalesTransform Apple Store visitors into loyal Apple customers. When customers enter the store, Read more
*Apple* Products Tester Needed - Apple (Unit...
…we therefore look forward to put out products to quality test for durability. Apple leads the digital music revolution with its iPods and iTunes online store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.