so for windows and panels i can write:
Code:if (myWindow isVisible) {bla bla bla};
but if i want to say if my color is red:
Code:if (NSColor = redColor) {doesn't work}; if (NSColor isEqual:redColor) {doesn't work, etc.};
any thoughts?
[myWindow isVisible]
if (result) { ... }
if ([myWindow isVisible]) { ... }
but if i want to say if my color is red:
Code:if (NSColor = redColor) {doesn't work}; if (NSColor isEqual:redColor) {doesn't work, etc.};
any thoughts?
The way to send a message to an object is to write for example...
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
int itemTag = [item tag];
if ((itemTag == 5 || itemTag == 6) && ([[NSColorPanel sharedColorPanel] isVisible]))
return NO;
if ((itemTag == 7 || itemTag == 8) && ([B][NSColor whiteColor][/B]))
return NO;
return YES;
}
is currently[/I] white, than return no... you know?
NSColorPanel *cp = [NSColorPanel sharedColorPanel];
NSColor *cpColor = [cp color];
if ([cpColor isEqual:[NSColor whiteColor])
{
// insert your code here
}
the color of my NSView...
if ([myView color]==[NSColor whiteColor])
{
}
if ([[myView color] isEqual:[NSColor whiteColor]])
{
}
robbieduncan is correct about creating an NSView color property or accessor method. For the code, however, you will need to use isEqual: instead of == . The == operator will return false as it compares the memory addresses rather than the color value. == typically is only used to compare scalar values.
They might be singletons, but does writing [NSColor redColor] give you the same object as, for example, [colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0]? Not sure. It's the same color but does Cocoa detect when you're trying to make red by components and hand you the singleton?In general I agree with you, but my understanding of NSColor was that any given colour was singleton. For example [NSColor whiteColor]==[NSColor whiteColor] should return true as NSColor returns the same object every time for whiteColor instead of creating a new instance which is white...
They might be singletons, but does writing [NSColor redColor] give you the same object as, for example, [colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0]? Not sure. It's the same color but does Cocoa detect when you're trying to make red by components and hand you the singleton?
NSViews don't have a single colour, unless your view has a custom attribute which is it's colour (most NSViews have at least background and foreground colours).
Anyway you should ask your view for it's current colour and compare that with the other colour. There is no way the NSColor class can guess what the hell you are thinking there: it's not as clever as us and we couldn't 😛
So assuming your custom NSView responds to -(NSColor *) color and we have an instance variable called myView pointing at the correct instance of the view something like this:
Code:if ([myView color]==[NSColor whiteColor]) { }
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
if ((itemTag == 7 || itemTag == 8) && ([NSView color] isEqual:[NSColor whiteColor]))
return NO;
return YES;
}
You're calling color on the *class* NSView, not a particular instance of it. That definitely won't work.
@interface ColorController : NSObject
{
NSView *myView;
}
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
if ((itemTag == 7 || itemTag == 8) && ([myView color]==[NSColor whiteColor]))
return NO;
return YES;
}
Do you want to see if the window's background color is some value? The frame? Some text?
-Lee
the window background... the NSRect... but it's in a different class than my validateMenuItem function and i can't seem t reach it
NSColor *myBGColor = [[myView window] backgroundColor];
if([myBGColor isEqual: [NSColor whiteColor]]) {
//do something
}
@interface ColorView : NSView
{
NSColor *backgroundColor
}
- (NSColor *)backgroundColor;
- (void)setBackgroundColor:(NSColor *)newColor;
@class ColorView;
@interface ColorController : NSObject
{
ColorView *myView;
}
#import <Cocoa/Cocoa.h>
------------ windowColor.h ------------
@interface windowColor : NSView
{
NSColor *fillColor;
}
- (IBAction)changeColor:(id)sender;
- (IBAction)changeWhite:(id)sender;
@end
------------ windowColor.m ------------
#import "windowColor.h"
@implementation windowColor
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSData *defaultViewColorData = [[NSUserDefaults standardUserDefaults] dataForKey:DEFAULTS_VIEW_COLOR_KEY];
if (defaultViewColorData != nil)
{
[fillColor release];
fillColor = [[NSUnarchiver unarchiveObjectWithData:defaultViewColorData] retain];
}
}
return self;
}
- (void)dealloc
{
[super dealloc];
[fillColor release];
}
- (IBAction)changeColor:(id)sender
{
NSData *defaultViewColorData = [NSArchiver archivedDataWithRootObject:[sender color]];
[[NSUserDefaults standardUserDefaults] setObject:defaultViewColorData forKey:DEFAULTS_VIEW_COLOR_KEY];
[fillColor release];
fillColor = [[sender color] retain];
[self setNeedsDisplay:YES];
}
- (IBAction)changeWhite:(id)sender
{
NSData *defaultViewColorData = [NSArchiver archivedDataWithRootObject:[NSColor whiteColor]];
[[NSUserDefaults standardUserDefaults] setObject:defaultViewColorData forKey:DEFAULTS_VIEW_COLOR_KEY];
[fillColor release];
fillColor = [[NSColor whiteColor] retain];
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)rect
{
[fillColor set];
NSRectFill(rect);
}
@end
------------ ColorController.h ------------
#import <Cocoa/Cocoa.h>
#import "windowColor.h"
@interface ColorController : NSObject
{
IBOutlet windowColor *myColorSelection;
IBOutlet windowColor *myWhiteSelection;
NSTimer *timer;
}
- (IBAction)selectColor:(id)sender;
- (IBAction)selectWhite:(id)sender;
@end
------------ ColorController.m ------------
#import "ColorController.h"
#import "windowColor.h"
#import "HUDColorPanel.h"
@implementation ColorController
+ (void)initialize
{
[[HUDColorPanel class] poseAsClass:[NSColorPanel class]];
NSData *defaultViewColorData = [NSArchiver archivedDataWithRootObject:[NSColor whiteColor]];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *myDefaults = [NSDictionary dictionaryWithObject:defaultViewColorData forKey:DEFAULTS_VIEW_COLOR_KEY];
[defaults registerDefaults:myDefaults];
}
- (id)init
{
if (self = [super init])
{
[[NSColorPanel sharedColorPanel] setDelegate:self];
}
return self;
}
- (IBAction)selectColor:(id)sender
{
if ([[NSColorPanel sharedColorPanel] isVisible])
{
[[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];
}
else
{
[[NSColorPanel sharedColorPanel] makeKeyAndOrderFront:nil];
[[NSColorPanel sharedColorPanel] setAlphaValue:0.0];
timer = [[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(colorPanelFadeIN:) userInfo:nil repeats:YES] retain];
}
}
- (void)colorPanelFadeIN:(NSTimer *)theTimer
{
if ([[NSColorPanel sharedColorPanel] alphaValue] < 1.0)
{
[[NSColorPanel sharedColorPanel] setAlphaValue:[[NSColorPanel sharedColorPanel] alphaValue] + 0.1];
}
else
{
[timer invalidate];
[timer release];
timer = nil;
}
[[NSColorPanel sharedColorPanel] setTarget:myColorSelection];
[[NSColorPanel sharedColorPanel] setAction:@selector(changeColor:)];
}
- (BOOL)windowShouldClose:(id)window
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(colorPanelFadeOUT:) userInfo:window repeats:YES];
return NO;
}
- (void)colorPanelFadeOUT:(NSTimer *)theTimer
{
NSWindow* window = [timer userInfo];
[window setAlphaValue:[window alphaValue] -0.1];
if([window alphaValue] <= 0.0)
{
[timer invalidate];
[window close];
}
}
- (IBAction)selectWhite:(id)sender
{
[myWhiteSelection changeWhite:nil];
}
#pragma mark Disable Menu Items
- (BOOL)validateMenuItem:(NSMenuItem *)item
{
int itemTag = [item tag];
if ((itemTag == 5 || itemTag == 6) && ([[NSColorPanel sharedColorPanel] isKeyWindow]))
return NO;
[COLOR="Red"][B] if ((itemTag == 7 || itemTag == 8) && (??? WHAT ??? == [color whiteColor]))
return NO;[/B][/COLOR]
return YES;
}
@end
- (NSColor *)fillColor
{
return fillColor;
}
- (void)setFillColor:(NSColor *)newColor
{
NSData *defaultViewColorData = [NSArchiver archivedDataWithRootObject:newColor];
[[NSUserDefaults standardUserDefaults] setObject:defaultViewColorData forKey:DEFAULTS_VIEW_COLOR_KEY];
[fillColor release];
fillColor = [newColor retain];
[self setNeedsDisplay:YES];
}
...
if ((itemTag == 7 || itemTag == 8) && ([myWhiteSelection fillColor] == [NSColor whiteColor]))
...