home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / CW CDEV Framework 1.1 / Framework / CDEVMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-09  |  2.2 KB  |  90 lines  |  [TEXT/MMCC]

  1. #include <A4Stuff.h>
  2. #include <Devices.h>
  3.  
  4. #ifndef    _H_TControlPanel
  5. #include "TControlPanel.h"
  6. #endif
  7.  
  8. // to be declared in your cdev code (see example CDEV.cp file)
  9. extern long    runable(void);
  10. extern TControlPanel *makeCDEV(short numItems,DialogPtr cp);
  11.  
  12. // main entry point for control panel
  13. pascal long main(short message,short item,short numItems,short /*privateValue*/,
  14.                  EventRecord *e, TControlPanel *cdev, DialogPtr d)
  15. {
  16.     // set up a4 so we can access the globals
  17.     long oldA4=SetCurrentA4();
  18.     
  19.     // return code
  20.     long     result=0;
  21.     
  22.     switch (message) {
  23.         // do initialization
  24.         case initDev:
  25.             if ((long)cdev == cdevUnset) {
  26.                 cdev = makeCDEV(numItems,d);
  27.                 cdev->Init();        // call init method
  28.             }
  29.             break;
  30.                 
  31.         // control panel is closing
  32.         case closeDev:
  33.             result=cdevUnset;
  34.             break;
  35.         
  36.         // can we run? return 1 if so, else 0
  37.         case macDev:
  38.             result=runable();
  39.             break;
  40.         
  41.         // it's not an init, open, or close message
  42.         default:
  43.             if ((long)cdev != cdevUnset) {
  44.                 // copy over this event and call the action proc
  45.                 cdev->fEvent = e;            
  46.                 result=cdev->actions(message,item);
  47.             }
  48.             break;
  49.     }
  50.     
  51.     // if there is no error code then make sure we return
  52.     // the cdev as the result, as per IM:MMT pg 8-30
  53.     if (result == 0)
  54.         result = (long)cdev;
  55.     else {
  56.         // there's either an err, or we are closing.  In either case, delete the cdev
  57.         cdev->Close();    // call close routine.
  58.         delete cdev;    // delete the object
  59.         
  60.         // if we are deleting then result is cdevUnset and there is no err.  Otherwise it
  61.         // must be an error code returned by one of the functions.
  62.         
  63.         // make sure the error message conforms to one of the standard return values.
  64.         // If the message doesn't match one of the standard ones, return a
  65.         // generic error that simply closes the cdev w/no message to the user.
  66.         // See NIM:More Mac Toolbox pg 8-47
  67.         switch (result) {
  68.             case cdevGenErr:
  69.             case cdevResErr:
  70.             case cdevUnset:
  71.                 break;
  72.             
  73.             // translate internal out-of-memory code to OS acceptable
  74.             // out-of-memory code.  See define of cdevFWMemErr for additional
  75.             // explanation.
  76.             case cdevFWMemErr:
  77.                 result=cdevMemErr;
  78.                 break;
  79.                 
  80.             default:
  81.                 result=cdevGenErr;
  82.                 break;
  83.         }
  84.     }
  85.     
  86.     // restore a4 before exiting
  87.     SetA4(oldA4);
  88.         
  89.     return(result);
  90. }