NSView outside of NSWindow - objective-c

I have an NSWindow and basically, what I am trying to pop up an NSWindow outside of the bounds of the NSWindow it's in every time a user hovers over the NSWindow.
But every time I try to do that, since the NSView is outside of the bounds of NSWindow it gets cut off.
Here's a picture of what I am trying to achieve:

You need to create a borderless NSWindow, large enough to contain your view, and make the window a child window of the main window it's attached to. To make a window a child of another window, you use the addChildWindow:ordered: method of NSWindow.
Child windows are attached to the parent window and will move with their parent window when the parent window moves. If you just open a new window without making it a child window, it will be "left behind" if the other window is moved.
To make a borderless window, pass NSBorderlessWindowMask as the styleMask to the initWithContentRect:styleMask:backing:defer: method of NSWindow.

The easiest approach is to create another NSWindow with no border and put the button in that.

Related

NSView Absorb Clicks?

I have my standard app setup, window with lots of views. From time to time I place an NSView over the top of everything, black with some transparency, to act an a dimmer/overlay.
I need this top overlay view to absorb all clicks so that any views underneath it can't be interacted with. E.g. an NSButton under this NSView won't be clickable.
How can I do this?
I have seen -(NSView *)hitTest:(NSPoint)aPoint but I don't want to put this on every single subview with a rule to block clicks while the overlay view is present.
Override the NSView with an empty mouseDown: and the views underneath will not receive any mouse events.

How do I force keyboard focus for a Modal Window without Title Bar

I want to run a simple dialog similar to Finder Go To Folder.
I have used a NSPanel made this Document Modal and run with runModal.
This works, but displays a title.
If I turn off the Title Bar (in IB) the buttons work, but the NSTextField does not get keyboard focus.
I have tried lots of techniques to make it firstResponder or set as key, setBecomesKeyOnlyIfNeeded:NO but to no avail.
You need to subclass NSWindow, override - (BOOL)canBecomeKeyWindow and return YES.
By default, borderless windows cannot become the key window.

Modal child windows - like sheets but not a sheet

I'm trying to replicate the modal behavior of a sheet in Cocoa without actually using a sheet. That is, attaching a borderless child window to the main window and have the child window be the only responder. The parent window should remain key, support resizing, but cannot be navigated/responded to via keyboard or mouse.
So far I've added a borderless NSPanel subclass to the main window, which returns YES from -acceptsFirstResponder, and run the child window modally of the parent. The parent window remains the key window (as the NSPanel subclass returns NO from -canBecomeKeyWindow) but the parent window still has focus. I can use the keyboard to selected a button, which does nothing because of the modal child window. I need the child window to become first responder and calling -makeFirstResponder and setting the -initialFirstResponder view for the window has not effect.
NSWindow has had an - (void)addChildWindow:(NSWindow *)childWindow ordered:(NSWindowOrderingMode)orderingMode method since Mac OS X 10.2. Using the NSWindowAbove ordering mode should be pretty close to what you're looking for. If you create the child window to cover the parent (make the child window borderless), but leave the parent's titlebar exposed – that should do what you're looking for. You may still need to disable interaction with the controls on the parent window while the child is shown if that's the behaviour you want.

How can I make sure a window is only displayed once in Cocoa?

I have an NSWindow which contains an NSImageView. This window gets activated everytime I click on a cell in my tableview. I only want 1 instance of the NSWindow to appear, but want to be able to change the contents of NSImageView.
How can I initialize NSWindow and display only 1 instance of it?
This is a job for a singleton!
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32
One possibility to do this is to create an NSWindowController subclass and an associated window XIB that gets loaded when the window controller is instantiated.
I'm sure you already have some controller class handling the mouse click in the NSTableView. In that class, simply keep around an instance of the NSWindowController subclass mentioned above as an instance variable. Whenever you need to display the window, tell that ivar to display its window.
If the window's contents are dependent on the clicked table cell, simply add some methods to the window controller that modify its window's contents and call these methods in your click-handling method before you display the window.
btw: I wouldn't use a singleton here because in this case it would just be a workaround for bad design (just my opinion, not a hard fact).

How can I make an undecorated window in Cocoa?

I like to create a Cocoa window without any chrome whatsoever. The only thing the user should see is what I draw.
I've discovered I can create a custom NSView but does this have to be in an NSWindow to display? If not, how can I display it without putting it in an NSWindow? If it does have to be in an NSWindow, how do I stop the window from drawing a title bar and other chrome?
Check out the sample:
http://developer.apple.com/samplecode/RoundTransparentWindow/index.html
I've discovered I can create a custom NSView but does this have to be in an NSWindow to display?
Yes.
If it does have to be in an NSWindow, how do I stop the window from drawing a title bar and other chrome?
Use NSBorderlessWindowMask when you create your window. (Assuming you aren't using a custom subclass of NSWindow, this means not creating the window instance in a nib. If you want to lay out your view hierarchy in a nib, do that in a top-level custom view, then load the nib and set that view as the window's content view.)