home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WWTCLKit / WWMovieProcView.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  16.0 KB  |  575 lines

  1.  
  2. #import "WWTCLInterp.h"
  3. #import "WWMovieProcView.h"
  4. #import <ctype.h>
  5.  
  6. @implementation WWMovieProcView
  7.  
  8. + initialize { [WWMovieProcView setVersion:1]; return self; }
  9.  
  10.  
  11. - initFrame:(const NXRect *)frameRect
  12. {
  13.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  14.  
  15.  
  16.    [super initFrame:frameRect];
  17.    [self registerForDraggedTypes:dragTypes count:1];
  18.  
  19.    tclProcNameSize = 32;
  20.    tclProcName = (char *)NXZoneCalloc([self zone], tclProcNameSize, sizeof(char));
  21.    tclProcArgsSize = 32;
  22.    tclProcArgs = (char *)NXZoneCalloc([self zone], tclProcArgsSize, sizeof(char));
  23.    tclProcBodySize = 32;
  24.    tclProcBody = (char *)NXZoneCalloc([self zone], tclProcBodySize, sizeof(char));
  25.    tclProcTypeSize = 32;
  26.    tclProcType = (char *)NXZoneCalloc([self zone], tclProcTypeSize, sizeof(char));
  27.    // "proc name {args} { body }" is 25; 
  28.    // "proc %s {%s} {%s}" needs a minimum of 11, where the 1st and 3rd %s must be non-NULL
  29.    cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  30.    framed = NO;
  31.    restrictDropToSameName = NO;
  32.    restrictDropToSameType = NO;
  33.    actAsSource = YES;
  34.    actAsSink = YES;
  35.    frameColor = NX_COLORBLACK;
  36.    frameWidth = 3.0;
  37.  
  38.    // for future stuff...
  39.    nameSwitchView = nil;
  40.    valueSwitchView = nil;
  41.    typeSwitchView = nil;
  42.  
  43.    return self;
  44. }
  45.  
  46. - awake
  47. {
  48.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  49.  
  50.  
  51.    [super awake];
  52.    [self registerForDraggedTypes:dragTypes count:1];
  53.  
  54.    framed = NO;
  55.    cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  56.    
  57.    return self;
  58. }
  59.  
  60. - free
  61. {
  62.    const char *fileType[] = { "" };
  63.    Pasteboard *pboard = [Pasteboard newName:NXDragPboard];
  64.  
  65.  
  66.    [pboard declareTypes:fileType num:0 owner:nil];
  67.    [self unregisterDraggedTypes];
  68.    image = nil; // don't free the potentially shared image
  69.    if (tclProcName) { free(tclProcName); }
  70.    if (tclProcArgs) { free(tclProcArgs); }
  71.    if (tclProcBody) { free(tclProcBody); }
  72.    if (tclProcType) { free(tclProcType); }
  73.    if (cmdBuf) { NXZoneFree([self zone], cmdBuf); }
  74.  
  75.    return [super free];
  76. }
  77.  
  78. - reinitializeStringsFromZone:(NXZone *)zone
  79. {
  80.   tclProcName = (char *)NXZoneCalloc(zone, tclProcNameSize, sizeof(char));
  81.   tclProcArgs = (char *)NXZoneCalloc(zone, tclProcArgsSize, sizeof(char));
  82.   tclProcBody = (char *)NXZoneCalloc(zone, tclProcBodySize, sizeof(char));
  83.   tclProcType = (char *)NXZoneCalloc(zone, tclProcTypeSize, sizeof(char));
  84.   cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  85.   return self;
  86. }
  87.  
  88. - copyFromZone:(NXZone *)zone
  89. {
  90.   id newCopy = [super copyFromZone:zone];
  91.  
  92.   [newCopy reinitializeStringsFromZone:zone];
  93.   [newCopy setTclProcName:tclProcName];
  94.   [newCopy setTclProcArgs:tclProcArgs];
  95.   [newCopy setTclProcBody:tclProcBody];
  96.   [newCopy setTclProcType:tclProcType];
  97.  
  98.   return newCopy;
  99. }
  100.  
  101. - drawSelf:(const NXRect *)rects :(int)num
  102. {
  103.   [super drawSelf:rects :num];
  104.   if (framed)
  105.   {  NXSetColor(frameColor);
  106.      NXFrameRectWithWidth(&bounds, (NXCoord)frameWidth);
  107.   }
  108.   return self;
  109. }
  110.  
  111. /*-------------------- methods to be the source of a dragging operation */
  112.  
  113. - mouseDown:(NXEvent *)theEvent
  114. {
  115.    NXPoint  zero = {0.0, 0.0};
  116.    id       thePboard;
  117.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  118.    static char *fooBarBaz = "fooBarBaz";
  119.  
  120.    if (theEvent->flags & NX_COMMANDMASK)  // show the control panel   
  121.    {  return [super mouseDown:theEvent]; // let our super class bring it up
  122.    }
  123.  
  124.    if (stationaryBehavior == WW_PLAY_MOUSE_CLICK)
  125.    {  //let our super deal with this first, and then do the drag and drop stuff...
  126.       [super mouseDown:theEvent];
  127.    }
  128.  
  129.    if (!image && ![imageList count]) // we're pretty darn boring; bail
  130.    { return self; 
  131.    }
  132.    if (!actAsSource) { return self; }
  133.  
  134.    // Prevent default window ordering (this is a new appkit feature)
  135.    [NXApp preventWindowOrdering];
  136.  
  137.    //get the Pboard
  138.    thePboard = [Pasteboard newName:NXDragPboard];
  139.    [thePboard declareTypes:dragTypes num:1 owner:nil];
  140.  
  141.    // we're not really going to use this info, we're just sticking something on the board...
  142.    [thePboard writeType:NXAsciiPboardType data:fooBarBaz length:(1 + strlen(fooBarBaz))];
  143.     
  144.    //start the drag
  145.    // if we've no image, use the movieImage
  146.    if (!image)
  147.    {  [self dragImage:movieImage // visible on screen during drag
  148.                    at:&zero     // offset the mouse point for the drag
  149.                offset:&zero     // offset the inital mouse pt
  150.                 event:theEvent     // theEvent structure
  151.            pasteboard:thePboard     // a Pasteboard with data on it
  152.                source:self     // source object
  153.             slideBack:YES];     // if no destination animate back to source
  154.    }
  155.    else
  156.    {  [self dragImage:image    // visible on screen during drag
  157.                    at:&zero    // offset the mouse point for the drag
  158.                offset:&zero    // offset the inital mouse pt
  159.                 event:theEvent    // theEvent structure
  160.            pasteboard:thePboard    // a Pasteboard with data on it
  161.                source:self        // source object
  162.             slideBack:YES];    // if no destination animate back to source
  163.    }
  164.     
  165.     return self;
  166. }
  167.  
  168. - draggedImage:(NXImage *)image beganAt:(NXPoint *)screenPoint  { return self; }
  169.  
  170. - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag  { return (NX_DragOperationCopy | NX_DragOperationGeneric); }
  171.  
  172. - draggedImage:(NXImage *)image endedAt:(NXPoint *)screenPoint deposited:(BOOL)flag { return self; }
  173.  
  174. /*-------------------- methods to be the destination of a dragging operation */
  175.  
  176. - (NXDragOperation)draggingEntered:sender
  177. {
  178.    if (!actAsSink)
  179.    {  if (framed)
  180.       {  framed = NO;
  181.      [self display];
  182.       }
  183.       return NX_DragOperationNone;
  184.    }
  185.  
  186.    // check to make sure we are not the source and this operation is a copy
  187.    if (   ([sender draggingSourceOperationMask] & NX_DragOperationCopy) 
  188.        && ([sender draggingSource] != self)
  189.        && ([[sender draggingSource] respondsTo:@selector(tclProcBody)]))
  190.    {  
  191.       // if we're selective, need to check the var's name
  192.       // if it's not the same as ours, we're outta here
  193.       if (restrictDropToSameName)
  194.       {  if (tclProcName && [[sender draggingSource] tclProcName] && strcmp(tclProcName, [[sender draggingSource] tclProcName]))
  195.          {  if (framed)
  196.         {  framed = NO;
  197.            [self display];
  198.          }
  199.             return NX_DragOperationNone;
  200.      }
  201.       }
  202.       if (restrictDropToSameType)
  203.       {  if (tclProcType && [[sender draggingSource] tclProcType] && strcmp(tclProcType, [[sender draggingSource] tclProcType]))
  204.          {  if (framed)
  205.         {  framed = NO;
  206.            [self display];
  207.          }
  208.             return NX_DragOperationNone;
  209.      }
  210.       }
  211.       framed = YES;
  212.       [self display];
  213.       //return the type of operation we want to do, this will dictate
  214.       //the type of cursor will show up when accepting the drag
  215.       return NX_DragOperationCopy;
  216.    }
  217.    if (framed) 
  218.    {  framed = NO;
  219.       [self display];
  220.    }
  221.    return NX_DragOperationNone;
  222. }
  223.  
  224. - (NXDragOperation)draggingUpdated:sender
  225. {
  226.    if (!actAsSink)
  227.    {  if (framed)
  228.       {  framed = NO;
  229.      [self display];
  230.       }
  231.       return NX_DragOperationNone;
  232.    }
  233.  
  234.    // make sure that this is a copy operation, and that this instance
  235.    // of DragView is not the source
  236.    if (   ([sender draggingSourceOperationMask] & NX_DragOperationCopy) 
  237.        && ([sender draggingSource] != self)
  238.        && ([[sender draggingSource] respondsTo:@selector(tclProcBody)]))
  239.     { 
  240.       if (restrictDropToSameName)
  241.       {  if (tclProcName && [[sender draggingSource] tclProcName] && strcmp(tclProcName, [[sender draggingSource] tclProcName]))
  242.          {  if (framed)
  243.         {  framed = NO;
  244.            [self display];
  245.          }
  246.             return NX_DragOperationNone;
  247.      }
  248.       }
  249.       if (restrictDropToSameType)
  250.       {  if (tclProcType && [[sender draggingSource] tclProcType] && strcmp(tclProcType, [[sender draggingSource] tclProcType]))
  251.          {  if (framed)
  252.         {  framed = NO;
  253.            [self display];
  254.          }
  255.             return NX_DragOperationNone;
  256.      }
  257.       }
  258.       framed = YES;
  259.       [self display];
  260.       return NX_DragOperationCopy;
  261.    }
  262.    if (framed) 
  263.    {  framed = NO;
  264.       [self display];
  265.    }
  266.    return NX_DragOperationNone;
  267. }
  268.  
  269. - draggingExited:sender
  270. {
  271.    if (!actAsSink)
  272.    {  if (framed)
  273.       {  framed = NO;
  274.      [self display];
  275.       }
  276.       return NX_DragOperationNone;
  277.    }
  278.  
  279.    if (framed) 
  280.    {  framed = NO;
  281.       [self display];
  282.    }
  283.    return self;
  284. }
  285.  
  286. - (BOOL)prepareForDragOperation:sender  
  287. {  
  288.    if (!actAsSink)
  289.    {  if (framed)
  290.       {  framed = NO;
  291.      [self display];
  292.       }
  293.       return NO;
  294.    }
  295.    return YES; 
  296. }
  297.  
  298. static int notJustBlanks(const char *str)
  299. {
  300.   const char  *ptr;
  301.  
  302.  
  303.   ptr = str;
  304.   if (ptr)
  305.   {  while (*ptr && isspace(*ptr))
  306.      {  ptr++;
  307.      }
  308.      if (*ptr) // if we're not at the end and we stopped, it's not just blanks...
  309.      {  return 1;
  310.      }
  311.   }
  312.   return 0;
  313. }
  314.  
  315.  
  316. - (BOOL)performDragOperation:sender
  317. {
  318.    if (!actAsSink)
  319.    {  if (framed)
  320.       {  framed = NO;
  321.      [self display];
  322.       }
  323.       return NO;
  324.    }
  325.  
  326.    // make sure that what we are getting is from the same application
  327.    // and did not originate from this view.
  328.    if (   [sender isDraggingSourceLocal] 
  329.        && ([sender draggingSource] != self) 
  330.        && ([[sender draggingSource] respondsTo:@selector(tclProcBody)]))
  331.  
  332.    {  // should check to make sure the sender will respond appropriately...
  333.       if (restrictDropToSameName)
  334.       {  if (tclProcName && [[sender draggingSource] tclProcName] && strcmp(tclProcName, [[sender draggingSource] tclProcName]))
  335.          {  if (framed) 
  336.         {  framed = NO;
  337.            [self display];
  338.         }
  339.         return NO;
  340.      }
  341.       }
  342.       if (restrictDropToSameType)
  343.       {  if (tclProcType && [[sender draggingSource] tclProcType] && strcmp(tclProcType, [[sender draggingSource] tclProcType]))
  344.          {  if (framed) 
  345.             {  framed = NO;
  346.            [self display];
  347.         }
  348.         return NO;
  349.      }
  350.       }
  351.       [self setImage:nil];
  352.       if ([[sender draggingSource] image])
  353.       {  [self setImage:[[sender draggingSource] image]];
  354.       }
  355.       else
  356.       {  [self setImage:[[sender draggingSource] movieImage]];
  357.       }
  358.       [self setTclProcArgs:[[sender draggingSource] tclProcArgs]];
  359.       [self setTclProcBody:[[sender draggingSource] tclProcBody]];
  360.       if (nameSwitchView)
  361.       {  //[nameSwitchView showViewNamed:tclProcName];
  362.       }
  363.       if (typeSwitchView)
  364.       {  //[typeSwitchView showViewNamed:tclProcType];
  365.       }
  366.       if (notJustBlanks(tclProcBody))
  367.       {  // cmdBuf is basically "proc <tclProcName> {<tclProcArgs>} {<tclProcBody>}"
  368.          if (notJustBlanks(tclProcArgs))
  369.          {  sprintf(cmdBuf, "proc %s {%s} {%s}", tclProcName, tclProcArgs, tclProcBody);
  370.             [interp globalEval:cmdBuf];
  371.       }
  372.          else
  373.          {  sprintf(cmdBuf, "proc %s {} {%s}", tclProcName, tclProcBody);
  374.             [interp globalEval:cmdBuf];
  375.       }
  376.       }
  377.       framed = NO;
  378.       [self display];
  379.    }
  380.    if (framed) 
  381.    {  framed = NO;
  382.       [self display];
  383.    }
  384.    return YES;
  385. }
  386.  
  387. - concludeDragOperation:sender {  return self; }
  388.  
  389. /*-------------------- methods to display, and other useful stuff */
  390.  
  391. - (BOOL)acceptsFirstMouse  {  return YES; }
  392.  
  393. - (BOOL)shouldDelayWindowOrderingForEvent:(NXEvent *)theEvent {  return YES; }
  394.  
  395. // set and get stuff
  396.  
  397. - setTclProcArgs:(const char *)str 
  398.   int   cnt;
  399.  
  400.  
  401.   if (tclProcArgsSize <= 0) 
  402.   {  tclProcArgsSize = 32; 
  403.      tclProcArgs = (char *)NXZoneCalloc([self zone], tclProcArgsSize, sizeof(char));
  404.      cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  405.   }
  406.   cnt = strlen(str);
  407.   while (cnt >= tclProcArgsSize)
  408.   {  tclProcArgsSize *= 2;
  409.      tclProcArgs = (char *)NXZoneRealloc([self zone], tclProcArgs, tclProcArgsSize);
  410.   }
  411.   cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  412.   strcpy(tclProcArgs, str); 
  413.   return self; 
  414. }
  415. //
  416. - (const char *)tclProcArgs { return tclProcArgs; }
  417.  
  418.  
  419. - setTclProcBody:(const char *)str 
  420.   int   cnt;
  421.  
  422.  
  423.   if (tclProcBodySize <= 0) 
  424.   {  tclProcBodySize = 32; 
  425.      tclProcBody = (char *)NXZoneCalloc([self zone], tclProcBodySize, sizeof(char));
  426.      cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  427.   }
  428.   cnt = strlen(str);
  429.   while (cnt >= tclProcBodySize)
  430.   {  tclProcBodySize *= 2;
  431.      tclProcBody = (char *)NXZoneRealloc([self zone], tclProcBody, tclProcBodySize);
  432.   }
  433.   cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  434.   strcpy(tclProcBody, str); 
  435.   return self; 
  436. }
  437. //
  438. - (const char *)tclProcBody { return tclProcBody; }
  439.  
  440.  
  441. - setTclProcName:(const char *)str 
  442.   int   cnt;
  443.  
  444.  
  445.   if (tclProcNameSize <= 0) 
  446.   {  tclProcNameSize = 32; 
  447.      tclProcName = (char *)NXZoneCalloc([self zone], tclProcNameSize, sizeof(char));
  448.      cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  449.   }
  450.   cnt = strlen(str);
  451.   while (cnt >= tclProcNameSize)
  452.   {  tclProcNameSize *= 2;
  453.      tclProcName = (char *)NXZoneRealloc([self zone], tclProcName, tclProcNameSize);
  454.   }
  455.   cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclProcNameSize + tclProcArgsSize + tclProcBodySize), sizeof(char));
  456.   strcpy(tclProcName, str); 
  457.   return self; 
  458. }
  459. //
  460. - (const char *)tclProcName { return tclProcName; }
  461.  
  462. - setTclProcType:(const char *)str 
  463.   int   cnt;
  464.  
  465.  
  466.   if (tclProcTypeSize <= 0) 
  467.   {  tclProcTypeSize = 32; 
  468.      tclProcType = (char *)NXZoneCalloc([self zone], tclProcTypeSize, sizeof(char));
  469.   }
  470.   cnt = strlen(str);
  471.   while (cnt >= tclProcTypeSize)
  472.   {  tclProcTypeSize *= 2;
  473.      tclProcType = (char *)NXZoneRealloc([self zone], tclProcType, tclProcTypeSize);
  474.   }
  475.   strcpy(tclProcType, str); 
  476.   return self; 
  477. }
  478. //
  479. - (const char *)tclProcType { return tclProcType; }
  480.  
  481.  
  482. - (BOOL)restrictDropToSameName { return restrictDropToSameName; }
  483. - setRestrictDropToSameName:(BOOL)flag {  restrictDropToSameName = flag; return self; }
  484.  
  485. - (BOOL)restrictDropToSameType { return restrictDropToSameType; }
  486. - setRestrictDropToSameType:(BOOL)flag {  restrictDropToSameType = flag; return self; }
  487.  
  488. - (BOOL)actAsSink { return actAsSink; }
  489. - setActAsSink:(BOOL)flag 
  490. {  if (actAsSink && !flag) // we're about to turn it off...
  491.    {  [self unregisterDraggedTypes];
  492.    }
  493.    if (!actAsSink && flag) // we're about to turn it on...
  494.    {  const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  495.       [self registerForDraggedTypes:dragTypes count:1];
  496.    }
  497.    
  498.    actAsSink = flag; 
  499.    return self; 
  500. }
  501.  
  502. - (BOOL)actAsSource { return actAsSource; }
  503. - setActAsSource:(BOOL)flag {  actAsSource = flag; return self; }
  504.  
  505. // IB stuff
  506. - (const char *)getInspectorClassName 
  507. {  
  508.    NXEvent  *event = [NXApp currentEvent];
  509.  
  510.    if (event->flags & NX_ALTERNATEMASK)
  511.    {  return [super getInspectorClassName];
  512.    }
  513.    
  514.    return "WWMovieProcViewIBInspector"; 
  515. }
  516.  
  517. - write:(NXTypedStream *)stream 
  518. {
  519.    [super write:stream];
  520.    NXWriteTypes(stream, "****cccfc", &tclProcName, &tclProcArgs, &tclProcBody, &tclProcType, 
  521.         &restrictDropToSameName, &restrictDropToSameType, &actAsSource, &frameWidth, &actAsSink);
  522.    NXWriteColor(stream, frameColor);
  523.    return self;
  524. }
  525. //
  526. - read:(NXTypedStream *)stream 
  527. {
  528.    int version;
  529.  
  530.    [super read:stream];
  531.  
  532.    version = NXTypedStreamClassVersion(stream, "WWMovieProcView");
  533.    if (version == 1)
  534.    {  NXReadTypes(stream, "****cccfc", &tclProcName, &tclProcArgs, &tclProcBody, &tclProcType, 
  535.            &restrictDropToSameName, &restrictDropToSameType, &actAsSource, &frameWidth, &actAsSink);
  536.       frameColor = NXReadColor(stream);
  537.       if (tclProcName)
  538.       {  tclProcNameSize = strlen(tclProcName) + 1;
  539.       }
  540.       else
  541.       {  tclProcNameSize = 0;
  542.       }
  543.  
  544.       if (tclProcArgs)
  545.       {  tclProcArgsSize = strlen(tclProcArgs) + 1;
  546.       }
  547.       else
  548.       {  tclProcArgsSize = 0;
  549.       }
  550.  
  551.       if (tclProcBody)
  552.       {  tclProcBodySize = strlen(tclProcBody) + 1;
  553.       }
  554.       else
  555.       {  tclProcBodySize = 0;
  556.       }
  557.  
  558.       if (tclProcType)
  559.       {  tclProcTypeSize = strlen(tclProcType) + 1;
  560.       }
  561.       else
  562.       {  tclProcTypeSize = 0;
  563.       }
  564.    }
  565.    return self;
  566. }
  567.  
  568.  
  569.  
  570. @end
  571.