home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / AlexNeXTSTEPSource / Source / Chapter7_Views / Shapes / ShapeView.m < prev    next >
Encoding:
Text File  |  1993-04-14  |  3.5 KB  |  178 lines

  1. /* Generated by Project Builder */
  2.  
  3. #import <appkit/appkit.h>
  4. #import "ShapeView.h"
  5.  
  6. @implementation ShapeView
  7.  
  8. - initFrame:(NXRect *)frameRect
  9. {
  10.     [super initFrame:frameRect];
  11.     angle = 360;
  12.     xScale = 1;
  13.     yScale = 1;
  14.     x = 50;
  15.     y = 50;
  16.     return self;
  17. }
  18.  
  19. - translate:sender
  20. {
  21.     // sender is a matrix: thus, obtain
  22.     // cell by asking the matrix
  23.     // for the selected cell
  24.     id cell = [sender selectedCell];
  25.     int tag = [cell tag];
  26.  
  27.     switch(tag)
  28.         {
  29.         case 0:
  30.             x = [cell floatValue];
  31.             break;
  32.         case 1:
  33.             y = [cell floatValue];
  34.             break;
  35.         }
  36.     // display view with updated values
  37.     [self display];
  38.     return self;
  39. }
  40.  
  41. - scale:sender
  42. {
  43.     // sender is a matrix: thus, obtain
  44.     // cell by asking the matrix
  45.     // for the selected cell
  46.     id cell = [sender selectedCell];
  47.     int tag = [cell tag];
  48.  
  49.     switch(tag)
  50.         {
  51.         case 0:
  52.             xScale = [cell floatValue];
  53.             break;
  54.         case 1:
  55.             yScale = [cell floatValue];
  56.             break;
  57.         }
  58.     // display view with updated values
  59.     [self display];
  60.     return self;
  61. }
  62.  
  63. - rotate:sender
  64. {
  65.     angle = [sender floatValue];
  66.     [self display];
  67.     return self;
  68. }
  69.  
  70. - erase
  71. {
  72.     // &bounds contains the view's location
  73.     // and size expressed in its own coordinate
  74.     // system: by drawing in bounds, the view
  75.     // avoids unnecessary drawing
  76.     PSsetgray(NX_DKGRAY);
  77.     NXRectFill(&bounds);
  78.     return self;
  79. }
  80.  
  81. - mouseDown:(NXEvent *)theEvent
  82. {
  83.     char buffer[30];
  84.  
  85.     // round co-ordinates to 1 digit
  86.     // after decimal point
  87.     // print co-ordinates in window co-ordinates
  88.     sprintf(buffer, "%.1f", theEvent->location.x);
  89.     [[downWindowMatrix cellAt:0 :0]
  90.         setStringValue:buffer];
  91.     sprintf(buffer, "%.1f", theEvent->location.y);
  92.     [[downWindowMatrix cellAt:1 :0]
  93.         setStringValue:buffer];
  94.  
  95.     // convert from window co-ordinates to
  96.     // view co-ordinates
  97.     [self convertPoint:&theEvent->location
  98.         fromView:nil];
  99.  
  100.     // lock focus so graphics commands
  101.     // are applied to this view
  102.     [self lockFocus];
  103.     // erase old instance drawing
  104.     PSnewinstance();
  105.     // start new instance drawing to draw directly
  106.     // to the window
  107.     PSsetinstance(YES);
  108.         // select font otherwise output
  109.         // will be upside down
  110.         PSselectfont("Times-Roman", 16.0);
  111.         // print co-ordinates on view
  112.         PSmoveto(theEvent->location.x,
  113.             theEvent->location.y);
  114.         sprintf(buffer, "%.1f, %.1f",
  115.             theEvent->location.x,
  116.             theEvent->location.y);
  117.         // set color to light gray
  118.         PSsetgray(NX_LTGRAY);
  119.         // show the string
  120.         PSshow(buffer);        
  121.     PSsetinstance(NO);
  122.  
  123.     // balance lockFocus with unlockFocus
  124.     [self unlockFocus];
  125.     return self;
  126. }
  127.  
  128. - mouseUp:(NXEvent *)theEvent
  129. {
  130.     char buffer[40];
  131.     sprintf(buffer, "%.1f", theEvent->location.x);
  132.     [[upWindowMatrix cellAt:0 :0]
  133.         setStringValue:buffer];
  134.     sprintf(buffer, "%.1f", theEvent->location.y);
  135.     [[upWindowMatrix cellAt:1 :0]
  136.         setStringValue:buffer];
  137.  
  138.     // convert from window co-ordinates to
  139.     // view co-ordinates
  140.     [self lockFocus];
  141.     [self convertPoint:&theEvent->location
  142.         fromView:nil];
  143.  
  144.     // don't use PSnewinstance(), since we don't
  145.     // want to erase the drawing from mouseDown:
  146.     // start new instance drawing to draw directly
  147.     // to the window
  148.     PSsetinstance(YES);
  149.         // select font otherwise output
  150.         // will be upside down
  151.         PSselectfont("Times-Roman", 16.0);
  152.         // print co-ordinates on view
  153.         PSmoveto(theEvent->location.x,
  154.             theEvent->location.y);
  155.         sprintf(buffer, "%.1f, %.1f",
  156.             theEvent->location.x,
  157.             theEvent->location.y);
  158.         // set color to black
  159.         PSsetgray(NX_BLACK);
  160.         // show the string
  161.         PSshow(buffer);        
  162.     PSsetinstance(NO);
  163.  
  164.     // balance lockFocus with unlockFocus
  165.     [self unlockFocus];
  166.  
  167.     return self;
  168. }
  169.  
  170. -(BOOL)acceptsFirstMouse
  171. {
  172.     // allows the view to use the first 
  173.     // mouse click that activates the window
  174.     return YES;
  175. }
  176.  
  177. @end
  178.