home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / CdevTester Folder / cdev tester.c next >
Encoding:
C/C++ Source or Header  |  1993-03-04  |  6.4 KB  |  300 lines  |  [TEXT/KAHL]

  1. #include "General.h"
  2. #include "cdev tester.proto.h"
  3.     
  4. WindowPtr theControlPanel=NIL;
  5. Boolean    gDone=FALSE,gHasCursor=FALSE;
  6. long cdevValue=0;
  7.  
  8. void main(void) 
  9. {    
  10.     InitGraf(&thePort);
  11.  
  12.     InitFonts();
  13.     FlushEvents(everyEvent,0);
  14.     InitWindows();
  15.     InitMenus();
  16.     TEInit();
  17.     InitDialogs(0L);
  18.     InitCursor();
  19.     MaxApplZone();
  20.     
  21.     SetUpMenus();
  22.     if(GetResource('CURS',-4064)!=NIL) gHasCursor=TRUE;
  23.     EventLoop();
  24.     
  25. }
  26.  
  27. void SetUpMenus(void)
  28. {
  29.     SetMenuBar(GetNewMBar(RES_ID));
  30.     AddResMenu(GetMHandle(APPLE_MENU),'DRVR');
  31.     MaintainMenus();
  32. }
  33.  
  34. void MaintainMenus(void)
  35. {
  36.     if(!theControlPanel)
  37.     {    DisableItem(GetMenu(EDIT_MENU),0);
  38.         DisableItem(GetMenu(FILE_MENU),CLOSE);
  39.         EnableItem(GetMenu(FILE_MENU),OPEN);
  40.     }
  41.     else
  42.     {    EnableItem(GetMenu(EDIT_MENU),0);
  43.         EnableItem(GetMenu(FILE_MENU),CLOSE);
  44.         DisableItem(GetMenu(FILE_MENU),OPEN);
  45.     }
  46.     DrawMenuBar();
  47. }    
  48.  
  49. void EventLoop(void)
  50. {    short theChar;
  51.     EventRecord theEvent;
  52.     Point theMouse;
  53.     Boolean dlogEvent;
  54.     DialogPtr theDialog;
  55.     int theItem;
  56.     
  57.     while (!gDone)
  58.     {    
  59.         WaitNextEvent(everyEvent,&theEvent,MIN_SLEEP,NIL_MOUSE_REGION);
  60.         dlogEvent=IsDialogEvent(&theEvent);
  61.         
  62.         if(dlogEvent && theEvent.what==keyDown)
  63.             ErrorCheck(cdev(keyEvtDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  64.         DialogSelect(&theEvent,&theDialog,&theItem);
  65.  
  66.         switch(theEvent.what)
  67.         {    case nullEvent:
  68.                 if(dlogEvent)
  69.                 {    ErrorCheck(cdev(nulDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  70.                     if(gHasCursor)
  71.                     {    theMouse=theEvent.where;
  72.                         GlobalToLocal(&theMouse);
  73.                         if(PtInRect(theMouse,&theControlPanel->portRect))
  74.                             ErrorCheck(cdev(cursorDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  75.                     }
  76.                 }
  77.                 break;
  78.             case mouseDown:
  79.                 if(dlogEvent)
  80.                     ErrorCheck(cdev(hitDev,theItem,0,0,&theEvent,cdevValue,theControlPanel));
  81.                 else HandleMouseDown(theEvent);
  82.                 break;
  83.             case keyDown:
  84.             case autoKey:
  85.                 theChar=theEvent.message & charCodeMask;
  86.                 if(theEvent.modifiers & cmdKey)
  87.                 {    if(theEvent.what!=autoKey)
  88.                         HandleMenuChoice(MenuKey(theChar));
  89.                 }
  90.                 break;
  91.             case updateEvt:
  92.                 if(dlogEvent)
  93.                     ErrorCheck(cdev(updateDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  94.                 break;
  95.             case activateEvt:
  96.                 if((theEvent.modifiers&activeFlag)!=0 && dlogEvent)
  97.                     ErrorCheck(cdev(activDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  98.                 else
  99.                     ErrorCheck(cdev(deactivDev,0,0,0,&theEvent,cdevValue,theControlPanel));
  100.                 break;
  101.             default:
  102.                 break;
  103.         }
  104.     }
  105.     ExitToShell();
  106. }
  107.  
  108. void SAlert(int index)
  109. {    Str63 errorText;
  110.  
  111.     GetIndString(errorText,128,index);
  112.     ParamText(errorText,"","","");
  113.     StopAlert(128,NIL);
  114. }
  115.  
  116. void ErrorCheck(long returnValue)
  117. {    Str63 errorText;
  118.  
  119.     switch(returnValue)
  120.     {    case 0:
  121.         case 1:
  122.             SAlert(returnValue+1);
  123.         case -1:
  124.             cdevValue=0;
  125.             CloseControlPanel();
  126.             MaintainMenus();
  127.             break;
  128.         default:
  129.             cdevValue=returnValue;
  130.             break;
  131.     }
  132. }
  133.  
  134. Boolean CheckControlPanel(void)
  135. {    Boolean result=1;
  136.     machHandle machHndl;
  137.     register int x;
  138.     
  139.     machHndl=(machHandle)GetResource('mach',-4064);
  140.     if((*machHndl)->softmask==0x0000 && (*machHndl)->hardmask==0xFFFF)
  141.         result=(Boolean)cdev(macDev,0,0,0,NIL,0,theControlPanel);
  142.     else
  143.     {    for(x=0;x<=15;x++)
  144.         {    if(BitTst(&(*machHndl)->softmask,x)==0)
  145.             {    if(BitTst(&ROM85,x)!=0) result=0;    }
  146.         }
  147.         /* for(x=0;x<=15;x++)
  148.         {    if(BitTst(&(*machHndl)->hardmask,x)==1)
  149.             {    if(BitTst(&HwCfgFlgs,x)!=1) result=0;    }
  150.         } */
  151.     }
  152.     cdevValue=cdevUnset;
  153.     return(result);
  154. }
  155.  
  156. void OpenControlPanel(void)
  157. {    Rect boundsRect;
  158.     Handle itemsHndl;
  159.     TEHandle textHndl;
  160.         
  161.     boundsRect=(*(nrctHandle)GetResource('nrct',-4064))->theRect[0];
  162.     OffsetRect(&boundsRect,-47,45);
  163.     itemsHndl=GetResource('DITL',-4064);
  164.     HandToHand(&itemsHndl);
  165.     SetDAFont(geneva);
  166.     theControlPanel=NewDialog(NIL,&boundsRect,"\pControl Panel Tester",
  167.                     FALSE,noGrowDocProc,(WindowPtr)-1L,TRUE,0L,itemsHndl);
  168.     SetDAFont(0);
  169.     textHndl=((DialogPeek)theControlPanel)->textH;
  170.     (*textHndl)->txSize=9;
  171.     (*textHndl)->lineHeight=12;
  172.     (*textHndl)->fontAscent=10;
  173.     TECalText(textHndl);    
  174.     SetPort(theControlPanel);
  175.     SetOrigin(87,-1);
  176.     TextSize(9);
  177.     
  178.     ShowWindow(theControlPanel);
  179.     SelectWindow(theControlPanel);
  180.     
  181.     ErrorCheck(cdev(initDev,0,0,0,NIL,cdevValue,theControlPanel));
  182. }
  183.  
  184. void CloseControlPanel(void)
  185. {    cdev(closeDev,0,0,0,NIL,cdevValue,theControlPanel);
  186.     CloseDialog(theControlPanel);
  187.     theControlPanel=NIL;
  188. }
  189.  
  190. void HandleMouseDown(EventRecord theEvent)
  191. {    WindowPtr theWindow;
  192.     Rect dragRect;
  193.     
  194.     switch(FindWindow(theEvent.where,&theWindow))
  195.     {    case inMenuBar:
  196.             HandleMenuChoice(MenuSelect(theEvent.where));
  197.             break;
  198.         case inSysWindow:
  199.             SystemClick(&theEvent,theWindow);
  200.             MaintainMenus();
  201.             break;
  202.         case inDrag:
  203.             SetRect(&dragRect,DRAG,DRAG,screenBits.bounds.right-DRAG,screenBits.bounds.bottom-DRAG);
  204.             DragWindow(theWindow,theEvent.where,&dragRect);
  205.             break;
  206.         case inGoAway:
  207.             if(TrackGoAway(theWindow,theEvent.where))
  208.             {    CloseControlPanel();
  209.                 MaintainMenus();
  210.             }
  211.             break;
  212.         default:
  213.             break;
  214.     }
  215. }
  216.  
  217. void HandleMenuChoice(long menuChoice)
  218. {    int    theMenu,theItem;
  219.     
  220.     if(menuChoice)
  221.     {    theMenu=HiShort(menuChoice);
  222.         theItem=LoShort(menuChoice);
  223.         
  224.         switch(theMenu)
  225.         {    case APPLE_MENU:
  226.                 HandleAppleChoice(theItem);
  227.                 break;
  228.             case FILE_MENU:
  229.                 HandleFileChoice(theItem);
  230.                 break;
  231.             case EDIT_MENU:
  232.                 HandleEditChoice(theItem);
  233.                 break;
  234.             default:
  235.                 break;
  236.         }
  237.     }
  238.     HiliteMenu(0);
  239. }
  240.  
  241. void HandleAppleChoice(int theItem)
  242. {    Str32 accName;
  243.     DialogPtr theDialog;
  244.     int itemHit;
  245.     
  246.     switch(theItem)
  247.     {    case ABOUT:
  248.             theDialog=GetNewDialog(128,NIL,(WindowPtr)-1L);
  249.             ModalDialog(NIL,&itemHit);
  250.             DisposDialog(theDialog);
  251.             break;
  252.         default:
  253.             GetItem(GetMHandle(APPLE_MENU),theItem,accName);
  254.             OpenDeskAcc(accName);
  255.             break;
  256.     }
  257. }
  258.  
  259. void HandleFileChoice(int theItem)
  260. {    switch(theItem)
  261.     {    case OPEN:
  262.             if(!CheckControlPanel())
  263.             {    SAlert(3);
  264.                 break;
  265.             }
  266.             OpenControlPanel();
  267.             MaintainMenus();
  268.             break;
  269.         case CLOSE:
  270.             CloseControlPanel();
  271.             MaintainMenus();
  272.             break;
  273.         case QUIT:
  274.             if(theControlPanel!=NIL) CloseControlPanel();
  275.             gDone=TRUE;
  276.             break;
  277.         default:
  278.             break;
  279.     }
  280. }
  281.  
  282. void HandleEditChoice(int theItem)
  283. {    switch(theItem)
  284.     {    case UNDO:
  285.             ErrorCheck(cdev(undoDev,0,0,0,NIL,cdevValue,theControlPanel));
  286.             break;
  287.         case CUT:
  288.             ErrorCheck(cdev(cutDev,0,0,0,NIL,cdevValue,theControlPanel));
  289.             break;
  290.         case COPY:
  291.             ErrorCheck(cdev(copyDev,0,0,0,NIL,cdevValue,theControlPanel));
  292.             break;
  293.         case PASTE:
  294.             ErrorCheck(cdev(pasteDev,0,0,0,NIL,cdevValue,theControlPanel));
  295.             break;
  296.         case CLR:
  297.             ErrorCheck(cdev(clearDev,0,0,0,NIL,cdevValue,theControlPanel));
  298.             break;
  299.     }
  300. }