Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Transparent window background with NSColor?

Transparent window background with NSColor?
Thread Tools
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 11, 2003, 07:12 PM
 
How do you create a window, which the background is 100% transparent?

I tried [[NSColor clearColor] set];, but the window is now coming up black. Is there a way to control the alpha channel?

Thanks!
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Mar 11, 2003, 07:20 PM
 
I'm assuming you have a subclass of NSWindow... here's an initializer to get a transparent window:

- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
NSWindow* self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[self setBackgroundColor: [NSColor clearColor]];
[self setAlphaValue:1.0];
[self setOpaque:NO];
return self;
}
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 11, 2003, 09:58 PM
 
Thanks -

I'm basically trying to edit the Sketch.app source code. So here's where the window background is delt with -

- (void)drawRect:(NSRect)rect {
SKTDrawWindowController *drawWindowController = [self drawWindowController];
NSArray *graphics;
unsigned i;
SKTGraphic *curGraphic;
BOOL isSelected;
NSRect drawingBounds;
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];

[[NSColor clearColor] set];
NSRectFill(rect);
if ([self showsGrid]) {
SKTDrawGridWithSettingsInRect([self gridSpacing], [self gridColor], rect, NSZeroPoint);
}

graphics = [[drawWindowController document] graphics];
i = [graphics count];
while (i-- > 0) {
curGraphic = [graphics objectAtIndex:i];
drawingBounds = [curGraphic drawingBounds];
if (NSIntersectsRect(rect, drawingBounds)) {
if (!_gvFlags.knobsHidden && (curGraphic != _editingGraphic)) {
// Figure out if we should draw selected.
isSelected = [self graphicIsSelected:curGraphic];
// Account for any current rubberband selection state
if (_rubberbandGraphics && (isSelected == _gvFlags.rubberbandIsDeselecting) && [_rubberbandGraphics containsObject:curGraphic]) {
isSelected = (isSelected ? NO : YES);
}
} else {
// Do not draw handles on graphics that are editing.
isSelected = NO;
}
[currentContext saveGraphicsState];
[NSBezierPath clipRect:drawingBounds];
[curGraphic drawInView:self isSelected:isSelected];
[currentContext restoreGraphicsState];
}
}

if (_creatingGraphic) {
drawingBounds = [_creatingGraphic drawingBounds];
if (NSIntersectsRect(rect, drawingBounds)) {
[currentContext saveGraphicsState];
[NSBezierPath clipRect:drawingBounds];
[_creatingGraphic drawInView:self isSelected:NO];
[currentContext restoreGraphicsState];
}
}
if (!NSEqualRects(_rubberbandRect, NSZeroRect)) {
[[NSColor knobColor] set];
NSFrameRect(_rubberbandRect);
}
}





Does this help???

Thanks!
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Mar 11, 2003, 10:18 PM
 
The problem is not with the view, but with the window -- you need to tell the windowserver that you should be able to see underneath the window (make it transparent), and can do this only by subclassing NSWindow.

What exactly are you trying to do?
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 11, 2003, 10:30 PM
 
Originally posted by 00101001:

What exactly are you trying to do?
Take a look at Sketch.app that came with the dev. tools. All I want to be able to do take SKTgraphicView which the shapes are being drawn on... I want that white background to be totally transparent.

Basically like when you crank up the transparency slider in terminal.app, so the termnal window background is completely clear.

Ideas???


Thanks!
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 11, 2003, 11:25 PM
 
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 12, 2003, 08:01 PM
 
See, what I want to be able to do is take the Custom view, in Sketch.apps case, SKTGraphicView and make it 100% transparent, so I can overlay that view on top of another view, in my case NSMovieView, so I can draw right on top of a playing quicktime movie.

Or... Is there an easier way?
     
Mac Enthusiast
Join Date: Jan 2001
Status: Offline
Reply With Quote
Mar 12, 2003, 09:50 PM
 
Ok, so you don't want the window transparent -- just the view. Very easy. [[NSColor clearColor] set] should work like you described above -- just make sure that you overide the isOpaque method of the view.
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 13, 2003, 09:37 AM
 
Pardon my ignorance, but I can't get it to work. The view background color is still black, not transparent. Which isOpaque method are you referring to?

Did you actually try this and get it to work???

I do appreciate your help.
     
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
Mar 13, 2003, 11:18 AM
 
NSView has a method named "isOpaque" that simply returns 'YES' or 'NO' (NO by default in NSView).
isOpaque

- (BOOL)isOpaque

Overridden by subclasses to return YES if the receiver is opaque, NO otherwise. A view object is opaque if it completely covers its frame rectangle when drawing itself. NSView, being an abstract class, performs no drawing at all and so returns NO.

