home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- *
- * A module for maintaining a phone book
- *
- * Jerry LeVan
- * 325 Boone Trail
- * Richmond Ky 40475
- *
- * New File: Sept 14,1987
- *
- **********************************************************************/
-
- # include <types.h> /* Nearly always required */
- # include <quickdraw.h> /* To access the qd globals */
- # include <fonts.h> /* Only for InitFonts() trap */
- # include <Lists.h>
- # include <Textedit.h>
- # include <Windows.h>
- # include <Events.h>
- # include <Menus.h>
- # include <Dialogs.h>
- # include <segload.h> /* UnloadSeg() */
- # include <resources.h>
- # include <OSUtils.h>
-
- /* Define Item numbers and dialog id */
- #define MYDIALOG 200
-
- #define NAME 7
- #define NUMBER 8
- #define USER 9
- #define CANCEL 2
- #define DIAL 1
- #define ADD 3
- #define DELETE 4
- #define CNUMBER 11
-
- /* key codes */
- #define CR 13
- #define ENTER 3
-
- /* max characters in buffers */
- #define MAXCHARS 80
-
-
- #define __SEG__ Dialer
- /**********************************************************************/
- static ListHandle InitList(theWindow)
- DialogPtr theWindow;
- { Cell theCell;
- short i;
- short tmp;
- char name[MAXCHARS],*p;
- short rescnt,id,whichRow;
- FontInfo finfo;
- Handle aHandle;
- ResType aType;
- Rect box,listRect,theRect;
- ListHandle theList;
-
- /* get the user item box */
- GetDItem(theWindow,USER,&aType,&aHandle,&box);
-
- /* set the view rectangle so that we have a snug fit */
- GetFontInfo(&finfo);
- tmp = finfo.ascent+finfo.descent+finfo.leading;
- theRect.top = box.top;
- theRect.left = box.left;
- theRect.bottom = box.top + ((box.bottom-box.top)/tmp)*tmp;
- theRect.right = box.right-20; /* 20 is room for scroll bar */
-
- /* set cell size */
- theCell.v=0; /* take default height */
- theCell.h=theRect.right-theRect.left; /* and use rect as width */
-
- /* Set the list size */
- SetRect(&listRect,0,0,0,0); /* empty list */
-
- /* create the list */
- theList = LNew(&theRect, /* Display rectangle */
- &listRect, /* Initial size of list */
- &theCell, /* Cell size */
- 0, /* Use standard list proc */
- theWindow, /* window to draw in */
- 0, /* drawing off */
- 0, /* no grow box */
- 0, /* has no horizontal scroll bar */
- 1); /* has vertical scroll bar */
-
- /* Load the cells */
-
- /* step one... count the names */
- rescnt = CountResources('DIAL');
-
- /* step two...Add The Cells */
- LAddColumn(1,0,theList); /* add the first column */
-
- /* Step three...Add the contents */
- whichRow = 0;
-
- for(i=0;i<rescnt;i++){
-
- SetResLoad(0);
- aHandle = GetIndResource('DIAL',i+1);
- GetResInfo(aHandle,&id,&aType,name);
- ReleaseResource(aHandle);
- SetResLoad(1);
-
- tmp = 0; p = (char *)name;
- while(*p++)tmp++;
-
- if (tmp){
- theCell.v = whichRow++; /* row */
- theCell.h = 0; /* first column */
- LAddRow(1,rescnt+1,theList); /* guarantee addition at end of list */
- LSetCell(name,tmp,&theCell,theList); /* set the value */
- }
- }
- (*theList)->selFlags = lOnlyOne; /* only one selection at a time */
- return theList;
- }
- /**********************************************************************/
- /* this is the "userItem" to be installed in the dialog */
-
- static pascal void DoUpDate(theWindow,item)
- DialogPtr theWindow;
- short item;
- { short atype,tmp;
- Handle ahandle;
- Rect box,theRect;
- FontInfo finfo;
-
-
- GetDItem(theWindow,item,&atype,&ahandle,&box);
- GetFontInfo(&finfo);
-
- tmp = finfo.ascent+finfo.descent+finfo.leading;
- theRect.top = box.top;
- theRect.left = box.left;
- theRect.bottom = box.top + ((box.bottom-box.top)/tmp)*tmp;
- theRect.right = box.right-20; /* 20 is room for scroll bar */
-
- InsetRect(&theRect,-1,-1); /* shrink for a nice fit */
- FrameRect(&theRect); /* redraw the frame */
- LUpdate(theWindow->visRgn,(ListHandle)((WindowPeek)theWindow)->refCon);
- }
- /**********************************************************************/
- /* this procedure is called by ModalDialog */
-
- static pascal char myFilter(theWindow,event,item)
- DialogPtr theWindow;
- EventRecord *event;
- short *item;
-
- { Point theLoc;
- char result,buffer[MAXCHARS];
- Cell tmpCell,theCell;
- Rect box;
- short atype,dataLen;
- Handle ahandle,numHandle;
- ListHandle theList;
-
- /* recover a handle to the list */
- theList = (ListHandle)((WindowPeek)theWindow)->refCon;
-
- result = 0; /* assume we don't want to handle */
-
- switch (event->what) {
- case mouseDown:
- {
- theLoc = event->where;
- GetDItem(theWindow,USER,&atype,&ahandle,&box);
- GlobalToLocal(&theLoc);
-
- /* check if mousedown in user item */
- if (PtInRect(&theLoc,&box))
- {
- if(LClick(&theLoc,event->modifiers,theList)){
- *item = DIAL; /* got a double click */
- result = 1;
- } /* end of double click processing */
- else { /* refresh the current number field */
- SetRect(&theCell,0,0,0,0);
- if(LGetSelect(true,&theCell,theList)){
- /* get the selection */
- dataLen = MAXCHARS; /* max length */
- LGetCell(buffer,&dataLen,&theCell,theList);
- buffer[dataLen] = 0; /* make into c-string */
- numHandle=GetNamedResource('DIAL',buffer);
- HLock(numHandle); /* lock it */
-
- GetDItem(theWindow,CNUMBER,&atype,&ahandle,&box);
- SetIText(ahandle,*numHandle);
-
- HUnlock(numHandle);
- ReleaseResource(numHandle);
- } else{ /* erase the number */
- GetDItem(theWindow,CNUMBER,&atype,&ahandle,&box);
- SetIText(ahandle,"");
- }
- } /* if point in rect */
- } /* click */
- } /* mouse down */
- break;
- case keyDown:
- case autoKey: /* fake a dial button hit */
- if(((char)event->message ==CR) || ((char)event->message==ENTER))
- {*item = DIAL; result =1;};
- break;
- break;
- }
- return(result);
- }
- /**********************************************************************/
- PhoneManager(refnum)
- short refnum;
- {
- Rect Box,theRect;
- short item ;
- short type;
- Handle theHandle,numHandle,ahandle;
- Cell theCell,tmpCell;
- char buffer[MAXCHARS],numbuff[MAXCHARS],*p;
- short dataLen,id,tmp;
- long two,one,tmplong;
- GrafPtr savePort;
- DialogPtr theWindow;
- ListHandle theList;
-
- GetPort(&savePort);
-
- /* get the dialog (created invisible) */
- theWindow = (DialogPtr)GetNewDialog(MYDIALOG,0,(Ptr)-1);
- SetPort(theWindow);
-
- /* build the list and store the list handle so the filter proc can find it */
- theList = InitList(theWindow);
- ((WindowPeek)theWindow)->refCon = (long)theList;
-
- /* set the frame draw procedure */
- GetDItem(theWindow,USER,&type,&theHandle,&Box);
- SetDItem(theWindow,USER, type,DoUpDate,&Box);
-
-
- /* outline the dial button */
- GetDItem(theWindow,DIAL,&type,&theHandle,&Box);
- PenSize(3,3);
- InsetRect(&Box,-4,-4);
- FrameRoundRect(&Box,16,16);
- PenSize(1,1);
-
- /* select the add entry fields */
- SelIText(theWindow,NAME,0,32767);
-
- /* turn on list drawing */
- LDoDraw(1,theList);
-
- /* show the dialog window */
- ShowWindow(theWindow);
-
- /* hang around until <cr> or Dial or cancel hit */
- do{
- ModalDialog(myFilter,&item);
- switch (item){
- case DIAL: /* dial the number if there is a selection */
- SetRect(&theCell,0,0,0,0);
- if(LGetSelect(true,&theCell,theList)){
- /* get the selection */
- dataLen = MAXCHARS; /* max length */
- LGetCell(buffer,&dataLen,&theCell,theList);
- buffer[dataLen] = 0; /* make into c-string */
- numHandle=GetNamedResource('DIAL',buffer);
- HLock(numHandle); /* lock it */
-
- /* use Hayes AT commands */
- two = 2;
- FSWrite(refnum,&two,"AT"); Delay(20,&tmplong);
- two =2;
- FSWrite(refnum,&two,"DT"); Delay(20,&tmplong);
- /* and now the number */
- tmplong = 0; p = *numHandle;
- while(*p++)tmplong++; /* compute length */
- FSWrite(refnum,&tmplong,*numHandle);
- one = 1;
- FSWrite(refnum,&one,"\n");
- HUnlock(numHandle);
- ReleaseResource(numHandle);
-
- break;
- } else { SysBeep(10); item = 0;} /* nothing was selected */
- case CANCEL: break; /* leave the dialog */
- case DELETE:
- SetRect(&theCell,0,0,0,0);
- if(LGetSelect(true,&theCell,theList)){
- /* get the selection */
- dataLen = MAXCHARS; /* max length */
- LGetCell(buffer,&dataLen,&theCell,theList);
- buffer[dataLen] = 0; /* make into c-string */
- numHandle=GetNamedResource('DIAL',buffer);
- RmveResource(numHandle);
- DisposHandle(numHandle);
- /* now delete the cell and its contents */
- LDelRow(1,theCell.v,theList);
- /* update the current number field */
- GetDItem(theWindow,CNUMBER,&type,&ahandle,&Box);
- SetIText(ahandle,"");
- } else SysBeep(10); /* no selection */
- break;
- case ADD:
- do
- id = UniqueID('DIAL');
- while (id <= 127); /* get a new id */
-
- GetDItem(theWindow,NAME,&type,&ahandle,&Box); /* get the name */
- GetIText(ahandle,buffer);
-
- /* if name in use, then ignore request */
- theHandle = GetNamedResource('DIAL',buffer);
-
- if(theHandle == nil){ /* no resource ,ok to add */
-
- GetDItem(theWindow,NUMBER,&type,&ahandle,&Box);
- GetIText(ahandle,numbuff); /* get the text */
-
- tmp = 0; p = (char *)numbuff; /* get length of number */
- while(*p++)tmp++;
-
- tmp++; /* for the null at the end of the string */
-
- PtrToHand(numbuff,&ahandle,(long)tmp);
-
- AddResource(ahandle,'DIAL',id,buffer);
- WriteResource(ahandle); /* write it */
- ReleaseResource(ahandle) ; /* free it */
-
- /* add to the list */
- theCell.v = (**theList).dataBounds.bottom;
- theCell.h = 0;
- LAddRow(1,theCell.v,theList);
-
- tmp = 0; p = (char *)buffer; /* get length of name */
- while(*p++)tmp++;
-
- LSetCell(buffer,tmp,&theCell,theList);
- }
- if(theHandle)SysBeep(10); /* signal failure */
- /* reset the fields */
- GetDItem(theWindow,NAME,&type,&ahandle,&Box); /* get the name field */
- SetIText(ahandle,"Put Name Here");
- SelIText(theWindow,NAME,0,32767);
- /* and now the number field */
- GetDItem(theWindow,NUMBER,&type,&ahandle,&Box); /* get the number field */
- SetIText(ahandle,"Put Number Here");
- break;
-
- default: break;
- } /* switch */
- }
- while((item != CANCEL) && (item != DIAL));
-
- /* get rid of data structures and return to finder */
- LDispose(theList);
- DisposDialog(theWindow);
- SetPort(savePort);
-
- }
- /**********************************************************************/
-