home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroTCL3.0 folder / TCL / NeoDemo / Source / CNDCamera.cp < prev    next >
Encoding:
Text File  |  1994-08-29  |  7.8 KB  |  289 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CNDCamera.cp
  3.  *
  4.  *    Methods for a persistent camera class.
  5.  *
  6.  *  Copyright © 1992 NeoLogic Systems.  All rights reserved.
  7.  *
  8.  ****/
  9.  
  10. #include "NeoTypes.h"
  11. #include CNeoStreamH
  12. #include CNeoMetaClassH
  13. #include CNeoSelectH
  14. #include CNeoDatabaseH
  15. #include CNeoNodeH
  16. #include "CNDCamera.h"
  17.  
  18. #ifndef NeoInherited
  19. #define NeoInherited    CNeoPersist
  20. #endif
  21.  
  22. short        CNDCamera::FCameraCnt                        = 0;
  23. NeoID        CNDCamera::FCamMenu2IDTable[kMaxCameras]    = {0};
  24.  
  25. CNDCamera::CNDCamera(const CNeoString &aName, const short aShutterCnt, const short *aSpeeds)
  26. {
  27.     short    index;
  28.  
  29.     fShutterCount = aShutterCnt;
  30.     for (index = 0; index <fShutterCount; index++)
  31.         fShutters[index] = aSpeeds[index];
  32.     fName = aName;
  33. }
  34.  
  35. // Return the class ID for this kind of object
  36. NeoID CNDCamera::getClassID(void) const
  37. {
  38.     return kNDCameraID;
  39. }
  40.  
  41. /*
  42.  * Allocate and initialize a new instance.
  43.  */
  44. CNeoPersist *CNDCamera::New(void)
  45. {
  46.     CNDCamera *    object;
  47.  
  48.     object = new CNDCamera("\p", 0, nil);
  49.  
  50.     return object;
  51. }
  52.  
  53. /*
  54.  * Overriding this virtual method from the parent is an optimization which allows us
  55.  * to avoid calling GetHandleSize().
  56.  */
  57. #pragma segment NeoInfo
  58. long CNDCamera::getLength(void) const
  59. {
  60.     return sizeof(CNDCamera);
  61. }
  62.  
  63. /*
  64.  * This method returns the amount of file space the object occupies in the file. It
  65.  * differs from the getLength() in that getLength returns the size of the object in
  66.  * memory.
  67.  */
  68. #pragma segment NeoInfo
  69. long CNDCamera::getFileLength(void) const
  70. {
  71.     return kNeoPersistFileLength + sizeof(fShutterCount) + sizeof(fShutters) + sizeof(fName);
  72. }
  73.  
  74. /* ****************************************************************** */
  75.                         /** I/O Methods **/
  76. /* ****************************************************************** */
  77.  
  78. /*
  79.  * The readObject method is used to read in the permanent data members of the
  80.  * object from the given stream. In most cases, this method operates without concern
  81.  * for the type of stream the data values are coming from. As a result, this method
  82.  * is capable of reading from such diverse sources as the data fork of a file, a
  83.  * resource fork, an AppleEvent, the clipboard, a memory block, and so on.
  84.  *
  85.  * The aTag argument indicates how much information to read from the stream.
  86.  * It is sometimes sufficient to read in a minimum amount of data and have all
  87.  * other data be read in on demand. Other times it is necessary to read all the
  88.  * data immediately as the stream may not be available later.
  89.  *
  90.  * The two tag values that are currently used are kNeoObjectTag and kNeoAllTag.
  91.  *
  92.  * The value kNeoObjectTag means that only those values of immediate interest
  93.  * need to be read from the stream, though implementations of readObject may read
  94.  * in additional data member values if they wish.
  95.  *
  96.  * The value kNeoAllTag indicates that all data member values for this class
  97.  * should be read in to the stream.
  98.  */
  99. #pragma segment NeoRead
  100. void CNDCamera::readObject(CNeoStream *aStream, const NeoTag aTag)
  101. {
  102.     short    index;
  103.  
  104.     NeoInherited::readObject(aStream, aTag);
  105.  
  106.     fShutterCount = aStream->readShort(pShutterCount);
  107.     if (fShutterCount) {
  108.         aStream->openList(pCameraShutters);
  109.         for (index = 0; index < fShutterCount; index++) {
  110.             fShutters[index] = aStream->readShort();
  111.         }
  112.         aStream->closeList();
  113.     }
  114.  
  115.     aStream->readNativeString(fName, sizeof(fName), pName);
  116. }
  117.  
  118. /*
  119.  * The writeObject method is used to write to the permanent data members of the
  120.  * object from the given stream. In most cases, this method operates without concern
  121.  * for the type of stream the data values are coming from. As a result, this method
  122.  * is capable of writing to such diverse sources as the data fork of a file, a
  123.  * resource fork, an AppleEvent, the clipboard, a memory block, and so on.
  124.  *
  125.  * The aTag argument indicates how much information to write to the stream.
  126.  * It is sometimes sufficient to write out only those parts that are dirty. Other
  127.  * times it is necessary to write all the data, as when doing Save As operation
  128.  * or when communicating an object's complete state across and RPC channel.
  129.  *
  130.  * The two tag values that are currently used are kNeoObjectTag and kNeoAllTag.
  131.  *
  132.  * The value kNeoObjectTag means that only those values of immediate interest
  133.  * need to be written to the stream, though implementations of writeObject may
  134.  * write out additional data member values if they wish.
  135.  *
  136.  * The value kNeoAllTag indicates that all data member values for this class
  137.  * should be writen out to the stream.
  138.  */
  139. #pragma segment NeoWrite
  140. void CNDCamera::writeObject(CNeoStream *aStream, const NeoTag aTag)
  141. {
  142.     short    index;
  143.  
  144.     NeoInherited::writeObject(aStream, aTag);
  145.  
  146.     aStream->writeShort(fShutterCount, pShutterCount);
  147.     if (fShutterCount) {
  148.         aStream->openList(pCameraShutters);
  149.         for (index = 0; index < fShutterCount; index++)
  150.             aStream->writeShort(fShutters[index]);
  151.         aStream->closeList();
  152.     }
  153.  
  154.     aStream->writeNativeString(fName, sizeof(fName), pName);
  155. }
  156.  
  157.                         /** Search Methods **/
  158. /*
  159.  * Once an object is made permanent it can continue be treated like any other object,
  160.  * including being disposed of. While disposing of an object involves elliminating a
  161.  * reference to it (and in the process, deleteing it from memory), disposing does not
  162.  * affect object's permanent state in the file.
  163.  *
  164.  * Locating a permanent object in a file is very easy. In order to identify the
  165.  * object (or objects) of interest its class and identity must be given. FindByID()
  166.  * is also capable of using the specified class as a base class so that it and all
  167.  * subclasses of the base class is searched to locate the object. If the given
  168.  * identity is ambiguous, then the search may find more than one object. Passing
  169.  * FindByID() an array will allow this function to return a list of all objects
  170.  * found.
  171.  */
  172. #pragma segment NeoSearch
  173. CNDCamera *CNDCamera::FindByName(CNeoDatabase *aDataBase, const CNeoString &aName)
  174. {
  175.     CNeoNameSelect    key(aName);
  176.     CNDCamera *        camera;
  177.  
  178.     camera = (CNDCamera *)aDataBase->findObject(kNDCameraID, &key, FALSE);
  179.  
  180.     return camera;
  181. }
  182.  
  183.                         /** Access Methods **/
  184. short CNDCamera::addShutterSpeed(const short aSpeed)
  185. {
  186.     short    index;
  187.     short    j;
  188.  
  189.     NeoAssert(fShutterCount < kShutterMax);
  190.  
  191.     if (aSpeed > fShutters[fShutterCount])
  192.         fShutters[++fShutterCount] = aSpeed;
  193.     else {
  194.         for (index = 0; index < fShutterCount; index++) {
  195.             if (fShutters[index] == aSpeed)
  196.                 break;        // This speed already exists in the list
  197.             if (fShutters[index] > aSpeed) {
  198.                 for (j = fShutterCount; j >= index; j--)
  199.                     fShutters[j] = fShutters[j +1];
  200.                 fShutters[index] = aSpeed;
  201.                 fShutterCount++;
  202.             }
  203.         }
  204.     }
  205.  
  206.     setDirty();
  207.  
  208.     return fShutterCount;
  209. }
  210.  
  211. void CNDCamera::ClearCameraTable(void)
  212. {
  213.     FCameraCnt = 0;
  214. }
  215.  
  216. void CNDCamera::deleteShutterSpeed(const short aIndex)
  217. {
  218.     short    index;
  219.  
  220.     NeoAssert(aIndex < fShutterCount);
  221.  
  222.     if (aIndex < fShutterCount -1)
  223.         for (index = aIndex; index < fShutterCount; index++)
  224.             fShutters[index] = fShutters[index +1];
  225.  
  226.     fShutterCount--;
  227.     setDirty();
  228. }
  229.  
  230. NeoID CNDCamera::GetCameraID(const short aIndex)
  231. {
  232.     return FCamMenu2IDTable[aIndex -2];
  233. }
  234.  
  235. void CNDCamera::getName(CNeoString &aName)
  236. {
  237.     aName = fName;
  238.     if (aName[0] > 32)
  239.         aName[0] = 32;
  240. }
  241.  
  242. short CNDCamera::getShutterCount(void)
  243. {
  244.     return fShutterCount;
  245. }
  246.  
  247. short CNDCamera::getShutter(const short aIndex)
  248. {
  249.     NeoAssert(aIndex < fShutterCount);
  250.  
  251.     return fShutters[aIndex];
  252. }
  253.  
  254. void CNDCamera::setName(const CNeoString &aName)
  255. {
  256.     fName = aName;
  257.     if (fName[0] > 32)
  258.         fName[0] = 32;
  259.  
  260.     setDirty();
  261. }
  262.  
  263.                         /** Search Methods **/
  264. /*
  265.  * This function is applied to every camera in the file. It adds the camera to
  266.  * the cameras popup menu
  267.  */
  268. void *CNDCamera::MakeCameraMenu(CNeoNode *aNode, const short aIndex, const NeoLockType aLock, void *aParam)
  269. {
  270.     MenuHandle        menu    = (MenuHandle)aParam;
  271.     CNDCamera *        camera;
  272.     CNeoString        name;
  273.  
  274.     if (aIndex < 0)
  275.         camera = (CNDCamera *)aNode;
  276.     else
  277.         NeoFailNil(camera = (CNDCamera *)aNode->getObject(aIndex));
  278.  
  279.     if (FCameraCnt < kMaxCameras) {
  280.         FCamMenu2IDTable[FCameraCnt] = camera->fID;
  281.         FCameraCnt++;
  282.         camera->getName(name);
  283.         AppendMenu(menu, name);
  284.     }
  285.  
  286.     return nil;
  287. }
  288.  
  289.