home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroPP3.0 folder / PowerPlant / Laughs / Source / CLaughsDoc.cp < prev    next >
Encoding:
Text File  |  1994-10-05  |  5.0 KB  |  203 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 CNeoIndexIteratorH
  32. #include CNeoSelectH
  33. #include "CLaughsApp.h"
  34. #include "CLaughsDoc.h"
  35. #include "CPerson.h"
  36.  
  37. /***
  38.  * CLaughsDoc
  39.  *
  40.  *    This is the document's initialization method.
  41.  *
  42.  ***/
  43. CLaughsDoc::CLaughsDoc(const Boolean aPrintable, const Boolean newDatabase, const Boolean aRemote)
  44.     : CNeoDocRoot(kLaughsSig, kLaughsFileType, aPrintable, newDatabase)
  45. {
  46. }
  47.  
  48. /***
  49.  * buildWindow
  50.  *
  51.  *    This is the auxiliary window-building method that the
  52.  *    newDatabase and openFile methods use to create a window.
  53.  *
  54.  *    Laughs doesn't need to explicitly create a window,
  55.  *    printf will do that for us.
  56.  ***/
  57. void CLaughsDoc::buildWindow(void)
  58. {
  59.     // Nothing to do.
  60. }
  61.  
  62. /***
  63.  * newDatabase
  64.  *
  65.  *    When the user chooses New from the File menu, the createDocument
  66.  *    method in your Application class will send a newly created document
  67.  *    this message.
  68.  *
  69.  ***/
  70. void CLaughsDoc::newDatabase(void)
  71. {
  72.     CNeoDatabase *    database        = getDatabase();
  73.     NeoFileSpec        spec;
  74.  
  75.     fNewDatabase = TRUE;
  76.     fOpenMode = fsRdWrPerm;
  77.     inherited::newDatabase();
  78.  
  79.     // Set the default name which appears in the get file dialog.
  80.     spec.vRefNum = 0;
  81.     spec.parID = 0;
  82.     NeoStringCopy("\pLaughter", spec.name);
  83.     database->setFileSpec(spec);
  84.  
  85.     // Stuff the database with objects.
  86.     createObjects();
  87.  
  88.     // Commit the changes that we made to the database.
  89.     DoSave();
  90.  
  91.     // That's all folks!
  92.     printf("\nDone!\n\n");
  93.  
  94.     // We need to do this due to a bug in SIOUX.
  95. //    ((LApplication *)GetSuperCommander())->SendAEQuit();
  96. }
  97.  
  98. /***
  99.  * openFile
  100.  *
  101.  *    When the user chooses Open… from the File menu, the OpenDocument()
  102.  *    method in your application will let the user choose a file
  103.  *    and then send a newly created document this message. The information
  104.  *    about the file is in the FSSpec record.
  105.  *
  106.  *    In this method, you need to open the file and display its contents
  107.  *    in a window. This method uses the auxiliary window-building method.
  108.  *
  109.  ***/
  110.  
  111. void CLaughsDoc::openFile(FSSpec *aSpec)
  112. {
  113.     fOpenMode = fsRdPerm;
  114.     inherited::openFile(aSpec);
  115.  
  116.     // Find each of the objects in the database in turn.
  117.     printOut();
  118.  
  119.     // That's all folks!
  120.     printf("\nDone!\n\n");
  121. }
  122.  
  123. /***
  124.  * createObjects
  125.  *
  126.  *    Add three objects to the database, two jokers and a clown, then commit
  127.  *    the changes to disk.
  128.  *
  129.  ***/
  130. void CLaughsDoc::createObjects(void)
  131. {
  132.     CJoker *        joker;
  133.     CClown *        clown;
  134.     CNeoDatabase *    database        = getDatabase();
  135.     CNeoString        string;
  136.     char            name[64];
  137.  
  138.     // Tell them what we're about to do.
  139.     database->getName(string);
  140.     BlockMove(&string[1], name, string[0]);
  141.     name[string[0]] = 0;
  142.     printf("Storing 2 Jokers & a Clown in \"%s\".\n", name);
  143.  
  144.     // Create a joker object.
  145.     joker = new CJoker("Jack", 1);
  146.     // Add it to the database.
  147.     database->addObject(joker);
  148.     // Don't need this guy any more. Remove our reference to it.
  149.     joker->unrefer();
  150.     joker = nil;
  151.  
  152.     // Create a clown.
  153.     clown = new CClown("Fred", 2);
  154.     // Add it to the database.
  155.     database->addObject(clown);
  156.     // Remember to remove our reference when we're done.
  157.     clown->unrefer();
  158.     clown = nil;
  159.  
  160.     // Create another joker.
  161.     joker = new CJoker("Harry", 3);
  162.     joker->setJoke("The world’s shortest poem: Flees. Adam had'em");
  163.     // Add it to the database.
  164.     database->addObject(joker);
  165.     // Remove our reference.
  166.     joker->unrefer();
  167.     joker = nil;
  168. }
  169.  
  170. /***
  171.  * printOut
  172.  *
  173.  *    Find in turn each of the three objects in the database, two jokers and
  174.  *    a clown, then commit the changes to disk.
  175.  *
  176.  ***/
  177. void CLaughsDoc::printOut(void)
  178. {
  179.     NeoID            loop;
  180.     CPerson *        person;
  181.     CNeoDatabase *    database        = getDatabase();
  182.     CNeoString        string;
  183.     char            name[64];
  184.  
  185.     // Tell them what we're about to do.
  186.     database->getName(string);
  187.     BlockMove(&string[1], name, string[0]);
  188.     name[string[0]] = 0;
  189.     printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n",
  190.         database->getObjectCount(kJokerID, FALSE),
  191.         database->getObjectCount(kClownID, FALSE), name);
  192.  
  193.     for (loop = 1; loop <= 3; loop++) {
  194.         person = (CPerson *)CNeoPersist::FindByID(database, kPersonID, loop, TRUE);
  195.         if (person) {
  196.             person->printName();
  197.             person->skill();
  198.             printf("\n");
  199.         }
  200.     }
  201. }            
  202.  
  203.