Activate all windows from background when clicking on main window - objective-c

I have an Objective-C application with a main window and a small progress window with a stack view to show the current progress.
If the application is put in the background by activating any other application and then clicking on the Dock icon, both the Main and secondary windows is brought to the front and shown.
But, if I just click one of the windows when in the background, only that window is activated and brought to the front, the other stays in the back.
I want to implement so that when I click on the main window it does the same thing as clicking on the Dock icon, it should show both windows on top with the Main window activated.
But if I click on the progress window, I don't want the main window to be brought to the front.
I haven't been able to find a way to do this, how should I go about achieving this?

You can detect the window being clicked with the window delegate's -windowDidBecomeKey: or -becomeKeyWindow, or the app delegate's -didBecomeActive:.
Then depending on your exact needs, you can use [NSApp activateIgnoringOtherApps:] or [[NSRunningApplication currentApplication] activateWithOptions:] (and possibly NSApplicationActivateAllWindows).

Related

Re-create Cocoa application menubar

I'm making a statusbar application and load a new xib containing the main window for the application when clicking on a statusbar menu item. However, in the process I deleted the application menu bar. I don't see a way to hook up the NSMenu object I created in the interface builder.
The window loads just fine, and the status bar icon is still present, but when I make the main window the key window, the application menu bar doesn't change, it just shows the previous app that was active.
I have followed the instructions/suggestions here and here, but neither of them work. Is there some other step I've missed?
Thanks!
The behavior you're describing is normal for background applications. If you don't have an icon in the Dock, you don't get your own menubar, even if you have a window in the foreground.

Hide app icon in dock when minimized

I am new at Mac OSx development.
The app that I am creating requires removing the app icon from the dock throughout the application. The app allows minimizing and closing of the app window. Relaunching or reopening of the closed or minimized app window is done by clicking the app's icon from the status bar.
I was able to set the dock icon to be disabled during app launching; however, when the app is minimized (clicking the minimize button), it captures the image of the app's current window and adds it to the dock. I don't want that to occur. The app should not add any item to the dock.
Questions:
Does Apple allow removal of the app's re-launcher image from the dock when minimized?
If Apple allows this, how can I hide or remove the app from the dock?
Any help would be a big help! Thanks!
You cannot disable the display of a proxy tile when your window is minimized -- that's the primary way that users will restore a minimized window. If you'd rather that the window disappear entirely when it's not being used, disable minimization (in the window's flags) and have the user close the window instead.

Cocoa put window in the foreground

I am building a Cocoa Mac application that runs in the background but has a main window.
To make it run in the background I've set "Application is background only" to "YES".
I built a system tray:
If you close the main window, you can re-open it by clicking "open".
I have however some issues with the layering of windows:
When I start the application, the main window opens however it appears in the background, behind any other windows or applications I have open at the time.
Clicking on "open" doesn't bring the window to the foreground. It opens it correctly if it was closed, however it stays behind any windows.
Clicking on preferences or about has the same issue. It opens the correct window but it appears behind any other windows.
On my main window there is a textfield. I can click on it, the cursor blinks as if I am ready to type. But when I type it actually types in some other background window! For example if I have TextWrangler open in the background, it will type there instead of the textfield...
Here is my code for handling the "open":
- (IBAction)show:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:sender];
}
Note that IF I set "Application is background only" to "NO" (which means I have a dock icon appearing), then clicking on "open" brings the window to the foreground as expected. And typing in the textfield works as expected.
Instead of background only, I think you want Application is agent (UIElement) set to YES. Background only is for application not intended to be visible for users.
Background only (LSBackgroundOnly YES) is intended for faceless background applications, Accessory (LSUIElement YES) is intended for background applications with a UI and status menu (menu extra/accessory menu/etc. - the name changes...).
An accessory will not appear in the dock, have a standard menu bar, or appear in the Finder's Force Quit dialog. It can be "active" and can have the key window.
Though it does not have a standard menu bar bizarrely (maybe a bug) if a MainMenu is declared in the XIB then it will respond to key shortcuts when it is active. To avoid this make sure you have no MainMenu or use [NSApp setMainMenu:nil] when you wish to disable the shortcuts.
The whole background/accessory/application/active/etc. area is not exactly well-defined, be prepared for "fun"...

Pop up window dialogs

In Cocoa, my application's main window has a button. How can I make it so when you click it, a new window will be generated and set focus to such window so that the main window can not be clicked or interacted with at all? This new window will have a textfield and a submit button. You click it and the window is supposed to close and send the textfield's data back to the main window (and it will recover focus as well).
I found this: How to open a new window on button click in Cocoa Mac Application?
But the answer doesn't seem to be working for me. The function showWindow doesn't seem to be recognized...
You're trying to create a modal window.
You can see the documentation at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/WinPanel/Concepts/UsingModalWindows.html for the full details.
Assuming you have created the window in your nib and can access it via an Outlet, you can call
[NSApp runModalForWindow:myWindowOutletVariable];
to make it modal like you want.

Show NSWindow as dropdown from NSStatusItem?

How can I show an NSWindow when clicking on an NSStatusItem, such that it shows over other applications when appearing, but without causing them to lose active status?
I have seen MAAttachedWindow, and it is neat but it does not show on top of other applications unless its own application is active.
I would check out this excellent tutorial and sample project:
Cocoa Popup window in the Status bar
That example shows how to "attach" a window to a status bar item, but you could position the window wherever you want. Basically, if you define your app as a LSUIElement you can display floating utility windows despite your app never activating or showing a dock icon, and other apps still remain key.
if you use MAAttachedWindow, you can set the winow level as NSStatusWindowLevel. Then it will show on top of other application.