home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / MiniExamples / ImageText / FileImage.m < prev    next >
Text File  |  1993-01-19  |  2KB  |  95 lines

  1. // FileImage.h
  2. // By Jayson Adams
  3. // NeXT Strategic Developer Engineer
  4. //
  5. // You may freely copy, distribute and reuse the code in this example.
  6. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  7. // fitness for any particular use.
  8.  
  9. #import <libc.h>            // for free()
  10. #import <sys/param.h>            // for MAXPATHLEN
  11. #import <appkit/NXImage.h>
  12. #import <appkit/Speaker.h>
  13. #import <appkit/Listener.h>
  14. #import <appkit/Panel.h>        // for NXRunAlertPanel()
  15. #import <appkit/Application.h>
  16.  
  17. #import "FileImage.h"
  18.  
  19.  
  20. @implementation FileImage
  21.  
  22.  
  23. - initForImage:anImage fileName:(const char *)name
  24. {
  25.     [super init];
  26.     
  27.   /* save our graphic image and the file we represent */
  28.     image = anImage;
  29.     fileName = NXCopyStringBuffer(name);
  30.     
  31.     return self;
  32. }
  33.  
  34. - free
  35. {
  36.     free(fileName);
  37.     return [super free];
  38. }
  39.  
  40. - readRichText:(NXStream *)stream forView:view
  41. {
  42.     char    stringBuffer[MAXPATHLEN], *currentPosition, nextChar,
  43.             *tiffBuffer;
  44.     int        length, flag;
  45.     NXStream    *imageStream;
  46.     
  47.   /* get and remember the file name */
  48.     currentPosition = stringBuffer;
  49.     while ((nextChar = NXGetc(stream)) != '\n') {
  50.         *(currentPosition++) = nextChar;
  51.     }
  52.     *currentPosition = '\0';
  53.     fileName = NXCopyStringBuffer(stringBuffer);
  54.  
  55.   /* ask the Workspace for its icon */
  56.     [[NXApp appSpeaker] setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  57.     [[NXApp appSpeaker] getFileIconFor:fileName TIFF:&tiffBuffer
  58.                 TIFFLength:&length ok:&flag];
  59.  
  60.   /* copy the image into our NXImage */
  61.     imageStream = NXOpenMemory(tiffBuffer, length, NX_READONLY);
  62.     image = [[NXImage alloc] initFromStream:imageStream];
  63.     
  64.     NXCloseMemory(imageStream, NX_FREEBUFFER);
  65.  
  66.     return self;
  67. }
  68.  
  69. - writeRichText:(NXStream *)stream forView:view
  70. {
  71.   /* 
  72.    * for files, we write the file name;  this isn't a robust solution since
  73.    * the file may not be present when the user reopens the RTF file
  74.    */
  75.     NXPrintf(stream, "%s\n", fileName);
  76.     
  77.     return self;
  78. }
  79.  
  80. - performDoubleClickAction
  81. {
  82.     int        ok;
  83.     
  84.     [[NXApp appSpeaker] setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
  85.     [[NXApp appSpeaker] openFile:fileName ok:&ok];
  86.     
  87.     if (!ok) {
  88.         NXRunAlertPanel(NULL, "Couldn't open %s", NULL, NULL, NULL, fileName);
  89.     }
  90.     
  91.     return self;
  92. }
  93.  
  94. @end
  95.