home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / NeoPersist 3.0.8 folder / Laughs / Source / CLaughsDoc.cp < prev    next >
Encoding:
Text File  |  1994-10-13  |  5.0 KB  |  201 lines  |  [TEXT/MMCC]

  1. /****
  2.  * CLaughsDoc.cp
  3.  *
  4.  *    Implementation of a document class full of laughs.
  5.  *
  6.  *  Copyright © 1994 NeoLogic Systems.  All rights reserved.
  7.  *
  8.  *    NOTE: This sample application uses the SIOUX serial i/o emulator for outputing
  9.  *    text using printf. There are a number of bugs in this mechanism which causes
  10.  *    rather unusual behavior.
  11.  *
  12.  *    While Laughs includes all the code necessary to open new and pre-existing files,
  13.  *    these SIOUX bugs do not allow Laughs' application menu to be accessible. The
  14.  *    application can not open pre-existing files as a result. The only possible work
  15.  *    around for this bug would be to comment out all printf statements and the call
  16.  *    to SendAEQuit in CLaughsDoc::newDatabase. The application will function correctly,
  17.  *    though no output will be written to the screen.
  18.  *
  19.  *    The window can't be moved on the screen. SIOUX ignores mouse clicks directed
  20.  *    to the window.
  21.  *
  22.  *    The Save menu item saves the text in the window NOT the NeoAccess database
  23.  *    that this application uses.
  24.  *
  25.  ****/
  26.  
  27. #include "NeoTypes.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include CNeoDatabaseNativeH
  31. #include "CLaughsApp.h"
  32. #include "CLaughsDoc.h"
  33. #include "CPerson.h"
  34.  
  35. /***
  36.  * CLaughsDoc
  37.  *
  38.  *    This is the document's initialization method.
  39.  *
  40.  ***/
  41. CLaughsDoc::CLaughsDoc(const Boolean aPrintable, const Boolean aNewDatabase, const Boolean aRemote)
  42.     : CNeoDocRoot(kLaughsSig, kLaughsFileType, aPrintable, aNewDatabase)
  43. {
  44. }
  45.  
  46. /***
  47.  * buildWindow
  48.  *
  49.  *    This is the auxiliary window-building method that the
  50.  *    newDatabase and openFile methods use to create a window.
  51.  *
  52.  *    Laughs doesn't need to explicitly create a window,
  53.  *    printf will do that for us.
  54.  ***/
  55. void CLaughsDoc::buildWindow(void)
  56. {
  57.     // Nothing to do.
  58. }
  59.  
  60. /***
  61.  * newDatabase
  62.  *
  63.  *    When the user chooses New from the File menu, the createDocument
  64.  *    method in your Application class will send a newly created document
  65.  *    this message.
  66.  *
  67.  ***/
  68. void CLaughsDoc::newDatabase(void)
  69. {
  70.     CNeoDatabase *    database        = getDatabase();
  71.     NeoFileSpec        spec;
  72.  
  73.     fNewDatabase = TRUE;
  74.     fOpenMode = fsRdWrPerm;
  75.     inherited::newDatabase();
  76.  
  77.     // Set the default name which appears in the get file dialog.
  78.     spec.vRefNum = 0;
  79.     spec.parID = 0;
  80.     NeoStringCopy("\pLaughter", spec.name);
  81.     database->setFileSpec(spec);
  82.  
  83.     // Stuff the database with objects.
  84.     createObjects();
  85.  
  86.     // Commit the changes that we made to the database.
  87.     DoSave();
  88.  
  89.     // That's all folks!
  90.     printf("\nDone!\n\n");
  91.  
  92.     // We need to do this due to a bug in SIOUX.
  93. //    ((LApplication *)GetSuperCommander())->SendAEQuit();
  94. }
  95.  
  96. /***
  97.  * openFile
  98.  *
  99.  *    When the user chooses Open… from the File menu, the OpenDocument()
  100.  *    method in your application will let the user choose a file
  101.  *    and then send a newly created document this message. The information
  102.  *    about the file is in the FSSpec record.
  103.  *
  104.  *    In this method, you need to open the file and display its contents
  105.  *    in a window. This method uses the auxiliary window-building method.
  106.  *
  107.  ***/
  108.  
  109. void CLaughsDoc::openFile(FSSpec *aSpec)
  110. {
  111.     fOpenMode = fsRdPerm;
  112.     inherited::openFile(aSpec);
  113.  
  114.     // Find each of the objects in the database in turn.
  115.     printOut();
  116.  
  117.     // That's all folks!
  118.     printf("\nDone!\n\n");
  119. }
  120.  
  121. /***
  122.  * createObjects
  123.  *
  124.  *    Add three objects to the database, two jokers and a clown, then commit
  125.  *    the changes to disk.
  126.  *
  127.  ***/
  128. void CLaughsDoc::createObjects(void)
  129. {
  130.     CJoker *        joker;
  131.     CClown *        clown;
  132.     CNeoDatabase *    database    = getDatabase();
  133.     CNeoString        string;
  134.     char            name[64];
  135.  
  136.     // Tell them what we're about to do.
  137.     database->getName(string);
  138.     BlockMove(&string[1], name, string[0]);
  139.     name[string[0]] = 0;
  140.     printf("Storing 2 Jokers & a Clown in \"%s\".\n", name);
  141.  
  142.     // Create a joker object.
  143.     joker = new CJoker("Jack", 1);
  144.     // Add it to the database.
  145.     database->addObject(joker);
  146.     // Don't need this guy any more. Remove our reference to it.
  147.     joker->unrefer();
  148.     joker = nil;
  149.  
  150.     // Create a clown.
  151.     clown = new CClown("Fred", 2);
  152.     // Add it to the database.
  153.     database->addObject(clown);
  154.     // Remember to remove our reference when we're done.
  155.     clown->unrefer();
  156.     clown = nil;
  157.  
  158.     // Create another joker.
  159.     joker = new CJoker("Harry", 3);
  160.     joker->setJoke("The world’s shortest poem: Flees. Adam had'em");
  161.     // Add it to the database.
  162.     database->addObject(joker);
  163.     // Remove our reference.
  164.     joker->unrefer();
  165.     joker = nil;
  166. }
  167.  
  168. /***
  169.  * printOut
  170.  *
  171.  *    Find in turn each of the three objects in the database, two jokers and
  172.  *    a clown, then commit the changes to disk.
  173.  *
  174.  ***/
  175. void CLaughsDoc::printOut(void)
  176. {
  177.     NeoID            loop;
  178.     CPerson *        person;
  179.     CNeoDatabase *    database    = getDatabase();
  180.     CNeoString        string;
  181.     char            name[64];
  182.  
  183.     // Tell them what we're about to do.
  184.     database->getName(string);
  185.     BlockMove(&string[1], name, string[0]);
  186.     name[string[0]] = 0;
  187.     printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n",
  188.         database->getObjectCount(kJokerID, FALSE),
  189.         database->getObjectCount(kClownID, FALSE), name);
  190.  
  191.     for (loop = 1; loop <= 3; loop++) {
  192.         person = (CPerson *)CNeoPersist::FindByID(database, kPersonID, loop, TRUE);
  193.         if (person) {
  194.             person->printName();
  195.             person->skill();
  196.             printf("\n");
  197.         }
  198.     }
  199. }            
  200.  
  201.