home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / Inspector.m < prev    next >
Text File  |  1996-04-18  |  5KB  |  168 lines

  1. #import "draw.h"
  2.  
  3. @implementation Inspector
  4.  
  5. - (void)reloadGraphic:(Graphic *)graphic
  6. /*
  7.  * Loads up the size fields if they have changed since last time
  8.  * we loaded up the panel with this graphic.  This is used since we
  9.  * know that none of the things controlled by the InspectorPanel
  10.  * except the size or the fill color can change from event to event
  11.  * (we should probably not make that assumption, but it makes the
  12.  * updating of this panel go much faster and since it has to happen
  13.  * on every event, it seems a worthwhile optimization).
  14.  */
  15. {
  16.     NSRect bounds;
  17.  
  18.     if (graphic) {
  19.         bounds = [graphic bounds];
  20.         if (lastSize.width != bounds.size.width) {
  21.             [width setFloatValue:bounds.size.width];
  22.             lastSize.width = bounds.size.width;
  23.         }
  24.         if (lastSize.height != bounds.size.height) {
  25.             [height setFloatValue:bounds.size.height];
  26.             lastSize.height = bounds.size.height;
  27.         }
  28.         if ([graphic fill] != [filled indexOfSelectedItem]) [filled selectItemAtIndex:[graphic fill]];
  29.         if (graphic && ![[fillColor color] isEqual:[graphic fillColor]]) [fillColor setColor:[graphic fillColor]];
  30.     }
  31. }
  32.  
  33. - (void)loadOrReloadGraphic:(Graphic *)graphic
  34. {
  35.     if (selectedGraphic == graphic) {
  36.         return [self reloadGraphic:graphic];
  37.     } else {
  38.         return [self loadGraphic:graphic];
  39.     }
  40. }
  41.  
  42. - (void)loadGraphic:(Graphic *)graphic
  43. /*
  44.  * Loads up the InspectorPanel with a new graphic's attributes.
  45.  */
  46. {
  47.     NSRect bounds;
  48.  
  49.     if ((selectedGraphic = graphic)) {
  50.         [lineWidthField setFloatValue:[graphic lineWidth]];
  51.         [lineWidthSlider setFloatValue:[graphic lineWidth]];
  52.         [lineColor setColor:[graphic lineColor]];
  53.         [fillColor setColor:[graphic fillColor]];
  54.         bounds = [graphic bounds];
  55.         [width setFloatValue:bounds.size.width];
  56.         [height setFloatValue:bounds.size.height];
  57.         lastSize = bounds.size;
  58.     [filled selectItemAtIndex:[graphic fill]];
  59.     [lineCap selectItemAtIndex:[graphic lineCap]];
  60.     [arrows selectItemAtIndex:[graphic lineArrow]];
  61.     [lineJoin selectItemAtIndex:[graphic lineJoin]];
  62.         [formEntry setIntValue:[graphic isFormEntry]];
  63.     }
  64. }
  65.  
  66. - (void)load:(GraphicView *)view
  67. /*
  68.  * If the view has only one selected graphic, then the panel is loaded with it.
  69.  */
  70. {
  71.     graphicView = view;
  72.     [self loadOrReloadGraphic:[view selectedGraphic]]; 
  73. }
  74.  
  75. - (void)initializeGraphic:(Graphic *)graphic
  76. /*
  77.  * Goes the opposite way of loadGraphic.  Gives the Graphic the attributes
  78.  * which are in the InspectorPanel.
  79.  */
  80. {
  81.     float value;
  82.     NSString *lineWidth;
  83.     NSColor * color;
  84.  
  85.     lineWidth = [lineWidthField stringValue];
  86.     if (![lineWidth isEqual:@""] && (value = [lineWidth floatValue])) [graphic setLineWidth:&value];
  87.     color = [lineColor color];
  88.     [graphic setLineColor:color];
  89.     color = [fillColor color];
  90.     [graphic setFillColor:color];
  91.     [graphic setFill:[filled indexOfSelectedItem]];
  92.     [graphic setLineCap:[lineCap indexOfSelectedItem]];
  93.     [graphic setLineArrow:[arrows indexOfSelectedItem]];
  94.     [graphic setLineJoin:[lineJoin indexOfSelectedItem]];
  95. }
  96.  
  97. - (void)preset
  98. {
  99.     [fillColor setColor:[NSColor whiteColor]];
  100.     [lineColor setColor:[NSColor blackColor]]; 
  101. }
  102.  
  103. /* Overridden from superclass */
  104.  
  105. - (void)windowDidUpdate:(NSWindow *)sender
  106. /*
  107.  * Called each time an event occurs.  Loads up the panel.
  108.  */
  109. {
  110.     [self load:[[NSApp currentDocument] view]];
  111. }
  112.  
  113. /* Target/Action methods */
  114. /* These go here to keep the Inspector self-consistent. */
  115. /* Other things just go down the responder chain. */
  116.  
  117. - (void)changeLineWidth:sender
  118. {
  119.     float linewidth;
  120.  
  121.     linewidth = [sender floatValue];
  122.     if (sender == lineWidthSlider) {
  123.     if ([[[graphicView window] currentEvent] type] == NSLeftMouseDragged) {
  124.         [[graphicView selectedGraphics] makeObjectsPerform:@selector(deselect)];
  125.     } else {
  126.         [[graphicView selectedGraphics] makeObjectsPerform:@selector(select)];
  127.     }
  128.     [lineWidthField setFloatValue:linewidth];
  129.     } else {
  130.     if ([lineWidthSlider maxValue] < linewidth) {
  131.         [lineWidthSlider setMaxValue:linewidth];
  132.     }
  133.     [lineWidthSlider setFloatValue:linewidth];
  134.     [[graphicView window] makeKeyWindow];
  135.     }
  136.     [graphicView takeLineWidthFrom:lineWidthField]; 
  137. }
  138.  
  139. - (void)changeFillColor:sender
  140. {
  141.     [graphicView takeFillColorFrom:sender];
  142.     if (![filled indexOfSelectedItem]) [filled selectItemAtIndex:2];
  143. }
  144.  
  145. - (void)changeDimensions:sender
  146. {
  147.     id change;
  148.     NSSize size;
  149.     NSWindow *window;
  150.  
  151.     size.width = [width floatValue];
  152.     size.height = [height floatValue];
  153.     change = [[DimensionsGraphicsChange alloc] initGraphicView:graphicView];
  154.     [change startChange];
  155.         [graphicView graphicsPerform:@selector(sizeTo:) with:&size];
  156.         window = [graphicView window];
  157.         [window flushWindow];
  158.         [window makeKeyWindow];
  159.     [change endChange]; 
  160. }
  161.  
  162. - (NSString *)description
  163. {
  164.     return [(NSDictionary *)[NSDictionary dictionaryWithObjectsAndKeys:selectedGraphic, @"Selected Graphic", graphicView, @"Active View", propertyListFromNSSize(lastSize), @"Size", nil] description];
  165. }
  166.  
  167. @end
  168.