home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / Tutorial / Cookbook / 22.mouseEvents / MyView.m < prev    next >
Encoding:
Text File  |  1993-01-19  |  2.9 KB  |  130 lines

  1. #import "MyView.h"
  2. #import <appkit/Control.h>
  3. #import <appkit/Form.h>
  4. #import <appkit/Application.h>
  5. #import <dpsclient/wraps.h>
  6. #import <strings.h>
  7. #include <stdio.h>
  8.  
  9. @implementation MyView
  10.  
  11. static char logString[10000];
  12.  
  13. /* add a string to the log and have text updated */
  14. static void addStringLog(target, string)
  15. char *string;
  16. id target;
  17. {
  18.     strcat(logString, string);
  19.     [target setStringValue:logString];
  20. }
  21.  
  22. /* add an event to the log text */
  23. static void addEventLog(target, string, x, y, type)
  24. char *string;
  25. id target;
  26. float x,y;
  27. int type;
  28. {
  29. static char tmpString[100];
  30.     strcat(logString, string);
  31.     if (type == 1) strcat(logString, " DOWN ");
  32.        else if (type == 2) strcat(logString, " UP ");
  33.           else if (type == 6) strcat(logString, " DRAG ");
  34.              else {
  35.                sprintf(tmpString, " type=%d ", type);
  36.                strcat(logString, tmpString);
  37.              }
  38.     sprintf(tmpString, "at x=%3.0f  y=%3.0f\n", x, y);
  39.     strcat(logString, tmpString);
  40.     [target setStringValue:logString];
  41. }
  42.  
  43.  
  44. - setXForm:anObject
  45. {
  46.     xForm = anObject;
  47.     return self;
  48. }
  49.  
  50. - setYForm:anObject
  51. {
  52.     yForm = anObject;
  53.     return self;
  54. }
  55.  
  56. - setLogText:anObject
  57. {
  58.     logText = anObject;
  59.     return self;
  60. }
  61.  
  62. - clearLog:sender
  63. {
  64.     logString[0] = '\0';
  65.     [logText setStringValue:logString];
  66.     return self;
  67. }
  68.  
  69. - drawSelf:(NXRect *)list:(int)count
  70. {
  71.     NXEraseRect(&bounds);
  72.     return self;
  73. }
  74.  
  75. // from chapter 7 page 61
  76. - mouseDown:(NXEvent *)thisEvent
  77. {
  78.     register int     inside; 
  79.     int                 shouldLoop = YES; 
  80.     int                 oldMask; 
  81.     NXEvent            *nextEvent;
  82.     NXPoint    mLoc, oldLoc, myPoint;
  83.     BOOL flipped = NO;
  84.     
  85.     oldLoc = thisEvent->location;
  86.     addEventLog(logText, "Enter mouseDown", oldLoc.x, oldLoc.y, thisEvent->type);
  87.     // [self doMyOwnHighlight];
  88.     oldMask = [window addToEventMask:NX_LMOUSEDRAGGEDMASK];
  89.  
  90.     while (shouldLoop) { 
  91.         nextEvent = [NXApp getNextEvent:(NX_LMOUSEUPMASK | NX_LMOUSEDRAGGEDMASK)];
  92.         mLoc = nextEvent->location;
  93.         addEventLog(logText, "NextEvent Window Coords:", mLoc.x, mLoc.y, nextEvent->type);
  94.         [self convertPoint:&mLoc fromView:nil];
  95.         // mLoc = nextEvent->location;
  96.         addEventLog(logText, "View Coords:", mLoc.x, mLoc.y, nextEvent->type);
  97.         inside = NXMouseInRect(&mLoc, &bounds, flipped);
  98.         [xForm setFloatValue:mLoc.x at:0];
  99.         [yForm setFloatValue:mLoc.y at:0];
  100.         switch (nextEvent->type) { 
  101.         case NX_LMOUSEUP:  
  102.             shouldLoop = NO; 
  103.             if ( inside ) {
  104.                 addStringLog(logText, "Mouse Up Inside View\n");
  105.                 // [self doMyOwnHighlight]; 
  106.                 // [self doMyOwnThing]; 
  107.             } 
  108.             // [self doMyOwnUnhighlight]; 
  109.             break; 
  110.         case NX_LMOUSEDRAGGED:  
  111.             if ( inside ) 
  112.                 addStringLog(logText, "Mouse Drag Inside View\n");
  113.                 // [self doMyOwnHighlight]; 
  114.             else 
  115.                 addStringLog(logText, "Mouse Drag Outside View\n");
  116.                 // [self doMyOwnUnhighlight]; 
  117.             break; 
  118.         default:
  119.             addEventLog(logText, "In Default", mLoc.x, mLoc.y, nextEvent->type); 
  120.             break; 
  121.         }
  122.  
  123.     } 
  124.     [window setEventMask:oldMask]; 
  125.     return(self); 
  126. }
  127.  
  128.  
  129. @end
  130.