CocoaSpinbox.m

Go to the documentation of this file.
00001 //
00002 //  CocoaSpinbox.m
00003 //  
00004 //
00005 //  Created by Samuel Strupp on 19.08.10.
00006 //
00007 
00008 #ifdef HAVE_CONFIG_H
00009 # include <config.h>
00010 #endif
00011 
00012 
00013 #import "CocoaSpinbox.h"
00014 
00015 #ifndef COCOA_SPINBOX_MM
00016 #define COCOA_SPINBOX_MM
00017 
00018 @implementation CocoaSpinbox
00019 
00020 @synthesize fillX;
00021 @synthesize fillY;
00022 
00023 - (id)initWithFrame:(NSRect)frame {
00024     self = [super initWithFrame:frame];
00025     if (self) {
00026                 
00027                 textfield = [[NSTextField alloc] initWithFrame:NSMakeRect(0.0, 0.0, 40.0, 27.0)];
00028                 [textfield setAlignment:NSRightTextAlignment];
00029                 [textfield setTarget:self];
00030                 [textfield setAction:@selector(getValueFromTextField:)];
00031                 [self addSubview:textfield];
00032                 
00033                 stepper = [[NSStepper alloc] initWithFrame:NSMakeRect(40.0, 0.0, 19.0, 27.0)];
00034                 [stepper setTarget:self];
00035                 [stepper setAction:@selector(getValueFromStepper:)];
00036                 [stepper setValueWraps:NO];
00037                 [self addSubview:stepper];
00038                 
00039                 fillX = NO;
00040                 fillY = NO;
00041                 
00042                 minValue = NSIntegerMin;
00043                 maxValue = NSIntegerMax;
00044                 
00045                 [stepper setMinValue:minValue];
00046                 [stepper setMaxValue:maxValue];
00047                 
00048                 c_actionPtr = nil;
00049                 c_actionData = nil;
00050                 
00051                 minWidth = 40.0;
00052     }
00053     return self;
00054 }
00055 
00056 -(void) dealloc {
00057         [stepper release];
00058         [textfield release];
00059         [super dealloc];
00060 }
00061 
00062 -(void) setC_ActionPtr:(gwenSpinBoxActionPtr)ptr Data:(void*)data {
00063         c_actionPtr = ptr;
00064         c_actionData = data;
00065 }
00066 
00067 -(void) valueDidChange {
00068         if (c_actionPtr) {
00069                 c_actionPtr(self, c_actionData);
00070         }
00071 }
00072 
00073 
00074 -(void) setFrame:(NSRect)frameRect {
00075         if (frameRect.size.height < 22.0) frameRect.size.height = 22.0;
00076         [super setFrame:frameRect];
00077         NSRect bounds = [self bounds];
00078         NSRect stepperFrame = NSMakeRect(bounds.origin.x+bounds.size.width-16.0, bounds.origin.y-3.0, 19.0, 27.0);
00079         [stepper setFrame:stepperFrame];
00080         
00081         NSRect textfieldFrame = NSMakeRect(bounds.origin.x, bounds.origin.y, bounds.size.width-16.0, 22.0);
00082         [textfield setFrame:textfieldFrame];
00083 }
00084 
00085 -(void) makeFirstResponder {
00086         if ([textfield window]) {
00087                 [[textfield window] makeFirstResponder:textfield];
00088         }
00089 }
00090 
00091 -(BOOL) isFirstResponder {
00092         if ([textfield window]) {
00093                 if ([[textfield window] firstResponder] == textfield) return YES;
00094         }
00095         return NO;
00096 }
00097 
00098 -(void) setEnabled:(BOOL)value {
00099         [textfield setEnabled:value];
00100         [stepper setEnabled:value];
00101 }
00102 
00103 -(BOOL) isEnabled {
00104         return [textfield isEnabled];
00105 }
00106 
00107 
00108 -(void) setIntegerValue:(NSInteger)new_value {
00109         if (new_value >= minValue && new_value <= maxValue) {
00110                 [textfield setIntegerValue:new_value];
00111                 [stepper setIntegerValue:new_value];
00112         }
00113 }
00114 
00115 -(NSInteger) integerValue {
00116         return [textfield integerValue];
00117 }
00118 
00119 -(void) setMinValue:(NSInteger)new_min_value {
00120         minValue = new_min_value;
00121         [stepper setMinValue:minValue];
00122         if (maxValue < minValue) maxValue = minValue;
00123         if ([self integerValue] < minValue) [self setIntegerValue:minValue];
00124 }
00125 
00126 -(NSInteger) minValue {
00127         return minValue;
00128 }
00129 
00130 -(void) setMaxValue:(NSInteger)new_max_value {
00131         maxValue = new_max_value;
00132         [stepper setMaxValue:maxValue];
00133         if (maxValue < minValue) minValue = maxValue;
00134         if ([self integerValue] > maxValue) [self setIntegerValue:maxValue];
00135 }
00136 
00137 -(NSInteger) maxValue {
00138         return maxValue;
00139 }
00140 
00141 
00142 - (void)setStringValue:(NSString *)aString {
00143         NSInteger value = 0;
00144         if (aString && [aString length] > 0) {
00145                 value = [aString integerValue];
00146         }
00147         [self setIntegerValue:value];
00148 }
00149 
00150 -(NSString*) stringValue {
00151         return [textfield stringValue];
00152 }
00153 
00154 -(void) getValueFromStepper:(id)sender {
00155         NSInteger new_value = [stepper integerValue];
00156         if (new_value >= minValue && new_value <= maxValue) {
00157                 [textfield setIntegerValue:new_value];
00158                 [self valueDidChange];
00159         }
00160 }
00161 
00162 -(void) getValueFromTextField:(id)sender {
00163         NSInteger new_value = [textfield integerValue];
00164         if (new_value < minValue) new_value = minValue;
00165         if (new_value > maxValue) new_value = maxValue;
00166         [textfield setIntegerValue:new_value];
00167         [stepper setIntegerValue:new_value];
00168         [self valueDidChange];
00169 }
00170 
00171 #pragma mark Protocol Methods
00172 
00173 
00174 - (NSSize) minSize {
00175         return NSMakeSize(minWidth, 22.0);
00176 }
00177 
00178 @end
00179 
00180 #endif