home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / SliderDualActing / SliderDualActing.m < prev    next >
Text File  |  1993-01-19  |  6KB  |  238 lines

  1. /*************************************************************************
  2. *        Copyright (c) 1989,1990 Stone Design Corp.  All rights reserved. 
  3. *        "Do what you will, you will anyway...."-- andrew stone 4-29-90
  4. ***************************************************************************/
  5.  
  6. //            SliderDualActing:Slider:Control:View:Responder:Object
  7.  
  8. #import "SliderDualActing.h"
  9. #import "SliderCellFine.h"
  10.  
  11. /********************************************************
  12.      
  13.  //  initialization routines called by clients:
  14.  
  15.  //  This allows dual messages to be sent:
  16. - setUpTarget:anObject action:(SEL)anAction;
  17. - setUpTarget:anObject action:(SEL)anAction isContinuous:(BOOL)flag;
  18.  
  19.  // This lets you configure how hi and lo the slider can go.
  20. - setMax:(double)max allowHigher:(BOOL)hi min:(double)min allowLower:(BOOL)lo;
  21.  
  22.  // call this after you have set the max and min: it sets your start value:
  23. - setAltStep:(double)step whole:(BOOL)flag default:(double)value;
  24.  
  25.  // Set the textFields's doubleing format this way:
  26. - setFormat:(BOOL)flag left:(int)l right:(int)r;
  27.  
  28.  // set your undo target with:
  29. - setUndoTarget:target position:(int)pos;
  30.  
  31.  // Example :
  32.  // (Works best after appDidInit message, textPal is guaranteed to exist):
  33.      
  34.     [[[[dualSlider setUpTarget:self action:@selector(display)]
  35.             setMax:360. allowHigher:NO min:-360. allowLower:NO]
  36.             setAltStep:1. whole:YES default:0]
  37.             setFormat:NO left:1 right:3]; 
  38.  
  39.  // If the client object wants to handle undo [by default SliderDA does]:
  40.  
  41.     [anObject setUndoTarget:(id)target  tag:(int)clientTag];
  42.     
  43.     example:
  44.     [anObject setUndoTarget:textArtView  tag:GRAY_POS];
  45.  
  46. ********************************************************/
  47.  
  48. @implementation SliderDualActing
  49.  
  50. + initialize    // who we are and where we stand in the struggle
  51. {
  52.     [SliderDualActing setCellClass:[SliderCellFine class]];
  53.     return self;
  54. }
  55.  
  56. + newFrame:(const NXRect *)frameRect
  57. {
  58.    self = [super newFrame:frameRect];
  59.    undoTarget = self;                    // override this if needed
  60.    return self;
  61. }
  62.  
  63.  // IB sender methods:
  64.  
  65. - sendTextAction:sender     /// method for textField
  66. {
  67.     [undoTarget setLastThing:undoPosition];   // param not used
  68.     [self setFloatValue:[sender floatValue]];
  69.     [self sendAction:upAction to:upTarget];
  70.     return self;
  71. }
  72. - incrementDecrement:sender    // method for buttons [let tag=position in IB]
  73. {
  74.     id retVal;
  75.     [self _sendSetLastThing];         ///Mon Nov 20 20:37:19 MST 1989 Undo
  76.     retVal =  [sender selectedTag]==0? [cell decrement]: [cell increment];    
  77.     [self display];
  78.     return retVal?[self sendAction:upAction to:upTarget]:nil;
  79. }
  80.   
  81.  
  82.  // overridden superclass methods:
  83.  
  84. - mouseDown:(NXEvent *)e
  85. {
  86.     // here we must trap oldvalue!!
  87.     [undoTarget setLastThing:undoPosition];   // param not used
  88.     [super mouseDown:e];
  89.     [self sendAction:upAction to:upTarget];    // send our up/text  method
  90.     return self;
  91. }
  92. - setFloatValue:(float)aFloat
  93. {
  94.     double val = [cell checkValue:aFloat];
  95.     [cell setFloatValue:val];
  96.     [textPal setFloatValue:val];
  97.     [self display];
  98.     return self;
  99. }
  100. - setIntValue:(int)anInt
  101. {
  102.     double val = [cell checkValue:(double)anInt];
  103.     [cell setFloatValue:val];
  104.     [textPal setFloatValue:val];
  105.     [self display];
  106.     return self;
  107. }
  108.  
  109. // initialization routines called by clients:
  110. - setUpTarget:anObject action:(SEL)anAction
  111. {
  112.     upTarget = anObject;
  113.     upAction = anAction;
  114.     [cell _setAlwaysSendUpAction:NO];
  115.     return self;
  116. }
  117. - setUpTarget:anObject action:(SEL)anAction isContinuous:(BOOL)flag
  118. {
  119.     upTarget = anObject;
  120.     upAction = anAction;
  121.     [cell _setAlwaysSendUpAction:flag];
  122.     return self;
  123. }
  124.     
  125. - setMax:(double)max allowHigher:(BOOL)hi min:(double)min allowLower:(BOOL)lo
  126. {
  127.     [cell setMax:max allowHigher:hi min:min allowLower:lo];
  128.     return self;
  129. }
  130.  
  131. - setAltStep:(double)step whole:(BOOL)flag default:(double) val
  132. {
  133.     lastValue = val; [self setFloatValue:val];              // init values
  134.     [cell setAltStep:step whole:flag default:val];   // pass it to cell
  135.     return self;
  136. }
  137.  
  138. - setFormat:(BOOL)flag left:(unsigned)l right:(unsigned)r
  139. {
  140.     [textPal setFloatingPointFormat:flag left:l right:r];
  141.     return self;
  142. }
  143.  
  144.  
  145.  // Historic ones:  
  146.  
  147. - setDefault:(double) def
  148. {
  149.     [cell setDefault:def];
  150.     return self;
  151. }
  152. - setUpTarget:anObject
  153. {
  154.     upTarget = anObject;
  155.     return self;
  156. }
  157. - setUpAction:(SEL)anAction
  158. {
  159.     upAction = anAction;
  160.        return self;
  161. }
  162.  
  163.  // archive methods:   // not tested C.E.
  164. - read:(NXTypedStream *)stream
  165. {
  166.     [super read:stream];
  167.     NXReadTypes(stream, "@@@ii", &textPal,&upTarget,&undoTarget,
  168.                                 &upAction,&undoPosition);
  169.     [cell setTextPal:textPal];
  170.     return self;
  171. }
  172.  
  173. - write:(NXTypedStream *)stream
  174. {
  175.     [super write:stream];
  176.     NXWriteTypes(stream, "@@@ii", &textPal,&upTarget,&undoTarget,
  177.                                 &upAction,&undoPosition);
  178.     [cell setTextPal:textPal];
  179.     return self;
  180. }
  181.  
  182. /// UNDO methods
  183.  
  184. - setUndoTarget:targ tag:(int)pos
  185. {
  186.    undoTarget = targ;
  187.    undoPosition = pos;
  188.    return self;
  189. }
  190.  
  191. - setLastThing:(int)thing
  192. {  
  193.  // uncomment next line if you implement an undo object
  194.  //  [[NXApp undoObj] setLastClass:self];  // We cache the old value.
  195.    lastValue =  [cell floatValue];
  196.    return self;
  197. }
  198.  
  199. - undo
  200. {
  201.    float temp = [cell floatValue];
  202.    [self setFloatValue:lastValue];
  203.    lastValue=temp;
  204.    // Here you may want to have some visual updating of a view:
  205.    // [[NXApp mainWindow] display]; 
  206.    return self;
  207. }
  208.  
  209. - _sendSetLastThing
  210. {
  211.    return [undoTarget setLastThing:undoPosition];
  212. }
  213. - _sendIt        // called by cell to continously send up message
  214. {
  215.     return  [self sendAction:upAction to:upTarget];
  216. }
  217.  
  218.  // Misc and private:
  219.  
  220. -(BOOL)isDecimal
  221.   return [cell isDecimal];
  222. }
  223.  
  224. - textPal
  225. {
  226.   return textPal;
  227. }
  228. - setTextPal:anObject
  229. {
  230.   textPal = anObject;
  231.   [cell setTextPal:textPal];        // let cell cache it as well
  232.   return self;
  233. }
  234.  
  235. @end
  236.  
  237.