home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / AppKit / VideoApp / CustomVideoView.m < prev    next >
Encoding:
Text File  |  1992-05-27  |  5.1 KB  |  210 lines

  1. #import <appkit/appkit.h>
  2. #import "CustomVideoView.h"
  3. #import <dpsclient/event.h>
  4. #import <dpsclient/psops.h>
  5. #import <dpsclient/wraps.h>
  6. #import <sys/param.h>
  7. #import <stdio.h>
  8. #import <strings.h>
  9.  
  10. @implStation CustomVideoView
  11.  
  12. - initFrame:(const NXRect *)theFrame
  13. {
  14.   [super initFrame:theFrame];
  15. // Get it to allocate the NXImage ahead of time so it will stop fast.
  16.   [self setGrabOnStop:YES];
  17.   theMode = NX_FROMINPUT;
  18.   actualSize = YES;
  19.   theImage = [NXImage findImageNamed:"AppIcon"];
  20.   [theImage setScalable:YES];
  21.   [theImage getSize:&imageSize];
  22.   imagePoint.x = bounds.origin.x + (bounds.size.width - imageSize.width)/2.0;
  23.   imagePoint.y = bounds.origin.y + (bounds.size.height-imageSize.height)/2.0;
  24.   changed = YES;
  25.   return self;
  26. }
  27.  
  28. - mouseDown:(NXEvent *)theEvent
  29. {
  30.   NXEvent anEvent = *theEvent;
  31.   NXPoint point1 = theEvent->location,point2;
  32.   int oldMask;
  33.   BOOL dragged = NO;
  34.   
  35.   oldMask = [window addToEventMask:NX_LMOUSEDRAGGEDMASK];
  36.   if(theMode==NX_FROMINPUT)
  37.   {
  38.     // Set Grab rectangle.
  39.     while(anEvent.type!=NX_MOUSEUP)
  40.     {
  41.       NXGetOrPeekEvent(DPSGetCurrentContext(),
  42.         &anEvent, NX_MOUSEUPMASK|NX_LMOUSEDRAGGEDMASK,NX_FOREVER,10,0);
  43.       if(anEvent.type==NX_MOUSEDRAGGED)
  44.       {
  45.         dragged = YES;
  46.         point2 = anEvent.location;
  47.         if(point1.x<point2.x)
  48.           {grabRect.origin.x=point1.x;grabRect.size.width=point2.x - point1.x;}
  49.         else
  50.           {grabRect.origin.x=point2.x;grabRect.size.width=point1.x - point2.x;}
  51.         if(point1.y<point2.y)
  52.         {
  53.           grabRect.origin.y=point1.y;
  54.     grabRect.size.height=point2.y - point1.y;
  55.         }
  56.         else
  57.         {
  58.           grabRect.origin.y=point2.y;
  59.     grabRect.size.height=point1.y - point2.y;
  60.         }
  61.         [self display];
  62.       }
  63.   
  64.     }
  65.     if(!dragged) grabRect.size.width = 0;
  66.     [self display];
  67.   }
  68.   else
  69.   {
  70.     float dx=0,dy=0;
  71.     if(actualSize)        // Move Image around
  72.     {
  73.       NXRect imageFrame,oldFrame;
  74.       imageFrame.origin = imagePoint;imageFrame.size = imageSize;
  75.       oldFrame = imageFrame;
  76.       if(NXMouseInRect(&theEvent->location,&imageFrame,NO))
  77.       {
  78.         dx = theEvent->location.x - imagePoint.x;
  79.         dy = theEvent->location.y - imagePoint.y;
  80.         while(anEvent.type!=NX_MOUSEUP)
  81.         {
  82.           NXGetOrPeekEvent(DPSGetCurrentContext(),&anEvent,
  83.       NX_MOUSEUPMASK|NX_LMOUSEDRAGGEDMASK,NX_FOREVER,10,0);
  84.           if(anEvent.type==NX_MOUSEDRAGGED)
  85.           {
  86.       imagePoint.x = anEvent.location.x -dx;
  87.       imagePoint.y = anEvent.location.y -dy;
  88.       imageFrame.origin = imagePoint;
  89.     TUnionRect(&imageFrame,&oldFrame);
  90.             [self display:&oldFrame :1];
  91.           }
  92.         }
  93.       }
  94.     }
  95.   }
  96.   [window setEventMask:oldMask];
  97.   return self;
  98. }
  99.  
  100. - grab:sender
  101. {
  102.   id bitmap;
  103.   id image;
  104.  
  105.   // Read the bits from the window
  106.   image = [self grab];
  107.   [image lockFocus];
  108.   bitmap = [[NXBitmapImageRep alloc] initData:NULL fromRect:(grabRect.size.width == 0) ? &bounds : &grabRect];
  109.   [image unlockFocus];
  110.   if (bitmap)
  111.   {
  112.     id mySavePanel = [SavePanel new];
  113.     char filename[MAXPATHLEN+1];
  114.     [mySavePanel runModal];
  115.     if([mySavePanel filename])
  116.     {
  117.       NXStream *s = NXOpenMemory (NULL, 0, NX_READWRITE);
  118.       strcpy(filename,[mySavePanel filename]);
  119.       if(strcmp(&filename[strlen(filename)-5],".tiff"))
  120.         strcat(filename,".tiff");
  121.       if (s)
  122.       {
  123.         [bitmap writeTIFF:s usingCompression:NX_TIFF_COMPRESSION_JPEG
  124.                             andFactor:10];
  125.         NXFlush (s);
  126.         if (NXSaveToFile (s, filename))
  127.           NXRunAlertPanel("Error Saving File","Filename: %s","OK",
  128.     NULL,NULL,filename);
  129.         NXCloseMemory (s, NX_FREEBUFFER);
  130.       }
  131.     }
  132.     [bitmap free];
  133.   }
  134.   return self;
  135. }
  136.  
  137. - setImage:sender
  138. {
  139.   // Set the graphic for output mode.
  140.   id myOpenPanel = [OpenPanel new];
  141.   
  142.   if([myOpenPanel runModalForTypes:[NXImage imageFileTypes]])
  143.   {
  144.     const char *theFileName = [myOpenPanel filename];
  145.     
  146.     if(theImage) [theImage free];
  147.     theImage = [[NXImage alloc] initFromFile:theFileName];
  148.     [theImage setScalable:YES];
  149.     [theImage getSize:&imageSize];
  150.     imagePoint.x = bounds.origin.x + (bounds.size.width - imageSize.width)/2.0;
  151.     imagePoint.y = bounds.origin.y + (bounds.size.height-imageSize.height)/2.0;
  152.     changed = YES;
  153.   }
  154.   [self display];
  155.   return self;
  156. }
  157.  
  158. - drawSelf:(const NXRect *)rects :(int)rectCount
  159. {
  160.   if(theMode==NX_FROMVIEW)
  161.   {
  162.     NXRectClip(rects);
  163.     NXEraseRect(&rects[0]);
  164.     if(!theImage) return self;
  165.     if(actualSize)
  166.     {
  167.       if(changed) {[theImage setSize:&imageSize];changed = NO;}
  168.       [theImage composite:NX_SOVER toPoint:&imagePoint];
  169.     }
  170.     else
  171.     {
  172.       if(changed) {[theImage setSize:&bounds.size];changed = NO;}
  173.       [theImage composite:NX_SOVER toPoint:&bounds.origin];
  174.     }
  175.   }
  176.   else
  177.   {
  178.    [super drawSelf:rects :rectCount];
  179.    PSsetgray(NX_WHITE);
  180.    PSsetalpha(1.0);
  181.    if(grabRect.size.width!=0) NXFramUt(&grabRect);
  182.   }
  183.   return self;
  184. }
  185.  
  186. - setOutputMode:(int)mode
  187. {
  188.   theMode = mode;
  189.   [super setOutputMode:mode];
  190.   return self;
  191. }
  192.  
  193. - setActualSize:sender
  194. {
  195.   actualSize = [[sender selectedCell] tag];
  196.   changed = YES;
  197.   [self display];
  198.   return self;
  199. }
  200.  
  201. - clear:sender
  202. {
  203.   if(theImage) [theImage free];
  204.   theImage = NULL;
  205.   [self display];
  206.   return self;
  207. }
  208.  
  209. @end
  210.