Is it possible to bring window to front without taking focus? - objective-c

I am working on an application for my personal usage that will remind me of stuff at regular intervals and/or will require text entry. Hence this popup window has an NSTextField.
If the window pop's up when I am in the middle of typing, my typing transfers to the popup window which is very annoying! Is there any way to stop this, currently I am using:
[NSApp activateIgnoringOtherApps:YES];
[hudWindow makeKeyAndOrderFront:nil];
I have also tried:
[NSApp activateIgnoringOtherApps:YES];
[hudWindow orderFrontRegardless];
Is there any other way to do it?

[hudWindow orderFront: nil];
Moves the window to the front of its level in the screen list, without changing either the key window or the main window.

Did you try using setLevel:? Setting it to one of the higher levels should do the trick.
like
[hudWindow
setLevel:NSFloatingWindowLevel];

Related

Show all NSWindows

Pretty simple idea here, I want to show all the NSWindows in an app. The idea being that there are two windows in the app, one his hidden the other is vissable. I want to show all the windows in the app and then hide one. I can hide the window I want to hide but I cant show the windows because I am unable to obtain a reference to it. is there anyway of getting a list of all the nswindows in the app then iterating through it and hiding them or something similar, I can use [NSApp windows] however trying to use
NSArray *windowArray = [NSApp windows];
[windowArray[0] makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
does not work, nor does:
NSArray *windowArray = [NSApp windows];
NSWindow *tempWindow = windowArray[0];
[tempWindow makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
any suggestions?
Well in hindsight I've been a bit stupid. I had two seep rate controller objects each running one window. One was hardly doing anything as the view inside it was being handled by a different object so I just removed that one and made the first controller object take care of both windows. Problem solved.

Disabling Window Behind

These days, I develop iPad applications. Since I've run out of subjects to work on, I'm working on a simple OS X application to see how far I can go.
Anyway, what I want to do is show a Preferences window (PrefWindowController). It has a separate xib (PrefWindowController.xib) from MainMenu.xib. When it appears, I want the main window to go behind it. The following is what I have.
// AppDelegate.m
- (void)preferencesClicked:(id)sender {
if (!preferencesWindow) {
preferencesWindow = [[PrefWindowController alloc] initWithWindowNibName:#"PrefWindowController"];
}
[preferencesWindow showWindow:self];
}
// PrefWindowController.m
- (void)windowDidLoad {
[super windowDidLoad];
[NSApp runModalForWindow:self.window];
NSLog(#"Hello!?");
}
- (IBAction)closeClicked:(id)sender {
[NSApp stopModal];
[self close];
}
So I've learnt that I can use runModalForWindow to put the current window on top of the main window. The problem that I have is that this preferences window will reopen when I click on the close button (closeClicked). If I click on it again, it will close. If I open Preferences and click on the same button, it won't reopen. An interesting thing is that the application won't read NSLog(#"Hello!?") when the Preferences window first opens. It does when I clicked on the close button. Do you know what I'm doing wrong?
Thank you for your help.
Modal doesn't mean display on top. Modal means stop the user interacting with anything else other than this. It does this by creating a new run loop, which means that anything after runModelForWindow won't happen till after the modal window is closed. Exactly what you are seeing with the NSLog.
You probably don't want to use a modal window for preferences. The convention for OSX is that the main app window stays active when a preferences window is open.
If you just want to bring the window to the front, and don't care if the user later clicks on the main window to bring it to the front, then use -makeKeyAndOrderFront on the window you have. If on the other hand you want this preferences window to always be in front, then make it an NSPanel rather than an NSWindow.

Re-show my main Cocoa application Window

Ok, as simple and silly as that may seem, this is my scenario :
The application window (there is just ONE main window) is minimized
The user clicks something on the dock menu
We want to bring that main window back
Simple?
Not quite.
I've tried accessing my main window via :
[[NSApplication sharedApplication] mainWindow]
[[NSApplication sharedApplication] windows]
and then sending a makeKeyAndOrderFront: message but it's simply NOT showing up.
I've even tried showing all windows (yep, it still sees as "windows" one-or-two (hidden) sheets I'm using...), but still nothing.
Could you suggest a way, I could finally show that window, WITHOUT having to set an outlet anywhere?
I don't know; I'm probably too tired to notice, but this thing is almost nerve-wrecking...
You can un-minimize a window like so:
if([aWindow isMiniaturized])
{
[aWindow deminiaturize:self];
}
However, the problem is that the window will lose main status when it is minimized, so you'll need some other way of identifying the window.
Why can't you just use an outlet?
You can make the app un-minimize all of its minimized windows like so:
for(NSWindow* win in [NSApp windows])
{
if([win isMiniaturized])
{
[win deminiaturize:self];
}
}
When the app launches, you could store a reference to the main window and register for the NSWindowDidMiniaturizeNotification. That will tell you when the main window minimizes, which might also help you.

Document sheet not responding to keyboard events

I think it's a first responder problem, but I'm not sure. I'm implementing an edit window for data in a table view. Very similar in concept to the UI for editing filter rules in Mail.app.
I have an editing window that I attach to my primary window with:
[NSApp beginSheet: criteriaEditPanel
modalForWindow: [self window]
modalDelegate: self
didEndSelector: #selector(criteriaEditDidEnd:returnCode:contextInfo:)
contextInfo: (void *)[criteriaList objectAtIndex: index]];
The panel displays properly, from the title bar of the main window. I can manipulate the pop-up controls on the panel with the mouse, but I can't edit the text fields and I can't tab between fields. Everything else works fine. Any ideas?
joe
Solved. Believe it or not, you have to have the Title Bar enabled for the panel, even though it's never visible when displayed as a Sheet. When enabled it in IB, keyboard input started working.
#spudwaffle - none of the objects had Refuse First Responder checked.

How to close the front application window

I want to close the front window of my application. How is that done?
Try [myWindow close] or [myWindow performClose:nil].
Use [NSApplication keyWindow] to get the key window if needed. If you want just the front-most one, grab the first one from [NSApplication orderedWindows].