How to center an NSView

jrs's picture

Could someone please explain to me how to center an NSView - I'm looking to use cwrights QCFullscreen as a basis for wrapping a composition into an app but can't get the composition to take up the full screen. Currently I'm just using a test composition which is a white square that takes up the full rendering dimensions and a slightly smaller red square on to of that. But when I use QCFullscreen to display it I get a black bar at the top when using my mac book pros native display and when using a macmini with a res of 2048x768 it displays ok height wise but I get a black bar down the left side of around 100px.

The attached picture (screen sharing into the mac mini) shows that it seems to be shifting the composition to the right and not just displaying it in a subset of the screen. As you can see in the below code I've checked the bounds for the view and they are fine and I'm not sure where else to look or why on one computer it shifts it to the right and on another to the bottom.

Any help would be much appreciated - James

- (void) applicationWillFinishLaunching:(NSNotification*)notification
{
   NSScreen *screen = [[NSScreen screens] lastObject];
   NSRect winFrame = [screen frame];
 
    NSLog(@"Bounds: %@", NSStringFromRect(winFrame));
 
   [visWindow setFrame: winFrame display: YES animate: NO];
 
    // Capture the main display
    if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
        NSLog( @"Couldn't capture the main display!" );
    }
 
    int windowLevel = CGShieldingWindowLevel();
    [visWindow setLevel: windowLevel];
 
   [visView loadCompositionFromFile:  
     [Bad link];
     [visView startRendering];
 
    NSLog(@"visView Bounds: %@", NSStringFromRect([visView bounds]));
    NSLog(@"visView Frame: %@", NSStringFromRect([visView frame]));
PreviewAttachmentSize
ContrbuteLeft.jpg
ContrbuteLeft.jpg29.32 KB

gtoledo3's picture
Re: How to center an NSView

The first thing that stands out about your comment is that you're saying the app isn't fullscreen; it should open fullscreen by default, and not have a menubar. I'd double check stuff if you renamed the project, to see if you didn't break something between the xib/nib and the project. Does the project open with a menubar in Lion without it being edited any?

cybero's picture
Re: How to center an NSView

I tried building using your code in XCode 4.3 and found that whilst the default project worked just fine, including your additional code resulted in a full screen, but one wherein the QCView composition rendered over to the bottom left and sadly vanished within a couple of seconds.

//
//  AppController.m
//  CMQCBackdrop
//
//  Created by Christopher Wright on 4/23/08.
//  Copyright 2008 Christopher Wright. All rights reserved.
//
 
#import "AppController.h"
#import <OpenGL/CGLMacro.h>
 
@implementation AppController
 
- (void) applicationWillFinishLaunching:(NSNotification*)notification
{
   NSScreen *screen = [[NSScreen screens] lastObject];
   NSRect winFrame = [screen frame];
 
    [visWindow setFrame: winFrame display: YES animate: NO];
   [visWindow setLevel: NSScreenSaverWindowLevel];
 
   [visView loadCompositionFromFile:
    [Bad link];
   [visView startRendering];
 
 
 
   // hide the cursor
   //CGDisplayHideCursor (kCGDirectMainDisplay);
}
 
@end

-- just works.

Are you getting the display problems you report above if you run this code instead of the slightly different code you have posted, I wonder?

jrs's picture
Re: How to center an NSView

Ok So I've taken a fresh copy of the default QCFullscreen code - http://kineme.net/wiki/SimpleFullscreenQCApp

I'm running Lion 10.7.3 and using xcode 4.3 and it won't build due to the following error

"The run destination My Mac 64-bit is not valid for Running the scheme 'QCFullscreen'."

So I've changed the architecture to "Standard (32/64-bit Intel)" and changed the base SDK to "Latest Mac OS X (Mac OS X 10.7)"

I've also changed the clear color of the myCompo composition to red to check if the composition is rendering full screen - When I run this I see the application run full screen in that there is no window frame but I can still see the menu bar - https://skitch.com/sheridan/8a4ia/fullscreen

So to fix this I've commented out the following line

    [visWindow setLevel: NSScreenSaverWindowLevel];

and added in to following code straight below it

    // Capture the main display
    if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
        NSLog( @"Couldn't capture the main display!" );
        // Note: you'll probably want to display a proper error dialog here
    }
 
    int windowLevel = CGShieldingWindowLevel();
   [visWindow setLevel: windowLevel];

This results in the a fullscreen app but where the menu bar is replaced by a black line - kind of makes sense - what really confuses me is if I then plug in an external monitor (1920x1200) and then shut the lid of my macbook pro such that I only have one display i get the following - https://skitch.com/sheridan/8a4s1/fullscreen

If I remove the extra code I added I get similarly placed QC composition (which looks to be 1680x1050) but no backing and no space for the menu bar at the top - https://skitch.com/sheridan/8a4ad/fullscreen

If I add in my logging messages again

    NSLog(@"visView Bounds: %@", NSStringFromRect([visView bounds]));
    NSLog(@"visView Frame: %@", NSStringFromRect([visView frame]));

it tells me both the frame and bounds for the view are

4/05/12 11:16:27.679 AM QCFullscreen: visView Bounds: {{0, 0}, {1920, 1200}}
4/05/12 11:16:27.679 AM QCFullscreen: visView Frame: {{0, 0}, {1920, 1200}}

I've attached my altered code - am I using the wring functions for lion? or are there some weird changes in xcode 4.3?

PreviewAttachmentSize
QCFullscreen 2.zip82.91 KB

cybero's picture
Re: How to center an NSView

Try changing the unsigned int for an NSUInteger on the styleMask definition in the Visualiser.m. Ran straight out of the build.

jrs's picture
Re: How to center an NSView

Nope still no go - if your using xcode 4.3 im guessing your on 10.7.3

When you say worked straight out of the build do you mean you changed my code and it worked or you were using the code in the zip at - http://kineme.net/wiki/SimpleFullscreenQCApp

Just wondering if your building against the 10.7 frameworks or not - do you get a complete fullscreen app or do you see the menubar? could you please attach a zip of your code

Cheers James

cybero's picture
Re: How to center an NSView

At your request .... the core contents of the project folder, including the composition I'm testing with.

I get a complete full screen application without any unwanted menu bar.

PreviewAttachmentSize
thecorecomponentparts.zip116.45 KB

jrs's picture
Re: How to center an NSView

It doesn't work, same issues as my previous post. I tried it on a fresh install though and it did work which is a little odd as my normal install is only a month old...although I did run the migration assistant and then upgrade from xcode 4.2. The problem seems to be something in my user space library as things work not only on the fresh install but also in a fresh account - any ideas as to what settings I may need to blitz.

I tried removing ~/Library/Developer/ and no luck - it seems to be something outside of xcode as if I take the compiled app from the fresh install or new account it still doesn't run on my day to day account

cybero's picture
Re: How to center an NSView

Understood - you got it working on a fresh install, but on an updated / upgraded install, with some other frameworks obstructing, no dice.

Any Console messages that might help clarify what is the problematic item?

jrs's picture
Re: How to center an NSView

No nothing in the console - I've assumed that frameworks are system wide though, and as it works on a different user account on the same system this would indicate that its not framework related and something else in my user ~/Library no?

I have a few programs installed that I thought may be the cause of the problem (air display, gfxcardstatus) but I've removed these and still no dice

I just looked in ~/Library and found a frameworks folder with three items in it but I've removed them and it also didn't help

I think I'm going to have to slowly try removing large chunks of my library folder until I can narrow down which area is causing it