See Also: - opaqueAncestor, - displayRectIgnoringOpacity:, - displayIfNeededIgnoringOpacity, - displayIfNeededInRectIgnoringOpacity:

isOpaque is already defined in SKTGraphicView.m:
Code:
-=-=-=-=-=- in SKTGraphicView.m, line 226 - (BOOL)isOpaque { return YES; }
If you just change the YES to NO, it will mean that the view doesn't fill its entire rect when it draws. This will mean that portions of the view below will show through. Hope this helps.
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 13, 2003, 11:26 AM
 
I really must be missing something then, because I've tried this and the view is drawn in black and not transparent.

Did you try this and get it to work???

I'm soooo confused...


Originally posted by Mskr:
NSView has a method named "isOpaque" that simply returns 'YES' or 'NO' (NO by default in NSView).



isOpaque is already defined in SKTGraphicView.m:
Code:
-=-=-=-=-=- in SKTGraphicView.m, line 226 - (BOOL)isOpaque { return YES; }
If you just change the YES to NO, it will mean that the view doesn't fill its entire rect when it draws. This will mean that portions of the view below will show through. Hope this helps.
     
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
Mar 13, 2003, 04:06 PM
 
Well, I have done this before in other projects, and it does work. In fact, the project I'm working on right now uses this. Do you have another view behind this SKTGraphicView? If you don't have another view behind, then it will appear black, because NOTHING is getting drawn in that space, not even the window.

Maybe if you could upload the code somewhere so I can see what you're doing... Do you have an iDisk or anything?
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 13, 2003, 05:25 PM
 
Originally posted by Mskr:

Maybe if you could upload the code somewhere so I can see what you're doing... Do you have an iDisk or anything?
Do you have the dev. tools installed? I would guess so??? If not, I can get you the code. Let me know.

I'm just trying to alter Sketch.app which is in Developer/Examples/AppKit I believe.

Let me know if you come up with anything.

Eric
     
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
Mar 14, 2003, 03:57 PM
 
Originally posted by eScrib:
Do you have the dev. tools installed? I would guess so??? If not, I can get you the code. Let me know.

I'm just trying to alter Sketch.app which is in Developer/Examples/AppKit I believe.

Let me know if you come up with anything.

Eric
I'll see what I can do in a very simple app and post what I can here, or on my iDisk for you to download.
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 14, 2003, 05:28 PM
 
That would be great. I'm looking forward to seeing what you come up with.
     
Forum Regular
Join Date: Jun 2001
Location: Savoy, IL USA
Status: Offline
Reply With Quote
Mar 15, 2003, 12:30 PM
 
Originally posted by eScrib:
That would be great. I'm looking forward to seeing what you come up with.
Ok, I've got an app that works that shows the basics of what you're trying to do. You can grab MovieTest here. It's got the compiled binary and the project will all source, resources, etc.

Unfortunately, I'm not sure that what you're trying to do will work. When I load a movie into the NSMovieView that I' use, the movie contents will clobber the views that are on top of the NSMovieView. I've got the CTSImageView (subclass of NSImageView) set on a level above the NSMovieView, which you can see in this shot:


[click to get full-sized view]

The white rectangle in the upper left is the NSMovieView and the MovieTest logo is in an NSImageView that overlaps the NSMovieView (obviously). But, when you load the NSMovie:


[click to get full-sized view]

... the NSMovieView's content comes up on top, clobbering the NSImageView that is above it. I bet that this is due to the way that QuickTime content is drawn to the screen. I bet there's a way around this, but it isn't pretty. Basically, you could probably put a transparent window ON TOP of the window containing the NSMovieView and set that window as a child of the NSMovieView window. That way, they would move together and be sync'ed in that regard. You'd have to give them the same frame information, and you'd have to manually resize the child window whenever the parent is resized. All-in-all, way too much work for me to do to show you as an example. I'll leave it up to you to try this, and all that I'm talking about here isn't very complex and would be a good learning exercise for you. Enjoy!
Software Architect, CodeTek Studios, Inc.

12" AlBook 867 (Combo drive) 640 MB/40 GB (work development machine) -- TiBook 400MHz/384MB/10GB (home machine)
CodeTek VirtualDesktop Pro: Power multitasking! -- DockExtender: Powerful, efficient launcher for Apps, Docs and everything else!
     
eScrib  (op)
Forum Regular
Join Date: Dec 2002
Status: Offline
Reply With Quote
Mar 15, 2003, 05:43 PM
 
Very cool!

I sent you an email with some followup questions. Let me know if you didn't get it.

Cheers!
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:53 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2