home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / MiniExamples / PerformanceTuning / VisibleView-02 / VisibleOne.m < prev    next >
Encoding:
Text File  |  1991-10-18  |  8.5 KB  |  351 lines

  1. //
  2. //      A Visible View
  3. //      Randy Nelson, NeXT Developer Training
  4. //      Created 5-1-90
  5. //    Modified 9-3-90 for 2.0
  6. //
  7. //    You may freely copy, distribute and reuse the code in this example.
  8. //    NeXT disclaims any warranty of any kind, expressed or implied, as to
  9. //    its fitness for any particular use.
  10. //
  11.  
  12. #import "VisibleOne.h"
  13. #import "StringManager.h"
  14. #import <appkit/View.h>
  15. #import <appkit/publicWraps.h>
  16. #import <dpsclient/wraps.h>
  17. #import <appkit/graphics.h>
  18. #import <appkit/Application.h>
  19. #import <appkit/Font.h>
  20. #import <appkit/Form.h>
  21. #import <appkit/Control.h>
  22. #import <appkit/Button.h>
  23. #import <appkit/NXImage.h>
  24. #import <objc/NXStringTable.h>
  25. #import <libc.h>
  26. #import "SubViewFramer.h"
  27. #import "Drawgrid.h"
  28. #import "SomePS.h"
  29. #import "Appender.h"
  30.  
  31. @implementation VisibleOne
  32.  
  33. //sets clipping off
  34. - initFrame:(const NXRect *)framerect;
  35. {
  36.     [super initFrame:framerect];
  37.     [self setClipping:NO];
  38.     //because we read strings in our drawSelf::
  39.     //we must assure they'll be there by the first time it's called
  40.     stringSet = [[[StringManager alloc] init] stringSet];
  41.     
  42.     //save some defs in the window server
  43.     somedefs();
  44.     
  45.     return self;
  46. }
  47.  
  48. //translate
  49. - setSelfOrigin:sender
  50. {
  51.     //the slider returns values that are the opposite of what we need
  52.     [self setDrawOrigin:([translateX intValue] *( -1))
  53.         :([translateY intValue] *( -1))];
  54.     [window display];
  55.     return self;
  56. }
  57.  
  58. //scale
  59. - setSelfScale:sender
  60. {
  61.     //can't scale by zero
  62.     if ([scaleX floatValue] == 0) {
  63.         [scaleX setIntValue:1];
  64.     }
  65.     if ([scaleY floatValue] == 0) {
  66.         [scaleY setIntValue:1];
  67.     }
  68.     [self scale:[scaleX floatValue] :[scaleY floatValue]];
  69.     [window display];
  70.  
  71.     //reset the interface values
  72.     [scaleX setIntValue:1];
  73.     [scaleY setIntValue:1];
  74.     
  75.     return self;
  76. }
  77.  
  78. //rotate
  79. - setSelfRotation:sender
  80. {
  81.     [self setDrawRotation:[sender intValue]];
  82.     [window display];
  83.     return self;
  84. }
  85.  
  86. //move frame
  87. - frameMove:sender
  88. {
  89.     [self moveTo:[frameX intValue] :[frameY intValue]];
  90.     [window display];
  91.     [self setTrackingRect];
  92.     return self;
  93. }
  94.  
  95. //rotate frame
  96. //tracking rects don't work when your frame is rotated
  97. - frameRotate:sender
  98. {
  99.     [self rotateTo:[sender intValue]];
  100.     [window display];
  101.     return self;
  102. }
  103.  
  104. //resize frame
  105. - frameChangeSize:sender
  106. {
  107.     [self sizeTo:[frameWidth intValue] :[frameHeight intValue]];
  108.     [self setTrackingRect];
  109.     [window display];
  110.     return self;
  111. }
  112.  
  113. //sent by the preferences buttons to force a redraw, reflecting their new state
  114. - newDisplayState:sender
  115. {
  116.         [window display];
  117.         return self;
  118. }
  119.  
  120. //delegate method of app
  121. - appDidInit:sender
  122. {
  123.     NXRect aRect = {0.0, 0.0, 0.0, 0.0};
  124.     
  125.     [appender appendToText:
  126.     [stringSet valueForStringKey:"Received appDidInit:"]];
  127.     
  128.     //gets the image for compositing & scales it to frame
  129.     myPic = [NXImage findImageNamed:[stringSet valueForStringKey:"Logo"]];
  130.     [myPic setScalable:YES];
  131.     [myPic setSize:&frame.size];
  132.     
  133.     //sets up the subViewFramer as our superview
  134.     newSuperView = [SubViewFramer alloc];
  135.     [newSuperView initFrame:&aRect];
  136.     [window setContentView:newSuperView];
  137.     [newSuperView addSubview:self];
  138.     [newSuperView display];
  139.         
  140.     [self setTrackingRect];
  141.     [window makeKeyAndOrderFront:self];
  142.     return self;
  143. }
  144.  
  145. //allows us to become first responder
  146. - (BOOL)acceptsFirstResponder
  147. {
  148.     [appender appendToText:
  149.     [stringSet valueForStringKey:
  150.                   "Received acceptsFirstResponder -- returning YES"]];
  151.     return YES;
  152. }
  153.  
  154. //allows us to use the mouse click that activates the window
  155. - (BOOL)acceptsFirstMouse
  156. {
  157.     [appender appendToText:
  158.     [stringSet valueForStringKey:
  159.                      "Received acceptsFirstMouse -- returning YES"]];
  160.     return YES;
  161. }
  162.  
  163. //draws the view and then calls the superview for framing
  164. - drawSelf:(NXRect *)r :(int)c
  165. {
  166.     NXPoint zero = {0.0, 0.0};
  167.     
  168.     [self updateVitals];
  169.     if([compositeIndicator state]){
  170.         [myPic composite:NX_COPY toPoint:&zero];
  171.     }
  172.     if([drawGridIndicator state]){
  173.         PSselectfont(NXCopyStringBuffer(
  174.         [stringSet valueForStringKey:"Times-Italic"]), 24.0);
  175.         PSsetgray(NX_WHITE);
  176.         RNdrawgrid();
  177.     }
  178.     
  179.     if (NXDrawingStatus != NX_DRAWING) {
  180.         //make sure we send the defs to the printer or pasteboard
  181.         somedefs();
  182.     }
  183.     
  184.     //draws the views basic contents
  185.     somePS();
  186.     
  187.     //displays the bounds rect
  188.     PSsetgray(NX_BLACK);
  189.     sprintf(buffer, NXCopyStringBuffer(
  190.     [stringSet valueForStringKey:"bounds.origin.x %.1f,.y %.1f"]),
  191.     bounds.origin.x, bounds.origin.y);
  192.     PSmoveto(bounds.origin.x, (bounds.origin.y - 18));
  193.     PSselectfont(NXCopyStringBuffer(
  194.     [stringSet valueForStringKey:"Times-Italic"]), 24.0);
  195.     PSshow(buffer);
  196.     NXFrameRect(&bounds);
  197.     
  198.     //calls the subViewFramer, our superview
  199.     [newSuperView frameMe:self];
  200.     
  201.     return self;
  202. }
  203.  
  204. //indicates the mouse down with a beep
  205. - mouseDown:(NXEvent *)e
  206. {
  207.     sprintf(buffer, NXCopyStringBuffer(
  208.         [stringSet valueForStringKey:
  209.         "Received mouseDown: -- window coordinates %.f, %.f"]),
  210.         e->location.x, e->location.y);  
  211.     [appender appendToText:buffer];
  212.     NXBeep();
  213.     return self;
  214. }
  215.  
  216. //indicates at the location of the mouse at mouse up using instance drawing
  217. - mouseUp:(NXEvent *)e
  218. {
  219.     NXPoint mousePoint = e->location;
  220.  
  221.     sprintf(buffer, NXCopyStringBuffer(
  222.         [stringSet valueForStringKey:
  223.         "Received mouseUp: -- window coordinates %.f, %.f"]),
  224.             e->location.x, e->location.y);
  225.     [appender appendToText:buffer];
  226.     [self convertPointFromSuperview:&mousePoint];
  227.     [self lockFocus];
  228.     PSselectfont(NXCopyStringBuffer(
  229.     [stringSet valueForStringKey:"Times-Italic"]), 24.0);
  230.     PSsetgray(NX_DKGRAY);
  231.     PSnewinstance();
  232.     PSsetinstance(YES);
  233.         PSmoveto(mousePoint.x, mousePoint.y);
  234.         PSsetgray(0.0);
  235.         PSrectfill(mousePoint.x-2.0,
  236.         mousePoint.y-2.0, 4.0, 4.0);
  237.         PSmoveto(mousePoint.x+4.0, mousePoint.y+4.0);
  238.         sprintf(buffer, NXCopyStringBuffer(
  239.         [stringSet valueForStringKey:"%.f, %.f"]),
  240.         mousePoint.x, mousePoint.y);
  241.         PSshow(buffer);
  242.     PSsetinstance(NO);
  243.     [self unlockFocus];
  244.     return self;
  245. }
  246.  
  247. //tracks the mouse entering the view
  248. - mouseEntered:(NXEvent *)e
  249. {
  250.     sprintf(buffer, NXCopyStringBuffer(
  251.         [stringSet valueForStringKey:
  252.         "Received mouseEntered: -- window coordinates %.f, %.f"]),
  253.         e->location.x, e->location.y);
  254.     [appender appendToText:buffer];
  255.     return self;
  256. }
  257.  
  258. //tracks the mouse leaving the view
  259. - mouseExited:(NXEvent *)e
  260. {
  261.     sprintf(buffer, NXCopyStringBuffer(
  262.         [stringSet valueForStringKey:
  263.         "Received mouseExited: -- window coordinates %.f, %.f"]),
  264.         e->location.x, e->location.y);
  265.     [appender appendToText:buffer];
  266.     return self;
  267. }
  268.  
  269. //gets keyUps
  270. - keyUp:(NXEvent *)e
  271. {
  272.     [appender appendToText:[stringSet valueForStringKey:"Received keyUp:"]];
  273.     return self;
  274. }
  275.  
  276. //gets keyDowns
  277. - keyDown:(NXEvent *)e
  278. {
  279.     sprintf(buffer, NXCopyStringBuffer(
  280.         [stringSet valueForStringKey:"Received keyDown: -- char '%c'"]),
  281.         e->data.key.charCode);
  282.     [appender appendToText:buffer];
  283.     return self;
  284. }
  285.  
  286. //checks to see if the mouse down was in this view
  287. - hitTest:(NXPoint *)aPt
  288. {
  289.     [appender appendToText:
  290.             [stringSet valueForStringKey:"Received hitTest:"]];
  291.     if ([super hitTest:aPt]){
  292.     [appender appendToText:
  293.         [stringSet valueForStringKey:" -- YES inside this view"]];
  294.     return self;
  295.     }
  296.     [appender appendToText:
  297.         [stringSet valueForStringKey:" -- NO not inside this view"]];
  298.     return  nil;
  299. }
  300.  
  301. //updates the statistics matrix
  302. - updateVitals
  303. {
  304.     [[[[[[[[[[vitalMatrix setFloatValue:bounds.origin.x at:0]
  305.     setFloatValue:bounds.origin.y at:1]
  306.     setFloatValue:bounds.size.width at:2]
  307.     setFloatValue:bounds.size.height at:3]
  308.     setFloatValue:[self boundsAngle] at:4]
  309.     setFloatValue:frame.origin.x at:5]
  310.     setFloatValue:frame.origin.y at:6]
  311.     setFloatValue:frame.size.width at:7]
  312.     setFloatValue:frame.size.height at:8]
  313.     setFloatValue:[self frameAngle] at:9];
  314.     return self;
  315. }
  316.  
  317. //for communication with the transparent superview
  318. - (BOOL)wantsTransparency
  319. {
  320.     return [transparencyIndicator state];
  321. }
  322.  
  323. - setTrackingRect
  324. {
  325.     NXRect aRect;
  326.     
  327.     //registers the tracking rect
  328.     aRect = frame;
  329.     [superview convertRect:&aRect toView:nil];
  330.     [appender appendToText:
  331.     [stringSet valueForStringKey:"Sending setTrackingRect:... to window"]];
  332.     [window setTrackingRect:&aRect
  333.         inside:NO
  334.         owner:self
  335.         tag:23
  336.         left:NO
  337.         right:NO];
  338.     return self;
  339. }
  340.  
  341. - infoPanel:sender
  342. {
  343.     if(infoPanel == nil){
  344.     [NXApp loadNibSection:"Info.nib" owner:self];
  345.     }
  346.     [infoPanel orderFront:sender];
  347.     return self;
  348. }
  349.  
  350. @end
  351.