deovrat singh
deovrat singh

Reputation: 1215

Making a window topmost within an application

In my application,I have a set of windows.I want one of the window to be top most all the time when the application is active.I tried doing this by changing the level of the window, but didn't succeed.

If I put NSNormalWindowLevel , then on clicking any other window of my application this window goes in background.While if I use any other level than NSNormalWindowLevel , then the window remains topmost even if I switch to some other application. I want the window to be topmost only when my application is active.How to do this in cocoa ?

Thanks

Upvotes: 3

Views: 6509

Answers (4)

pier_nasos
pier_nasos

Reputation: 678

code for copy/paste (look deovrat singh answer)

-(void) windowDidLoad {
    [super windowDidLoad];
    [self.window setLevel: NSFloatingWindowLevel];
    [self.window setHidesonDeactivate:YES];

    //....
}

Upvotes: 2

deovrat singh
deovrat singh

Reputation: 1215

I did this finally , by hiding the NSWindow when the application is deactivated by using function setHidesonDeactivate and using a NSFloatingWindowLevel for the window.

Upvotes: 5

JackPearse
JackPearse

Reputation: 2942

This is possible, but can be only done programatically (that means not in Interface Builder):

if you have a window, use

[window setLevel: NSMainMenuWindowLevel];

if you use a window controller (NSWindowController), use

[windowController showWindow:self];
[windowController.window setLevel: NSMainMenuWindowLevel];

in general you may use on of the following levels:

NSNormalWindowLevel       
NSFloatingWindowLevel   
NSSubmenuWindowLevel    
NSTornOffMenuWindowLevel
NSMainMenuWindowLevel   
NSStatusWindowLevel      
NSDockWindowLevel        
NSModalPanelWindowLevel   
NSPopUpMenuWindowLevel    
NSScreenSaverWindowLevel 

If you would like to show a window topmost from Interfacebuilder (i.e. a call from the menu), create a IBAction in your App-Delegate and assign the call to this action:

-(IBAction)showWindowCalledFromMenu:(id)sender {

   [self.window setLevel: NSMainMenuWindowLevel];
   [self.window makeKeyAndOrderFront:self];
}

If you want the window being topmost only when your application is active, you have to know if your application is active. In your "main.xib" watch into the "actions" from "First Responder". You can use the following events to reset your windowlevels: "deminiaturize", "order Front", "order Back" and a lot more.

Hope this helps. Have fun!

Upvotes: 5

Chuck
Chuck

Reputation: 237110

There is not really any such concept as "topmost within an application" in the OS X windowing system. An application does not have a level of its own. The normal way to do what it sounds like you want is to have a floating panel that disappears when the application is inactive (see NSPanel's setFloatingPanel:). Otherwise, you'd have to switch its window level around yourself.

Upvotes: 0

Related Questions