home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Starter / Source / Document.m < prev    next >
Encoding:
Text File  |  1995-02-13  |  5.3 KB  |  249 lines

  1.  
  2. /*  Document.m written by Robert Vasvari, Oct 1994.
  3.     This class is a generic document that has its own nibfile.
  4.     The Document object comes with: name,path and window. Also,
  5.     it implements some of the most basic document handling
  6.     methods that most apps need.
  7.  */
  8. #import "Document.h"
  9. #import "MenuController.h"
  10. #import "util.h"
  11. #import "defs.h"
  12.  
  13. @implementation Document
  14.  
  15. /* ==== init section ==== */
  16.  
  17. - init
  18. {    [super init];
  19.     [NXApp  loadNibSection:"Document.nib" owner:self withNames:NO
  20.         fromZone:(NXZone *)[self zone]];
  21.     return self;
  22. }
  23.  
  24. - awakeFromNib
  25. {    
  26.     createTime=time(0);
  27.     lastModifyTime=createTime;
  28.     return self;
  29. }
  30.  
  31. /* ==== access to instance variables ==== */
  32.  
  33. - window
  34. {    return window;
  35. }
  36.  
  37. - (int)tag
  38. {    return tag;
  39. }
  40.  
  41. - setTag:(int)aTag
  42. {    tag=aTag;
  43.     return self;
  44. }
  45.  
  46. - (const char *)documentName
  47. {    return documentName;
  48. }
  49.  
  50. - (const char *)documentPath
  51. {    return documentPath;
  52. }
  53.  
  54. - setDocumentName:(const char *)aName
  55. {    copyStringBufferFromZone(&documentName,aName,[self zone]);
  56.     return self;
  57. }
  58.  
  59. - setDocumentPath:(const char *)aPath
  60. {    copyStringBufferFromZone(&documentPath,aPath,[self zone]);
  61.     return self;
  62. }
  63.  
  64. - getInspectorData:(const char **)nm :(const char **)pth :(int *)t
  65.         :(unsigned long *)ct :(unsigned long *)lt
  66. {
  67.     if(nm) *nm=documentName;
  68.     if(pth) *pth=documentPath;
  69.     if(t) *t=tag;
  70.     if(ct) *ct=createTime;
  71.     if(lt) *lt=lastModifyTime;
  72.     return self;
  73. }
  74.  
  75. /* ==== file handling ==== */
  76.  
  77. - setupNewDoc:(const char *)newPath
  78. {
  79. const char *p=getDefaultWithErrorCheck("WindowSize");
  80. int winWidth, winHeight;
  81.  
  82.     time(&createTime);
  83.     lastModifyTime=createTime;
  84.     [self setDocumentPath:newPath];
  85.     [self setDocumentName:findNameInPath(newPath)];
  86.     
  87.     /* set the window size according to the default */
  88.     if(p)
  89.      { sscanf(p,"%d %d", &winWidth, &winHeight);
  90.        [window sizeWindow:(NXCoord)winWidth :(NXCoord)winHeight];
  91.        [window setTitleAsFilename:documentPath];
  92.      }
  93.     [window makeKeyAndOrderFront:nil];
  94.     return self;
  95. }
  96.  
  97. - (BOOL)openDocFile:(const char *)aPath
  98. {
  99. NXStream *st;
  100. NXRect r;
  101. int scrNum;
  102.  
  103.     /* initialize all instance variables */
  104.     [self setDocumentPath:aPath];
  105.     st=NXMapFile(aPath, NX_READONLY);
  106.  
  107.     NXScanf(st,"Window Frame: %f %f %f %f\n",&r.origin.x,
  108.                        &r.origin.y,&r.size.width,&r.size.height);
  109.     NXScanf(st,"Screen Number: %d\n",&scrNum);
  110.     NXScanf(st,"Created: %d",&createTime);
  111.     NXScanf(st,"Modified: %d",&lastModifyTime);
  112.     
  113.     [self setDocumentName:findNameInPath(aPath)];
  114.     [window placeWindow:&r screen:getScreenOfNumber(scrNum)];
  115.     [window setTitleAsFilename:documentPath];
  116.     [window makeKeyAndOrderFront:nil];
  117.     return YES;
  118. }
  119.  
  120. - saveDocument
  121. {    [self saveDocumentTo:documentPath];
  122.     return self;
  123. }
  124.  
  125. - saveDocumentAs:(const char *)aPath
  126. {    
  127.     copyStringBufferFromZone(&documentPath, aPath, [self zone]);
  128.     [self setDocumentName:findNameInPath(aPath)];
  129.     [self saveDocumentTo:documentPath];
  130.     [window setTitleAsFilename:documentPath];
  131.     return self;
  132. }
  133.  
  134. - saveDocumentTo:(const char *)aPath 
  135. {    
  136. NXStream *st;
  137. NXRect r;
  138. NXScreen *scr;
  139.  
  140.     [window getFrame:&r andScreen:&scr];
  141.     if((st=NXOpenMemory(NULL, 0, NX_WRITEONLY))==NULL)
  142.      { NXRunAlertPanel("Error","Could not open stream for file %s",
  143.         NULL,NULL,NULL, aPath);
  144.         return self;
  145.      }
  146.  
  147.     NXPrintf(st,"Window Frame: %d. %d. %d. %d.\n",(int)NX_X(&r),
  148.        (int)NX_Y(&r),(int)NX_WIDTH(&r),(int)NX_HEIGHT(&r));
  149.     NXPrintf(st,"Screen Number: %d\n",scr->screenNumber);
  150.     NXPrintf(st,"Created: %d\n",createTime);
  151.     NXPrintf(st,"Modified: %d\n", lastModifyTime);
  152.     
  153.     NXSaveToFile(st, aPath);
  154.     NXCloseMemory(st, NX_FREEBUFFER);
  155.  
  156.     [NXApp perform:@selector(updateWindows) with:nil
  157.        afterDelay:1 cancelPrevious:YES];
  158.  
  159.     return self;
  160. }
  161.  
  162. /* ==== Window delegate methods ==== */
  163.  
  164. - windowDidBecomeKey:sender
  165. {
  166.     [[NXApp delegate] setCurrentDoc:self];
  167.     [NXApp perform:@selector(updateWindows) with:nil
  168.        afterDelay:100 cancelPrevious:YES];
  169.     return self;
  170. }
  171.  
  172. - windowWillClose:sender
  173. {
  174.     if([window isDocEdited])
  175.      { switch(NXRunAlertPanel("Close",
  176.         "Save changes to %s",
  177.         "Save", "Do not save", "Cancel",documentName))
  178.         { case NX_ALERTDEFAULT:[self saveDocument]; break;
  179.           case NX_ALERTALTERNATE: break;
  180.           case NX_ALERTOTHER: return nil;
  181.         }
  182.      }
  183.     [[NXApp delegate] perform:@selector(freeDoc:) with:self
  184.       afterDelay:1 cancelPrevious:YES];
  185.     return self;
  186. }
  187.  
  188. - windowDidResize:sender
  189. {    [NXApp perform:@selector(updateWindows) with:nil
  190.        afterDelay:1 cancelPrevious:YES];
  191.     return self;
  192. }
  193.  
  194. - windowDidUpdate:sender
  195. {
  196.     /* if you have buttons in the window, enable/disable
  197.        them here
  198.      */
  199.     return self;
  200. }
  201.  
  202. /* ==== Edit methods ==== */
  203.  
  204. - delete:sender
  205. {   
  206.     if(![[NXApp delegate] isDeleteEnabled]) return self;
  207.     if(NXRunAlertPanel("Are you sure to delete ?",
  208.     "This operation is not reversable",
  209.     "Do not delete","Delete All",NULL)==NX_ALERTDEFAULT) return self;
  210.  
  211.     [window disableFlushWindow];
  212.  
  213.     /* do deletion here */
  214.     
  215.     [window reenableFlushWindow];
  216.     [window flushWindow];
  217.     [NXApp perform:@selector(updateWindows) with:nil
  218.        afterDelay:1 cancelPrevious:YES];
  219.     return self;
  220. }
  221.  
  222. - selectAll:sender
  223. {
  224.     [window disableFlushWindow];
  225.  
  226.     /* do selection here */
  227.     
  228.     [window reenableFlushWindow];
  229.     [window flushWindow];
  230.     [NXApp perform:@selector(updateWindows) with:nil
  231.        afterDelay:1 cancelPrevious:YES];
  232.     return self;
  233. }
  234.  
  235. - free
  236. {    
  237. NXZone *z=[self zone];    
  238.     if(window) [window free];
  239.     if(documentPath) free((char *)documentPath);
  240.     if(documentName) free((char *)documentName);
  241.     [super free];
  242.     
  243.     /* sanity check */
  244.     if(z!=[NXApp zone]) NXDestroyZone(z);
  245.     return nil;
  246. }
  247.  
  248. @end
  249.