home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / ClassBuilder / Source / WindowManager.m < prev    next >
Encoding:
Text File  |  1993-01-25  |  3.3 KB  |  144 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "WindowManager.h"
  5. #import <appkit/Application.h>
  6. #import <appkit/ScrollView.h>
  7. #import <appkit/Text.h>
  8. #import <appkit/TextField.h>
  9. #import <appkit/OpenPanel.h>
  10. #import <libc.h>
  11. #import <strings.h>
  12.  
  13. @implementation WindowManager
  14.  
  15. - (char *) extension ;
  16. { // subclasses should override to provide file extension
  17.   // for saving window
  18.   return "" ;
  19. }
  20.  
  21. - fileName: (char *) fname ;
  22. { // copy fname into ivar, set the
  23.   // window's title to fname, with the
  24.   // filename followed by the path
  25.   char fullName[128], *name ;
  26.   strncpy(fileName, fname, 127) ;
  27.   strncpy(fullName, fileName, 127) ;
  28.   name = rindex(fullName,'/') ;
  29.   if(name != NULL) // is there is a path,
  30.   { char title[129] ;
  31.     name[0] = '\0' ; // split string into 2 pieces
  32.     name++ ;
  33.     sprintf(title,"%s  %s",name,fullName) ;
  34.     [self setTitle: title] ;
  35.   }
  36.   else // no path, just use the filename
  37.     [self setTitle: fname] ;
  38.   return self ;
  39. }
  40.  
  41. - message: (char *) aMsg ;
  42. { // show aMsg in the msgField
  43.   [msgField setStringValue: aMsg] ;
  44.   [self flushWindow] ;
  45.   return self ;
  46. }
  47.  
  48. -revert:sender ;
  49. { return [self readFile] ;
  50. }
  51.  
  52. - readFile ;
  53. // read "fileName" into the text object 
  54. { NXStream *fileStream ;
  55.   if(fileName[0] == '\0')
  56.     return self ; // no file to read!
  57.   [self message: "Reading file..."] ;
  58.   if((fileStream = NXMapFile(fileName,NX_READONLY)) != NULL)
  59.   { [textView readText: fileStream] ;
  60.     NXCloseMemory(fileStream,NX_FREEBUFFER);
  61.     [self setDocEdited: NO] ;
  62.   }
  63.   else
  64.     NXRunAlertPanel("CB", "Error, couldn't read: %s",
  65.         NULL,NULL,NULL,fileName) ;
  66.   [self message: ""] ;
  67.   return self ;
  68. }
  69.  
  70. -(BOOL) save:sender ;
  71. { if([self isDocEdited])
  72.   { if(fileName[0] == '\0') 
  73.       return [self saveAs: sender] ;
  74.     else
  75.       return [self saveTextToFileName] ;
  76.   }
  77.   return YES ;
  78. }
  79.  
  80.  
  81. -(BOOL) saveAs:sender ;
  82. { id savePanel = [SavePanel new] ;
  83.   [savePanel setRequiredFileType: [self extension]] ;
  84.   if([savePanel runModalForDirectory: NULL file: fileName])
  85.   { [self fileName: (char *) [savePanel filename]] ;
  86.     return [self saveTextToFileName] ;
  87.   }
  88.   else
  89.     return NO ;
  90. }
  91.  
  92. -(BOOL) saveTextToFileName ;
  93. // pre:  -ivar fileName contains a valid
  94. //       file pathname.
  95. //       -ivar textView contains a TextView object
  96. // post: if file can be opened or created, with mode
  97. //       644: text of textView is written out to the
  98. //       file named by fileName; file is closed, and
  99. //       YES is returned.
  100. //       otherwise returns NO
  101. { int fd ;
  102.   NXStream *fileStream ;
  103.   [self message: "Saving file..."] ;
  104.   fd = open(fileName, O_WRONLY|O_CREAT|O_TRUNC, 0644) ;
  105.   if(fd != -1)
  106.   { fileStream = NXOpenFile(fd, NX_WRITEONLY) ;
  107.     [textView writeText: fileStream] ;
  108.     NXClose(fileStream) ;
  109.    [self setDocEdited:NO];
  110.   }
  111.   else
  112.   { NXRunAlertPanel("Error","Could not save file:%s",
  113.       NULL,NULL,NULL,fileName) ;
  114.     [self message: ""] ;
  115.     return NO ;
  116.   }
  117.   [self message: ""] ;
  118.   return YES ;
  119. }
  120.  
  121. - setTextView: sender ;
  122. { // get a handle on the text in the scrolling window.
  123.   // make ourselves its delegate, so we know when the 
  124.   // text has been edited. 
  125.   textView = [sender docView] ;
  126.   [textView setDelegate: self] ;
  127.   return self ;
  128. }
  129.  
  130.  
  131. - text:text isEmpty:(BOOL)empty
  132. { // this is a delegate message for the textView
  133.   if(![self isDocEdited])
  134.      [self setDocEdited: YES] ;
  135.   return NO;
  136. }
  137.  
  138. - textView ;
  139. { return textView ;
  140. }
  141.  
  142.  
  143. @end
  144.