Printing Windows
Volume Number: | | 6
|
Issue Number: | | 5
|
Column Tag: | | C Workshop
|
Related Info: Quickdraw TextEdit Picture Utilities
Printing Windows and Dialogs ![](img001.gif)
By Kirk Chase, Anaheim, CA
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
Printing Windows
Not so long ago, I was working on a small, in-house project. I got it running, debugged it, and then showed it to a co-worker for that ever-refreshing pat on the back for a job well done. Well, the co-worker looked at it and played around with it for a moment. Then he looked up to me and said, I cant print it out!
The project did not have the ability to print out its contents. It was small enough that I thought that nobody would ever be on it long enough to care about a hard copy print out. In fact, the only reason I had put a save and open procedure was because I had a small, single data structure that was easy to write out and read in. I figured printing was not really needed since the job was small and the window contents contained some items that were not made for printing (scroll bars, buttons, lists, and so on).
After talking myself into finding a way to print it out, I went back to the keyboard to try and find an easy, generic way, to print out the window. My first attempt was this:
1. Open up a picture.
2. Call the update procedure for my window, thereby recording the drawing commands.
3. Close the picture.
4. When it came time to print, I would draw the picture in the print port in a rectangle that was positioned where I wanted it to be.
This had all the elements of being simple. A quick cut and paste brought the print loop in. A few more lines of code, and I was printing out the picture just fine. It worked great, at least on windows that were not very complex. Another approach was needed for more complex windows.
Another Approach
Looking at my update procedure, I saw many simple drawing commands, a line here, a rectangle there, some text, and so on. So I said, why not just skip the picture and call the drawing routines directly when needed. Cut, cut, paste, paste. And all was well until I printed it out. Oh, most of it came out. I took a closer look at those items that did not show up on the printed page. There were no controls, no scrolling list, and no edit text field. A quick look at the offending structures all yielded a simple conclusion: they were all tied to the grafport that was my window. It was a battle between the window and the printer port. When it came time to draw those structures, they could not be coaxed into changing sides for just a moment. Well, no bucket of sand and metal was going to tell me what I could and could not print.
It was clear that I needed to put in a flag so that I could branch off to a different update routine when printing. Then I got to work on the structures: the TextEdit record, the list, and the controls.
The TextEdit record was fairly simple. I just changed the port temporarily to the print port. The code looked like this:
/* 1 */
if (theTE != NIL)
if (!forPrinting)
TEUpdate(&tempRect,theTE); /* normal updating */
else {
OldPort = (**theTE).inPort; /* change port in TE */
GetPort(&aPort);
(**theTE).inPort = aPort;
TEUpdate(&tempRect,theTE); /* normal updating */
(**theTE).inPort = OldPort; /* restore port in TE */
}
Simple and sweet.
In Control
The control routines were a touch more difficult. I started out with my own version of DrawControls(). It looked like this:
/* 2 */
/* Draw or Print Controls */
if (!forPrinting)
DrawControls(MyWindow);
else
PrintControls(((WindowPeek) MyWindow)->controlList);
and the PrintControls() looked like this:
/* 3 */
PrintControls(theControl)
ControlHandle theControl;
{ /* PrintControls() */
ProcPtr controlRoutine;
long dummy;
int varCode;
while (theControl != NULL) { /* control loop */
varCode = GetCVariant(theControl);
HLock((Handle) (**theControl).contrlDefProc);
controlRoutine =(ProcPtr) *((**theControl).contrlDefProc);
dummy = CallPascalL(varCode, theControl, 0, 0L, controlRoutine); /*
Call CDEF to Draw */
HUnlock((Handle) (**theControl).contrlDefProc);
theControl = (**theControl).nextControl;
} /* control loop */
} /* PrintControls() */
What this bit of code does is this. It loops through the control list. It gets the controls variant and CDEF proc. It then calls the control proc with the message to draw the control.
This worked like a dream except for the controls the that were inactive. In there place was a gray box. The problem was simple. When controls go inactive, for the most part, they just paint a gray rectangle over their control with the pen mode set to exclusive-or. This little trick dims the control. Unfortunately, the LaserWriter does not support this mode of drawing. Hence the gray box is the only thing that is seen (overdrawing the control). The only solution I could think of was to temporarily activate the control and then set it back when finished drawing. Something like this:
/* 4 */
PrintControls(theControl)
ControlHandle theControl;
{ /* PrintControls() */
ProcPtr controlRoutine;
long dummy;
int varCode, hilite;
while (theControl != NULL) { /* control loop */
varCode = GetCVariant(theControl);
hilite = (**theControl).contrlHilite;
HiliteControl(theControl, 0); /* LW does not support XOR */
HLock((Handle) (**theControl).contrlDefProc);
controlRoutine =(ProcPtr) *((**theControl).contrlDefProc);
dummy = CallPascalL(varCode, theControl, 0, 0L, controlRoutine); /*
Call CDEF to Draw */
HUnlock((Handle) (**theControl).contrlDefProc);
HiliteControl(theControl, hilite);
theControl = (**theControl).nextControl;
} /* control loop */
} /* PrintControls() */
This worked out well, and I was happy again. In addition to this, it would work on all controls, not just the standard ones.
Next on the List
The next step was the printing of the list. This turned out to be the hardest. My first step was to try and fake out the list into thinking that it belonged to the printer port. No such luck. I then tried calling the LDEF like with the controls, still no luck. At last resort, I wrote a routine for just text lists. The call in my window update procedure looks like:
/* 5 */
if (List_I_AList != NIL)
if (!forPrinting)
LUpdate(MyWindow->visRgn,List_I_AList);
else
PrintList(List_I_AList); /* call print list */
And PrintList() is as follows:
/* 6 */
PrintList(theList)
ListHandle theList;
{ /* PrintList() */
Cell lCell;
Rect lRect, dstRect;
int lDataOffset, lDataLen;
char dataPtr[255];
if (theList == NULL) return;
lCell.h = 0;
lCell.v = 0;
LDoDraw(TRUE, theList);
do { /* loop through cells */
LFind(&lDataOffset, &lDataLen, lCell, theList);
LGetCell(&dataPtr, &lDataLen, lCell, theList);
LRect(&lRect, lCell, theList);
if (SectRect(&((**theList).rView), &lRect, &dstRect))
if (EqualRect(&lRect, &dstRect)) { /* Draw it */
TextBox(&dataPtr, lDataLen, &lRect, teJustLeft);
}
} while (LNextCell(TRUE, TRUE, &lCell, theList)); /* loop through cells
*/
} /* PrintList() */
What this procedure does is to loop through all the cells checking if the cell is visible. If it is, it extracts the text and prints it out using TextBox() (Not very efficient).
PrintWindow()
PrintWindow() is fairly simple. You need only offset the origin of the print port by the proper amount, set the printing flag, and call your update routine. In your update routine, you should make sure not to set the port to your window (do this before the call) and then check the printing flag to use the proper update routines for TE records, lists, and controls. PrintWindow() could stand for a few improvements.
1. Thumb controls are still not drawn.
2. A routine that handled any LDEF not just the standard text LDEF.
3. Scrolling the text in a TE record to the proper place.
4. Hilighting list and text selections.
Printing Dialogs
Dialogs turned out to be a bit more difficult. Granted, dialogs are not printed very often, but it is nice to be able to do so when the occasion requires. Drawing is a two step process. First you print the items in the dialog item list, and then you call any update routines you use (like bolding the default button) just as you did for printing the window. In my print loop, I call all the various drawing routines, passing it the port I wish to print, and let the various routines decide if this is their window/dialog or not.
The Dialog Item List
The dialog item list is found in the dialog structure as shown in figure 1.
![](img003.gif)
Figure 1. The Dialog Structure
![](img004.gif)
Figure 2. The Item List Structure
The item list is a more complicated structure. I found the item list at a single indirection rather than a double indirection (items in the dialog record is a pointer instead of a handle) Refer to figure 2 while I explain the item list structure.
The first item is a 2 byte number that is the number of items in the item list minus 1 (for 7 items, the number would be 6). Next comes the items.
Each item has a 4 byte placeholder for a handle to the object or a procedure pointer. The next 8 bytes are the display rectangle (Top, Left, Bottom, Right order). The next byte is the item type. The types are as follows:
0 User item (userItem)
4 Control item (ctrlItem)
add the following constants for the exact control
0 for a button (btnCtrl)
1 for a checkbox (chkCtrl)
2 for a radio button (radCtrl)
3 for a resource defined control (resCtrl)
8 Static text item (statText)
16 Editable text item (editText)
32 Icon (iconItem)
64 Picture (picItem)
In addition to this, for any disabled item, you add 128 (itemDisable).
Following the item type byte is a length byte of the data that is to follow. Then it is followed by that many bytes of data. If the number of bytes is odd, then an extra byte is added to pad the item to an even byte boundary. The length byte still contains the actual data length.
The data and the placeholder vary by the item type. A user item has 0 bytes of data and the placeholder is interpreted as a procedure pointer to the drawing routine. A picture or icon has the resource ID of the item in the data bytes and the placeholder holds the handle to the picture or icon. Static and edit text hold the default text in their data bytes; the placeholder holds a handle to the actual text. All controls, except for the resource defined control have their control titles in the data bytes; for a resource control, it has the resource ID number; the placeholder holds the control handle for all control types.
Printing the Dialog
My routine for printing the dialog is as follows:
/* 7 */
PrintDialog(d, s0, s1, s2, s3)
DialogPeek d;
Str255 s0, s1, s2, s3;
{ /* PrintDialog() */
ditlheader *dh;
Ptr p;
int itemCount, i, theItem, datalen;
int ResID, theType;
Handle hItem;
char text[256];
Rect tempRect, box;
p = (Ptr) *(*d).items;
itemCount = *((int *) p); /* get number of dialog items */
p = p + 2;
for (i=0; i<= itemCount; i++) { /* Item Loop */
dh = (ditlheader *) p;
/* get type */
theItem = dh->itemType;
if (theItem < 0)
theItem = theItem * -1;
if (theItem >= 128)
theItem -= 128;
/* get length of data */
datalen = dh->dataLength;
if ((datalen % 2) != 0)
datalen += 1;
switch (theItem) { /* ItemType Switch */
case ctrlItem + btnCtrl: /* Standard Button */
case ctrlItem + chkCtrl: /* Checkbox */
case ctrlItem + radCtrl: /* Radio Button */
case ctrlItem + resCtrl: /* Resource Control */
PrintControls((ControlHandle) dh->HorP); /* Draw controls */
break;
case editText:
case statText:
GetDItem((DialogPtr) d, i + 1, &theType, &hItem, &box);
GetIText(hItem, (Str255 *) text);
PtoCstr(text);
/* make substitution of ParamText */
PtoCstr((char *) s0);
PtoCstr((char *) s1);
PtoCstr((char *) s2);
PtoCstr((char *) s3);
while(StrReplace(text, ^0, (char *) s0));
while(StrReplace(text, ^1, (char *) s1));
while(StrReplace(text, ^2, (char *) s2));
while(StrReplace(text, ^3, (char *) s3));
CtoPstr((char *) s0);
CtoPstr((char *) s1);
CtoPstr((char *) s2);
CtoPstr((char *) s3);
TextBox(text, strlen(text), &(dh->displayRect), teJustLeft); /* draw
text */
if (theItem == editText) { /* draw edit text box */
tempRect = dh->displayRect;
FrameRect(&tempRect);
}
break;
case iconItem:
PlotIcon(&(dh->displayRect), (Handle) dh->HorP); /* draw icon */
break;
case picItem:
DrawPicture((PicHandle) dh->HorP, &(dh->displayRect)); /* draw picture
*/
break;
case userItem:
CallPascal((WindowPtr) ThePrintPort, i+ 1, (ProcPtr) dh->HorP); /* draw
user item */
break;
default:
break;
} /* ItemType Switch */
p = p + sizeof(ditlheader) + datalen;
} /* Item Loop */
} /* PrintDialog() */
The first step in the routine is to figure out how many items there are in the item list. Then it loops through the item getting the associated data and moving the pointer if needed so that it can get the next dialog item.
It then switches on the item type (accounting for any disabled items in the process). For controls, it just sends the placeholder as a control handle to my PrintControls() that I wrote for printing a window. For icons and pictures, it interprets the placeholder as an icon or picture handle and calls the toolbox routines for printing these items. For user items, it calls the procedure pointer found in the placeholder.
Editable and static text took just a little more coding. After calling GetDItem() and GetIText() to get the actual text, I call a substitution routine to remove the parameter text characters (^0, ^1, ^2, ^3) with the strings passed to PrintDialog(). The substitution routine makes the first possible substitution then returns 1 if a substitution was made and 0 if no substitutions were made. Putting the call in a while loop removes all occurrences. After this, it just calls TextBox() again to draw the text, and, if the item was an edit text item, it frames the box.
Conclusion
All in all, printing dialogs and windows are not too hard with a little work on the drawing procedures and an routine to go through a dialogs item list. In my listing, I reference "Messenger.h" which was in last month's MacTutor. These routines are not made for printing out more than the contents of the window or dialog that you can see. It is not made for text editors or drawing applications. But if the window just needs a quick print with minimal set up, PrintWindow() and PrintDialog() are for you.
Listing: PrintWD.c
/*******************
PrintWD.c
*******************/
/**********************
Include files
***********************/
#include String.h
#include Messenger.h
#include PrintTraps.h
/**********************
Structures
***********************/
typedef struct ditlheader {
long HorP;
Rect displayRect;
char itemType;
char dataLength;
} ditlheader, *ditlheaderPtr, **ditlheaderHdl;
/**********************
Globals
***********************/
THPrint ThePrintRec; /* Printing Stuff */
TPPrPortThePrintPort;
TPrStatus PrintStatus;
Rect PageRect;
char forPrinting;
/*************************************************/
/*********** Printing Routines *******************/
/*************************************************/
/***************************/
/* PrintControls() handles printing of standard buttons */
PrintControls(theControl)
ControlHandle theControl;
{ /* PrintControls() */
ProcPtr controlRoutine;
long dummy;
int varCode, hilite;
while (theControl != NULL) { /* control loop */
varCode = GetCVariant(theControl);
hilite = (**theControl).contrlHilite;
HiliteControl(theControl, 0); /* LW does not support XOR */
HLock((Handle) (**theControl).contrlDefProc);
controlRoutine =(ProcPtr) *((**theControl).contrlDefProc);
dummy = CallPascalL(varCode, theControl, 0, 0L, controlRoutine); /*
Call CDEF to Draw */
HUnlock((Handle) (**theControl).contrlDefProc);
HiliteControl(theControl, hilite);
theControl = (**theControl).nextControl;
} /* control loop */
} /* PrintControls() */
/***************************/
/* PrintList() handles printing of standard list */
PrintList(theList)
ListHandle theList;
{ /* PrintList() */
Cell lCell;
Rect lRect, dstRect;
int lDataOffset, lDataLen;
char dataPtr[255];
if (theList == NULL) return;
lCell.h = 0;
lCell.v = 0;
LDoDraw(TRUE, theList);
do { /* loop through cells */
LFind(&lDataOffset, &lDataLen, lCell, theList);
LGetCell(&dataPtr, &lDataLen, lCell, theList);
LRect(&lRect, lCell, theList);
if (SectRect(&((**theList).rView), &lRect, &dstRect))
if (EqualRect(&lRect, &dstRect)) { /* Draw it */
TextBox(&dataPtr, lDataLen, &lRect, teJustLeft);
}
} while (LNextCell(TRUE, TRUE, &lCell, theList)); /* loop through cells
*/
} /* PrintList() */
/***************************/
/* StrReplace() substitutes the first occurrence of t with r in s. It
returns TRUE if there was a substitution*/
int StrReplace(s, t, r)
char *s, *t, *r;
{ /* StrReplace() */
char temp[256], *p;
strcpy(temp, s);
p = strstr(temp, t); /* find first occurrence */
if (*p == \0) return (0); /* no occurence */
*p = \0; /* delete t */
p = p + strlen(t); /* go to second half of string */
strcpy(s, temp); /* get first part */
strcat(s, r); /* put in replacement */
strcat(s, p); /* put in second half */
return (1);
} /* StrReplace() */
/***************************/
/* PrintDialog()
prints dialog substituting sX for ParamText */
PrintDialog(d, s0, s1, s2, s3)
DialogPeek d;
Str255 s0, s1, s2, s3;
{ /* PrintDialog() */
ditlheader *dh;
Ptr p;
int itemCount, i, theItem, datalen;
int ResID, theType;
Handle hItem;
char text[256];
Rect tempRect, box;
p = (Ptr) *(*d).items;
itemCount = *((int *) p); /* get number of dialog items */
p = p + 2;
for (i=0; i<= itemCount; i++) { /* Item Loop */
dh = (ditlheader *) p;
/* get type */
theItem = dh->itemType;
if (theItem < 0)
theItem = theItem * -1;
if (theItem >= 128)
theItem -= 128;
/* get length of data */
datalen = dh->dataLength;
if ((datalen % 2) != 0)
datalen += 1;
switch (theItem) { /* ItemType Switch */
case ctrlItem + btnCtrl: /* Standard Button */
case ctrlItem + chkCtrl: /* Checkbox */
case ctrlItem + radCtrl: /* Radio Button */
case ctrlItem + resCtrl: /* Resource Control */
PrintControls((ControlHandle) dh->HorP); /* Draw controls */
break;
case editText:
case statText:
GetDItem((DialogPtr) d, i + 1, &theType, &hItem, &box);
GetIText(hItem, (Str255 *) text);
PtoCstr(text);
/* make substitution of ParamText */
PtoCstr((char *) s0);
PtoCstr((char *) s1);
PtoCstr((char *) s2);
PtoCstr((char *) s3);
while(StrReplace(text, ^0, (char *) s0));
while(StrReplace(text, ^1, (char *) s1));
while(StrReplace(text, ^2, (char *) s2));
while(StrReplace(text, ^3, (char *) s3));
CtoPstr((char *) s0);
CtoPstr((char *) s1);
CtoPstr((char *) s2);
CtoPstr((char *) s3);
TextBox(text, strlen(text), &(dh->displayRect), teJustLeft); /* draw
text */
if (theItem == editText) { /* draw edit text box */
tempRect = dh->displayRect;
FrameRect(&tempRect);
}
break;
case iconItem:
PlotIcon(&(dh->displayRect), (Handle) dh->HorP); /* draw icon */
break;
case picItem:
DrawPicture((PicHandle) dh->HorP, &(dh->displayRect)); /* draw picture
*/
break;
case userItem:
CallPascal((WindowPtr) ThePrintPort, i+ 1, (ProcPtr) dh->HorP); /* draw
user item */
break;
default:
break;
} /* ItemType Switch */
p = p + sizeof(ditlheader) + datalen;
} /* Item Loop */
} /* PrintDialog() */
/***************************/
/* InitPrint() initializes print variables */
InitPrint()
{
ThePrintRec = NUL;
PrOpen();
ThePrintRec = (THPrint) NewHandle(sizeof(TPrint));
PrintDefault(ThePrintRec);
PageRect = (*ThePrintRec)->prInfo.rPage;
PrClose();
forPrinting = FALSE;
PrSetError(noErr);
} /* end InitPrint() */
/***************************/
/* doPageSetUp() handles page setup dialog */
doPageSetUp()
{
char confirmed;
WindowPtr SavePort;
OSErr theError;
InitCursor();
GetPort(&SavePort);
PrOpen();
confirmed = PrValidate(ThePrintRec);
confirmed = PrStlDialog(ThePrintRec);
PrClose();
theError = PrError();
SetPort(SavePort);
if (confirmed) {
PageRect = (*ThePrintRec)->prInfo.rPage;
}
} /* end doPageSetUp() */
/***************************/
/* PrintWindow() handles printing */
PrintWindow(theWindow)
WindowPtr theWindow;
{
char DoIt; /* flag to do printing */
TPrStatus PrintStatus; /* print variables */
TPPrPortmyPrPort;
WindowPtr savePort;
intcopies, i, j, firstPage, lastPage, oldiPlayer;
char dummy;
Rect tempRect, pictRect, nameRect, windowRect;
OSErr theError;
GrafPtr aPort;
Point oldOrigin;
GetPort(&savePort); /* initialize printing */
InitCursor();
PrOpen();
if (PrError() == noErr){ /* do print dialog */
DoIt = PrValidate(ThePrintRec);
DoIt = PrJobDialog(ThePrintRec);
dummy = (int) AnOSError(PrError(), \pProblem with Print Dialog, \p);
if (DoIt && !dummy) { /* Print Document */
tempRect = theWindow->portRect;
/* SetCursor(*watch); */
InsetRect(&tempRect, -4, -4);
pictRect = tempRect; /* set print variables up */
PositionRect(&pictRect, &PageRect, CENTER, THIRD);
ThePrintPort = PrOpenDoc(ThePrintRec, NUL, NUL);
theError = PrError();
if (!AnOSError(theError, \pProblem with Printer, \p)) { /* good
port */
/* get copies and page range */
copies = (*ThePrintRec)->prJob.iCopies;
firstPage = (*ThePrintRec)->prJob.iFstPage;
lastPage = (*ThePrintRec)->prJob.iLstPage;
if ((*ThePrintRec)->prJob.bJDocLoop == bSpoolLoop) /* check for spooling
*/
copies = 1;
/* check bad page range */
if (firstPage > lastPage) {
InitCursor();
dummy = Message(M_OK, noIcon, \pBad page range, \p, \p, \p);
}
else { /* good page range */
for (i=0; i<copies; i++) { /* valid picture */
PrOpenPage(ThePrintPort, NUL); /* print page */
theError = PrError();
if (!AnOSError(theError, \pProblem with page, \p)) {
FrameRect(&pictRect);
GetPort(&aPort);
oldOrigin = topLeft(aPort->portRect);
SetOrigin(oldOrigin.h - pictRect.left, oldOrigin.v - pictRect.top);
forPrinting = TRUE;
UpDate_MyWindow(theWindow);
UpDate_AboutDialog(theWindow);
UpDate_TestDialog(theWindow);
forPrinting = FALSE;
SetOrigin(oldOrigin.h, oldOrigin.v);
}
PrClosePage(ThePrintPort);
} /* end valid picture */
}
} /* end else good port */
PrCloseDoc(ThePrintPort);
if (((*ThePrintRec)->prJob.bJDocLoop == bSpoolLoop) && (PrError() ==
noErr))
PrPicFile(ThePrintRec, NUL, NUL, NUL, &PrintStatus);
/* print spool file, if any */
} /* end of print document */
} /* no error on PrOpen() */
PrClose();
SetPort(savePort);
InvalRect(&tempRect);
InitCursor();
} /* end doPrint() */