Last active
May 9, 2020 19:45
-
-
Save andreberg/361265 to your computer and use it in GitHub Desktop.
[Number Dialing Text Field Widget] Cocoa widget which combines a number-formatted text field with a up/down arrow control for "dialing" the displayed number. #cocoa #widget #gui #objective_c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CDNumberDialingTextField.m | |
// Countdown | |
// | |
// Created by Andre Berg on 20.03.10. | |
// Copyright 2010 Berg Media. All rights reserved. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
#import "CDNumberDialingTextField.h" | |
#import "CDAppController.h" | |
#import "CDPrefsController.h" | |
@implementation CDNumberDialingTextFieldEditor | |
- (void) keyDown:(NSEvent *)event { | |
NSString * characters = [event characters]; | |
NSTextField * timeTextField = [[CDAppController sharedAppController] timeTextField]; | |
NSStepper * timeStepper = [[CDAppController sharedAppController] timeStepper]; | |
NSLog(@"timeTextField = %@", timeTextField); | |
NSLog(@"timeStepper = %@", timeStepper); | |
if ([characters length]) { | |
unichar character = [characters characterAtIndex:0]; | |
if (character == NSLeftArrowFunctionKey) { | |
NSLog(@"LEFT pressed"); | |
} else if (character == NSRightArrowFunctionKey) { | |
NSLog(@"RIGHT pressed"); | |
} else if (character == NSUpArrowFunctionKey) { | |
NSLog(@"UP pressed"); | |
if ([event modifierFlags] & NSAlternateKeyMask) { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] + 0.1)]; | |
} else if (([event modifierFlags] & NSShiftKeyMask) || | |
([event modifierFlags] & NSCommandKeyMask)) { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] + 10.0)]; | |
} else { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] + 1.0)]; | |
} | |
[timeStepper takeDoubleValueFrom:timeTextField]; | |
} else if (character == NSDownArrowFunctionKey) { | |
NSLog(@"DOWN pressed"); | |
if ([event modifierFlags] & NSAlternateKeyMask) { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] - 0.1)]; | |
} else if (([event modifierFlags] & NSShiftKeyMask) || | |
([event modifierFlags] & NSCommandKeyMask)) { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] - 10.0)]; | |
} else if ([event modifierFlags] & (NSShiftKeyMask & NSAlternateKeyMask)) { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] - 100)]; | |
} else { | |
[timeTextField setDoubleValue:([timeTextField doubleValue] - 1.0)]; | |
} | |
[timeStepper takeDoubleValueFrom:timeTextField]; | |
} | |
} | |
[super keyDown:event]; | |
} | |
@end | |
@implementation CDNumberDialingTextField | |
- (void) awakeFromNib { | |
[self addTrackingRect:[self bounds] owner:self userData:nil assumeInside:YES]; | |
} | |
- (void) mouseEntered:(NSEvent *)event { | |
if (![[CDAppController sharedAppController] isCounting]) { | |
[self becomeFirstResponder]; | |
[self takeDoubleValueFrom:timeStepper]; | |
} | |
[super mouseEntered:event]; | |
} | |
- (void) mouseExited:(NSEvent *)event { | |
if (![[CDAppController sharedAppController] isCounting]) { | |
[timeStepper takeDoubleValueFrom:self]; | |
} | |
[super mouseEntered:event]; | |
} | |
- (void) scrollWheel:(NSEvent *)event { | |
if (![self isEnabled]) { | |
return; | |
} | |
[self becomeFirstResponder]; | |
CGFloat dy; | |
NSUInteger mf; | |
dy = [event deltaY]; | |
mf = [event modifierFlags]; | |
if (mf & NSCommandKeyMask) { | |
dy *= 10.0; | |
} else if (mf & NSAlternateKeyMask) { | |
dy *= 0.1; | |
} | |
dy = [self doubleValue] + dy; | |
[self setDoubleValue:dy]; | |
[timeStepper setDoubleValue:dy]; | |
[super scrollWheel:event]; | |
if (dy > 0) { | |
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:dy] forKey:CDLastTimeValueKey]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment