home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / object oriented files / CObjectDoc.cp < prev    next >
Encoding:
Text File  |  1993-11-10  |  6.4 KB  |  256 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CObjectDoc.c
  3.  *
  4.  *    Document methods for a typical application.
  5.  *
  6.  *  Copyright © 1990 Symantec Corporation.  All rights reserved.
  7.  *
  8.  ****/
  9.  
  10. #include <Global.h>
  11. #include <Commands.h>
  12. #include <CApplication.h>
  13. #include <CBartender.h>
  14. #include <CDataFile.h>
  15. #include <CDecorator.h>
  16. #include <CDesktop.h>
  17. #include <CError.h>
  18. #include <CPanorama.h>
  19. #include <CScrollPane.h>
  20. #include <TBUtilities.h>
  21. #include <CWindow.h>
  22. #include <Packages.h>
  23. #include <CTextEnvirons.h>
  24. #include "CObjectDoc.h"
  25. #include "CARTable.h"
  26. #include "CARArray.h"
  27.  
  28. #define    WINDObject        500        /* Resource ID for WIND template */
  29.  
  30. extern    CApplication *gApplication;    /* The application */
  31. extern    CBartender    *gBartender;    /* The menu handling object */
  32. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  33. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  34. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  35. extern    OSType        gSignature;        /* The application's signature */
  36. extern    CError        *gError;        /* The global error handler */
  37.  
  38. void CObjectDoc::IObjectDoc(CApplication *aSupervisor, Boolean printable)
  39.  
  40. {
  41.     CDocument::IDocument(aSupervisor, printable);
  42. }
  43.  
  44. void CObjectDoc::Dispose()
  45.  
  46. {
  47.     if (itsData) DisposHandle( itsData);
  48.     inherited::Dispose();
  49. }
  50.  
  51. void CObjectDoc::DoCommand(long theCommand)
  52.  
  53. {
  54.     switch (theCommand) {
  55.         default:    inherited::DoCommand(theCommand);
  56.                     break;
  57.     }
  58. }
  59.  
  60. void CObjectDoc::UpdateMenus()
  61.  {
  62.   inherited::UpdateMenus();
  63.   gBartender->DisableCmd(cmdSave);
  64.   gBartender->DisableCmd(cmdSaveAs);
  65.  }
  66.  
  67. void CObjectDoc::OpenFile(SFReply *macSFReply)
  68. {
  69.     CDataFile    *theFile;
  70.     Str63        theName;
  71.     OSErr        theError;
  72.     
  73.     TRY
  74.     {
  75.         itsData = NULL;
  76.         itsFile = NULL;
  77.         theFile = new(CDataFile);
  78.         theFile->IDataFile();
  79.         theFile->SFSpecify(macSFReply);
  80.     
  81.         theFile->Open(fsRdPerm);
  82.         itsData = theFile->ReadAll();     
  83.         BuildWindow(itsData);
  84.         theFile->GetName(theName);
  85.         theFile->Close();
  86.         theFile->Dispose();
  87.  
  88.         itsWindow->SetTitle(theName);
  89.         itsWindow->Select();            
  90.     }
  91.     
  92.     CATCH
  93.     {
  94.         /*
  95.          * This exception handler will be executed if an exception occurs
  96.          * anywhere within the scope of the TRY block above.
  97.          * You should perform any cleanup of things that won't be needed
  98.          * since the document could not be opened. By convention,
  99.          * the creator of an object is responsible for sending it
  100.          * the Dispose message. This means that we should only dispose
  101.          * of things that would not be taken care of in Dispose.
  102.          * In this case, we just make sure that the Handle theData
  103.          * has been disposed of. The exception will propagate up to
  104.          * CApplications's exception handler, which handles displaying
  105.          * an error alert.
  106.          */
  107.          
  108.          if (itsData) DisposHandle( itsData);
  109.          
  110.     }
  111.     ENDTRY;
  112. }
  113.  
  114.  
  115.  
  116. /***
  117.  * BuildWindow
  118.  *
  119.  *    This is the auxiliary window-building method that the
  120.  *    NewFile() and OpenFile() methods use to create a window.
  121.  *
  122.  *    In this implementation, the argument is the data to display.
  123.  *
  124.  ***/
  125.  
  126. void CObjectDoc::BuildWindow (Handle theData)
  127.  
  128. {
  129.     CScrollPane        *theScrollPane;
  130.     CARTable        *theMainPane;
  131.     CARArray        *theArray;
  132.     
  133.         /**
  134.          **    First create the window and initialize
  135.          **    it. The first argument is the resource ID
  136.          **    of the window. The second argument specifies
  137.          **    whether the window is a floating window.
  138.          **    The third argument is the window's enclosure; it
  139.          **    should always be gDesktop. The last argument is
  140.          **    the window's supervisor in the Chain of Command;
  141.          **    it should always be the Document object.
  142.          **
  143.          **/
  144.  
  145.     itsWindow = new(CWindow);
  146.     itsWindow->IWindow(WINDObject, FALSE, gDesktop, this);
  147.     
  148.         /**
  149.          **    After you create the window, you can use the
  150.          **    SetSizeRect() message to set the window’s maximum
  151.          **    and minimum size. Be sure to set the max & min
  152.          **    BEFORE you send a PlaceNewWindow() message to the
  153.          **    decorator.
  154.          **
  155.          ** The default minimum is 100 by 100 pixels. The
  156.          **    default maximum is the bounds of GrayRgn() (The
  157.          **    entire display area on all screens.)
  158.          **
  159.          ** We'll use the defaults.
  160.          **
  161.          **/
  162.     
  163.         /**
  164.          ** Our window will contain a ScrollPane,
  165.          ** which in turn will contain a Panorama.
  166.          ** Now, let's create the ScrollPane.
  167.          **/
  168.  
  169.     theScrollPane = new(CScrollPane);
  170.     
  171.         /**
  172.          **    You can initialize a scroll pane two ways:
  173.          **        1. You can specify all the values
  174.          **           right in your code, like this.
  175.          **        2. You can create a ScPn resource and
  176.          **           initialize the pane from the information
  177.          **           in the resource.
  178.          **
  179.          **/
  180.  
  181.     theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
  182.                                 sizELASTIC, sizELASTIC,
  183.                                 TRUE, TRUE, TRUE);
  184.  
  185.         /**
  186.          **    The FitToEnclFrame() method makes the
  187.          **    scroll pane be as large as its enclosure.
  188.          **    In this case, the enclosure is the window,
  189.          **    so the scroll pane will take up the entire
  190.          **    window.
  191.          **
  192.          **/
  193.  
  194.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  195.  
  196.  
  197.         /**
  198.          **    itsMainPane is the document's focus
  199.          **    of attention. Some of the standard
  200.          **    classes (particularly CPrinter) rely
  201.          **    on itsMainPane pointing to the main
  202.          **    pane of your window.
  203.          **
  204.          **    itsGopher specifies which object
  205.          ** should become the gopher when the document
  206.          ** becomes active. By default
  207.          **    the document becomes the gopher. It’s
  208.          **    likely that your main pane handles commands
  209.          **    so you’ll almost always want to set itsGopher
  210.          **    to point to the same object as itsMainPane.
  211.          **
  212.          **    Note that the main pane is the
  213.          **    panorama in the scroll pane and not
  214.          **    the scroll pane itself.
  215.          **
  216.          **/
  217.  
  218.         {
  219.         TextInfoRec    textInfo;
  220.         textInfo.fontNumber = monaco;
  221.         textInfo.theSize = 12;
  222.         textInfo.theStyle = 0;
  223.         textInfo.theMode = srcOr;          
  224.  
  225.         theMainPane = new(CARTable);
  226.         theMainPane->IARTable(theScrollPane, this, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
  227.         theMainPane->FitToEnclosure(TRUE, TRUE);
  228.         ((CTextEnvirons *)(theMainPane->itsEnvironment))->SetTextInfo( &textInfo);        
  229.         itsMainPane = theMainPane;
  230.         itsGopher = theMainPane;
  231.         }
  232.     
  233.     theArray = new(CARArray);
  234.     theArray->IARArray(theData);
  235.     theMainPane->SetArray(theArray, true);
  236.  
  237.     
  238.         /**
  239.          **    Send the scroll pane an InstallPanorama()
  240.          ** message to associate the panorama with 
  241.          ** the scroll pane.
  242.          **
  243.          **/
  244.  
  245.     theScrollPane->InstallPanorama(theMainPane);
  246.     
  247.         /**
  248.          **    The Decorator is a global object that takes care
  249.          **    of placing and sizing windows on the screen.
  250.          **    You don't have to use it.
  251.          **
  252.          **/
  253.  
  254.     gDecorator->StaggerWindow(itsWindow);
  255. }
  256.