home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Think Class Libraries / WASTE TCL 2.0b2 / WASTEEdit / CEditDoc.cp < prev    next >
Encoding:
Text File  |  1996-06-16  |  8.6 KB  |  364 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditDoc.c
  3.     
  4.     Document methods for a tiny editor.
  5.         
  6.     Copyright © 1989 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 "CEditDoc.h"
  22. #include "CEditPane.h"
  23. #include "CWindow.h"
  24. #include <Packages.h>
  25. #include "CResFile.h"
  26.  
  27. #define    WINDculture        500        /* Resource ID for WIND template */
  28.  
  29. extern    CApplication *gApplication;    /* The application */
  30. extern    CBartender    *gBartender;    /* The menu handling object */
  31. extern    CDecorator    *gDecorator;    /* Window dressing object    */
  32. extern    CDesktop    *gDesktop;        /* The enclosure for all windows */
  33. extern    CBureaucrat    *gGopher;        /* The current boss in the chain of command */
  34. extern    OSType        gSignature;        /* The application's signature */
  35. extern    CError        *gError;        /* The error handling object */
  36.  
  37. #define kTypeSoup    'SOUP'
  38.  
  39. /***
  40.  * IEditDoc
  41.  *
  42.  *    This is your document's initialization method.
  43.  *    If your document has its own instance variables, initialize
  44.  *    them here.
  45.  *    The least you need to do is invoke the default method.
  46.  *
  47.  ***/
  48.  
  49. void CEditDoc::IEditDoc(CApplication *aSupervisor, Boolean printable)
  50.  
  51. {
  52.     CDocument::IDocument(aSupervisor, printable);
  53. }
  54.  
  55.  
  56. /***
  57.  * NewFile
  58.  *
  59.  *    When the user chooses New from the File menu, the CreateDocument()
  60.  *    method in your Application class will send a newly created document
  61.  *    this message. This method needs to create a new window, ready to
  62.  *    work on a new document.
  63.  *
  64.  *    Since this method and the OpenFile() method share the code for creating
  65.  *    the window, you should use an auxiliary window-building method.
  66.  *
  67.  ***/
  68. void CEditDoc::NewFile(void)
  69.  
  70. {
  71.         /**
  72.          **    BuildWindow() is the method that
  73.          **    does the work of creating a window.
  74.          **    It's parameter should be the data that
  75.          **    you want to display in the window.
  76.          **    Since this is a new window, there's nothing
  77.          **    to display.
  78.          **
  79.          **/
  80.  
  81.     BuildWindow();
  82.  
  83.         /**
  84.          **    Send the window a Select() message to make
  85.          **    it the active window.
  86.          **/
  87.     
  88.     itsWindow->Select();
  89. }
  90.  
  91.  
  92. /***
  93.  * OpenFile
  94.  *
  95.  *    When the user chooses Open… from the File menu, the OpenDocument()
  96.  *    method in your Application class will let the user choose a file
  97.  *    and then send a newly created document this message. The information
  98.  *    about the file is in the SFReply record.
  99.  *
  100.  *    In this method, you need to open the file and display its contents
  101.  *    in a window. This method uses the auxiliary window-building method.
  102.  *
  103.  ***/
  104.  
  105. void CEditDoc::OpenFile(SFReply *macSFReply)
  106.  
  107. {
  108.     CDataFile    *theFile;
  109.     Str63        theName;
  110.     OSErr        theError;
  111.  
  112.         /**
  113.          ** Create a file and send it a SFSpecify()
  114.          **    message to set up the name, volume, and
  115.          **    directory.
  116.          **
  117.          **/
  118.  
  119.     theFile = new(CDataFile);
  120.  
  121.         /**
  122.          **    Be sure to set the instance variable
  123.          **    so other methods can use the file if they
  124.          **    need to. This is especially important if
  125.          **    you leave the file open in this method.
  126.          **    If you close the file after reading it, you
  127.          **    should be sure to set itsFile to NULL.
  128.          **
  129.          **/
  130.  
  131.     itsFile = theFile;
  132.  
  133.     theFile->IDataFile();
  134.     theFile->SFSpecify(macSFReply);
  135.     
  136.  
  137.         /**
  138.          **    Send the file an Open() message to
  139.          **    open it. You can use the ReadSome() or
  140.          **    ReadAll() methods to get the contents of the file.
  141.          **
  142.          **/
  143.  
  144.     theFile->Open(fsRdWrPerm);
  145.     
  146.     BuildWindow();
  147.  
  148.         /**
  149.          **    In this implementation, we leave the file
  150.          **    open. You might want to close it after
  151.          **    you've read in all the data.
  152.          **
  153.          **/
  154.  
  155.     itsFile->GetName(theName);
  156.     itsWindow->SetTitle(theName);
  157.     itsWindow->Select();            /* Don't forget to make the window active */
  158. }
  159.  
  160.  
  161.  
  162. /***
  163.  * BuildWindow
  164.  *
  165.  *    This is the auxiliary window-building method that the
  166.  *    NewFile() and OpenFile() methods use to create a window.
  167.  *
  168.  *    In this implementation, the argument is the data to display.
  169.  *
  170.  * Modified by DWC to use WASTE TCL routine for reading data from file
  171.  *
  172.  ***/
  173.  
  174. void CEditDoc::BuildWindow (void)
  175. {
  176.     CScrollPane        *theScrollPane;
  177.     CEditPane        *theMainPane;
  178.     Rect            margin;
  179.  
  180.         /**
  181.          **    First create the window and initialize
  182.          **    it. The first argument is the resource ID
  183.          **    of the window. The second argument specifies
  184.          **    whether the window is a floating window.
  185.          **    The third argument is the window's enclosure; it
  186.          **    should always be gDesktop. The last argument is
  187.          **    the window's supervisor in the Chain of Command;
  188.          **    it should always be the Document object.
  189.          **
  190.          **/
  191.  
  192.     itsWindow = new(CWindow);
  193.     itsWindow->IWindow(WINDculture, FALSE, gDesktop, this);
  194.     
  195.         /**
  196.          **    After you create the window, you can use the
  197.          **    SetSizeRect() message to set the window’s maximum
  198.          **    and minimum size. Be sure to set the max & min
  199.          **    BEFORE you send a PlaceNewWindow() message to the
  200.          **    decorator.
  201.          **
  202.          ** The default minimum is 100 by 100 pixels. The
  203.          **    default maximum is the bounds of GrayRgn() (The
  204.          **    entire display area on all screens.)
  205.          **
  206.          **/
  207.  
  208.     theScrollPane = new(CScrollPane);
  209.     
  210.         /**
  211.          **    You can initialize a scroll pane two ways:
  212.          **        1. You can specify all the values
  213.          **           right in your code, like this.
  214.          **        2. You can create a ScPn resource and
  215.          **           initialize the pane from the information
  216.          **           in the resource.
  217.          **
  218.          **/
  219.  
  220.     theScrollPane->IScrollPane(itsWindow, this, 10, 10, 0, 0,
  221.                                 sizELASTIC, sizELASTIC,
  222.                                 TRUE, TRUE, TRUE);
  223.  
  224.         /**
  225.          **    The FitToEnclFrame() method makes the
  226.          **    scroll pane be as large as its enclosure.
  227.          **    In this case, the enclosure is the window,
  228.          **    so the scroll pane will take up the entire
  229.          **    window.
  230.          **
  231.          **/
  232.  
  233.     theScrollPane->FitToEnclFrame(TRUE, TRUE);
  234.  
  235.  
  236.         /**
  237.          **    itsMainPane is the document's focus
  238.          **    of attention. Some of the standard
  239.          **    classes (particularly CPrinter) rely
  240.          **    on itsMainPane pointing to the main
  241.          **    pane of your window.
  242.          **
  243.          **    itsGopher specifies which object
  244.          **    should become the gopher. By default
  245.          **    the document becomes the gopher. It’s
  246.          **    likely that your main pane handles commands
  247.          **    so you’ll almost want to set itsGopher
  248.          **    to point to the same object as itsMainPane.
  249.          **
  250.          **    Note that the main pane is the
  251.          **    panorama in the scroll pane and not
  252.          **    the scroll pane itself.
  253.          **
  254.          **/
  255.  
  256.     theMainPane = new (CEditPane);
  257.  
  258.     itsMainPane = theMainPane;
  259.     itsGopher = theMainPane;
  260.  
  261.         /**
  262.          **    The IEditPane method automatically
  263.          **    fits the pane to the enclosure and
  264.          **    gives us a little margin.
  265.          **
  266.          **/
  267.  
  268.     theMainPane->IEditPane(theScrollPane, this);
  269.  
  270.         /**
  271.          **    Send the scroll pane an InstallPanorama()
  272.          **    to associate our pane with the scroll pane.
  273.          **
  274.          **/
  275.  
  276.     theScrollPane->InstallPanorama(theMainPane);
  277.  
  278.     // modified by DWC
  279.     if (itsFile)
  280.     {
  281.         theMainPane->ReadFromFile((CDataFile *)itsFile);
  282.     }
  283.     
  284.         /**
  285.          **    The Decorator is a global object that takes care
  286.          **    of placing and sizing windows on the screen.
  287.          **    You don't have to use it.
  288.          **
  289.          **/
  290.  
  291.     gDecorator->PlaceNewWindow(itsWindow);
  292. }
  293.  
  294.  
  295.  
  296. /***
  297.  * DoSave
  298.  *
  299.  *    This method handles what happens when the user chooses Save from the
  300.  *    File menu. This method should return TRUE if the file save was successful.
  301.  *    If there is no file associated with the document, you should send a
  302.  *    DoSaveFileAs() message.
  303.  *
  304.  ***/
  305.  
  306. Boolean CEditDoc::DoSave(void)
  307.  
  308. {
  309.     Handle        theData;
  310.  
  311.     if (itsFile == NULL)
  312.         return(DoSaveFileAs());
  313.     else {
  314.         ((CEditPane *)itsMainPane)->WriteToFile((CDataFile *)itsFile);
  315.  
  316.         SetChanged(false); // fixed 1/10/96
  317.         // dirty = FALSE;                    /* Document is no longer dirty        */
  318.         gBartender->DisableCmd(cmdSave);
  319.         return(TRUE);                    /* Save was successful                */
  320.     }
  321. }
  322.  
  323.  
  324. /***
  325.  * DoSaveAs
  326.  *
  327.  *    This method handles what happens when the user chooses Save As… from
  328.  *    File menu. The default DoCommand() method for documents sends a DoSaveFileAs()
  329.  *    message which displays a standard put file dialog and sends this message.
  330.  *    The SFReply record contains all the information about the file you're about
  331.  *    to create.
  332.  *
  333.  ***/
  334.  
  335. Boolean CEditDoc::DoSaveAs(SFReply *macSFReply)
  336.  
  337. {
  338.         /**
  339.          **    If there's a file associated with this document
  340.          **    already, close it. The Dispose() method for files
  341.          **    sends a Close() message to the file before releasing
  342.          **    its memory.
  343.          **
  344.          **/
  345.          
  346.     TCLForgetObject(itsFile);
  347.  
  348.  
  349.         /**
  350.          **    Create a new file, and then save it normally.
  351.          **
  352.          **/
  353.  
  354.     itsFile = new(CDataFile);
  355.     ((CDataFile *)itsFile)->IDataFile();
  356.     itsFile->SFSpecify(macSFReply);
  357.     itsFile->CreateNew(gSignature, 'TEXT');
  358.     itsFile->Open(fsRdWrPerm);
  359.     
  360.     itsWindow->SetTitle(macSFReply->fName);
  361.  
  362.     return( DoSave() );
  363. }
  364.