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;
PrOpen();
printerPort = PrOpenDoc( printRecord, nil, nil );
PrOpenPage( printerPort, nil );
// QuickDraw calls that define what's to be printed
PrClosePage( printerPort );  
PrCloseDoc( printerPort );
PrClose();

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) );
PrOpen();
PrintDefault( gPrintRecord );
PrClose();

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:
	PrOpen();   
	PrStlDialog( gPrintRecord );
	PrClose();   
	break;

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();
	PrOpen();
	doPrint = PrJobDialog( gPrintRecord );
	if ( doPrint == true )
		DoPrintChoice( window );
	PrClose();
	break;

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 );
}

PrintDoc

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 <ftp://ftp.mactech.com/src/>. 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 )
{  
	ToolBoxInit();
	PrintingInit();
	MenuBarInit();
	EventLoop();
}

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

/******************** ToolBoxInit ********************/
void		ToolBoxInit( void )
{
	InitGraf( &qd.thePort );
	InitFonts();
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs( nil );
	InitCursor();
}

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) );
  PrOpen();
  PrintDefault( gPrintRecord );
  PrClose();
}

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 );  
	DrawMenuBar();
}

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 - pictRect.top;
	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 );
			break;
		case keyDown:
		case autoKey:
			theChar = eventPtr->message & charCodeMask;
			if ( (eventPtr->modifiers & cmdKey) != 0 ) 
				HandleMenuChoice( MenuKey( theChar ) );
			break;
		case updateEvt:
			BeginUpdate( (WindowPtr)(eventPtr->message) );
			EndUpdate( (WindowPtr)(eventPtr->message) );
			break;
	}
}

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:
			AdjustMenus();
			menuChoice = MenuSelect( eventPtr->where );
			HandleMenuChoice( menuChoice );
			break;
		case inSysWindow : 
			SystemClick( eventPtr, window );
			break;
		case inContent:
			SelectWindow( window );
			break;
		case inDrag : 
			DragWindow( window, eventPtr->where,
								 &qd.screenBits.bounds);
			break;
		case inGoAway:
			if ( TrackGoAway( window, eventPtr->where ) )
				DisposeWindow( window );
			break;
	}
}

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 );
	}
	else
	{
		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 );
				break;
			case mFile:
				HandleFileChoice( item );
				break;
		}
		HiliteMenu( 0 );
	}
}
/***************** HandleAppleChoice *****************/
void		HandleAppleChoice( short item )
{
	MenuHandle		appleMenu;
	Str255				accName;
	short				accNumber;
	switch ( item )
	{
		case iAbout:
			SysBeep( 10 );
			break;
		default:
			appleMenu = GetMenuHandle( mApple );
			GetMenuItemText( appleMenu, item, accName );
			accNumber = OpenDeskAcc( accName );
			break;
	}
}

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:
			OpenPictureDisplayWindow();
			break;
		case iClose:
			window = FrontWindow();
			DisposeWindow( window );
		break;
		case iPageSetup:
			PrOpen();   
			PrStlDialog( gPrintRecord );
			PrClose();   
			break;
		case iPrint:
			window = FrontWindow();
			PrOpen();   
			doPrint = PrJobDialog( gPrintRecord );
			if ( doPrint == true )
				DoPrintChoice( window );
				PrClose();   
			break;
		case iQuit:
			gDone = true;
			break;
	}
} 

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 );
	ExitToShell();
}

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

Latest Forum Discussions

See All

Netflix Games expands its catalogue with...
It is a good time to be a Netflix subscriber this month. I presume there's a good show or two, but we are, of course, talking about their gaming service that seems to be picking up steam lately. May is adding five new titles, and there are some... | Read more »
Seven Knights Idle Adventure drafts in a...
Seven Knights Idle Adventure is opening up more stages, passing the 15k mark, and players may find themselves in need of more help to clear these higher stages. Well, the cavalry has arrived with the introduction of the Legendary Hero Iris, as... | Read more »
AFK Arena celebrates five years of 100 m...
Lilith Games is quite the behemoth when it comes to mobile games, with Rise of Kingdom and Dislyte firmly planting them as a bit name. Also up there is AFK Arena, which is celebrating a double whammy of its 5th anniversary, as well as blazing past... | Read more »
Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »

Price Scanner via MacPrices.net

Apple introduces the new M4-powered 11-inch a...
Today, Apple revealed the new 2024 M4 iPad Pro series, boasting a surprisingly thin and light design that pushes the boundaries of portability and performance. Offered in silver and space black... Read more
Apple introduces the new 2024 11-inch and 13-...
Apple has unveiled the revamped 11-inch and brand-new 13-inch iPad Air models, upgraded with the M2 chip. Marking the first time it’s offered in two sizes, the 11-inch iPad Air retains its super-... Read more
Apple discontinues 9th-gen iPad, drops prices...
With today’s introduction of the new 2024 iPad Airs and iPad Pros, Apple has (finally) discontinued the older 9th-generation iPad with a home button. In response, they also dropped prices on 10th-... Read more
Apple AirPods on sale for record-low prices t...
Best Buy has Apple AirPods on sale for record-low prices today starting at only $79. Buy online and choose free shipping or free local store pickup (if available). Sale price for online orders only,... Read more
13-inch M3 MacBook Airs on sale for $100 off...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices, along with Amazon’s, are the lowest currently available for new 13″... Read more
Amazon is offering a $100 discount on every 1...
Amazon has every configuration and color of Apple’s 13″ M3 MacBook Air on sale for $100 off MSRP, now starting at $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD): $999 $100 off... Read more
Sunday Sale: Take $150 off every 15-inch M3 M...
Amazon is now offering a $150 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 15″ M3 MacBook... Read more
Apple’s 24-inch M3 iMacs are on sale for $150...
Amazon is offering a $150 discount on Apple’s new M3-powered 24″ iMacs. Prices start at $1149 for models with 8GB of RAM and 256GB of storage: – 24″ M3 iMac/8-core GPU/8GB/256GB: $1149.99, $150 off... Read more
Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more

Jobs Board

Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
LPN-Physician Office Nurse - Orthopedics- *Ap...
LPN-Physician Office Nurse - Orthopedics- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Supervisor/Therapist Rehabilitation Medicine...
Supervisor/Therapist Rehabilitation Medicine - Apple Hill (Outpatient Clinic) - Day Location: York Hospital, York, PA Schedule: Full Time Sign-On Bonus Eligible Read more
BBW Sales Support- *Apple* Blossom Mall - Ba...
BBW Sales Support- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 04388 Job Area: Store: Sales and Support Read more
BBW Supervisor- *Apple* Blossom Mall - Bath...
BBW Supervisor- APPLE BLOSSOM MALL Brand: Bath & Body Works Location: Winchester, VA, US Location Type: On-site Job ID: 0435C Job Area: Store: Management Employment Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.