home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / DBkit / BooleanFormatter / BooleanFormatter.m < prev    next >
Encoding:
Text File  |  1993-01-19  |  5.0 KB  |  194 lines

  1. /*
  2. *    BooleanFormatter.h
  3. *              Original code: William Shipley
  4. *            Revised for string display: Mai Nguyen
  5. */
  6.  
  7. #import "BooleanFormatter.h"
  8.  
  9. #define _delWill1    @selector(formatterWillChangeValueFor:at:sender:)
  10. #define _delWill2    @selector(formatterWillChangeValueFor:at:to:sender:)
  11. #define _delDid        @selector(formatterDidChangeValueFor:at:to:sender:)
  12.  
  13. #define _delWillStatic1    @selector(formatterWillChangeValueFor::sender:)
  14. #define _delWillStatic2    @selector(formatterWillChangeValueFor::to:sender:)
  15. #define _delDidStatic    @selector(formatterDidChangeValueFor::to:sender:)
  16.  
  17. #define TEXT_OFFSET    3.0
  18.  
  19. @implementation BooleanFormatter
  20.  
  21. - init
  22. {
  23.     font = [Font newFont:"Helvetica" size:12.0];
  24.     newValue = [[DBValue allocFromZone:[self zone]] init];
  25.     return[super init];
  26. }
  27.  
  28. - free
  29. {
  30.     [newValue free];
  31.     return[super free];
  32. }
  33.  
  34.  
  35. - drawFieldAt:(unsigned int)row :(unsigned int)column
  36.     inside:(NXRect *)frame inView:view
  37.     withAttributes:(id <DBTableVectors >)rowAttrs
  38.      :(id <DBTableVectors >)columnAttrs
  39.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos;
  40. {
  41.  
  42.     NXPoint             toPoint;
  43.     NXSize              string;
  44.     char                buf[5];
  45.     
  46.     [self getValueAt:row :column withAttributes:rowAttrs :columnAttrs
  47.      usePositions:useRowPos :useColumnPos];
  48.  
  49.  /*
  50.   * set proper string
  51.   */
  52.     if ([value isNull] || ([value intValue] == 0))
  53.         sprintf(buf, "%s", "NO");
  54.     else
  55.         sprintf(buf,"%s", "YES");
  56.     
  57.  /*
  58.   * Set the size on the font.
  59.   */
  60.       [font set];    
  61.     string.height = [font pointSize];
  62.     string.width = [font getWidthOf:buf];
  63.  
  64.     
  65.     toPoint.y = frame->origin.y + frame->size.height -
  66.       (frame->size.height - string.height) / 2;
  67.     toPoint.x = frame->origin.x +
  68.       (frame->size.width - string.width) / 2 - TEXT_OFFSET;
  69.     
  70.        PSmoveto(toPoint.x, toPoint.y);
  71.     PSsetgray(NX_BLACK);
  72.     PSshow(buf);
  73.     return self;
  74. }
  75.  
  76. - mouseDown:(NXEvent *)theEvent at:(int)row :(int)column
  77.     inside:(NXRect *)frame inView:(View *) view
  78.     withAttributes:(id <DBTableVectors >)rowAttrs
  79.      :(id <DBTableVectors >)columnAttrs
  80.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  81. {
  82.     if (theEvent->data.mouse.click < 2)
  83.     return nil;
  84.  
  85.      /* get the current value */
  86.  
  87.     [self getValueAt:row :column withAttributes:rowAttrs :columnAttrs
  88.      usePositions:useRowPos :useColumnPos];
  89.  
  90.     /* Toggle value */
  91.     if ([value isNull] || ([value intValue] == 0))
  92.         [newValue setIntValue:1];
  93.     else
  94.         [newValue setIntValue:0];
  95.  
  96.     [self setValueAt:row :column
  97.      withAttributes:rowAttrs :columnAttrs
  98.      usePositions:useRowPos :useColumnPos];
  99.    
  100.  
  101.  /* Redraw the modified field */
  102.     [self drawFieldAt:row :column
  103.      inside:frame inView:view
  104.      withAttributes:rowAttrs :columnAttrs
  105.      usePositions:useRowPos :useColumnPos];
  106.     [[view window] flushWindow];
  107.  
  108.     return self;
  109. }
  110.  
  111.  
  112. - setValueAt:(int)row :(int)column
  113.     withAttributes:(id <DBTableVectors >)rowAttrs
  114.      :(id <DBTableVectors >)columnAttrs
  115.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  116. {
  117.     BOOL                isChangeOk = YES;
  118.  
  119.  /* Ask delegate if we can change */
  120.     if (useRowPos && !useColumnPos && delWill2)
  121.     isChangeOk = [delegate formatterWillChangeValueFor:
  122.               [columnAttrs identifier]
  123.               at:row to:newValue sender:self];
  124.     else if (useColumnPos && !useRowPos && delWill2)
  125.     isChangeOk = [delegate formatterWillChangeValueFor:
  126.               [rowAttrs identifier]
  127.               at:column to:newValue sender:self];
  128.     else if (delWillStatic2)
  129.     isChangeOk = [delegate formatterWillChangeValueFor:
  130.               [rowAttrs identifier] :[columnAttrs identifier]
  131.               to:newValue sender:self];
  132.  
  133.     if (!isChangeOk)
  134.     return nil;
  135.  
  136.  /* Tell dataSource our newValue */
  137.     if (useRowPos && !useColumnPos)
  138.     [dataSource setValueFor:[columnAttrs identifier] at:row
  139.      from:newValue];
  140.     else if (useColumnPos && !useRowPos)
  141.     [dataSource setValueFor:[rowAttrs identifier] at:column
  142.      from:newValue];
  143.     else
  144.     [dataSource setValueFor:[rowAttrs identifier]
  145.       :[columnAttrs identifier] from:newValue];
  146.  
  147.  /* Tell delegate we changed */
  148.     if (useRowPos && !useColumnPos && delDid)
  149.     [delegate formatterDidChangeValueFor:[columnAttrs identifier]
  150.      at:row to:newValue sender:self];
  151.     else if (!useRowPos && useColumnPos && delDid)
  152.     [delegate formatterDidChangeValueFor:[rowAttrs identifier]
  153.      at:column to:newValue sender:self];
  154.     else if (delDidStatic)
  155.     [delegate formatterDidChangeValueFor:[rowAttrs identifier]
  156.       :[columnAttrs identifier] to:newValue sender:self];
  157.  
  158.     return self;
  159. }
  160.  
  161. - _setDelegateFlags
  162. {
  163.     delWill1 = delWill2 = delDid = NO;
  164.     delWillStatic1 = delWillStatic2 = delDidStatic = NO;
  165.  
  166.     if ([delegate respondsTo:_delWill1])
  167.     delWill1 = YES;
  168.     if ([delegate respondsTo:_delWill2])
  169.     delWill2 = YES;
  170.     if ([delegate respondsTo:_delDid])
  171.     delDid = YES;
  172.     if ([delegate respondsTo:_delWillStatic1])
  173.     delWillStatic1 = YES;
  174.     if ([delegate respondsTo:_delWillStatic2])
  175.     delWillStatic2 = YES;
  176.     if ([delegate respondsTo:_delDidStatic])
  177.     delDidStatic = YES;
  178.     return self;
  179. }
  180.  
  181. - setDelegate:newDelegate
  182. {
  183.     if (newDelegate == delegate)
  184.     return self;
  185.     else
  186.     [super setDelegate:newDelegate];
  187.  
  188.     [self _setDelegateFlags];
  189.  
  190.     return self;
  191. }
  192.  
  193. @end
  194.