Transparent NSWindow, but not subviews? - objective-c

Is it possible to have a transparent NSWindow, but not for it's subviews (NSTextField and NSButton). Right now the text is also showing wat's below, I'd like that to be 0% transparent.
Here's what I do in the NSWindow init:
[self setAlphaValue:0.9];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];

You need to put a dark backgound image having the default colour of window behind each of textfield and button.
For this purpose you can subclass all required objects and add the above.

Related

Objective-C NSScrollView Foreground Colour

I am getting into app development and just started using NSScrollViews to display large chunks of text.
I am able to set the background colour by writing the following:
[_HeadersScrollView setBackgroundColor:[NSColor darkGrayColor]];
But not set the foreground colour by doing something similar to:
[_HeadersScrollView setTextColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForegroundColor:[NSColor whiteColor]]; // nope
[_HeadersScrollView setForeground:[NSColor whiteColor]]; // nope
Is there a method or any other way that I could get this type of setup to work? I'd really appreciate it.
The NSScrollView contains an NSClipView (a helper for scrolling) which contains the NSTextView. If you want to operate on the text view you either need an outlet to that or you can request the documentView from the scroll view.
You can do either:
[_HeadersScrollView.documentView setTextColor:[NSColor whiteColor]];
Or, if you have an outlet to the text view (called _textView in my example), you can do:
_textView.textColor = [NSColor whiteColor];
In this case, you would probably want to set the background color on the text view, too, rather than the scroll view. And tell it to draw its background by setting drawsBackground.
There is no setting like that for changing foreground color in scrollview.
_HeadersScrollView.tintColor = [NSColor whiteColor];
or
[_HeadersScrollView setTintColor:[NSColor whiteColor]];

Invisible Fullscreen View with Cocoa

I'm trying to create an application which change the mouse cursor, but to do that the cursor must be inside a NSView. To do this all the time, I would need to have an intangible and invisible fullscreen view.
I've created a subclass of NSView which implement the following code to became full screen in initWithFrame:
[self enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
And using that code, I can change the cursor inside the view:
- (void)resetCursorRects{
[self addCursorRect:[self bounds] cursor:appCursor];
}
The cursor changed successfully, but here comes the first problem: the view is in fullscreen, but it's visible, like a gray background. I've tried this method:
[[self window] setOpaque:NO];
And this one:
[[self window] setBackgroundColor:[NSColor colorWithWhite:1.0 alpha:0.0]];
But both of them just change the view color to black instead of gray. How can I fix that?

Grey border around view when using NSBorderlessWindowMask

I'm having the exact same problem as in this question:
Gray border when using NSBorderlessWindowMask
However, the accepted answer (as in the comments) of removing the window shadow doesn't seem to work, at least on Lion.
I've subclassed NSWindow, and created a borderless window in this manner:
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect
styleMask:(NSBorderlessWindowMask | NSResizableWindowMask)
backing:bufferingType
defer:flag];
[self setMovableByWindowBackground:YES];
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setHasShadow:YES];
[self setLevel:NSMainMenuWindowLevel];
return self;
}
Please note that this app will only be run on Lion (so NSResizableWindowMask doesn't change the appearance). I tried disabling the shadow, and toggling numerous settings for my window but I can't seem to remove this grey border:
Nowhere in my code do I add a border. I simply have a NSSplitView added in Interface Builder in a window. During runtime I add the colored view as a subview to the left pane of the split view, completely filling the bounds of the left split view.
Edit: This happens even using a simple NSView, not even a split view.
TL;DR: Why does my NSView have a grey border around it?
ok got it. to remove the shadow simply add this to your NSWindow subclass:
- (BOOL)hasShadow {
return NO;
}
and to remove the border you need to know that this border is coming from the view - not the window (just like you said it in your edit). So you have to disable the border for the view with this code:
[myview setBorderType:NSNoBorder];

Custom NSButton with semi-transparent background

I'm trying to create a custom NSButton with a 50% opaque black background and white text. To do this I've subclassed NSButton and overloaded DrawRect:
- (void) drawRect:(NSRect)dirtyRect
{
[self setBordered:NO];
//REMED since it has same effect as NSRectFill below
//[[self cell] setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.2]];
NSColor* backgroundColor = [NSColor colorWithCalibratedWhite:0 alpha:0.3f];
[backgroundColor setFill];
NSRectFill(dirtyRect);
[super drawRect:dirtyRect];
}
The white text appears fine but the button's background is always 100% opaque. The alpha value is not interpreted.
Any ideas? Thanks!
The default operation of NSRectFill() is copy which is not what you want. Replace it with
NSRectFillUsingOperation(dirtyRect, NSCompositeSourceAtop);
Another solution I found was to keep my code the same but turn on the Core Animation Layer for each button in Interface Builder. I don't know enough about Core Animation Layer to know why this worked. I had previously turned CAL off because it was making my fonts look very jagged.

Custom NSView Fill Paints Over Bottom Bar

I have a window which has a custom NSView and has a bottom bar with controls on it, one of which is an NSColorWheel.
For simplicity sake the Window is 332px high, with the custom NSView being 300px high and the bottom bar being 32px high.
The bottom bar is created as part of my awakeFromNib when the app loads the window using the following code:
[[self window] setAutorecalculatesContentBorderThickness:YES forEdge:NSMinYEdge];
[[self window] setContentBorderThickness: 32.0 forEdge: NSMinYEdge];
In my custom NSView class I fill the rectangle with color. Everything works fine when the app loads using the following in my NSView class:
- (void)drawRect:(NSRect)dirtyRect
{
dirtyRect = [self bounds];
NSColor * mNewColor = [NSColor blackColor];
[mNewColor set];
[NSBezierPath fillRect:dirtyRect];
}
However, if I subsequently call a method that changes the color of the custom NSView when a color wheel in the bottom bar is changed, the bottom bar gets overwritten with the color. The following code illustrates this method (this code is in the custom NSView class:
- (void)changeBackgroundColor:(NSNotification *)notification
{
NSLog(#"Changed background color");
NSRect mRect = [self bounds];
NSColor * mNewColor = [theColorWell color];
[mNewColor set];
[NSBezierPath fillRect:mRect];
[self setNeedsDisplay:YES];
}
Resizing the window instantly corrects the problem, but obviously I don't want the user to have to resize the window for an obvious bug!
What I don't understand is why my bounds appear to be mapping to the parent window and not the custom NSView when I call setNeedsDisplay and yet the bound correctly adjust when I resize the window using the mouse (even if just by 1 pixel).
Do I somehow need to account for the bottom bar on the redraw?
Any and all help much appreciated.
You should do all your drawing in the drawRect: method of your custom NSView. Cocoa automatically sets up the graphics context for you when it calls this method - things may not draw correctly if you perform drawing operations in other methods.
Your code in drawRect: could set the colour to the the current background colour as specified by your NSColorWell and fill the dirtyRect rectangle with this.
Then in the other method just call [self setNeedsDisplay:YES]; and then drawRect: will automatically be called to redraw the view.
See here for more information: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html#//apple_ref/doc/uid/TP40002978-CH7-SW4 (in particular the Drawing View Content section)