home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / LOBE.src.sit / LOBE.src / cdev.c < prev    next >
Text File  |  1994-12-13  |  7KB  |  228 lines

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    Macintosh Developer Technical Support
  4. #
  5. #    Sample Control Panel Device and INIT Combination
  6. #
  7. #    Program:    INIT - CDEV
  8. #    File:        CDEV.c    -    C Source
  9. #
  10. #    Copyright ゥ 1990 Apple Computer, Inc.
  11. #    All rights reserved.
  12. #
  13. ------------------------------------------------------------------------------*/
  14. #include "common.h"
  15.  
  16. #include <Quickdraw.h>
  17. #include <Files.h>
  18. #include <Memory.h>
  19. #include <OSUtils.h>
  20. #include <TextUtils.h>
  21. #include <Devices.h>
  22. #include <Dialogs.h>
  23. #include <Packages.h>
  24. #include <Resources.h>
  25. #include <Retrace.h>
  26. #include <Packages.h>
  27.  
  28.  
  29. /*------------------------------------------------------------------------------
  30.     Constants
  31. ------------------------------------------------------------------------------*/
  32.  
  33. #define kNoINIT            -4048            /* Alert: INIT was not installed    */
  34. #define kCantWritePrefs    -4047            /* Alert: error writing preferences    */
  35.  
  36. #define kAboutText1                1
  37. #define kAboutText2                2
  38. #define kDebugStrStopButton        3
  39. #define kDebugStrGoButton        4
  40. #define kIgnoreButton            5
  41. #define kManage4                6
  42.  
  43. /*------------------------------------------------------------------------------
  44.     Var block
  45. ------------------------------------------------------------------------------*/
  46.  
  47. typedef struct myVars {
  48.     BusVars        BusVarsCopy;
  49.     BusVars        *running;
  50.     } myVars, *myVarsPtr, **myVarsHandle;
  51.  
  52. /*------------------------------------------------------------------------------
  53.     Procedure Prototypes and External Routines
  54. ------------------------------------------------------------------------------*/
  55. void ItemHit(int item, int numItems, BusVars *initStorage, DialogPtr dialog);
  56. void UpdatePrefs(BusVars *initStorage);
  57. void InitItems( BusVars *initStorage, short numItems, DialogPtr CPDialog);
  58.  
  59. BusVars    *GetCommonStorage();    /* assembly code */
  60. void InitToPrefs(BusVars *,BusVars *);
  61.  
  62.  
  63. pascal long cdevEntry(short message, short item, short numItems, short CPanelID,
  64.         EventRecord    *theEvent, myVarsHandle storage, DialogPtr CPDialog)
  65. {
  66. #pragma unused (CPanelID,theEvent)            /* unused formal parameters */
  67.  
  68.     if (message == macDev) {
  69.         return 1;                /* shouldn't happen if mach resource is okay */
  70.         }
  71.     if (storage != nil) {
  72.         switch (message) {
  73.             case initDev:
  74.                 storage = (myVarsHandle)NewHandle(sizeof(myVars));
  75.                 HLock((Handle)storage);
  76.                 (*storage)->running = GetCommonStorage();
  77.                 if ((*storage)->running) {
  78.                     BlockMove((*storage)->running,&(*storage)->BusVarsCopy,sizeof(BusVars));
  79.                     InitItems( &(*storage)->BusVarsCopy, numItems, CPDialog);
  80.                     HUnlock((Handle)storage);
  81.                     }
  82.                 else {
  83.                     (void) StopAlert(kNoINIT, (ModalFilterProcPtr) nil);
  84.                     return(cdevGenErr);
  85.                 }
  86.                 break;
  87.  
  88.             case closeDev:                    /* clean up and dispose */
  89.                 HLock((Handle)storage);
  90.                 UpdatePrefs( &(*storage)->BusVarsCopy );
  91.                 HUnlock((Handle)storage);
  92.                 DisposHandle((Handle)storage);
  93.                 break;
  94.  
  95.             case hitDev:                    /* handle hit on item */
  96.                 HLock((Handle)storage);
  97.                 ItemHit(item, numItems, &(*storage)->BusVarsCopy, CPDialog);
  98.                 InitToPrefs( &(*storage)->BusVarsCopy, (*storage)->running );
  99.                 HUnlock((Handle)storage);
  100.                 break;
  101.             
  102.             case activDev:                    /* activate any needed items */
  103.             case deactivDev:                /* deactivate any needed items */
  104.             case updateDev:                    /* handle any update drawing */
  105.             case keyEvtDev:                    /* respond to keydown */
  106.             case macDev:
  107.             case undoDev:
  108.             case cutDev:
  109.             case copyDev:
  110.             case pasteDev:
  111.             case clearDev:
  112.             case nulDev:
  113.                 break;
  114.  
  115.         }
  116.  
  117.         return ((long) storage);
  118.     }  /* cdevStorage != nil */
  119.  
  120.     /*
  121.     **    We get here iff cdevStorage = NIL. Return 0 so that Control Panel
  122.     **  will put up "out of memory" error
  123.     */
  124.     return (0);
  125. }
  126.  
  127.  
  128. /* Gets the ControlHandle for the item you want in the dialog box thebox.  */
  129. /* Handy for setting checkboxes and radio buttons */
  130. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  131. {
  132.     short itemtype;
  133.     Rect itemrect;
  134.     Handle thandle;
  135.     
  136.     GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  137.     return((ControlHandle)thandle);
  138. }
  139.  
  140.  
  141. void InitItems( BusVars *initStorage, short numItems, DialogPtr CPDialog) {
  142.     if ( initStorage->Locked ) {
  143.         DebugStr("¥pHm. Seems to be locked. This should never happen.");
  144.         }
  145.     /* Clear the three controls first */
  146.     SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrStopButton),0);
  147.     SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrGoButton),0);
  148.     SetCtlValue(SnatchHandle(CPDialog,numItems+kIgnoreButton),0);
  149.     switch(initStorage->Behavior)
  150.     {
  151.         case Behavior_Ignore:
  152.             SetCtlValue(SnatchHandle(CPDialog,numItems+kIgnoreButton),1);
  153.             break;
  154.         case Behavior_DebugStrGo:
  155.             SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrGoButton),1);
  156.             break;
  157.         case Behavior_DebugStrStop:
  158.             SetCtlValue(SnatchHandle(CPDialog,numItems+kDebugStrStopButton),1);
  159.             break;
  160.     }
  161.     HiliteControl(SnatchHandle(CPDialog,numItems+kManage4),0);
  162.  
  163.     if (initStorage->Do4)
  164.         SetCtlValue(SnatchHandle(CPDialog,numItems+kManage4),1);
  165.     else
  166.         SetCtlValue(SnatchHandle(CPDialog,numItems+kManage4),0);
  167.     }
  168.  
  169.  
  170. /*------------------------------------------------------------------------------
  171.  
  172.     void UpdatePrefs(CDEVHnd cdevStorage)
  173.  
  174.     Called when the CDEV is being closed. This routine writes to a preferences
  175.     file, using the INIT's public globals as the output buffer. The data is
  176.     written to the data fork; no resources are used at all. If the preferences
  177.     file doesn't exist, it is created. Any errors are reported through an
  178.     Alert saying that error number such-and-such occured.
  179.  
  180. ------------------------------------------------------------------------------*/
  181.  
  182. void    UpdatePrefs(BusVars *initStorage) {
  183.     OSErr        err;
  184.     Str255        errString;
  185.     Handle        prefs;
  186.     
  187.     prefs = GetResource('PREF',128);
  188.     if ( prefs ) {
  189.         HLock(prefs);
  190.         BlockMove(initStorage,*prefs,6);            /* HARD CODED LENGTH! EVIL! */
  191.         ChangedResource(prefs);
  192.         WriteResource(prefs);
  193.         HUnlock(prefs);
  194.         }
  195.     else {
  196.         err = ResError();
  197.         NumToString(err, errString);
  198.         ParamText(errString, nil, nil, nil);
  199.         (void) StopAlert(kCantWritePrefs, (ModalFilterProcPtr) nil);
  200.         }
  201.     }
  202.  
  203.  
  204. void ItemHit(int item, int numItems, BusVars *initStorage, DialogPtr dialog) {
  205.     switch( item - numItems ) {
  206.         case kDebugStrStopButton:
  207.             initStorage->Behavior = Behavior_DebugStrStop;
  208.             InitItems(initStorage, numItems, dialog);
  209.             break;
  210.             
  211.         case kDebugStrGoButton:
  212.             initStorage->Behavior = Behavior_DebugStrGo;
  213.             InitItems(initStorage, numItems, dialog);
  214.             break;
  215.             
  216.         case kIgnoreButton:
  217.             initStorage->Behavior = Behavior_Ignore;
  218.             InitItems(initStorage, numItems, dialog);
  219.             break;
  220.             
  221.         case kManage4:
  222.             initStorage->Do4 = !initStorage->Do4;
  223.             InitItems(initStorage, numItems, dialog);
  224.             break;
  225.         }
  226.     }
  227.     
  228.