home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- Project : MacPerl - Real Perl Application
- File : MPHelp.c - Various helpful functions
- Author : Matthias Neeracher
- Language : MPW C
-
- $Log: MPHelp.c,v $
- Revision 1.1 1994/02/27 23:01:10 neeri
- Initial revision
-
- Revision 0.2 1993/09/08 00:00:00 neeri
- Corrected some misunderstandings of dbm
-
- Revision 0.1 1993/08/17 00:00:00 neeri
- Use Application directory
-
- *********************************************************************/
-
- #include "MPHelp.h"
- #include "MPConsole.h"
- #include "MPUtils.h"
-
- #include <Menus.h>
- #include <Balloons.h>
- #include <ToolUtils.h>
- #include <TFileSpec.h>
- #include <ndbm.h>
- #include <ctype.h>
- #include <PLStringFuncs.h>
- #include <Folders.h>
- #include <fcntl.h>
- #include <ioctl.h>
- #include <stdio.h>
- #include <string.h>
-
- static void MetaHelp(StringPtr msg, StringPtr m1, StringPtr m2, StringPtr m3)
- {
- SetCursor(&qd.arrow);
- ParamText(msg, m1, m2, m3);
- Alert(ErrorAlert, nil);
- }
-
- static MenuHandle gHelpMenus[20];
- static Handle gHelpURLs[20];
- static DBM * gHelpIndex = nil;
- static Boolean gHasHelp = true;
-
- void InitHelpIndex()
- {
- int depth = 0;
- int curIndex = 0;
- int nextIndex= 0;
- MenuHandle curMenu;
- char * menu;
- char * scan;
- char * next;
- datum key;
- datum data;
- Str255 contrib;
- int menuStack[10];
-
- AppendMenu(myMenus[helpM], "\p-(");
-
- if (!gHelpIndex)
- goto whyNoHelp;
-
- key.dptr = " MENU";
- key.dsize = 5;
- data = dbm_fetch(gHelpIndex, key);
-
- if (!data.dptr)
- goto whyNoHelp;
-
- curMenu = gHelpMenus[curIndex = nextIndex++] = myMenus[helpM];
- menuStack[depth] = curIndex;
- PtrToHand("\n\n\n\n\n\n\n\n\n\n", &gHelpURLs[curIndex], CountMItems(curMenu));
- for (menu = data.dptr; depth>=0; menu = next+1) {
- next = strchr(menu, '\n');
- if (menu == next)
- if (!depth--)
- break;
- else {
- curIndex = menuStack[depth];
- curMenu = gHelpMenus[curIndex];
- continue;
- }
- scan = strchr(menu, '\t');
- contrib[0] = scan - menu;
- memcpy(contrib+1, menu, contrib[0]);
- AppendMenu(curMenu, contrib);
- PtrAndHand(scan+1, gHelpURLs[curIndex], next - scan);
- if (*++scan == '!') {
- curIndex = nextIndex++;
- gHelpMenus[curIndex] = NewMenu(kHierHelpMenu+curIndex, "\p");
- SetItemCmd(curMenu, CountMItems(curMenu), 0x1B);
- SetItemMark(curMenu, CountMItems(curMenu), kHierHelpMenu+curIndex);
- curMenu = gHelpMenus[curIndex];
- InsertMenu(curMenu, -1);
- gHelpURLs[curIndex] = NewHandle(0);
- menuStack[++depth] = curIndex;
- }
- }
- return;
- whyNoHelp:
- gHasHelp = false;
- AppendMenu(myMenus[helpM], "\pEnabling online Help...");
- }
-
- void InitHelp()
- {
- CInfoPBRec info;
- FSSpec BalloonPath;
-
- if (gHelpIndex || !gHasHelp)
- return;
-
- BalloonPath.vRefNum = gAppVol;
- BalloonPath.parID = gAppDir;
- PLstrcpy(BalloonPath.name, (StringPtr) "\pMacPerl Help");
-
- if (!FSpCatInfo(&BalloonPath, &info))
- if (gHelpIndex = dbm_open(FSp2FullPath(&BalloonPath), DBM_RDONLY, 0))
- return;
-
- if (!FindFolder(
- kOnSystemDisk,
- kPreferencesFolderType,
- false,
- &BalloonPath.vRefNum,
- &BalloonPath.parID)
- && !FSpCatInfo(&BalloonPath, &info)
- )
- gHelpIndex = dbm_open(FSp2FullPath(&BalloonPath), DBM_RDONLY, 0);
- }
-
- void EndHelp()
- {
- if (gHelpIndex) {
- dbm_close(gHelpIndex);
-
- gHelpIndex = nil;
- }
- }
-
- void Explain(DPtr doc)
- {
- OSErr err;
- TEHandle te;
- datum key;
- datum data;
- char * text;
- int len;
- short pos;
- short start;
-
- InitHelp();
-
- if (!gHelpIndex) {
- MetaHelp(
- "\pTo enable online help, put the file \"MacPerl Help\" "
- "in the same folder as the MacPerl application and "
- "restart MacPerl.", (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
- return;
- }
-
- if (doc) {
- te = doc->theText;
-
- if (pos = (*te)->selEnd - (*te)->selStart) {
- HLock((*te)->hText);
- text = *(*te)->hText;
- start= (*te)->selStart;
- while (pos && isspace(text[start]))
- ++start, --pos;
- while (pos && isspace(text[start+pos-1]))
- --pos;
- }
- if (!pos)
- HUnlock((*te)->hText);
- } else
- pos = 0;
-
- if (!pos) {
- MetaHelp("\pYou didn't select any text to look up.",
- (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
-
- EndHelp();
-
- return;
- }
-
- key.dptr = text+start;
- key.dsize = pos;
- data = dbm_fetch(gHelpIndex, key);
- HUnlock((*te)->hText);
- EndHelp();
-
- if (!data.dptr) {
- Str255 keyStr;
-
- keyStr[0] = key.dsize;
- memcpy(keyStr+1, key.dptr, key.dsize);
-
- MetaHelp("\pUnfortunately, I can't give you any help for \"",
- keyStr, (StringPtr) "\p\"", (StringPtr) "\p");
- } else
- LaunchHelpURL(data.dptr, data.dsize);
- }
-
- void LaunchHelpURL(char * urlPtr, int urlLen)
- {
- OSErr err;
- char * text;
- int len;
- char url[100];
-
- if (*urlPtr == '!')
- return; /* False positive */
- if (!strncmp(urlPtr, "file:", 5) && urlPtr[5] != '/') {
- FSSpec here;
-
- here.vRefNum = gAppVol;
- here.parID = gAppDir;
-
- FSpUp(&here);
- strcpy(url, "file:///");
- strcpy(url+8, FSp2FullPath(&here));
- for (text = url+8; text = strchr(text, ':'); *text = '/');
- len = strlen(url);
- if (url[len-1] != '/')
- url[len++] = '/';
- memcpy(url+len, urlPtr+5, urlLen-5);
- len += urlLen-5;
- } else {
- memcpy(url, urlPtr, urlLen);
- len = urlLen;
- }
-
- if (gICInstance) {
- long selStart = 0;
- long selEnd = len;
-
- if (!ICLaunchURL(gICInstance, "\p", url, len, &selStart, &selEnd))
- return;
- }
- MetaHelp("\pFailed to launch help viewer. "
- "Please make sure that you have Internet Config 1.2b3 or later "
- "installed and valid Helpers for http: and file: set",
- (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
- }
-
- void DoHelp(short menu, short item)
- {
- if (gHasHelp) {
- Handle urls = gHelpURLs[menu];
- char * url;
-
- HLock(urls);
- for (url = *urls; --item; url = strchr(url, '\n')+1);
- LaunchHelpURL(url, strchr(url, '\n') - url);
- HUnlock(urls);
- } else
- MetaHelp(
- "\pTo enable online help, put the file \"MacPerl Help\" "
- "in the same folder as the MacPerl application and "
- "restart MacPerl.", (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
- }
-
-