home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiniExamples / AppKit / Grabber / Grabber.m < prev    next >
Encoding:
Text File  |  1993-07-14  |  1.8 KB  |  72 lines

  1. /* Grabber.m
  2. *  Purpose:  This class grabs the contents of the screen under the 
  3. *  grabberWindow and uses
  4. *  it to create a TIFF file.
  5. *
  6. *  You may freely copy, distribute, and reuse the code in this example.
  7. *  NeXT disclaims any warranty of any kind, expressed or  implied, as to 
  8. *  its fitness for any particular use.
  9. */
  10.  
  11. #import "Grabber.h"
  12. #import <dpsclient/wraps.h>
  13.  
  14. @implementation Grabber
  15.  
  16. - appDidInit:sender
  17. {
  18.     /* Set the window level... */
  19.     PSsetwindowlevel (100, [grabberWindow windowNum]);
  20.  
  21.     /* Without the next line the level doesn't seem to be set; */
  22.     /* a window server bug. */
  23.     [grabberWindow orderFront:nil];
  24.     [grabberWindow display];
  25.     return self;
  26. }
  27.  
  28. - grab:sender
  29. {
  30.     NXRect grabRect;
  31.     id bitmap;
  32.  
  33.     /* Grab the screen bits into the window. */
  34.     PSsetautofill (NO, [grabberWindow windowNum]);
  35.     [grabberWindow orderOut:nil];
  36.     [grabberWindow orderFront:nil];
  37.  
  38.     /* Read the bits from the window */
  39.     [[grabberWindow contentView] lockFocus];
  40.     [[grabberWindow contentView] getBounds:&grabRect];
  41.     bitmap = [[NXBitmapImageRep alloc] initData:NULL fromRect:&grabRect];
  42.     [[grabberWindow contentView] unlockFocus];
  43.  
  44.     PSsetautofill (YES, [grabberWindow windowNum]);
  45.     [grabberWindow display];
  46.  
  47.     if (bitmap) {
  48.     NXStream *s = NXOpenMemory (NULL, 0, NX_READWRITE);
  49.     printf ("spp %d bps %d w %d h %d\n",
  50.         [bitmap samplesPerPixel],
  51.         [bitmap bitsPerSample],
  52.         [bitmap pixelsWide],
  53.         [bitmap pixelsHigh]);
  54.     if (s) {
  55.         [bitmap writeTIFF:s usingCompression:NX_TIFF_COMPRESSION_LZW];
  56.         NXFlush (s);
  57.         if (NXSaveToFile (s, "/tmp/screenshot.tiff")) {
  58.         perror ("/tmp/screenshot.tiff");
  59.         } else {
  60.         printf ("Image written to /tmp/screenshot.tiff\n");
  61.         }
  62.         NXCloseMemory (s, NX_FREEBUFFER);
  63.     }
  64.     [bitmap free];
  65.     }
  66.  
  67.     return self;
  68. }
  69.  
  70.  
  71. @end
  72.