home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Palettes / MiscLogSliderPalette / MiscLogSlider.subproj / MiscLogSliderCell.m < prev    next >
Encoding:
Text File  |  1994-03-27  |  2.6 KB  |  120 lines

  1. //
  2. //    MiscLogSliderCell.h -- a SliderCell with logarithmic transfer function
  3. //        Written by Don Yacktman, Copyright (c) 1994 by Don Yacktman.
  4. //                Version 1.0  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13.  
  14. #import "MiscLogSliderCell.h"
  15. #import <math.h>
  16. #import <sys/param.h>
  17.  
  18. @interface MiscLogSliderCell(Private)
  19.  
  20. - _setLB;
  21.  
  22. @end
  23.  
  24.  
  25. @implementation MiscLogSliderCell
  26.  
  27. - init
  28. {
  29.     id ret = [super init];
  30.     base = 10;
  31.     realMax = 1000;
  32.     realMin = 1;
  33.     [self _setLB];
  34.     [self setDoubleValue:1];
  35.     return ret;
  36. }
  37.  
  38. - _setLB
  39. {
  40.     _lbx = log(realMax) / log(base);
  41.     _lbn = log(realMin) / log(base);
  42.     return self;
  43. }
  44.  
  45. - (double)base { return base; }
  46. - (double)maxValue  { return realMax; }
  47. - (double)minValue  { return realMin; }
  48.  
  49. // ***** need to implement range checking to make sure we are using sane numbers here!
  50. - setBase:(double)newBase
  51. {
  52.     base = MAX(2, newBase);
  53.     [self _setLB];
  54.     return self;
  55. }
  56.  
  57. - setMaxValue:(double)aDouble
  58. {
  59.     realMax = MAX(1, aDouble);
  60.     [self _setLB];
  61.     return self;
  62. }
  63.  
  64. - setMinValue:(double)aDouble
  65. {
  66.     realMin = MAX(1, aDouble);
  67.     [self _setLB];
  68.     return self;
  69. }
  70.  
  71. // -doubleValue returns the right value and does all the work; the other
  72. // methods generate their values from the return from -doubleValue.
  73.  
  74. // ***** These need to be implemented properly.
  75. - (const char *)stringValue { return [super stringValue]; }
  76. - setStringValue:(const char *)aString { return [super setStringValue:aString]; }
  77.  
  78. // These are finished:
  79. - (int)intValue { return [self doubleValue]; }
  80. - setIntValue:(int)anInt { return [self setDoubleValue:anInt]; }
  81. - (float)floatValue { return [self doubleValue]; }
  82. - setFloatValue:(float)aFloat { return [self setDoubleValue:aFloat]; }
  83.  
  84. - (double)doubleValue
  85. {
  86.     return pow(base, (_lbn + [super doubleValue] * (_lbx - _lbn)));
  87. }
  88.  
  89. - setDoubleValue:(double)aDouble
  90. {
  91.     return [super setDoubleValue:
  92.             (((log(aDouble) / log(base)) - _lbn) / (_lbx - _lbn))];
  93. }
  94.  
  95. - read:(NXTypedStream *)stream
  96. {
  97.     [super read:stream];
  98.     NXReadTypes(stream, "ddd", &base, &realMax, &realMin);
  99.     [self _setLB];
  100.     return self;
  101. }
  102.  
  103. - write:(NXTypedStream *)stream
  104. {
  105.     [super write:stream];
  106.     NXWriteTypes(stream, "ddd", &base, &realMax, &realMin);
  107.     return self;
  108. }
  109.  
  110. - awake
  111. { // this is all just "to be sure"
  112.     if (base < 2) base = 1;
  113.     if (realMax < 1) realMax = 1;
  114.     if (realMin < 1) realMin = 1;
  115.     [self _setLB];
  116.     return [super awake];
  117. }
  118.  
  119. @end
  120.