Display NSWindow on secondary screen - cocoa

I would like to display an NSWindow created in a storyboard full screen on a secondary monitor. The below code results in the window being displayed on the primary screen/main screen. The Y origin is ok but the X origin is 0 where it should be -1680. The code below worked before Yosemite.
NSScreen *screen = [[NSScreen screens] objectAtIndex:2];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow makeKeyAndOrderFront:sender];
[PresenterWindow setLevel: CGShieldingWindowLevel()];
I had also tried the following with the same result:
[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, [screen frame].size.width, [screen frame].size.height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:screen];
Please advise how I can fix this.
I've tried solutions from some other questions:
Cocoa Open a fullscreen window on the second screen maintaining the app visible on the first - did not work - same result.
Display os x window full screen on secondary monitor using Cocoa - nope - neither with enterFullScreenMode:withOptions: method of NSView.
I have some new observations:
If i activate the "Launch At Startup" in the storyboard for my NSWindow the window is viewed fullscreen on my secondary monitor. If i then do a orderOut and run the above code the window is viewed in correct size but on my primary/main screen. The window is not activated to "Release When Close". So when I deactivate "Launch At Startup" the window is again viewed in the primary/main screen with the size of the secondary screen.

After some days I got it to work:
The NSWindow in the storyboard was marked with "Launch At Startup"
In the applcationDidFinishLaunching i wrote the following:
[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:self];
To open full screen I used this:
NSScreen *screen = [[NSScreen screens] objectAtIndex:1];
NSRect mainDisplayRect = [screen frame];
[PresenterWindow setFrame: mainDisplayRect display:YES animate:YES];
[PresenterWindow setLevel: CGShieldingWindowLevel()];
[PresenterWindow makeKeyAndOrderFront:screen];
To close full screen and to be able to open full screen again correctly:
[PresenterWindow setLevel: NSNormalWindowLevel];
[PresenterWindow orderOut:(sender)];

Related

How to click through a window's transparent part in Cocoa and SFML

I use window.getSystemHandle() in SFML and convert it to NSWindow *, the following code is used to make the window transparent:
NSWindow *cocoa_window = (NSWindow*)window.getSystemHandle() ;
GLint opaque = 0;
[cocoa_window setStyleMask:NSWindowStyleMaskBorderless];
[cocoa_window setLevel: NSFloatingWindowLevel];
[[[cocoa_window contentView] openGLContext] setValues:&opaque forParameter:NSOpenGLContextParameterSurfaceOpacity];
[cocoa_window setBackgroundColor:[NSColor clearColor]];
[cocoa_window setOpaque:NO];
And I clean the window by:
window.clear(sf::Color::Transparent);
However, I can't click through the transparent part ( I want to receive mouse events when the user click on the opaque part) of the window. What can I do?
Should I replace the line [[[cocoa_window contentView] openGLContext] ...; with something else? Or do something on SFML part instead?

How UIToolbarPosition is handled by UIToolbar?

I'm using setBackgroundImage:forToolbarPosition:metrics: method of UIToolbar.
That's my code:
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
[toolbar setBackgroundImage:[UIImage imageNamed:#"top"] forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[toolbar setBackgroundImage:[UIImage imageNamed:#"bottom"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
[self.view addSubview:toolbar];
As you can see I've put my toolbar on top. However when I run the code the toolbar image used is [UIImage imageNamed:#"bottom"].
How UIToolbarPosition is obtained?
My first idea is frame check ( < 1/2 superview frame is top, otherwise is bottom... or sort of).
It still on bottom. Any idea?
on iPhone UIToolbarPosition is always on bottom because UIToolbar is not supposed to be on top.
on iPad it works fine, UIToolbarPositionTop is used when toolbar.frame.origin.y = 0, UIToolbarPositionBottom otherwise.

Show application on multiple screens

I have an Application which should be shown on multiple screens simultaneously. With the main screen is not a problem, but it does not work for the second screen
I have found the solution, just retrieve frame of the second screen and show window in main screen but using the frame of the second screen
NSRect secScreenRect = [secondScreen frame];
myWindow = [[NSWindow alloc] initWithContentRect:secScreenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:[NSScreen mainScreen]];
...

How to avoid double window opening at launch?

I've been porting my cocos2D iOS game to Mac and it works without problems but I don't understand why I get two windows open every time I launch the app.
One of them is the cocos2d window with the main menu scene and the properties and name I give but there is another blank white window with the app name (I mean the Xcode project name). I suppose this is a trivial issue but I really can't avoid that window from appearing.
What am I doing wrong?
This is my AppDelegate window initialization:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
//Posiciona ventana y define escalado
NSRect aFrame=[[NSScreen mainScreen] frame];
CGSize winSize = CGSizeMake(1024,768);
CC_DIRECTOR_INIT(winSize);
[self.window showsResizeIndicator];
[director setResizeMode:kCCDirectorResize_AutoScale];
[director setProjection:kCCDirectorProjection2D];
[window_ setContentAspectRatio:NSMakeSize(winSize.width,winSize.height)];
[window_ setStyleMask:[window_ styleMask] | NSResizableWindowMask | NSMiniaturizableWindowMask];
[window_ setTitle:#"Barman Hero"];
aFrame=[[NSScreen mainScreen] frame];
if (aFrame.size.width<=winSize.width || aFrame.size.height<=winSize.height) [window_ zoom:self];
[window_ center];
[glView_ setFrameSize:NSMakeSize(window_.frame.size.width,window_.frame.size.height-22)];
// Enable "moving" mouse event. Default no.
[window_ setAcceptsMouseMovedEvents:NO];
.....
.....
.....
//Carga escena principal
[[CCDirectorMac sharedDirector] runWithScene:[MainMenu scene]];
}
Thanks in advance.
It is likely that you have a Window defined in MainMenu.xib that is being opened semi-automatically. Remove the window from MainMenu.xib and any code that might reference it and it should no longer open the second window.

NSWindow problem

Please help me. I set frame to the window like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[window makeKeyWindow];
[window setFrame:NSMakeRect(0, 0, 1024, 768) display:YES];
[window setBackgroundColor:[NSColor clearColor]];
[window center];
}
But real window size: width = 1024, height = 620. I think because my screen size (1280x720).
So, when [window setContentView:myView], myView is disable a part.
I can't solve this problem. Please help me solve this.
Thanks.
Size your content view to fit the space available using -[NSWindow contentRectForFrameRect:]. You should also consider sizing your window's frame to fit the available space, as well, such as by using -[NSScreen visibleFrame]. Both the Dock and the menu bar eat some space, so you should not expect to have the full screen size available to your application.
If you want to show your window at the maximum size, have a look at NSWindow's -zoom: method

Resources