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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.