home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * The FontShow *
- * *
- * (c) 1989 Rainer Fuchs, Am Weingarten 13, 6000 Frankfurt 90, FRG *
- * *
- * History: 17. Oktober 1989 (v1.1.1) Compatible with EZ-Menu *
- * 10. Oktober 1989 (v1.1) Font number added; Fontsize added *
- * 3. Oktober 1989 (v1.0) multiple sample strings *
- * 2. Oktober 1989 (v1.0b3) changed print format *
- * 26. September 1989 (v1.0b2) changed print format *
- * 25. September 1989 (v1.0b1) added character table *
- * 24. September 1989 (v0.0d1) first hack *
- * *
- ******************************************************************************/
-
- #include <PrintMgr.h>
- #include <ctype.h>
- #include <unix.h>
- #include "fontshow.h"
-
- /********************************* Prototypes ********************************/
-
- extern void ErrorMsg(int index);
- extern void ShowInfo(void);
- extern Boolean BreakCheck(void);
- extern void DrawPage(int i);
- extern void ShowFonts(void);
- extern int HowMany(void);
- extern void PrintFonts(void);
- extern pascal void DrawList(WindowPtr theWindow,int itemNo);
- extern pascal Boolean myFilter(DialogPtr theDialog,EventRecord *theEvent,
- int *itemHit);
- extern void InitList(WindowPtr theWindow);
- extern Boolean SelectFonts(void);
- extern void HandleMenu(long sel);
- extern void HandleMouseDown(EventRecord event);
- extern void MainLoop(void);
- extern void InitMenu(void);
- extern void InitText(void);
- extern void InitPrint(void);
- extern void BuildList(void);
- extern void InitThings(void);
- extern void CleanUp(void);
- extern void main(void);
-
- /******************************** Global variables ****************************/
-
- typedef struct {
- Str255 fontName; /* font name */
- int fontNum; /* font number */
- int lineHeight; /* line height */
- int widMax; /* maximal character width */
- Boolean flag; /* flag set if font is selected */
- }myFontInfo; /* infos about each font */
-
- myFontInfo **fontHdl; /* pointer to dynamic array of myFontInfos */
- ListHandle myList; /* list of font names */
- CursHandle myCursor[4]; /* busy cursor */
- MenuHandle myMenus[5]; /* menu bar */
- THPrint prRecHdl; /* print info */
- Handle myText; /* handle to sample text */
- int textLength; /* sample text length */
- int fontCount; /* nr of fonts in system */
- int opt; /* sample text or character map */
- Boolean prFlag; /* printer or screen output */
- Boolean quit; /* quit main loop */
- int fontSize; /* selected font size */
- int oldItem; /* font size menu item */
-
- /********************** Error and information handling ************************/
-
- void ErrorMsg(index) /* alert */
- int index;
- {
- Str255 theString;
-
- InitCursor();
- GetIndString(theString,128,index); /* get error msg from rsrc */
- ParamText(theString,'','','');
- StopAlert(128,NIL); /* show error dialog */
- }
-
- void ShowInfo() /* About FontShow… */
- {
- DialogPtr myDialog;
- EventRecord myEvent;
-
- myDialog=GetNewDialog(ABOUT,NIL,(WindowPtr)-1); /* get dialog from rsrc */
- DrawDialog(myDialog); /* draw it */
- while(!GetNextEvent(keyDownMask|autoKeyMask|mDownMask,&myEvent))
- SystemTask(); /* wait until event */
- DisposDialog(myDialog); /* dispose of dialog */
- }
-
- /*********************** Sample text output *********************************/
-
- Boolean BreakCheck() /* check for break after each page */
- {
- EventRecord theEvent;
- WindowPtr whichWindow;
-
- for(;;) { /* mouse clicks in menu bar ignored for comp. with EZ-Menu
- *sigh* */
- while(!GetNextEvent(keyDownMask|autoKeyMask|mDownMask,&theEvent))
- SystemTask(); /* wait until event */
- if ((theEvent.what==keyDown) || (theEvent.what==autoKey)) /* keyDown? */
- return( (char)BitAnd(theEvent.message,charCodeMask)=='.' &&
- BitAnd(theEvent.modifiers, cmdKey) != 0);
- /* if Cmd-dot then TRUE else FALSE */
- else
- if(theEvent.what==mouseDown && FindWindow(theEvent.where,&whichWindow)!=inMenuBar)
- return(FALSE); /* click not in MenuBar */
- }
- }
-
- void DrawPage(i) /* draw sample page */
- int i;
- {
- myFontInfo *myFont;
- int lineNr=0;
- char *pos;
- register int from,to,nr;
- register int c,x,y;
- int lineH;
- int wMax;
- Str255 myStr;
- char buf[4];
-
- myFont= *fontHdl; /* Block is already locked ! */
- lineH=myFont[i].lineHeight;
- wMax=myFont[i].widMax;
-
- TextFont(0); /* draw font name in system font */
- TextSize(12); /* and 12 pt size */
- MoveTo(TOPH,TOPF); /* goto title line */
- DrawString(myFont[i].fontName); /* draw font name */
- DrawString("\p (ID=");
- stci_d(buf,myFont[i].fontNum,3);
- DrawString(CtoPstr(buf));
- DrawString("\p) ");
- stci_d(buf,fontSize,2);
- DrawString(CtoPstr(buf));
- DrawString("\p point.");
-
- TextFont(myFont[i].fontNum); /* set font */
- TextSize(fontSize);
-
- if(RealFont(myFont[i].fontNum,fontSize)) {
-
- /* sample text */
-
- if(opt==SAMPLE || prFlag) {
- lineNr++;
- HLock(myText);
- from=nr=to=0;
- pos=*myText;
- while(to<textLength) { /* break text into single lines */
- to++;
- if( (*pos++==0x0D) || (to==textLength)) { /* search for CR */
- MoveTo( TOPH, TOPV + lineH*lineNr++ ); /* go to next line */
- DrawText(*myText,from,to-from); /* draw line */
- from = to;
- }
- }
- HUnlock(myText);
- }
-
- /* sample string */
-
- if(opt==STRING || prFlag ) {
- for(i=1;i;i++) {
- lineNr++;
- GetIndString(myStr,STRINGS,i);
- if(*myStr) {
- MoveTo( TOPH, TOPV + lineH*lineNr++ );
- DrawString(myStr);
- }
- else break;
- }
- }
-
- /* character map */
-
- if(opt==MAP || prFlag) {
- lineNr++;
- for(c=32;c<256;c++) { /* only printable characters */
- if(c%32==0) { /* 32 char per row */
- x=TOPH;
- y=TOPV + lineH*lineNr++; /* new line */
- }
- MoveTo(x,y);
- if(isprint(c)) /* skip 127 */
- DrawChar((char)c);
- else
- DrawChar(32);
- x+=wMax; /* makes a pretty output */
- }
- }
- }
- else {
- TextSize(12);
- TextFont(0);
- DrawString("\p Font size not available.");
- }
- }
-
- void ShowFonts() /* Display fonts */
- {
- register int i;
- DialogPtr myDialog;
- WindowPtr myWindow;
-
- prFlag=FALSE;
-
- if(SelectFonts()) { /* only when select dialog is ok */
- myWindow=GetNewWindow( FONTWINDOW, NIL, (WindowPtr)-1 ); /* open a window */
- myDialog=GetNewDialog(SCREENBREAK,NIL,myWindow); /* show break information */
- DrawDialog(myDialog);
- SetPort(myWindow);
- HLock(fontHdl);
- for (i=0; i<fontCount;i++) {
- if( (*fontHdl)[i].flag) { /* draw only selected fonts */
- EraseRect(&myWindow->portRect); /* clear page */
- DrawPage(i); /* draw sample page */
- if (BreakCheck()) /* check for break */
- break;
- }
- }
- HUnlock(fontHdl);
- DisposeWindow(myWindow); /* close window and dialog */
- DisposDialog(myDialog);
- }
- }
-
- int HowMany()
- {
- return( ((**prRecHdl).prJob.bJDocLoop==bDraftLoop) ?
- (**prRecHdl).prJob.iCopies : 1 );
- }
-
- void PrintFonts() /* print fonts */
- {
- TPPrPort myPrPort;
- TPrStatus myPrStatus;
- DialogPtr mySpoolDlg,myPrintDlg;
- GrafPtr savePort;
- register int i;
- register int copies;
- int curs=0;
-
- prFlag=TRUE; /* indicate printing */
-
- GetPort(&savePort); /* save old grafPort */
- PrOpen(); /* open PrintMgr */
-
- if(SelectFonts() && PrJobDialog(prRecHdl)) { /* get print job infos */
- /* just in case last printout was cancelled by user: */
- PrintErr=noErr;
- for (copies=HowMany(); copies>0 && PrintErr==noErr; copies--) {
- /* show status information: */
- if((**prRecHdl).prJob.bJDocLoop==bSpoolLoop)
- DrawDialog(mySpoolDlg=GetNewDialog(SPOOLINFO,NIL,(WindowPtr)-1));
- else
- DrawDialog(myPrintDlg=GetNewDialog(PRINTINFO,NIL,(WindowPtr)-1));
-
- SetCursor(*myCursor[curs++]); /* we´re busy (rotating watch */
- if(curs==4) curs=0;
-
- myPrPort=PrOpenDoc(prRecHdl,NIL,NIL); /* open printPort */
- SetPort(myPrPort);
- TextSize(12);
- HLock(fontHdl);
- for (i=0; i<fontCount;i++) {
-
- SetCursor(*myCursor[curs++]); /* still busy */
- if(curs==4) curs=0;
-
- if (PrintErr==noErr) {
- if( (*fontHdl)[i].flag) { /* print only selected fonts */
- PrOpenPage(myPrPort,NIL);
- if(PrintErr==noErr)
- DrawPage(i); /* print sample page */
- PrClosePage(myPrPort);
- }
- }
- }
- HUnlock(fontHdl);
- PrCloseDoc(myPrPort); /* close printPort */
-
- /* now print spool file */
-
- if((**prRecHdl).prJob.bJDocLoop==bSpoolLoop && PrintErr==noErr) {
- DisposDialog(mySpoolDlg);
- DrawDialog(myPrintDlg=GetNewDialog(PRINTINFO,NIL,(WindowPtr)-1));
- PrPicFile(prRecHdl,NIL,NIL,NIL,&myPrStatus); /* print spool file */
- }
-
- DisposDialog(myPrintDlg);
- }
-
- InitCursor();
- if(PrintErr!=noErr)
- ErrorMsg(ERR_PRINT); /* something went wrong */
- else
- SysBeep(10); /* notify user */
- PrClose(); /* close printing manager */
- SetPort(savePort); /* reset old grafPort */
- }
- }
-
- /**************************** Font dialog handling ****************************/
-
- pascal void DrawList(theWindow,itemNo) /* user item routine */
- WindowPtr theWindow;
- int itemNo;
- {
- Rect rView;
- RgnHandle theRgn;
- int itemType;
- Handle item;
- Rect box;
-
- GetDItem(theWindow,OK,&itemType,&item,&box); /* get rect of OK button */
- PenSize(3,3);
- InsetRect(&box,-4,-4);
- FrameRoundRect(&box,16,16); /* draw bold */
- PenSize(1,1);
-
- HLock(myList);
- rView=(**myList).rView;
- InsetRect(&rView,-1,-1);
- FrameRect(&rView); /* frame around list */
- theRgn=(*theWindow).visRgn;
- LUpdate(theRgn,myList); /* update list */
- HUnlock(myList);
- }
-
- pascal Boolean myFilter(theDialog,theEvent,itemHit) /* dialog filter */
- DialogPtr theDialog;
- EventRecord *theEvent;
- int *itemHit;
- {
- char charCode;
- GrafPtr savePort;
- Cell theCell;
- Boolean flag;
-
- if ((theEvent->what==keyDown) || (theEvent->what==autoKey)) {
- charCode=(char)BitAnd(theEvent->message,charCodeMask);
- if ((charCode==13) || (charCode==3)) { /* OK button is default */
- *itemHit=1;
- return(TRUE);
- }
- else if ((charCode=='.') && (BitAnd(theEvent->modifiers, cmdKey) != 0))
- { /* Cmd-dot cancels dialog */
- *itemHit=2;
- return(TRUE);
- }
- else return(FALSE);
- }
-
- else {
- if (*itemHit==FLIST) { /* list handling */
- GetPort(&savePort);
- SetPort((**myList).port);
- GlobalToLocal(&(theEvent->where)); /* convert to local coordinates */
- flag=LClick(theEvent->where,theEvent->modifiers|cmdKey,myList);
- /* simulate cmd key to fool default list
- definition routine !!! */
- SetPort(savePort);
- }
-
- if(flag==TRUE) { /* double click is equal to ok */
- *itemHit=1;
- return(TRUE);
- }
- else
- return(FALSE);
- }
- }
-
- void InitList(theWindow) /* initialize font list */
- WindowPtr theWindow;
- {
- int itemType;
- Handle item;
- Point csize,theCell;
- Rect rView,dataBounds;
- register int i;
- myFontInfo *myFont;
-
- GetDItem(theWindow,FLIST,&itemType,&item,&rView);
- SetRect(&dataBounds,0,0,1,fontCount); /* set borders of list */
- SetPt(&csize,0,0);
- rView.right-=15; /* leave room for scroll bar */
- myList=LNew(&rView,&dataBounds,csize,0,theWindow,TRUE,FALSE,FALSE,TRUE);
-
- HLock(fontHdl);
- myFont= *fontHdl;
- for (i=0; i<fontCount;i++) { /* fill list with entries from myFontInfo array */
- SetPt(&theCell,0,i);
- LSetCell(myFont[i].fontName+1,*myFont[i].fontName,theCell,myList);
- LSetSelect(myFont[i].flag,theCell,myList); /* preselect entries */
- }
- HUnlock(fontHdl);
- }
-
- Boolean SelectFonts() /* handle font select dialog */
- {
- register int i;
- int button;
- int itemType;
- Handle item;
- Rect box;
- Cell theCell;
- Boolean stop=FALSE;
- myFontInfo *myFont;
- DialogPtr myDialog=GetNewDialog(FONTLIST,NIL,(WindowPtr)-1);
- /* Auswahl-Dialog */
-
- GetDItem(myDialog,FLIST,&itemType,&item,&box); /* get rect of list button */
- SetDItem(myDialog,FLIST,userItem,DrawList,&box); /* install user item */
-
- InitList(myDialog); /* initialize list */
-
- ShowWindow(myDialog); /* make dialog visible */
- while (!stop) {
- SystemTask();
- ModalDialog(myFilter,&button); /* Dialog anzeigen */
-
- switch (button) {
- case EXALL: /* deselect all fonts */
- for (i=0; i<fontCount;i++) {
- SetPt(&theCell,0,i);
- LSetSelect(FALSE,theCell,myList);
- }
- break;
-
- case INALL: /* select all fonts */
- for (i=0; i<fontCount;i++) {
- SetPt(&theCell,0,i);
- LSetSelect(TRUE,theCell,myList);
- }
- break;
-
- case REVERT: /* revert to old */
- HLock(fontHdl);
- myFont= *fontHdl;
- for (i=0; i<fontCount;i++) {
- SetPt(&theCell,0,i);
- LSetSelect(myFont[i].flag,theCell,myList);
- }
- HUnlock(fontHdl);
- break;
-
- case Cancel: /* dialog cancelled */
- stop=TRUE;
- break;
-
- case OK:
- HLock(fontHdl);
- myFont= *fontHdl;
- for (i=0; i<fontCount;i++) { /* update myFontInfo array */
- SetPt(&theCell,0,i);
- myFont[i].flag=LGetSelect(FALSE,&theCell,myList);
- }
- HUnlock(fontHdl);
- stop=TRUE;
- break;
- } /* end case */
- }
-
- LDispose(myList); /* dispose of list */
- DisposDialog(myDialog); /* dispose of dialog */
- return( (button==OK)?TRUE:FALSE);
- }
-
- /**************************** Event handling ****************************/
-
- void HandleMenu(sel) /* handle menu bar */
- long sel;
- {
- int theItem=LoWord(sel);
- Str255 name;
- GrafPtr savePort;
- register int i;
- myFontInfo *myFont;
- FontInfo fInfo;
-
- switch (HiWord(sel)) {
- case APPLE_M:
- GetItem(myMenus[APPLE], theItem, &name);
- if(theItem==INFO) /* About… */
- ShowInfo();
- else {
- GetPort(&savePort); /* save old grafPort (just in case…)*/
- OpenDeskAcc(&name); /* open DA */
- InitCursor(); /* DA may have changed cursor */
- SetPort(savePort); /* DA may have changed grafPort */
- }
- break;
-
- case FILE_M:
- switch(theItem) {
- case SETUP:
- PrOpen(); /* open/close PrintMgr acc. to TN */
- PrStlDialog(prRecHdl); /* get page setup */
- PrClose();
- break;
-
- case DISPLAY:
- ShowFonts();
- break;
-
- case PRINT:
- PrintFonts();
- break;
-
- case QUIT:
- quit=TRUE;
- break;
- }
- break;
-
- case EDIT_M:
- SystemEdit(theItem-1); /* our application doesn´t handle EDIT menu */
- break;
-
- case OPTIONS_M:
- CheckItem(myMenus[OPTIONS],opt,FALSE); /* deselect menu item */
- CheckItem(myMenus[OPTIONS],opt=theItem,TRUE); /* select new Item */
- break;
-
- case SIZE_M:
- if(theItem!=oldItem) {
- SetCursor(*myCursor[0]);
- CheckItem(myMenus[SIZE],oldItem,FALSE);
- CheckItem(myMenus[SIZE],oldItem=theItem,TRUE);
- switch(oldItem) {
- case SIZE9:
- fontSize=9;
- break;
-
- case SIZE10:
- fontSize=10;
- break;
-
- case SIZE12:
- fontSize=12;
- break;
-
- case SIZE14:
- fontSize=14;
- break;
-
- case SIZE18:
- fontSize=18;
- break;
-
- case SIZE20:
- fontSize=20;
- break;
-
- case SIZE24:
- fontSize=24;
- break;
- }
- TextSize(fontSize);
- HLock(fontHdl);
- myFont= *fontHdl;
- for(i=0;i<fontCount;i++) {
- TextFont(myFont[i].fontNum); /* set font */
- GetFontInfo(&fInfo); /* and get font info */
- myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; /* calc. line height */
- myFont[i].widMax=fInfo.widMax; /* calc. char width */
- }
- HUnlock(fontHdl);
- TextSize(12);
- TextFont(0);
- InitCursor();
- }
- break;
- }
- HiliteMenu(0);
- }
-
- void HandleMouseDown(theEvent) /* handle mouseDown events */
- EventRecord theEvent;
- {
- WindowPtr whichWindow;
-
- switch (FindWindow( theEvent.where, &whichWindow )) {
- case inDesk:
- SysBeep(10);
- break;
-
- case inMenuBar:
- HandleMenu( MenuSelect(theEvent.where) ) ;
- break;
-
- case inSysWindow:
- SystemClick( &theEvent, whichWindow );
- break;
- } /* end switch */
- }
-
- /*********************************** MainLoop() *******************************/
-
- void MainLoop()
- {
- EventRecord theEvent;
-
- quit=FALSE;
- while(!quit){
- SystemTask();
- if (GetNextEvent (everyEvent, &theEvent))
- switch (theEvent.what) {
- case mouseDown: /* mouse click */
- HandleMouseDown(theEvent);
- break;
-
- case keyDown: /* key click */
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0) /* we handle only Cmd-key */
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- break;
- } /* end switch (and if) */
- } /* end while */
- }
-
- /*************************** Initialization and main() *************************/
-
- void InitMenu() /* Draw menu bar */
- {
- register int i;
-
- myMenus[APPLE]=GetMenu(APPLE_M);
- AddResMenu(myMenus[APPLE],'DRVR'); /* insert DAs in Apple-menu */
-
- myMenus[FILE]=GetMenu(FILE_M);
- myMenus[EDIT]=GetMenu(EDIT_M);
- myMenus[OPTIONS]=GetMenu(OPTIONS_M);
- myMenus[SIZE]=GetMenu(SIZE_M);
-
- CheckItem(myMenus[OPTIONS],opt=SAMPLE,TRUE); /* preselect "sample text" */
- CheckItem(myMenus[SIZE],oldItem=SIZE12,TRUE); /* preselect font size 12 */
- fontSize=12;
-
- for(i=0;i<5;InsertMenu(myMenus[i++],0))
- ;
- DrawMenuBar();
- }
-
- void InitText() /* get sample text from rsrc */
- {
- myText=GetResource('TEXT',128);
- textLength=(int)SizeResource(myText);
- }
-
- void InitPrint()
- {
- PrOpen();
- prRecHdl=(THPrint)NewHandle(sizeof(TPrint)); /* get new print record */
- if(prRecHdl)
- PrintDefault(prRecHdl); /* validate it */
- else
- ErrorMsg(ERR_NOROOM);
- PrClose();
- }
-
- void BuildList() /* build font list */
- {
- register int i;
- myFontInfo *myFont;
- FontInfo fInfo;
- MenuHandle tempMenu;
-
- /* Get a fake menu; use the Menu Manager to fill it with font names -
- the Menu Manager sorts entries alphabetically ! */
-
- tempMenu = NewMenu(TEMP_M,"x");
- AddResMenu(tempMenu,'FONT');
- fontCount=CountMItems(tempMenu);
-
- /* dynamic allocation of heap space for myFontInfo array */
-
- if(fontHdl=(myFontInfo **)NewHandle(fontCount*sizeof(myFontInfo))) {
- HLock(fontHdl);
- TextSize(12);
- myFont= *fontHdl;
- for(i=0;i<fontCount;i++) {
- GetItem(tempMenu,i+1,myFont[i].fontName); /* get font names */
- GetFNum(myFont[i].fontName,&myFont[i].fontNum); /* get font numbers */
- TextFont(myFont[i].fontNum); /* set font */
- GetFontInfo(&fInfo); /* and get font info */
- myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; /* calc. line height */
- myFont[i].widMax=fInfo.widMax; /* calc. char width */
- myFont[i].flag=TRUE; /* preselect font */
- }
- HUnlock(fontHdl);
- }
- else
- ErrorMsg(ERR_NOROOM);
-
- DisposeMenu(tempMenu); /* We’re done with the menu; dispose of it */
- TextFont(0); /* and reset system font */
- }
-
- void InitThings()
- {
- DialogPtr myDialog;
- register int i;
-
- InitGraf(&thePort);
- MaxApplZone(); /* we want it all, and we want it NOW ! */
- MoreMasters(); /* extra pointer blocks at the bottom of the heap */
- MoreMasters(); /* this is 5 X 64 master pointers */
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- InitFonts(); /* startup the font manager */
- InitWindows(); /* startup the window manager */
- InitMenus(); /* startup the menu manager */
- TEInit(); /* startup the text edit manager */
- InitDialogs(NIL); /* startup the dialog manager */
-
- FlushEvents(everyEvent,0);
-
- /* get the cursors we use and lock them down - no clutter */
-
- myCursor[0] = GetCursor(watchCursor);
- myCursor[1] = GetCursor(128);
- myCursor[2] = GetCursor(129);
- myCursor[3] = GetCursor(130);
-
- for(i=0;i<4;i++) {
- MoveHHi((Handle)myCursor[i]);
- HLock((Handle)myCursor[i]);
- }
-
- myDialog=GetNewDialog(WAIT,NIL,(WindowPtr)-1); /* tell user we´re busy */
- DrawDialog(myDialog);
- InitMenu(); /* build menus */
- InitText(); /* get sample text */
- InitPrint(); /* initialize printing */
- BuildList(); /* build font name list */
- DisposDialog(myDialog);
- InitCursor(); /* show arrow cursor */
- }
-
- void CleanUp()
- {
- register int i;
-
- if(fontHdl)
- DisposHandle(fontHdl);
- if(prRecHdl)
- DisposHandle(prRecHdl);
- for(i=0;i<4;i++)
- HUnlock((Handle)myCursor[i]);
- }
-
- void main()
- {
- InitThings(); /* initializations */
- if(fontHdl && prRecHdl)
- MainLoop(); /* main routine */
- CleanUp(); /* clean up */
- }