home *** CD-ROM | disk | FTP | other *** search
- /* Dialog handler routines. Quick and easy */
- /* Copyright 1987 */
- /* David Palmer */
- /* Mail code 220-47 */
- /* California Institute Of Technology */
- /* Uses EasyDialog.c (also ⌐ 1987 By David Palmer) */
- /* Duplication, modification, and examination allowed on a */
- /* non-commercial basis only. Commercial use prohibited */
- /* without prior written agreement with the author. (This */
- /* includes sale by for-profit companies, and use as an */
- /* inducement to buy something.) */
-
- /* This package handles modal dialogs in a very easy to program */
- /* way. Objects in a dialog box either change data, cause actions, */
- /* or both. What they do is controlled by a data structure passed, */
- /* along with the dialog's resource id, to the dialog handler. */
- /* The format of the data structure is described in the headerfile */
- /* EasyDialog.h */
-
- #include <DialogMgr.h>
- #include <unix.h>
- #include "LSC Stars:EasyDialog.h"
- #include <StdFilePkg.h>
-
- char *CtoPstr();
- char *PtoCstr();
-
- int atoi();
- long atol();
- double atof();
-
- char *Pto2Cstr(pchS, pchD)
- char *pchS, *pchD;
- {
- int i;
- char *pch;
- pch = pchD;
- for (i = *pchS++ ; i > 0 ; i--)
- *pch++ = *pchS++;
- return pchD;
- }
-
-
- char *Cto2Pstr(pchS, pchD)
- char *pchS, *pchD;
- {
- char *pch;
- for (pch = pchD + 1 ; *pchS != '\0' ; )
- *pch++ = *pchS++;
- *pchD = pch - pchD - 1;
- return pchD;
- }
-
- RetrieveValue(pdi, pedi)
- EDITEM *pedi;
- DialogPtr pdi;
- {
- int cch;
- char *pch, *pch2;
- char rgch[MAXEDCCH];
- int i;
- long type;
- Handle item;
- Rect box;
-
- for (i = 0 ; i < pedi->nitems ; i++) {
- GetDItem(pdi, i + pedi->firstitem, &type, &item, &box);
- switch (pedi->itemtype) {
- case edbutton:
- break;
- case edchar:
- GetIText(item, rgch);
- ((char *)(pedi->pvalue))[i] = rgch[1];
- break;
- case edstring:
- GetIText(item, rgch);
- Pto2Cstr(rgch, ((char *)pedi->pvalue) + i*MAXEDCCH);
- break;
- case edint:
- GetIText(item, rgch);
- PtoCstr(rgch);
- stcd_i(rgch, ((int *)pedi->pvalue) + i);
- break;
- case edfloat:
- GetIText(item, rgch);
- PtoCstr(rgch);
- sscanf(rgch, "%lg", ((double *)pedi->pvalue)+i);
- break;
- case edrad:
- if (0 != GetCtlValue(item))
- *(int *)pedi->pvalue = i + 1;
- break;
- case edcheck:
- ((int *)pedi->pvalue)[i] = GetCtlValue(item);
- break;
- default:
- SysBeep(10);
- }
- }
- }
-
- ShowValue(pdi, pedi)
- DialogPtr pdi;
- EDITEM *pedi;
- {
- int cch;
- char *pch, *pch2;
- char rgch[MAXEDCCH];
- int i;
- long type;
- Handle item;
- Rect box;
-
- for (i = 0 ; i < pedi->nitems ; i++) {
- GetDItem(pdi, i + pedi->firstitem, &type, &item, &box);
- switch (pedi->itemtype) {
- case edbutton: /* surrounds default button */
- /* to be written */
- break;
- case edchar:
- rgch[0] = '\001';
- rgch[1] = ((char *)(pedi->pvalue))[i];
- SetIText(item, rgch);
- break;
- case edstring:
- SetIText(item,
- Cto2Pstr(((char *)pedi->pvalue) + i*MAXEDCCH, rgch));
- break;
- case edint:
- rgch[0] = stci_d(&rgch[1], ((int *)pedi->pvalue)[i], 100);
- SetIText(item, rgch);
- break;
- case edfloat:
- sprintf(rgch, "%g", ((double *)pedi->pvalue)[i]);
- SetIText(item, CtoPstr(rgch));
- break;
- case edrad:
- SetCtlValue(item, Bool10((i + 1) == *(int *)pedi->pvalue));
- break;
- case edcheck:
- SetCtlValue(item, Bool10(((int *)pedi->pvalue)[i]));
- break;
- default:
- SysBeep(10);
- }
- }
- }
-
-
- ChangeRCValue(pdi, pedi, itemnum) /* change the value of an radio button or check box */
- DialogPtr pdi;
- EDITEM *pedi;
- int itemnum;
- {
- if (pedi->itemtype == edrad) {
- *(int *)pedi->pvalue = itemnum - pedi->firstitem + 1;
- ShowValue(pdi, pedi);
- } else if (pedi->itemtype == edcheck) {
- *((int *)pedi->pvalue + itemnum - pedi->firstitem) =
- ! *((int *)pedi->pvalue + itemnum - pedi->firstitem);
- ShowValue(pdi, pedi);
- } else {
- SysBeep(3);
- }
- }
-
-
- EDITEM *FindDiHit(pdi, pedi, itemnum)
- DialogPtr pdi;
- EDITEM *pedi;
- int itemnum;
- {
- int i;
- for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
- if (pedi[i].firstitem <= itemnum &&
- itemnum < pedi[i].firstitem + pedi[i].nitems)
- return &pedi[i];
- }
- return 0;
- }
-
- int DoDiHit(pdi, pedi, itemnum)
- DialogPtr pdi;
- EDITEM *pedi;
- int itemnum;
- {
- int returnvalue;
- EDITEM *pedihit;
- pedihit = FindDiHit(pdi, pedi, itemnum);
- if (pedihit == 0) {
- SysBeep(10);
- return 0;
- }
-
- switch (pedihit->itemtype) {
- case edbutton: /* buttons are ordinarily returned */
- /* unless the button runs a function */
- returnvalue = itemnum;
- break;
- case edrad:
- case edcheck:
- ChangeRCValue(pdi, pedihit, itemnum);
- returnvalue = 0;
- break;
- case edchar:
- case edstring:
- case edint:
- case edfloat:
- returnvalue = 0;
- break;
- default:
- SysBeep(5);
- returnvalue = 0;
- }
- if (pedihit->pfunc != NULL) {
- returnvalue = (*(pedihit->pfunc))(pdi, pedi, pedihit, itemnum);
- }
- return returnvalue;
- }
-
-
- int EasyDialog(id, pedi)
- int id; /* dialog resource number*/
- EDITEM *pedi; /* list of dialog items */
- {
- int i;
- DialogPtr pdi;
- int itemhit;
- int whichbutton;
-
- pdi = GetNewDialog(id, NULL, -1l);
- if (pdi == NULL) {
- SysBeep(30);
- return 0;
- }
- for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
- if (pedi[i].finit) {
- ShowValue(pdi, &pedi[i]);
- }
- }
- if ( ((DialogPeek)pdi)->editField != -1) {
- TESetSelect(0l, 32767l, ((DialogPeek)pdi)->textH);
- }
- do {
- ModalDialog(NULL, &itemhit);
- } while (0 == (whichbutton = DoDiHit(pdi, pedi, itemhit)));
- for (i = 0 ; pedi[i].itemtype != edlast ; i++) {
- RetrieveValue(pdi, &pedi[i]);
- }
- DisposDialog(pdi);
- return whichbutton;
- }
-
- GetFile(pchtypes, pchfname, pvol) /* Get volume and filename info */
- char *pchtypes, *pchfname;
- int *pvol;
- {
- int trash;
- int ntypes;
- Point where;
- SFReply sfr;
-
- where.h = 75; where.v = 75;
- ntypes = strlen(pchtypes)/4;
- if (ntypes == 0)
- ntypes = -1;
- SFGetFile(where, NULL, NULL, ntypes, pchtypes, NULL, &sfr);
- if (sfr.good) {
- if (pvol != 0)
- *pvol = sfr.vRefNum;
- SetVol("", sfr.vRefNum); /* Set up volume for next open */
- Pto2Cstr(sfr.fName, pchfname);
- return TRUE;
- } else
- return FALSE;
- }
-
- PutFile(pchoname, pchnname, pvol)
- char *pchoname, *pchnname; /* old name, new name */
- int *pvol; /* volume number */
- {
- Point where;
- SFReply sfr;
-
- where.h = 75; where.v = 75;
- if (pvol != 0 && *pvol != -1) {
- sfr.vRefNum = *pvol;
- } else {
- GetVol(&sfr.fName, &sfr.vRefNum);
- }
- Cto2Pstr(pchoname, sfr.fName);
- SFPutFile(where, "", &sfr.fName, NULL, &sfr);
- if (sfr.good) {
- if (pvol != NULL)
- *pvol = sfr.vRefNum;
- Pto2Cstr(sfr.fName, pchnname);
- return TRUE;
- } else
- return FALSE;
- }