2020-04-13 18:15:07 +00:00
|
|
|
/* Implementation of class NSSwitch
|
2020-04-16 15:42:21 +00:00
|
|
|
Copyright (C) 2020 Free Software Foundation, Inc.
|
2020-04-13 18:15:07 +00:00
|
|
|
|
|
|
|
By: Gregory John Casamento
|
|
|
|
Date: Wed Apr 8 22:01:02 EDT 2020
|
|
|
|
|
|
|
|
This file is part of the GNUstep Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
2020-04-16 15:42:21 +00:00
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2020-04-13 18:15:07 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110 USA.
|
|
|
|
*/
|
|
|
|
|
2020-04-16 16:16:30 +00:00
|
|
|
#import "AppKit/NSSwitch.h"
|
|
|
|
#import "GNUstepGUI/GSTheme.h"
|
2020-04-13 18:15:07 +00:00
|
|
|
|
|
|
|
@implementation NSSwitch
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSSwitch class])
|
|
|
|
{
|
|
|
|
[self setVersion: 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:42:21 +00:00
|
|
|
- (void) setState: (NSControlStateValue)s
|
2020-04-13 18:15:07 +00:00
|
|
|
{
|
2020-04-16 15:42:21 +00:00
|
|
|
_state = s;
|
|
|
|
[self setNeedsDisplay];
|
2020-04-13 18:15:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:42:21 +00:00
|
|
|
- (NSControlStateValue) state
|
2020-04-13 18:15:07 +00:00
|
|
|
{
|
2020-04-16 15:42:21 +00:00
|
|
|
return _state;
|
2020-04-13 18:15:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 11:40:40 +00:00
|
|
|
- (void) setAction: (SEL)action
|
|
|
|
{
|
|
|
|
_action = action;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (SEL) action
|
|
|
|
{
|
|
|
|
return _action;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTarget: (id)target
|
|
|
|
{
|
|
|
|
_target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) target
|
|
|
|
{
|
|
|
|
return _target;
|
|
|
|
}
|
|
|
|
|
2020-04-24 06:34:59 +00:00
|
|
|
- (void) setEnabled: (BOOL)flag
|
|
|
|
{
|
|
|
|
_enabled = flag;
|
2020-04-24 10:52:12 +00:00
|
|
|
[self setNeedsDisplay];
|
2020-04-24 06:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEnabled
|
|
|
|
{
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
2020-04-22 03:36:11 +00:00
|
|
|
- (void) setDoubleValue: (double)val
|
|
|
|
{
|
|
|
|
if (val < 1.0)
|
|
|
|
{
|
|
|
|
[self setState: NSControlStateValueOff];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self setState: NSControlStateValueOn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (double) doubleValue
|
|
|
|
{
|
|
|
|
return (double)(([self state] == NSControlStateValueOn) ? 1.0 : 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setFloatValue: (float)val
|
|
|
|
{
|
|
|
|
[self setDoubleValue: (double)val];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (float) floatValue
|
|
|
|
{
|
|
|
|
return (float)[self doubleValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setIntValue: (int)val
|
|
|
|
{
|
|
|
|
[self setDoubleValue: (double)val];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) intValue
|
|
|
|
{
|
|
|
|
return (int)[self doubleValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setIntegerValue: (NSInteger)val
|
|
|
|
{
|
|
|
|
[self setDoubleValue: (double)val];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSInteger) integerValue
|
|
|
|
{
|
|
|
|
return (NSInteger)[self doubleValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setStringValue: (NSString *)val
|
|
|
|
{
|
|
|
|
[self setDoubleValue: [val doubleValue]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) stringValue
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat: @"%ld", [self integerValue]];
|
|
|
|
}
|
|
|
|
|
2020-04-24 07:32:57 +00:00
|
|
|
- (void) setObjectValue: (id)obj
|
|
|
|
{
|
|
|
|
if ([obj respondsToSelector: @selector(stringValue)])
|
|
|
|
{
|
|
|
|
[self setStringValue: [obj stringValue]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) objectValue
|
|
|
|
{
|
|
|
|
return [self stringValue];
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:42:21 +00:00
|
|
|
- (void) drawRect: (NSRect)rect
|
|
|
|
{
|
2020-04-16 16:16:30 +00:00
|
|
|
[[GSTheme theme] drawSwitchInRect: rect
|
|
|
|
forState: _state
|
2020-04-22 21:34:14 +00:00
|
|
|
enabled: [self isEnabled]];
|
2020-04-16 15:42:21 +00:00
|
|
|
}
|
2020-04-13 18:15:07 +00:00
|
|
|
|
2020-04-17 11:22:24 +00:00
|
|
|
- (void) mouseDown: (NSEvent *)event
|
|
|
|
{
|
2020-04-22 03:41:13 +00:00
|
|
|
if (![self isEnabled])
|
|
|
|
{
|
|
|
|
[super mouseDown: event];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:22:24 +00:00
|
|
|
if (_state == NSControlStateValueOn)
|
|
|
|
{
|
|
|
|
[self setState: NSControlStateValueOff];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self setState: NSControlStateValueOn];
|
|
|
|
}
|
2020-04-18 15:46:34 +00:00
|
|
|
|
|
|
|
if (_action)
|
|
|
|
{
|
|
|
|
[self sendAction: _action
|
|
|
|
to: _target];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:34:14 +00:00
|
|
|
// Accessibility
|
|
|
|
- (NSRect)accessibilityFrame
|
|
|
|
{
|
|
|
|
return [self frame];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)accessibilityIdentifier
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)accessibilityParent
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isAccessibilityFocused
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)accessibilityLabel
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)accessibilityPerformPress
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) accessibilityPerformDecrement
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) accessibilityPerformIncrement
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) accessibilityValue
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NSCoding
|
2020-04-22 09:20:04 +00:00
|
|
|
- (id) initWithCoder: (NSCoder *)coder
|
2020-04-18 15:46:34 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
if ((self = [super initWithCoder: coder]) != nil)
|
2020-04-18 15:46:34 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
if ([coder allowsKeyedCoding])
|
2020-04-18 15:46:34 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
if ([coder containsValueForKey: @"NSControlContents"])
|
2020-04-18 15:46:34 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
[self setState: [coder decodeIntegerForKey: @"NSControlContents"]];
|
2020-04-18 15:46:34 +00:00
|
|
|
}
|
2020-04-22 09:20:04 +00:00
|
|
|
if ([coder containsValueForKey: @"NSControlAction"])
|
2020-04-18 15:46:34 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
NSString *s = [coder decodeObjectForKey: @"NSControlAction"];
|
|
|
|
[self setAction: NSSelectorFromString(s)];
|
|
|
|
}
|
|
|
|
if ([coder containsValueForKey: @"NSControlTarget"])
|
|
|
|
{
|
|
|
|
id t = [coder decodeObjectForKey: @"NSControlTarget"];
|
|
|
|
[self setTarget: t];
|
2020-04-18 15:46:34 +00:00
|
|
|
}
|
2020-04-26 08:47:25 +00:00
|
|
|
if ([coder containsValueForKey: @"NSEnabled"])
|
2020-04-24 06:34:59 +00:00
|
|
|
{
|
2020-04-26 08:47:25 +00:00
|
|
|
BOOL e = [coder decodeBoolForKey: @"NSEnabled"];
|
|
|
|
|
|
|
|
// NSControl decodes this, but does not use the value which
|
|
|
|
// is decoded. See comment in NSControl.m initWithCoder:.
|
|
|
|
[self setEnabled: e];
|
2020-04-24 06:34:59 +00:00
|
|
|
}
|
2020-04-18 15:46:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
[coder decodeValueOfObjCType: @encode(NSInteger)
|
|
|
|
at: &_state];
|
|
|
|
[self setAction: NSSelectorFromString((NSString *)[coder decodeObject])];
|
|
|
|
[self setTarget: [coder decodeObject]];
|
2020-04-18 15:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2020-04-17 11:22:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 09:20:04 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
2020-04-22 03:36:11 +00:00
|
|
|
{
|
2020-04-22 09:20:04 +00:00
|
|
|
[super encodeWithCoder: coder];
|
|
|
|
if ([coder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
[coder encodeInteger: _state
|
|
|
|
forKey: @"NSControlContents"];
|
|
|
|
[coder encodeObject: NSStringFromSelector(_action)
|
|
|
|
forKey: @"NSControlAction"];
|
|
|
|
[coder encodeObject: _target
|
|
|
|
forKey: @"NSControlTarget"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[coder encodeValueOfObjCType: @encode(NSInteger)
|
|
|
|
at: &_state];
|
|
|
|
[coder encodeObject: NSStringFromSelector([self action])];
|
|
|
|
[coder encodeObject: [self target]];
|
|
|
|
}
|
2020-04-22 03:36:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:15:07 +00:00
|
|
|
@end
|
|
|
|
|