home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / GridView.m < prev    next >
Text File  |  1996-04-11  |  3KB  |  144 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. - (void)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.     [NSApp runModalForWindow:[self window]];
  30.     [[self window] orderOut:self]; 
  31. }
  32.  
  33. - (void)drawGrid:(int)grid
  34. {
  35.     float x, y, max, increment;
  36.     NSRect bounds = [self bounds];
  37.  
  38.     increment = (float)grid;
  39.     max = bounds.origin.y + bounds.size.height;
  40.     for (y = bounds.origin.y; y < max; y += increment) {
  41.     PSmoveto(0.0, y);
  42.     PSlineto(bounds.size.width, y);
  43.     }
  44.     max = bounds.origin.x + bounds.size.width;
  45.     for (x = bounds.origin.x; x < max; x += increment) {
  46.     PSmoveto(x, 0.0);
  47.     PSlineto(x, bounds.size.height);
  48.     }
  49.     PSstroke(); 
  50. }
  51.  
  52. - (void)drawRect:(NSRect)rect
  53. {
  54.     int grid;
  55.     float gray;
  56.  
  57.     grid = [spacing intValue];
  58.     grid = MAX(grid, 0.0);
  59.     PSsetgray(NSWhite);
  60.     NSRectFill(rect);
  61.     if (grid >= 4) {
  62.     gray = [grayField floatValue];
  63.     gray = MIN(gray, 1.0);
  64.     gray = MAX(gray, 0.0);
  65.     PSsetgray(gray);
  66.     PSsetlinewidth(0.0);
  67.     [self drawGrid:grid];
  68.     }
  69.     PSsetgray(NSBlack);
  70.     NSFrameRect([self bounds]);
  71. }
  72.  
  73. - (void)mouseDown:(NSEvent *)event 
  74. {
  75.     NSPoint p, start;
  76.     int grid, gridCount;
  77.  
  78.     start = [event locationInWindow];
  79.     start = [self convertPoint:start fromView:nil];
  80.     grid = MAX([spacing intValue], 1.0);
  81.     gridCount = (int)MAX(start.x, start.y) / grid;
  82.     gridCount = MAX(gridCount, 1.0);
  83.  
  84.     event = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask];
  85.     while ([event type] != NSLeftMouseUp) {
  86.     p = [event locationInWindow];
  87.     p = [self convertPoint:p fromView:nil];
  88.     grid = (int)MAX(p.x, p.y) / gridCount;
  89.     grid = MAX(grid, 1.0);
  90.     if (grid != [spacing intValue]) {
  91.         [form abortEditing];
  92.         [spacing setIntValue:grid];
  93.         [self display];
  94.     }
  95.     event = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask];
  96.     }
  97. }
  98.  
  99. /* Target/Action methods */
  100.  
  101. - (void)show:sender
  102. {
  103.     [NSApp stopModal];
  104.     [graphicView setGridSpacing:[spacing intValue] andGray:[grayField floatValue]];
  105.     [graphicView setGridVisible:YES]; 
  106. }
  107.  
  108. - (void)off:sender
  109. {
  110.     [NSApp stopModal];
  111.     [graphicView setGridSpacing:1]; 
  112. }
  113.  
  114. - (void)cancel:(id)sender
  115. {
  116.     [NSApp stopModal];
  117. }
  118.  
  119. - (void)changeSpacing:sender
  120. {
  121.     [self setNeedsDisplay:YES]; 
  122. }
  123.  
  124. - (void)changeGray:sender
  125. {
  126.     if (sender == graySlider) {
  127.     [form abortEditing];
  128.     [grayField setFloatValue:[sender floatValue]];
  129.     } else {
  130.     [graySlider setFloatValue:[sender floatValue]];
  131.     }
  132.     [self setNeedsDisplay:YES]; 
  133. }
  134.  
  135. /* IB outlet-setting methods */
  136.  
  137. - setAppIconButton:anObject
  138. {
  139.     [anObject setImage:[NSImage imageNamed:@"appIcon"]];
  140.     return self;
  141. }
  142.  
  143. @end
  144.