home *** CD-ROM | disk | FTP | other *** search
- /*
- * HelpText.c - source file for the "HELP«»TEXT" application
- *
- * Kent Kersten, 7-Jul-85
- *
- * This program takes a 'TEXT' type file and writes it to a given
- * file's resource fork, under the resource name of 'HELP', with
- * a given id # and (optional) name. It can also perform the reverse
- * operation, taking a HELP resource and writing a TEXT file. This
- * is helpful for preparing 'help' type dialogs, such as the type
- * that Microsoft Multiplan has.
- *
- * NOTE: The maximum size of a resource or text file is 32K bytes;
- * this is because we use TextEdit, and it has that limitation.
- * Also, you can't write a single resource that is greater
- * than 32K, so it works out all around. Even so, I can't
- * imagine a single HELP selection being anywhere near that long!
- *
- * TEXT » HELP command instructions:
- * When you have a 'TEXT' type file, such as created with Edit,
- * and you wish to convert it to a HELP resource, this is the command
- * to use. You will be presented with a Standard File dialog to
- * choose a TEXT type file to read from. You will then be
- * prompted to enter a filename to save the HELP resource in.
- * The final dialog will ask you what resource number and name
- * you wish to assign to the resource. If all goes well, you
- * will get a message to that effect; if not, then an appropriate
- * error message will be displayed.
- *
- * HELP » TEXT command instructions:
- * When you want to convert a HELP resource to a TEXT file, use
- * this command. You will be prompted with a Standard File dialog
- * to choose a file to read the HELP resource from. After that,
- * a dialog will open asking for the HELP resource number to use.
- * You will then be asked to name the TEXT file that the resource
- * will be written to.
- *
- * Please note that this program only works for vanilla text files.
- * No formatting information is kept, such as with Microsoft Word.
- *
- * This program and its associated modules
- * are the property of and copyright (c) 1985 by:
- * Kent Kersten
- * Harrison Systems
- * 437 Atlas Drive
- * Nashville, TN 37211
- * (615) 834-1366
- * CompuServe [72277,2726]
- */
-
- #include <quickdraw.h>
- #include <window.h>
- #include <menu.h>
- #include <resource.h>
- #include <packages.h>
- #include <event.h>
- #include <desk.h>
- #include <control.h>
- #include <textedit.h>
- #include <dialog.h>
- #include <font.h>
- #include <osutil.h>
-
- #define copyright_string "Copyright (c) 1985 by Kent Kersten"
-
- #define HELP_FILE "\007HT.Help"
-
- #define ABOUT_DLOG 256
- #define HELP_DLOG 257
- #define RSRC_DLOG 300
- #define RSRC_2_DLOG 301
- #define OVERWRITE_DLOG 320
- #define CONV_DLOG 350
- #define GENERAL_ERROR_ALRT 400
-
- #define KENT_ICON 306
-
- #define nil (long) 0
-
- #define NUM_MENUS 3
- #define AppleMenu 256
- #define FileMenu 257
- #define EditMenu 258
-
- #define Helvetica 21 /* font for help boxes */
-
- #define MAX_TITLES_IN_BOX 7 /* how many can be seen at once */
- #define MAX_HELP_TITLES 64
- #define MAX_LINES_IN_BOX 14 /* # of lines in help text box */
-
- MenuHandle HTMenu[NUM_MENUS];
-
- char *calloc(), *realloc();
-
- Boolean ProcessEvents(), DoCommand();
- Boolean ReadTextFile(), WriteTextFile();
- Boolean ReadRsrcFile(), WriteRsrcFile();
- Boolean OverWriteDialog(), RsrcDialog();
-
- Point whereOpen = { 80, 80 }; /* where to put the SF dialog */
- Point whereSaveAs = { 85, 95 };
-
- struct hNode {
- char *hName; /* Pascal string */
- int hNum;
- } *hNodeList[MAX_HELP_TITLES];
-
- Boolean noHelpResources, inHelpTextBox;
- int selection, hTListNum;
- ControlHandle vsb;
- Rect htBox;
- TEHandle hTE;
- pascal void DScrollUp(), DScrollDown();
-
-
- main()
- {
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- InitDialogs(nil);
- TEInit();
-
- FlushEvents(everyEvent, 0); /* flush ALL events */
- GetHelpTitles();
- FormMenuBar();
- InitCursor();
-
- Introduction();
-
- while(ProcessEvents())
- ;
- }
-
-
- /*
- * Create our menu bar.
- */
-
- FormMenuBar()
- {
- int i;
-
- HTMenu[0] = GetMenu(AppleMenu);
- HTMenu[1] = GetMenu(FileMenu);
- HTMenu[2] = GetMenu(EditMenu);
- AddResMenu(HTMenu[0], 'DRVR');
-
- for(i = 0;i < NUM_MENUS;++i)
- InsertMenu(HTMenu[i], 0);
-
- DrawMenuBar();
- }
-
-
- /*
- * Get events and process them.
- */
-
- Boolean ProcessEvents()
- {
- char theChar;
- Boolean NoQuitFlag = TRUE;
- WindowPtr whichWindow;
- EventRecord EditEvent;
- GrafPtr savePort;
-
- HiliteMenu(0); /* turn off any highlighting in menu bar */
- SystemTask();
-
- GetNextEvent(everyEvent, &EditEvent);
- switch(EditEvent.what) {
- case mouseDown:
- switch(FindWindow(pass(EditEvent.where), &whichWindow)) { /* major Aztec C68K hack-ack */
- case inDesk:
- break;
- case inMenuBar:
- NoQuitFlag = DoCommand(MenuSelect(pass(EditEvent.where))); /* Aztec hack */
- break;
- case inSysWindow:
- SystemClick(&EditEvent,whichWindow);
- break;
- case inGrow:
- break;
- case inContent:
- break;
- case inDrag:
- break;
- case inGoAway:
- break;
- }
- break;
- case keyDown:
- case autoKey:
- theChar = EditEvent.message % 256;
- if((EditEvent.modifiers & cmdKey) != 0) /* command key pressed */
- NoQuitFlag = DoCommand(MenuKey(theChar));
- break;
- case updateEvt:
- GetPort(&savePort);
- SetPort(EditEvent.message);
- BeginUpdate(EditEvent.message);
- EndUpdate(EditEvent.message);
- SetPort(savePort);
- break;
- case activateEvt:
- break;
- }
-
- return NoQuitFlag;
- }
-
-
- /*
- * Decipher menu commands here.
- */
-
- Boolean DoCommand(command)
- long command;
- {
- int refNum;
- char name[256];
- short MenuNum, ItemNum;
- Boolean NoQuitFlag = TRUE;
-
- InitCursor();
- MenuNum = HiWord(command);
- ItemNum = LoWord(command);
-
- switch(MenuNum) {
- case AppleMenu:
- if(ItemNum == 1) {
- /* we have to bracket the call to our 'About' box with */
- /* the open & close resource file calls, otherwise */
- /* we could erroneously pick off a HELP resource from */
- /* our .Help file when in 'HELPConvert()' */
- refNum = OpenResFile(HELP_FILE);
- AboutHT();
- CloseResFile(refNum);
- }
- else {
- GetItem(HTMenu[0], ItemNum, name);
- OpenDeskAcc(name);
- }
- break;
- case FileMenu:
- switch(ItemNum) {
- case 1:
- TEXTConvert();
- break;
- case 2:
- HELPConvert();
- break;
- case 3:
- NoQuitFlag = FALSE;
- break;
- }
- break;
- case EditMenu:
- switch(ItemNum) {
- case 1:
- if(FrontWindow() != nil)
- SystemEdit(undoCmd);
- break;
- case 3:
- if(FrontWindow() != nil)
- SystemEdit(cutCmd);
- break;
- case 4:
- if(FrontWindow() != nil)
- SystemEdit(copyCmd);
- break;
- case 5:
- if(FrontWindow() != nil)
- SystemEdit(pasteCmd);
- break;
- case 6:
- if(FrontWindow() != nil)
- SystemEdit(clearCmd);
- break;
- }
- break;
- }
-
- return NoQuitFlag;
- }
-
-
- /*
- * Get the string resource whose number was passed and
- * call the GeneralError alert function.
- */
-
- ErrorCall(num)
- int num;
- {
- Handle hdl;
-
- hdl = GetString(num);
- HLock(hdl);
- GeneralError(*hdl, nil, nil, nil);
- HUnlock(hdl);
- ReleaseResource(hdl);
- }
-
-
- /*
- * Convert a 'HELP' resource file to a 'TEXT' file.
- */
-
- HELPConvert()
- {
- SFReply rec, rec2;
- int id, number, refNum;
- long grow, theType;
- char name[256];
- Handle hdl;
- Rect tRect;
- TEHandle hTE;
- WindowPtr wptr; /* a necessary hack, as it turns out */
-
- MaxMem(&grow); /* free up some heap space */
-
- ParamText("\030Read HELP resource from:", nil, nil, nil);
-
- SFGetFile(pass(whereOpen), nil, nil, 0, nil, nil, &rec);
-
- if(rec.good == 0) /* 'Cancel' button hit */
- return;
-
- if(!RsrcDialog(&number, name, TRUE))
- return; /* 'Cancel' hit */
-
- if((refNum = OpenResFile(rec.fName)) < 0) { /* get the name of the resource */
- ErrorCall(303);
- return;
- }
-
- if((hdl = GetResource('HELP', number)) == nil) {
- CloseResFile(refNum);
- ErrorCall(304);
- return;
- }
- GetResInfo(hdl, &id, &theType, name);
- ReleaseResource(hdl);
- CloseResFile(refNum);
-
- hdl = GetString(257);
- HLock(hdl);
- SFPutFile(pass(whereSaveAs), *hdl, name, nil, &rec2); /* text file to write to */
- HUnlock(hdl);
-
- if(rec2.good == 0) /* 'Cancel' hit */
- return -1;
-
- SetRect(&tRect, 5000, 5000, 5050, 5050); /* off the screen and invisible */
- wptr = NewWindow(nil, &tRect, "\004Hack", FALSE, 0, (long) -1, FALSE, (long) 0);
- tRect = wptr->portRect;
- hTE = TENew(&tRect, &tRect);
-
- if(!ReadRsrcFile(rec.fName, number, hTE)) {
- TEDispose(hTE);
- DisposeWindow(wptr);
- return;
- }
-
- TECalText(hTE); /* co-conspirators in the hack */
- TEUpdate(&tRect, hTE);
-
- if(!WriteTextFile(rec2.fName, rec2.vRefNum, hTE)) {
- TEDispose(hTE);
- DisposeWindow(wptr);
- return;
- }
-
- TEDispose(hTE);
- DisposeWindow(wptr);
-
- ConversionCompleted();
- }
-
-
- /*
- * Convert a 'TEXT' file to a 'HELP' resource file.
- */
-
- TEXTConvert()
- {
- SFReply rec, rec2;
- SFTypeList typeList;
- short numTypes = 1;
- int number;
- long grow;
- char temp[256], name[256];
- Handle hdl, hdl2;
- Rect tRect;
- TEHandle hTE;
-
- MaxMem(&grow); /* clear out some heap space */
-
- typeList[0] = 'TEXT';
-
- ParamText("\017Read TEXT from:", nil, nil, nil);
-
- SFGetFile(pass(whereOpen), nil, nil, numTypes, typeList, nil, &rec);
-
- if(rec.good == 0) /* 'Cancel' button hit */
- return;
-
- Pstrcpy(temp, rec.fName);
- ptoc(temp);
- strcat(temp, ".Rsrc"); /* append ".Rsrc" to original filename */
- ctop(temp);
-
- hdl = GetString(256);
- HLock(hdl);
- SFPutFile(pass(whereSaveAs), *hdl, temp, nil, &rec2);
- HUnlock(hdl);
-
- if(rec2.good == 0) /* 'Cancel' hit */
- return -1;
-
- Pstrcpy(name, rec.fName);
- if(!RsrcDialog(&number, name, FALSE))
- return; /* 'Cancel' hit */
-
- SetRect(&tRect, 2000, 2000, 2020, 2020);
- hTE = TENew(&tRect, &tRect);
-
- if(!ReadTextFile(rec.fName, rec.vRefNum, hTE)) {
- TEDispose(hTE);
- return;
- }
-
- if(!WriteRsrcFile(rec2.fName, name, number, hTE)) {
- TEDispose(hTE);
- return;
- }
-
- TEDispose(hTE);
-
- ConversionCompleted();
- }
-
-
- /*
- * Read from the resource file from the given filename & resource number,
- * and put the info into the textedit handle passed.
- */
-
- Boolean ReadRsrcFile(fName, rNum, hTE)
- char *fName;
- int rNum;
- TEHandle hTE;
- {
- int refNum;
- Handle hdl;
-
- if((refNum = OpenResFile(fName)) < 0) {
- ErrorCall(303);
- return FALSE;
- }
-
- if((hdl = GetResource('HELP', rNum)) == nil) {
- CloseResFile(refNum);
- ErrorCall(304);
- return FALSE;
- }
-
- TESetText(*hdl, SizeResource(hdl), hTE);
-
- ReleaseResource(hdl);
- CloseResFile(refNum);
-
- return TRUE;
- }
-
-
- /*
- * Read a TEXT file from the filename and refnum passed, and put the
- * text into the textedit handle passed.
- */
-
- Boolean ReadTextFile(fname, vRefNum, hTE)
- char *fname;
- int vRefNum;
- TEHandle hTE;
- {
- int refNum;
- long logEOF;
-
- if(FSOpen(fname, vRefNum, &refNum) != 0) {
- FSClose(refNum);
- ErrorCall(300);
- return FALSE; /* file wasn't read */
- }
-
- if(GetEOF(refNum, &logEOF) != 0) {
- FSClose(refNum);
- ErrorCall(312);
- return FALSE;
- }
-
- if(logEOF > 32000) {
- FSClose(refNum);
- ErrorCall(302);
- return FALSE;
- }
-
- SetHandleSize((*hTE)->hText, logEOF);
-
- if(FSRead(refNum, &logEOF, *((*hTE)->hText)) != 0) {
- FSClose(refNum);
- ErrorCall(313);
- return FALSE;
- }
-
- if(FSClose(refNum) != 0) {
- ErrorCall(314);
- return FALSE;
- }
-
- (*hTE)->teLength = (int) logEOF;
-
- return TRUE;
- }
-
-
- /*
- * Write to the resource file from the given filename, resource name & number,
- * using the text referenced by the textedit handle. Check to see if the
- * resource already exists, and if so, allow them to cancel or continue.
- */
-
- Boolean WriteRsrcFile(fName, rName, rNum, hTE)
- char *fName, *rName;
- int rNum;
- TEHandle hTE;
- {
- int refNum;
- Handle hdl, hdl2;
-
- if((refNum = OpenResFile(fName)) < 0) { /* doesn't exist, create it */
- CreateResFile(fName);
- if((refNum = OpenResFile(fName)) < 0) { /* fatal error of some kind */
- ErrorCall(305);
- return FALSE;
- }
- }
-
- if((hdl = GetResource('HELP', rNum)) != nil)
- if(!OverWriteDialog(fName, rName, rNum)) {
- CloseResFile(refNum);
- return FALSE;
- }
- else {
- RmveResource(hdl);
- if(ResError() < 0) { /* remove failed */
- hdl2 = GetString(316);
- HLock(hdl2);
- GeneralError(*hdl2, nil, nil, nil);
- HUnlock(hdl2);
- CloseResFile(refNum);
- return FALSE;
- }
- DisposHandle(hdl);
- UpdateResFile(refNum);
- }
-
- AddResource((*hTE)->hText, 'HELP', rNum, rName);
- UpdateResFile(refNum);
- CloseResFile(refNum);
-
- return TRUE;
- }
-
-
- /*
- * Write out the file, using the filename and refnum passed, and the
- * text to write is referenced by the textedit handle.
- */
-
- Boolean WriteTextFile(fname, vRefNum, hTE)
- char *fname;
- int vRefNum;
- TEHandle hTE;
- {
- int io_code, refNum;
- long count;
-
- count = (long) (*hTE)->teLength;
-
- if((io_code = FSOpen(fname, vRefNum, &refNum)) != 0) {
- if(io_code == -43) { /* 'file not found' - create it! */
- if(Create(fname, vRefNum, ' ', 'TEXT') != 0) {
- ErrorCall(315);
- return FALSE;
- }
- if(FSOpen(fname, vRefNum, &refNum) != 0) {
- ErrorCall(306);
- return FALSE;
- }
- }
- else {
- ErrorCall(307);
- return FALSE; /* error occurred during open */
- }
- }
-
- if(FSWrite(refNum, &count, *((*hTE)->hText)) != 0) {
- ErrorCall(308);
- return FALSE;
- }
-
- if(SetEOF(refNum, count) != 0) {
- ErrorCall(309);
- return FALSE;
- }
-
- if(FSClose(refNum) != 0) {
- ErrorCall(310);
- return FALSE;
- }
-
- if(FlushVol(nil, vRefNum) != 0) {
- ErrorCall(311);
- return FALSE;
- }
-
- return TRUE;
- }
-
-
- /********** dialog & alert routines begin here ***********/
-
-
- /*
- * Pop up our 'About...' box.
- */
-
- AboutHT()
- {
- int i, type, itemHit = 0;
- DialogPtr DPtr;
- Rect tRect, i3Rect, i4Rect, i5Rect, i9Rect;
- Handle item, icon;
- static long lastClickTime = 0;
-
- selection = 0;
- inHelpTextBox = FALSE;
-
- icon = GetIcon(KENT_ICON);
-
- DPtr = GetNewDialog(ABOUT_DLOG, nil, (long) -1); /* invisible */
-
- GetDItem(DPtr, 9, &type, &item, &i9Rect); /* icon */
- GetDItem(DPtr, 3, &type, &item, &i3Rect); /* big box for help titles */
- htBox = i3Rect;
- GetDItem(DPtr, 4, &type, &item, &i4Rect); /* scroll bar */
- GetDItem(DPtr, 5, &type, &item, &i5Rect); /* dashed line */
-
- ShowWindow(DPtr);
- SetPort(DPtr);
-
- PlotIcon(&i9Rect, icon);
-
- PenPat(&gray); /* dashed seperator line */
- MoveTo(i5Rect.left, i5Rect.top);
- LineTo(i5Rect.left, i5Rect.bottom);
- PenNormal();
-
- FrameRect(&i3Rect); /* big help titles box */
-
- vsb = NewControl(DPtr, &i4Rect, "\003vsb", (Boolean) -1, 0, 0, 0, scrollBarProc, (long) 0);
-
- GetDItem(DPtr, 2, &type, &item, &tRect);
- if(noHelpResources)
- HiliteControl((ControlHandle) item, 255); /* disable 'Help' button */
- else { /* put help titles into box */
- if(hTListNum > MAX_TITLES_IN_BOX)
- SetCtlMax(vsb, hTListNum - MAX_TITLES_IN_BOX);
- HTDisplay();
- }
-
- while(1) {
- inHelpTextBox = FALSE;
- ModalDialog(nil, &itemHit);
- if(itemHit == 1) { /* 'Cancel' */
- CloseDialog(DPtr);
- break;
- }
- else if(itemHit == 2) { /* 'Help' */
- CloseDialog(DPtr);
- HelpDialog();
- break;
- }
- else if(itemHit == 3) { /* a title was selected */
- if(noHelpResources)
- continue;
- while(Button()) /* check for double-click */
- ;
- if(TickCount() - lastClickTime <= GetDbleTime()) {
- CloseDialog(DPtr);
- HelpDialog();
- break;
- }
- else
- HTSelect();
- lastClickTime = TickCount();
- }
- else if(itemHit == 4) /* in scroll bar */
- MyScroll((WindowPtr) DPtr);
- }
- }
-
-
- /*
- * This dialog tells the user that conversion process is over.
- */
-
- ConversionCompleted()
- {
- int itemHit = 0;
- DialogPtr DPtr;
- EventRecord myRec;
-
- HiliteMenu(0);
-
- DPtr = GetNewDialog(CONV_DLOG, nil, (long) -1);
- SetPort(DPtr);
- DrawDialog(DPtr);
-
- while(!EventAvail(mDownMask | keyDownMask, &myRec))
- ; /* wait for a mouse down or key down event */
-
- CloseDialog(DPtr);
- }
-
-
- /*
- * Pop up an alert box and pass some strings to tell what happened.
- */
-
- GeneralError(str1, str2, str3, str4)
- char *str1, *str2, *str3, *str4; /* Pascal format */
- {
- ParamText("\000", "\000", "\000", "\000");
- ParamText(str1, str2, str3, str4);
- StopAlert(GENERAL_ERROR_ALRT, nil);
- }
-
-
- /*
- * Pop up the dialog box that actually displays the text of the help messages.
- */
-
- HelpDialog()
- {
- int itemHit, type;
- Rect box, textBox;
- Handle hdl;
- DialogPtr DPtr;
-
- DPtr = GetNewDialog(HELP_DLOG, nil, (long) -1);
- SetPort(DPtr);
-
- GetDItem(DPtr, 6, &type, &hdl, &textBox);
- FrameRect(&textBox);
- textBox.top += 1; /* borders for help text */
- textBox.left += 5;
- textBox.bottom -= 1;
- textBox.right -= 5;
-
- GetDItem(DPtr, 7, &type, &hdl, &box);
- vsb = NewControl(DPtr, &box, "\003vsb", (Boolean) -1, 0, 0, 0, scrollBarProc, (long) 0);
-
- loop:
- inHelpTextBox = TRUE;
-
- GetDItem(DPtr, 3, &type, &hdl, &box);
- if(selection == (hTListNum - 1)) /* disable 'Next' */
- HiliteControl((ControlHandle) hdl, 255);
- else
- HiliteControl((ControlHandle) hdl, 0);
-
- GetDItem(DPtr, 4, &type, &hdl, &box);
- if(selection == 0) /* disable 'Previous' */
- HiliteControl((ControlHandle) hdl, 255);
- else
- HiliteControl((ControlHandle) hdl, 0);
-
- GetDItem(DPtr, 5, &type, &hdl, &box); /* help title */
- EraseRect(&box);
- TextFont(systemFont);
- MoveTo(box.left+2, box.bottom-3);
- DrawString(hNodeList[selection]->hName);
-
- if((hdl = GetResource('HELP', hNodeList[selection]->hNum)) == nil) {
- hdl = GetString(270);
- HLock(hdl);
- GeneralError(*hdl, nil, nil, nil);
- HUnlock(hdl);
- CloseDialog(DPtr);
- return;
- }
-
- hTE = TENew(&textBox, &textBox);
- TESetText(*hdl, SizeResource(hdl) - 1, hTE);
- ReleaseResource(hdl); /* free the resource handle */
-
- (*hTE)->txFont = Helvetica; /* Helvetica-12, in .Help file */
- (*hTE)->crOnly = -1; /* signals a wrap at newline only */
- TECalText(hTE);
- EraseRect(&textBox); /* wipe out what was there before */
- TEUpdate(&textBox, hTE);
-
- SetCtlValue(vsb, 0);
-
- if((*hTE)->nLines > MAX_LINES_IN_BOX)
- SetCtlMax(vsb, (*hTE)->nLines - MAX_LINES_IN_BOX);
- else
- SetCtlMax(vsb, 0);
-
- while(1) {
- ModalDialog(nil, &itemHit);
- if(itemHit == 1) { /* 'Cancel' */
- CloseDialog(DPtr);
- TEDispose(hTE);
- break;
- }
- else if(itemHit == 2) { /* 'Topics' */
- CloseDialog(DPtr);
- TEDispose(hTE);
- AboutHT();
- break;
- }
- else if(itemHit == 3) { /* 'Next' */
- TEDispose(hTE);
- ++selection;
- goto loop;
- }
- else if(itemHit == 4) { /* 'Previous' */
- TEDispose(hTE);
- --selection;
- goto loop;
- }
- else if(itemHit == 7) /* scroll bar */
- MyScroll();
- }
- }
-
-
- /*
- * A small graphical introduction to our program.
- */
-
- Introduction()
- {
- Rect boundsRect, theFrame;
- DialogPtr DPtr;
- PicHandle myPic;
- EventRecord myRec;
-
- myPic = GetPicture(256);
- theFrame = (*myPic)->picFrame;
- OffsetRect(&theFrame, -theFrame.left + 5, -theFrame.top + 10);
-
- SetRect(&boundsRect, 120,60,390,250);
-
- DPtr = NewDialog(nil, &boundsRect, "\000", TRUE, 3, (long) -1, FALSE, (long) 0, nil);
- SetPort(DPtr);
-
- HLock(myPic);
- DrawPicture(myPic, &theFrame);
- HUnlock(myPic);
-
- while(!EventAvail(mDownMask | keyDownMask, &myRec)) /* wait for a mouse or key event */
- ;
-
- KillPicture(myPic); /* free up the space */
- CloseDialog(DPtr);
- }
-
-
- /*
- * Pop up this dialog if a resource being written to already exists.
- * The user has the option to cancel the operation or overwrite the
- * existing resource in favor of the new one being created.
- * Return TRUE to overwrite, FALSE otherwise.
- */
-
- Boolean OverWriteDialog(fileName, resName, resNumber)
- char *fileName, *resName;
- int resNumber;
- {
- int itemHit = 0;
- char str[256];
- DialogPtr DPtr;
-
- SysBeep(3);
- sprintf(str, "%d", resNumber);
- ParamText(fileName, ctop(str), resName, nil);
-
- DPtr = GetNewDialog(OVERWRITE_DLOG, nil, (long) -1);
- SetPort(DPtr);
-
- while(1) {
- ModalDialog(nil, &itemHit);
- if(itemHit == 1) { /* 'Overwrite' */
- CloseDialog(DPtr);
- return TRUE;
- }
- else if(itemHit == 2) { /* 'Cancel' */
- CloseDialog(DPtr);
- return FALSE;
- }
- }
- }
-
-
- /*
- * This dialog asks the user to input the resource name & number
- * to use in the conversion process. Return FALSE if the Cancel
- * button was hit, else return TRUE.
- */
-
- Boolean RsrcDialog(number, name, use_help_dialog)
- int *number;
- char *name;
- Boolean use_help_dialog;
- {
- int type, itemHit = 0;
- char str[256];
- DialogPtr DPtr;
- Handle hdl;
- Rect box;
-
- if(use_help_dialog)
- DPtr = GetNewDialog(RSRC_2_DLOG, nil, (long) -1);
- else {
- DPtr = GetNewDialog(RSRC_DLOG, nil, (long) -1);
- GetDItem(DPtr, 6, &type, &hdl, &box); /* resource number */
- SetIText(hdl, name); /* the text file's name as a default */
- }
-
- SetPort(DPtr);
-
- while(1) {
- ModalDialog(nil, &itemHit);
- if(itemHit == 1) { /* 'OK' */
- GetDItem(DPtr, 4, &type, &hdl, &box); /* resource number */
- GetIText(hdl, &str);
- *number = atoi(ptoc(str));
- if(*number < 0 || *number > 32767) {
- hdl = GetString(317);
- HLock(hdl);
- GeneralError(*hdl, nil, nil, nil);
- HUnlock(hdl);
- continue;
- }
- GetDItem(DPtr, 6, &type, &hdl, &box); /* resource name */
- GetIText(hdl, &str);
- Pstrcpy(name, str);
- CloseDialog(DPtr);
- return TRUE;
- }
- else if(itemHit == 2) { /* 'Cancel' */
- CloseDialog(DPtr);
- return FALSE;
- }
- }
- }
-
-
- /********** support routines for the 'help' dialog stuff start here *********/
-
-
- /*
- * Read the 'help' resource file and load all of the available
- * names into the global list. This is done at startup
- * time, so that it will not have to be done every time that
- * the 'About...' box is opened, thus saving some time.
- */
-
- GetHelpTitles()
- {
- int i, num, refNum;
- long rType;
- char name[256];
- Rect tRect;
- Handle hdl;
-
- if((refNum = OpenResFile(HELP_FILE)) == -1)
- noHelpResources = TRUE;
- else { /* check to make sure there are 'HELP' resources there */
- if((hTListNum = CountResources('HELP')) == 0) /* how many titles */
- noHelpResources = TRUE;
- else {
- noHelpResources = FALSE;
- for(i = 0;i < hTListNum;++i) {
- hdl = GetIndResource('HELP', i+1);
- GetResInfo(hdl, &num, &rType, name);
- hNodeList[i] = (struct hNode *) calloc(1, sizeof(struct hNode));
- hNodeList[i]->hName = calloc(1, (int) (name[0] + 1));
- Pstrcpy(hNodeList[i]->hName, name);
- hNodeList[i]->hNum = num;
- ReleaseResource(hdl);
- }
- }
-
- CloseResFile(refNum);
- }
- }
-
-
- /*
- * Show the strings in a textbox sort of environment just like the
- * standard file package. If one of the visible titles is selected,
- * then invert the rectangle around it, thus highlighting it.
- */
-
- HTDisplay()
- {
- int i, ctl_value;
- Rect tRect;
-
- tRect = htBox;
-
- tRect.top += 1;
- tRect.left += 1;
- tRect.bottom = tRect.top + 16;
- tRect.right -= 1;
- ctl_value = GetCtlValue(vsb);
-
- for(i = ctl_value;i < hTListNum;++i) {
- if((i - ctl_value) == MAX_TITLES_IN_BOX)
- break;
- EraseRect(&tRect);
- MoveTo(tRect.left+2, tRect.bottom-4);
- DrawString(hNodeList[i]->hName);
-
- if(i == selection)
- InvertRect(&tRect);
- tRect.top += 16;
- tRect.bottom += 16;
- }
- }
-
-
- /*
- * Handle a mouse-down event in our 'box' that shows all of the
- * titles the user has to choose from.
- */
-
- HTSelect()
- {
- int i, temp_i;
- Rect new_sel_rect, old_sel_rect, tRect;
- Point pt;
-
- SetRect(&old_sel_rect, 0, 0, 0, 0); /* empty rect */
- new_sel_rect = old_sel_rect;
-
- tRect = htBox;
- tRect.top += 1;
- tRect.left += 1;
- tRect.bottom = tRect.top + 16;
- tRect.right -= 1;
- GetMouse(&pt);
-
- for(i = 0;i < MAX_TITLES_IN_BOX;++i) {
- if(PtInRect(pass(pt), &tRect)) {
- temp_i = i;
- new_sel_rect = tRect;
- }
-
- if(selection == (i + GetCtlValue(vsb)))
- old_sel_rect = tRect;
-
- tRect.top += 16;
- tRect.bottom += 16;
- }
-
- if(!EqualRect(&old_sel_rect, &new_sel_rect)) { /* is the selection the same? */
- if(!EmptyRect(&old_sel_rect)) /* is it visible? */
- InvertRect(&old_sel_rect);
- if(temp_i < hTListNum) {
- selection = temp_i + GetCtlValue(vsb);
- InvertRect(&new_sel_rect);
- }
- }
- }
-
-
- /*
- * Handle the scroll bar we set up in our dialog. This routine can
- * handle the titles box and the box where the help text appears, since
- * they are done differently.
- */
-
- MyScroll(wptr)
- WindowPtr wptr;
- {
- int max, oldValue;
- Point pt;
- ControlHandle whichControl;
-
- GetMouse(&pt);
- if(inHelpTextBox)
- max = MAX_LINES_IN_BOX;
- else
- max = MAX_TITLES_IN_BOX;
-
- switch(FindControl(pass(pt), wptr, &whichControl)) {
- case inUpButton:
- TrackControl(vsb, pass(pt), DScrollUp);
- break;
- case inDownButton:
- TrackControl(vsb, pass(pt), DScrollDown);
- break;
- case inPageUp:
- do {
- GetMouse(&pt);
- if(TestControl(whichControl, pass(pt)) == inPageUp) {
- if(inHelpTextBox) {
- oldValue = GetCtlValue(whichControl);
- SetCtlValue(whichControl, oldValue - MAX_LINES_IN_BOX);
- TEScroll(0, (oldValue - GetCtlValue(whichControl)) * (*hTE)->lineHeight, hTE);
- }
- else {
- SetCtlValue(whichControl, GetCtlValue(whichControl) - MAX_TITLES_IN_BOX);
- HTDisplay();
- }
- }
- } while(StillDown());
- break;
- case inPageDown:
- do {
- GetMouse(&pt);
- if(TestControl(whichControl, pass(pt)) == inPageDown) {
- if(inHelpTextBox) {
- oldValue = GetCtlValue(whichControl);
- SetCtlValue(whichControl, oldValue + MAX_LINES_IN_BOX);
- TEScroll(0, (oldValue - GetCtlValue(whichControl)) * (*hTE)->lineHeight, hTE);
- }
- else {
- SetCtlValue(whichControl, GetCtlValue(whichControl) + MAX_TITLES_IN_BOX);
- HTDisplay();
- }
- }
- } while(StillDown());
- break;
- case inThumb:
- oldValue = GetCtlValue(whichControl);
- TrackControl(whichControl, pass(pt), nil);
- if(inHelpTextBox)
- TEScroll(0, (oldValue - GetCtlValue(whichControl)) * (*hTE)->lineHeight, hTE);
- else
- HTDisplay();
- break;
- }
-
- }
-
-
- pascal void DScrollDown(theControl,theCode)
- ControlHandle theControl;
- int theCode;
- {
- if(theCode == inDownButton) {
- if(GetCtlValue(theControl) == GetCtlMax(theControl))
- return; /* don't whip a dead horse */
- SetCtlValue(theControl, GetCtlValue(theControl) + 1);
- if(inHelpTextBox)
- TEScroll(0, -(*hTE)->lineHeight, hTE);
- else
- HTDisplay();
- }
- }
-
-
- pascal void DScrollUp(theControl,theCode)
- ControlHandle theControl;
- int theCode;
- {
- if(theCode == inUpButton) {
- if(GetCtlValue(theControl) == GetCtlMin(theControl))
- return;
- SetCtlValue(theControl, GetCtlValue(theControl) - 1);
- if(inHelpTextBox)
- TEScroll(0, (*hTE)->lineHeight, hTE);
- else
- HTDisplay();
- }
- }
-
-
- /************* miscellaneous routines that don't fit elsewhere *************/
-
-
- /*
- * Copy a Pascal string to a pascal string.
- */
-
- Pstrcpy(dest, src)
- char *dest, *src;
- {
- int i;
-
- for(i = 1;i <= src[0];++i)
- dest[i] = src[i];
-
- dest[0] = src[0];
- }
-