How to open a window on click on NSStatusItem? - objective-c

I'm pretty new to cocoa, so please excuse me for any stupid mistakes I make.
I have a NSStatusItem, which I want to use to open up a menu. However as far as I know and have heard across different forms, without a custom view you are restricted to just a pop down menu. Is this true? And if so how do you make a custom view to do something (e.g. open a window in my case)? Thanks for any help.

No, it is not true. You need to set up the target and action for the status item to call a method which does what you want (opens the window).
// This goes where you set up the status item
NSStatusItem *statusItem; // You need to get this from the status bar
[statusItem setTarget:self];
[statusItem setAction:#selector(openWindow:)];
// This method is called when the status item is clicked
- (void)openWindow:(id)sender {
NSWindow *window = [self window]; // Get the window to open
[window makeKeyAndOrderFront:nil];
}
You may also want to call [NSApp activateIgnoringOtherApps:nil]; to your openWindow: method to ensure that the window you open is not behind some other application's window.

Related

Custom Created Window Getting Closed Automatically By Selecting Contextual Menu Option Automatically

I have builded a custom window; in this window I am showing nsbuttons.
I am showing a contextual menu if button clicked.
The problem is I don't want to close my window but somehow mouse exited event is getting triggered as soon as I choose option from nsmenu.
I want to prevent this effect.
I am not able to figure it out.
Any help would be appreciated.
Thanks in Advance
-(void)rightMouseDown:(NSEvent *)theEvent
{
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:#"Contextual Menu"];
[[theMenu addItemWithTitle:#"Remove From List" action:#selector(removeWithIdentifier) keyEquivalent:#""] setTarget:self];
[[theMenu addItemWithTitle:#"Open" action:#selector(openAppWithIdentifier) keyEquivalent:#""] setTarget:self];
[theMenu popUpMenuPositioningItem:nil atLocation:NSMakePoint(self.bounds.size.width-20, self.bounds.size.height-10) inView:self];
}
-(void)removeWithIdentifier
{
//My custom view is getting mouse exited event from here
//I want prevent it.
}
Hey Friends I have got the answer.
I have found that If your window level is "NSPopUpMenuWindowLevel" and its
view has implemented any context menu over it then this view will always gets mouse exited event.
posting it for future reference.
thanks.

Show Window without activating (keep application below it active)

I need to show a window (without title bar) above third party applications without my window taking focus.
I have tried using an NSPanel and setting enabling non-activating, but that didn't help.
I tried orderFront:self, but that didn't help either.
I always needed to add [NSApp activateIgnoringOtherApps:YES]; because the window wouldn't show otherwise.
I have here a sample project for just this functionality:
http://users.telenet.be/prullen/TopW2.zip
UIElement is set to true in the application's plist file, so there is no dock. You can activate the window by pressing ALT + SPACE at the same time. You will see that the app below it looses focus. Any thoughts on how to fix this? I've seen other apps do it so I know it's possible.
Edit: here's the code so far. Remember the window is a non-activating NSPanel.
I still need that last NSApp activateIgnoringOtherApps line or otherwise it doesn't display. But of course that makes the window the active one.
_windowController = [[MyWindowController alloc] initWithWindowNibName:#"MyWindowController"];
[[_windowController window] setLevel:NSNormalWindowLevel+1];
[[_windowController window] orderFrontRegardless];
[_windowController showWindow:self];
[NSApp activateIgnoringOtherApps:YES];
I've also subclassed NSPanel and added two methods:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)canBecomeMainWindow
{
return YES;
}
Edit: OK, unchecking setHidesOnDeactivate fixes this, but now the window will never hide. I need it to hide when the user presses the app below it or switches to another app.
Edit 2: OK, this seems to fix the above issue:
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideWindow) name:NSWindowDidResignKeyNotification object:nil];
}
- (void)hideWindow {
[self setHidesOnDeactivate:YES];
}
Not sure if there's a better way.
And for those that want to know how to display the window:
[[_windowController window] setLevel:NSPopUpMenuWindowLevel];
[[_windowController window] orderFrontRegardless];
[[_windowController window] makeKeyWindow];
[_windowController showWindow:self];
Either one of these should do the trick:
Use -[NSWindow orderFrontRegardless] to get a normal level window to the front without activating the corresponding app, or
Use -[NSWindow setLevel:] to increase the window level to something higher than NSNormalWindowLevel
Not to take away from #puzzle's useful answer, but it sounds like your problem has something to do with using an NSPanel instead of an NSWindow.
The "How Panels Work" docs say:
Onscreen panels, except for alert dialogs, are removed from the screen when the application isn’t active and are restored when the application again becomes active. This reduces screen clutter.
Specifically, the NSWindow implementation of the hidesOnDeactivate method returns NO, but the NSPanel implementation of the same method returns YES.
So perhaps you could override hidesOnDeactivate to return NO, or change to NSWindow

Improper setup of window displayed through menu item?

I am working on my first native mac app after playing with iOS for a while.
I am attempting to launch a window from a menu item, but I suspect I am doing it wrong. Any IBAction I connect to buttons on this new window returns an error.
Here is what I am doing. From a menu item I launch this:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc] initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
This launches the InputViewController nib that is owned by the InputViewController class. I set the InputViewController class to inherit from NSWindowController.
On InputViewController.m I have tested IBActions such as:
-(IBAction)testButton:(id)sender{
NSLog(#"Data recalled?");
}
I connect this IBAction to a button through the Interface Builder. All looks okay.
When I build and open the InputViewController window I receive this error in the console before clicking anything:
Could not connect the action testButton: to target of class NSWindowController
I have searched extensively but my ignorance prevents me from connecting the dots. This thread based on a similar error with NSApplication looks promising, but I don't quite understand what I'd need to make the connections happen related to the NSWindowController error.
This should be simple. What am I missing?
Your code:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[NSWindowController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Notice you are alloc/initing a generic instance of NSWindowController, not your custom subclass where you've implemented the testButton: method. I assume you'd likely want that changed to:
-(IBAction)displaySAInput:(id)sender{
NSLog(#"Input selected.");
inputSAViewController = [[InputViewController alloc]
initWithWindowNibName:#"InputViewController"];
[inputSAViewController showWindow:self];
}
Just load the NSWindow before hand and make it invisible and when the IBAction is called, just do the following:
[myWindow setIsVisible:YES];
[myWindow setLevel:NSFloatingWindowLevel];
Yay!

Show NSStatusItem menu only if App is active

I'm using this method to show the NSStatusItem menu only if application is Active.
-(void)menuWillOpen:(NSMenu*)menu{
if(![NSApp isActive]){
[menu cancelTracking];
}
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:self];
}
It perfectly works, but cancelTracking seems to block the blue highlight of the NSStatusItem. So when i click on status menu item it doesn't show the submenu and it presents the main window, but the icon is not highlighted.
Is there a way to make it happen ?
I suggest creating a custom view if you need more precise control over when the status item highlights itself. Then you can use mouseDown: etc. This is a good example of putting your custom view in a status item.

Creating a Modal Dialog or Window in Cocoa Objective-C?

I need to create an modal dialog, which is to loaded from a nib file and should be displayed on a button click in the main window.
I can create a custom window in a nib file and load the custom dialog on button click, but it's not a modal dialog. I can switch back to the main window.
MyWindowController is the NSWindowController subclass.
I used the code below to display my window in response to the button event:
MyWindowController *pController = [[MyWindowController alloc]
initWithWindowNibName:#"nibfilename"];
[MyWindowController showWindow:self];
There are several ways to do this — and in fact two different kinds of modal dialog in OS X: application-modal and window-modal. Using the wrong one will annoy people. One is a sheet, which is attached to the window that it affects (save dialogs are an example of this). The other is application-modal, which blocks the whole application (open dialogs work this way, since they don't apply to any window that exists yet). Apple's sheet documentation should help get you oriented.
Thank you...
(Example).
Create a nib with name "About"
if(pAbtCtrl == nil)
pAbtCtrl = [[AboutWindowController alloc] initWithWindowNibName:#"About"];
pAbtWindow = [pAbtCtrl window];
[NSApp runModalForWindow: pAbtWindow];
[NSApp endSheet: pAbtWindow];
[pAbtWindow orderOut: self];