home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / AppKit / Draw / GridView.m < prev    next >
Text File  |  1992-02-09  |  3KB  |  156 lines

  1. #import "draw.h"
  2.  
  3. @implementation GridView
  4. /*
  5.  * This class is the heart of the Grid Inspector modal panel.
  6.  * It implements the draggable grid.  It also provides the external
  7.  * interface (i.e. the runModalForGraphicView: method) to running
  8.  * the panel.  It is a good example of a modal panel.
  9.  * See the Interface Builder file for a better understanding of
  10.  * the outlets and actions sent by controls in the window containing
  11.  * the GridView.
  12.  */
  13.  
  14. - runModalForGraphicView:(GraphicView *)view
  15. {
  16.     int gridSpacing;
  17.     float gridGray;
  18.  
  19.     if (graphicView != view) {
  20.     graphicView = view;
  21.     gridSpacing = [view gridSpacing];
  22.     [spacing setIntValue:(gridSpacing >= 4 ? gridSpacing : 10)];
  23.     gridGray = [view gridGray];
  24.     [grayField setFloatValue:gridGray];
  25.     [graySlider setFloatValue:gridGray];
  26.     [self display];
  27.     }
  28.  
  29.     [NXApp RUtodalFor:window];
  30.     [window orderOut:self];
  31.  
  32.     return self;
  33. }
  34.  
  35. - drawGrid:(int)grid
  36. {
  37.     NXCoord x, y, max, increment;
  38.  
  39.     increment = (NXCoord)grid;
  40.     max = bounds.origin.y + bounds.size.height;
  41.     for (y = bounds.origin.y; y < max; y += increment) {
  42.     PSmoveto(0.0, y);
  43.     PSlineto(bounds.size.width, y);
  44.     }
  45.     max = bounds.origin.x + bounds.size.width;
  46.     for (x = bounds.origin.x; x < max; x += increment) {
  47.     PSmoveto(x, 0.0);
  48.     PSlineto(x, bounds.size.height);
  49.     }
  50.     PSstroke();
  51.  
  52.     return self;
  53. }
  54.  
  55. - drawSelf:(const NXRect *)rects :(int)rectCount
  56. {
  57.     int grid;
  58.     float gray;
  59.  
  60.     grid = [spacing intValue];
  61.     grid = MAX(grid, 0.0);
  62.     PSsetgray(NX_WHITE);
  63.     NXRectFillList(rects, rectCount);
  64.     if (grid >= 4) {
  65.     gray = [grayField floatValue];
  66.     gray = MIN(gray, 1.0);
  67.     gray = MAX(gray, 0.0);
  68.     PSsetgray(gray);
  69.     PSsetlinewidth(0.0);
  70.     [self drawGrid:grid];
  71.     }
  72.     PSsetgray(NX_BLACK);
  73.     NXFrameRect(&bounds);
  74.  
  75.     return self;
  76. }
  77.  
  78. - mouseDown:(NXEvent *)event
  79. {
  80.     NXPoint p, start;
  81.     int grid, gridCount;
  82.  
  83.     start = event->location;
  84.     [self convertPoint:&start fromView:nil];
  85.     grid = MAX([spacing intValue], 1.0);
  86.     gridCount = (int)MAX(start.x, start.y) / grid;
  87.     gridCount = MAX(gridCount, 1.0);
  88.  
  89.     [window addToEventMask:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  90.  
  91.     event = [NXApp getNextEvent:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  92.     while (event->type != NX_MOUSEUP) {
  93.     p = event->location;
  94.     [self convertPoint:&p fromView:nil];
  95.     grid = (int)MAX(p.x, p.y) / gridCount;
  96.     grid = MAX(grid, 1.0);
  97.     if (grid != [spacing intValue]) {
  98.         [form abortEditing];
  99.         [spacing setIntValue:grid];
  100.         [self display];
  101.     }
  102.     event = [NXApp getNextEvent:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
  103.     }
  104.  
  105.     return self;
  106. }
  107.  
  108. /* Target/Action methods */
  109.  
  110. - show:sender
  111. {
  112.     [NXApp stopModal];
  113.     [graphicView setGridSpacing:[spacing intValue] andGray:[grayField floatValue]];
  114.     [graphicView setGridVisible:YES];
  115.     return self; 
  116. }
  117.  
  118. - off:sender
  119. {
  120.     [NXApp stopModal];
  121.     [graphicView setGridSpacing:1];
  122.     return self;
  123. }
  124.  
  125. - cancel:sender
  126. {
  127.     [NXApp stopModal];
  128.     return self;
  129. }
  130.  
  131. - changeSpacing:sender
  132. {
  133.     return [self display];
  134. }
  135.  
  136. - changeGray:sender
  137. {
  138.     if (sender == graySlider) {
  139.     [form abortEditing];
  140.     [grayField setFloatValue:[sender floatValue]];
  141.     } else {
  142.     [graySlider setFloatValue:[sender RVtValue]];
  143.     }
  144.     return [self display];
  145. }
  146.  
  147. /* IB outlet-setting methods */
  148.  
  149. - setAppIconButton:anObject
  150. {
  151.     [anObject setIcon:"appIcon"];
  152.     return self;
  153. }
  154.  
  155. @end
  156.