home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / MacUserProj / MacUser Projects / June / 2GenApp Src / DocUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  6.2 KB  |  244 lines  |  [TEXT/KAHL]

  1. /* *****************************************************************************
  2.     FILE:             DocUtil.c
  3.     
  4.     DESCRIPTION:     Document Utilities 
  5.  
  6.     AUTHOR:            Kurt W.G. Matthies
  7.         
  8.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  9.     
  10.     Revision History:
  11.     ==========================================================
  12.     4.24.90    -    June 1990 MacUser (multi-document support)
  13.     3.30.90    -    May 1990 MacUser Release
  14.     ==========================================================
  15.  
  16.    ***************************************************************************** */
  17. #include "Version.h"
  18.  
  19. #ifdef V4
  20. #include <stdio.h>
  21. #endif
  22.  
  23. #ifdef V3
  24. #include <Proto.h>
  25. #endif
  26.  
  27. #ifdef V2
  28. #include <Proto.h>
  29. #include <Pascal.h>
  30. #endif
  31.  
  32. #include "AppGlobals.h"
  33. #include "StrRsrcDefs.h"
  34.  
  35. #include "FileUtilPr.h"
  36. #include "MenuUtilPr.h"
  37. #include "MiscUtilPr.h"
  38. #include "WindowUtilPr.h"
  39. #include "DocUtilPr.h"
  40.  
  41. /* ------------------------  Local Prototypes  --------------------------------- */
  42.  
  43. DocPtr            createNewDoc        ( DocParamsPtr );
  44. DocPtr            allocDoc            ( void );
  45.  
  46. /* -----------------------------------------------------------------------------
  47.     doNewDoc -        open a new window
  48.     3.30.90kwgm
  49. -------------------------------------------------------------------------------- */
  50. Boolean
  51. doNewDoc ()
  52. {
  53.     DocParams            docParams;
  54.     DocPtr                theDoc;
  55.     Boolean                result;
  56.     
  57.     result = false;
  58.     
  59.     /*     
  60.         The DocParams data structure is used to tell the creation function
  61.         how to create the document and what file if any to associate
  62.         with the window
  63.     */
  64.     docParams.attributes = 0;
  65.     docParams.fileParams.fileName [0] = 0;
  66.     docParams.fileParams.openFileRefNum = 0;
  67.     docParams.fileParams.volRefNum = 0;
  68.  
  69.     if (theDoc = createNewDoc (&docParams))
  70.         result = true;
  71.     
  72.     return (result);
  73.  
  74. } /* doNewDoc */
  75.  
  76. /* -----------------------------------------------------------------------------
  77.     doCloseDoc -    close the top document.
  78.     3.30.90kwgm
  79.     4.24.90kwgm - modified for multiple windows
  80. -------------------------------------------------------------------------------- */
  81. Boolean
  82. doCloseDoc (theDoc)
  83.     WindowPtr        theDoc;
  84. {
  85.     ControlHandle        control;
  86.     Boolean                result;
  87.     WindowPtr            topWin;
  88.  
  89.     result = false;
  90.  
  91.     if (!theDoc)
  92.         return (result);
  93.     
  94.     if (((WindowPeek) theDoc)->windowKind  < 1)    /* desk accessory */
  95.          CloseDeskAcc (((WindowPeek) theDoc)->windowKind);
  96.      else if ((result = closeDocFile (theDoc)) != kSaveChangeCancel)  /* close the file,  */
  97.      {                                                                 /* then dispose of  */
  98.                                                                       /* the window stuff */
  99.           while (control = ((WindowPeek)theDoc)->controlList)
  100.               DisposeControl (control);
  101.         
  102.         CloseWindow ((WindowPtr) theDoc);
  103.  
  104.         gNumOpenDocs--;        /* decrement the global open window counter */
  105.         
  106.         DisposPtr (theDoc);
  107.         result = true;
  108.     }
  109.     else
  110.         result = false;        /* the user canceled the close */
  111.  
  112.     return (result);
  113.     
  114. } /* doCloseDoc */
  115.  
  116. /*-------------------------------------------------------------------------
  117.     createNewDoc -    open a new document window
  118.     3.30.90kwgm
  119.     4.24.90kwgm    -    revised to support multiple documents
  120. --------------------------------------------------------------------------*/
  121. static DocPtr 
  122. createNewDoc (docParams)
  123.     DocParamsPtr        docParams;
  124. {
  125.     register DocPtr        theDoc;
  126.     WindowPtr            topWin;
  127.     Rect                newWinRect, scrollBarRect;
  128.     Str64                title;
  129.  
  130.     if (!(theDoc = allocDoc ()))        /* allocate the document structures */
  131.         return (0L);
  132.  
  133.     if (topWin = FrontWindow ())         /* get the new window rect */
  134.     {
  135.         if (((WindowPeek)topWin)->windowKind == userKind) 
  136.             calcNewWinRect (topWin, &newWinRect);
  137.         else
  138.             newWinRect = gWindowRect;    /* DA window on top, use default */
  139.     }
  140.     else /* no open windows, so use the default window rect */
  141.     {
  142.         gNextWindow = 1;
  143.         newWinRect = gWindowRect;
  144.     }
  145.     
  146.     /* create new document structure */
  147.     if (theDoc = NewWindow (theDoc, &newWinRect, "\p", false, 
  148.         documentProc + 8, -1L, true, 0L))
  149.     {
  150.         /* add scroll bars to the window */
  151.         hScrollBarRect (theDoc, &scrollBarRect);
  152.         NewControl (theDoc, &scrollBarRect, 0L, 
  153.             false, 0, 0, kControlMax, scrollBarProc, kHScrollTag);
  154.  
  155.         vScrollBarRect (theDoc, &scrollBarRect);
  156.         NewControl (theDoc, &scrollBarRect, 0L, 
  157.             false, 0, 0, kControlMax, scrollBarProc, kVScrollTag);
  158.     
  159.         if (!docParams->fileParams.fileName[0])
  160.         {
  161.             /* create a numbered Untitled file name */
  162.             GetIndString (title, kNameStrRsrc, kNameUntitledStr);
  163.             PtoCstr (title);
  164.             sprintf (title, "%s %d",title, gNextWindow++);
  165.             CtoPstr (title);
  166.         }
  167.         else
  168.             BlockMove (docParams->fileParams.fileName, title, 
  169.                 (Size)(docParams->fileParams.fileName + 1));
  170.             
  171.         /* initialize window data */
  172.         SetWTitle (theDoc, title);
  173.     
  174.         theDoc->maxScroll.h = theDoc->maxScroll.v = kControlMax;
  175.         
  176.         BlockMove (title, theDoc->fileName, (Size) (title [0] + 1));
  177.         theDoc->volRefNum = docParams->fileParams.volRefNum;
  178.         theDoc->openFileRefNum = docParams->fileParams.openFileRefNum;
  179.     
  180.         gNumOpenDocs++;        /* bump open doc counter */
  181.         
  182.         ShowWindow (theDoc);
  183.         SelectWindow (theDoc);    
  184.  
  185.         SetPort (theDoc);
  186.     }
  187.     
  188.     return (theDoc);
  189.     
  190. } /* createNewDoc */
  191.  
  192. /* -----------------------------------------------------------
  193.     allocDoc -        allocate the document's data structs
  194.     3.30.90kwgm
  195.     4.24.90kwgm -    modified for multiple window support
  196. --------------------------------------------------------------  */
  197. static DocPtr
  198. allocDoc ()
  199. {
  200.     DocPtr        newDoc;
  201.         
  202.     newDoc = 0L;
  203.     
  204.     if (gNumOpenDocs < kMaxOpenDocs)
  205.         newDoc = newClearPtr ((Size)sizeof (Doc));
  206.  
  207.     return (newDoc);
  208.         
  209. } /* allocDoc */
  210.  
  211. /* -----------------------------------------------------------------------------
  212.     closeAllDocs -    close all open documents. present user with choice 
  213.     4.24.90kwgm        to save/cancel
  214. -------------------------------------------------------------------------------- */
  215. Boolean
  216. closeAllDocs ()
  217. {
  218.     DocPtr                theDoc;
  219.     register short        count;
  220.     Boolean                result;
  221.     
  222.     result = true;
  223.     while (gNumOpenDocs > 0)        /* loop on all open windows */
  224.     {
  225.         if (theDoc = FrontWindow ())    
  226.         {
  227.             if (ISDIRTY(theDoc))
  228.                 SelectWindow (theDoc);    /* bring to front and hilight */
  229.             
  230.             if (doCloseDoc (theDoc) == kSaveChangeCancel)
  231.             {
  232.                 result = false;        /* user canceled */
  233.                 break;
  234.             }
  235.         }
  236.     }
  237.         
  238.     return (result);
  239.     
  240. } /* closeAllDocs */
  241.  
  242. /* ===============================  EOF  =======================================
  243.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  244. ================================================================================ */