1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import <Cocoa/Cocoa.h>
  27 #import <JavaNativeFoundation/JavaNativeFoundation.h>
  28 
  29 #import "sun_lwawt_macosx_CPlatformWindow.h"
  30 #import "com_apple_eawt_event_GestureHandler.h"
  31 #import "com_apple_eawt_FullScreenHandler.h"
  32 #import "ApplicationDelegate.h"
  33 
  34 #import "AWTWindow.h"
  35 #import "AWTView.h"
  36 #import "GeomUtilities.h"
  37 #import "ThreadUtilities.h"
  38 
  39 #define MASK(KEY) \
  40     (sun_lwawt_macosx_CPlatformWindow_ ## KEY)
  41 
  42 #define IS(BITS, KEY) \
  43     ((BITS & MASK(KEY)) != 0)
  44 
  45 #define SET(BITS, KEY, VALUE) \
  46     BITS = VALUE ? BITS | MASK(KEY) : BITS & ~MASK(KEY)
  47 
  48 static JNF_CLASS_CACHE(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow");
  49 
  50 // Cocoa windowDidBecomeKey/windowDidResignKey notifications
  51 // doesn't provide information about "opposite" window, so we
  52 // have to do a bit of tracking. This variable points to a window
  53 // which had been the key window just before a new key window
  54 // was set. It would be nil if the new key window isn't an AWT
  55 // window or the app currently has no key window.
  56 static AWTWindow* lastKeyWindow = nil;
  57 
  58 // This variable contains coordinates of a window's top left
  59 // which was positioned via java.awt.Window.setLocationByPlatform.
  60 // It would be NSZeroPoint if 'Location by Platform' is not used.
  61 static NSPoint lastTopLeftPoint;
  62 
  63 // --------------------------------------------------------------
  64 // NSWindow/NSPanel descendants implementation
  65 #define AWT_NS_WINDOW_IMPLEMENTATION                            \
  66 - (id) initWithDelegate:(AWTWindow *)delegate                   \
  67               frameRect:(NSRect)contectRect                     \
  68               styleMask:(NSUInteger)styleMask                   \
  69             contentView:(NSView *)view                          \
  70 {                                                               \
  71     self = [super initWithContentRect:contectRect               \
  72                             styleMask:styleMask                 \
  73                               backing:NSBackingStoreBuffered    \
  74                                 defer:NO];                      \
  75                                                                 \
  76     if (self == nil) return nil;                                \
  77                                                                 \
  78     [self setDelegate:delegate];                                \
  79     [self setContentView:view];                                 \
  80     [self setInitialFirstResponder:view];                       \
  81     [self setReleasedWhenClosed:NO];                            \
  82     [self setPreservesContentDuringLiveResize:YES];             \
  83                                                                 \
  84     return self;                                                \
  85 }                                                               \
  86                                                                 \
  87 /* NSWindow overrides */                                        \
  88 - (BOOL) canBecomeKeyWindow {                                   \
  89     return [(AWTWindow*)[self delegate] canBecomeKeyWindow];    \
  90 }                                                               \
  91                                                                 \
  92 - (BOOL) canBecomeMainWindow {                                  \
  93     return [(AWTWindow*)[self delegate] canBecomeMainWindow];   \
  94 }                                                               \
  95                                                                 \
  96 - (BOOL) worksWhenModal {                                       \
  97     return [(AWTWindow*)[self delegate] worksWhenModal];        \
  98 }                                                               \
  99                                                                 \
 100 - (void)sendEvent:(NSEvent *)event {                            \
 101     [(AWTWindow*)[self delegate] sendEvent:event];              \
 102     [super sendEvent:event];                                    \
 103 }
 104 
 105 @implementation AWTWindow_Normal
 106 AWT_NS_WINDOW_IMPLEMENTATION
 107 
 108 // Gesture support
 109 - (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b {
 110     AWT_ASSERT_APPKIT_THREAD;
 111 
 112     JNIEnv *env = [ThreadUtilities getJNIEnv];
 113     jobject platformWindow = [((AWTWindow *)self.delegate).javaPlatformWindow jObjectWithEnv:env];
 114     if (platformWindow != NULL) {
 115         // extract the target AWT Window object out of the CPlatformWindow
 116         static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 117         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 118         if (awtWindow != NULL) {
 119             // translate the point into Java coordinates
 120             NSPoint loc = [event locationInWindow];
 121             loc.y = [self frame].size.height - loc.y;
 122 
 123             // send up to the GestureHandler to recursively dispatch on the AWT event thread
 124             static JNF_CLASS_CACHE(jc_GestureHandler, "com/apple/eawt/event/GestureHandler");
 125             static JNF_STATIC_MEMBER_CACHE(sjm_handleGestureFromNative, jc_GestureHandler, "handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V");
 126             JNFCallStaticVoidMethod(env, sjm_handleGestureFromNative, awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b);
 127             (*env)->DeleteLocalRef(env, awtWindow);
 128         }
 129         (*env)->DeleteLocalRef(env, platformWindow);
 130     }
 131 }
 132 
 133 - (void)beginGestureWithEvent:(NSEvent *)event {
 134     [self postGesture:event
 135                    as:com_apple_eawt_event_GestureHandler_PHASE
 136                     a:-1.0
 137                     b:0.0];
 138 }
 139 
 140 - (void)endGestureWithEvent:(NSEvent *)event {
 141     [self postGesture:event
 142                    as:com_apple_eawt_event_GestureHandler_PHASE
 143                     a:1.0
 144                     b:0.0];
 145 }
 146 
 147 - (void)magnifyWithEvent:(NSEvent *)event {
 148     [self postGesture:event
 149                    as:com_apple_eawt_event_GestureHandler_MAGNIFY
 150                     a:[event magnification]
 151                     b:0.0];
 152 }
 153 
 154 - (void)rotateWithEvent:(NSEvent *)event {
 155     [self postGesture:event
 156                    as:com_apple_eawt_event_GestureHandler_ROTATE
 157                     a:[event rotation]
 158                     b:0.0];
 159 }
 160 
 161 - (void)swipeWithEvent:(NSEvent *)event {
 162     [self postGesture:event
 163                    as:com_apple_eawt_event_GestureHandler_SWIPE
 164                     a:[event deltaX]
 165                     b:[event deltaY]];
 166 }
 167 
 168 @end
 169 @implementation AWTWindow_Panel
 170 AWT_NS_WINDOW_IMPLEMENTATION
 171 @end
 172 // END of NSWindow/NSPanel descendants implementation
 173 // --------------------------------------------------------------
 174 
 175 
 176 @implementation AWTWindow
 177 
 178 @synthesize nsWindow;
 179 @synthesize javaPlatformWindow;
 180 @synthesize javaMenuBar;
 181 @synthesize javaMinSize;
 182 @synthesize javaMaxSize;
 183 @synthesize styleBits;
 184 @synthesize isEnabled;
 185 @synthesize ownerWindow;
 186 @synthesize preFullScreenLevel;
 187 @synthesize standardFrame;
 188 @synthesize isMinimizing;
 189 
 190 // Key notification received status
 191 static bool keyNotificationRecd = false;
 192 
 193 - (void) updateMinMaxSize:(BOOL)resizable {
 194     if (resizable) {
 195         [self.nsWindow setMinSize:self.javaMinSize];
 196         [self.nsWindow setMaxSize:self.javaMaxSize];
 197     } else {
 198         NSRect currentFrame = [self.nsWindow frame];
 199         [self.nsWindow setMinSize:currentFrame.size];
 200         [self.nsWindow setMaxSize:currentFrame.size];
 201     }
 202 }
 203 
 204 // creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits
 205 + (NSUInteger) styleMaskForStyleBits:(jint)styleBits {
 206     NSUInteger type = 0;
 207     if (IS(styleBits, DECORATED)) {
 208         type |= NSTitledWindowMask;
 209         if (IS(styleBits, CLOSEABLE))   type |= NSClosableWindowMask;
 210         if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask;
 211         if (IS(styleBits, RESIZABLE))   type |= NSResizableWindowMask;
 212     } else {
 213         type |= NSBorderlessWindowMask;
 214     }
 215 
 216     if (IS(styleBits, TEXTURED))      type |= NSTexturedBackgroundWindowMask;
 217     if (IS(styleBits, UNIFIED))       type |= NSUnifiedTitleAndToolbarWindowMask;
 218     if (IS(styleBits, UTILITY))       type |= NSUtilityWindowMask;
 219     if (IS(styleBits, HUD))           type |= NSHUDWindowMask;
 220     if (IS(styleBits, SHEET))         type |= NSWindowStyleMaskDocModalWindow;
 221     if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask;
 222 
 223     return type;
 224 }
 225 
 226 // updates _METHOD_PROP_BITMASK based properties on the window
 227 - (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask {
 228     if (IS(mask, RESIZABLE)) {
 229         BOOL resizable = IS(bits, RESIZABLE);
 230         [self updateMinMaxSize:resizable];
 231         [self.nsWindow setShowsResizeIndicator:resizable];
 232         // Zoom button should be disabled, if the window is not resizable,
 233         // otherwise button should be restored to initial state.
 234         BOOL zoom = resizable && IS(bits, ZOOMABLE);
 235         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:zoom];
 236     }
 237 
 238     if (IS(mask, HAS_SHADOW)) {
 239         [self.nsWindow setHasShadow:IS(bits, HAS_SHADOW)];
 240     }
 241 
 242     if (IS(mask, ZOOMABLE)) {
 243         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)];
 244     }
 245 
 246     if (IS(mask, ALWAYS_ON_TOP)) {
 247         [self.nsWindow setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel];
 248     }
 249 
 250     if (IS(mask, HIDES_ON_DEACTIVATE)) {
 251         [self.nsWindow setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)];
 252     }
 253 
 254     if (IS(mask, DRAGGABLE_BACKGROUND)) {
 255         [self.nsWindow setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)];
 256     }
 257 
 258     if (IS(mask, DOCUMENT_MODIFIED)) {
 259         [self.nsWindow setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)];
 260     }
 261 
 262     if (IS(mask, FULLSCREENABLE) && [self.nsWindow respondsToSelector:@selector(toggleFullScreen:)]) {
 263         if (IS(bits, FULLSCREENABLE)) {
 264             [self.nsWindow setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/];
 265         } else {
 266             [self.nsWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault];
 267         }
 268     }
 269 }
 270 
 271 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow
 272                   ownerWindow:owner
 273                     styleBits:(jint)bits
 274                     frameRect:(NSRect)rect
 275                   contentView:(NSView *)view
 276 {
 277 AWT_ASSERT_APPKIT_THREAD;
 278 
 279     NSUInteger newBits = bits;
 280     if (IS(bits, SHEET) && owner == nil) {
 281         newBits = bits & ~NSWindowStyleMaskDocModalWindow;
 282     }
 283     NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:newBits];
 284 
 285     NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
 286     if (contentRect.size.width <= 0.0) {
 287         contentRect.size.width = 1.0;
 288     }
 289     if (contentRect.size.height <= 0.0) {
 290         contentRect.size.height = 1.0;
 291     }
 292 
 293     self = [super init];
 294 
 295     if (self == nil) return nil; // no hope
 296 
 297     if (IS(bits, UTILITY) ||
 298         IS(bits, NONACTIVATING) ||
 299         IS(bits, HUD) ||
 300         IS(bits, HIDES_ON_DEACTIVATE) ||
 301         IS(bits, SHEET))
 302     {
 303         self.nsWindow = [[AWTWindow_Panel alloc] initWithDelegate:self
 304                             frameRect:contentRect
 305                             styleMask:styleMask
 306                           contentView:view];
 307     }
 308     else
 309     {
 310         // These windows will appear in the window list in the dock icon menu
 311         self.nsWindow = [[AWTWindow_Normal alloc] initWithDelegate:self
 312                             frameRect:contentRect
 313                             styleMask:styleMask
 314                           contentView:view];
 315     }
 316 
 317     if (self.nsWindow == nil) return nil; // no hope either
 318     [self.nsWindow release]; // the property retains the object already
 319 
 320     self.isEnabled = YES;
 321     self.isMinimizing = NO;
 322     self.javaPlatformWindow = platformWindow;
 323     self.styleBits = bits;
 324     self.ownerWindow = owner;
 325     [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
 326 
 327     if (IS(self.styleBits, IS_POPUP)) {
 328         [self.nsWindow setCollectionBehavior:(1 << 8) /*NSWindowCollectionBehaviorFullScreenAuxiliary*/];
 329     }
 330 
 331     if (IS(bits, SHEET) && owner != nil) {
 332         [self.nsWindow setStyleMask: NSWindowStyleMaskDocModalWindow];
 333     }
 334 
 335     return self;
 336 }
 337 
 338 + (BOOL) isAWTWindow:(NSWindow *)window {
 339     return [window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]];
 340 }
 341 
 342 // Retrieves the list of possible window layers (levels)
 343 + (NSArray*) getWindowLayers {
 344     static NSArray *windowLayers;
 345     static dispatch_once_t token;
 346 
 347     // Initialize the list of possible window layers
 348     dispatch_once(&token, ^{
 349         // The layers are ordered from front to back, (i.e. the toppest one is the first)
 350         windowLayers = [NSArray arrayWithObjects:
 351                             [NSNumber numberWithInt:CGWindowLevelForKey(kCGPopUpMenuWindowLevelKey)],
 352                             [NSNumber numberWithInt:CGWindowLevelForKey(kCGFloatingWindowLevelKey)],
 353                             [NSNumber numberWithInt:CGWindowLevelForKey(kCGNormalWindowLevelKey)],
 354                             nil
 355                         ];
 356         [windowLayers retain];
 357     });
 358     return windowLayers;
 359 }
 360 
 361 // returns id for the topmost window under mouse
 362 + (NSInteger) getTopmostWindowUnderMouseID {
 363     NSInteger result = -1;
 364 
 365     NSArray *windowLayers = [AWTWindow getWindowLayers];
 366     // Looking for the window under mouse starting from the toppest layer
 367     for (NSNumber *layer in windowLayers) {
 368         result = [AWTWindow getTopmostWindowUnderMouseIDImpl:[layer integerValue]];
 369         if (result != -1) {
 370             break;
 371         }
 372     }
 373     return result;
 374 }
 375 
 376 + (NSInteger) getTopmostWindowUnderMouseIDImpl:(NSInteger)windowLayer {
 377     NSInteger result = -1;
 378 
 379     NSRect screenRect = [[NSScreen mainScreen] frame];
 380     NSPoint nsMouseLocation = [NSEvent mouseLocation];
 381     CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y);
 382 
 383     NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
 384 
 385     for (NSDictionary *window in windows) {
 386         NSInteger layer = [[window objectForKey:(id)kCGWindowLayer] integerValue];
 387         if (layer == windowLayer) {
 388             CGRect rect;
 389             CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect);
 390             if (CGRectContainsPoint(rect, cgMouseLocation)) {
 391                 result = [[window objectForKey:(id)kCGWindowNumber] integerValue];
 392                 break;
 393             }
 394         }
 395     }
 396     [windows release];
 397     return result;
 398 }
 399 
 400 // checks that this window is under the mouse cursor and this point is not overlapped by others windows
 401 - (BOOL) isTopmostWindowUnderMouse {
 402     return [self.nsWindow windowNumber] == [AWTWindow getTopmostWindowUnderMouseID];
 403 }
 404 
 405 + (AWTWindow *) getTopmostWindowUnderMouse {
 406     NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator];
 407     NSWindow *window;
 408 
 409     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 410 
 411     while ((window = [windowEnumerator nextObject]) != nil) {
 412         if ([window windowNumber] == topmostWindowUnderMouseID) {
 413             BOOL isAWTWindow = [AWTWindow isAWTWindow: window];
 414             return isAWTWindow ? (AWTWindow *) [window delegate] : nil;
 415         }
 416     }
 417     return nil;
 418 }
 419 
 420 + (void) synthesizeMouseEnteredExitedEvents:(NSWindow*)window withType:(NSEventType)eventType {
 421 
 422     NSPoint screenLocation = [NSEvent mouseLocation];
 423     NSPoint windowLocation = [window convertScreenToBase: screenLocation];
 424     int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask;
 425 
 426     NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType
 427                                                  location: windowLocation
 428                                             modifierFlags: modifierFlags
 429                                                 timestamp: 0
 430                                              windowNumber: [window windowNumber]
 431                                                   context: nil
 432                                               eventNumber: 0
 433                                            trackingNumber: 0
 434                                                  userData: nil
 435                            ];
 436 
 437     [[window contentView] deliverJavaMouseEvent: mouseEvent];
 438 }
 439 
 440 + (void) synthesizeMouseEnteredExitedEventsForAllWindows {
 441 
 442     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 443     NSArray *windows = [NSApp windows];
 444     NSWindow *window;
 445 
 446     NSEnumerator *windowEnumerator = [windows objectEnumerator];
 447     while ((window = [windowEnumerator nextObject]) != nil) {
 448         if ([AWTWindow isAWTWindow: window]) {
 449             BOOL isUnderMouse = ([window windowNumber] == topmostWindowUnderMouseID);
 450             BOOL mouseIsOver = [[window contentView] mouseIsOver];
 451             if (isUnderMouse && !mouseIsOver) {
 452                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseEntered];
 453             } else if (!isUnderMouse && mouseIsOver) {
 454                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseExited];
 455             }
 456         }
 457     }
 458 }
 459 
 460 + (NSNumber *) getNSWindowDisplayID_AppKitThread:(NSWindow *)window {
 461     AWT_ASSERT_APPKIT_THREAD;
 462     NSScreen *screen = [window screen];
 463     NSDictionary *deviceDescription = [screen deviceDescription];
 464     return [deviceDescription objectForKey:@"NSScreenNumber"];
 465 }
 466 
 467 - (void) dealloc {
 468 AWT_ASSERT_APPKIT_THREAD;
 469 
 470     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 471     [self.javaPlatformWindow setJObject:nil withEnv:env];
 472 
 473     self.nsWindow = nil;
 474     self.ownerWindow = nil;
 475     [super dealloc];
 476 }
 477 
 478 // Tests whether window is blocked by modal dialog/window
 479 - (BOOL) isBlocked {
 480     BOOL isBlocked = NO;
 481 
 482     JNIEnv *env = [ThreadUtilities getJNIEnv];
 483     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 484     if (platformWindow != NULL) {
 485         static JNF_MEMBER_CACHE(jm_isBlocked, jc_CPlatformWindow, "isBlocked", "()Z");
 486         isBlocked = JNFCallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO;
 487         (*env)->DeleteLocalRef(env, platformWindow);
 488     }
 489 
 490     return isBlocked;
 491 }
 492 
 493 // Test whether window is simple window and owned by embedded frame
 494 - (BOOL) isSimpleWindowOwnedByEmbeddedFrame {
 495     BOOL isSimpleWindowOwnedByEmbeddedFrame = NO;
 496 
 497     JNIEnv *env = [ThreadUtilities getJNIEnv];
 498     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 499     if (platformWindow != NULL) {
 500         static JNF_MEMBER_CACHE(jm_isBlocked, jc_CPlatformWindow, "isSimpleWindowOwnedByEmbeddedFrame", "()Z");
 501         isSimpleWindowOwnedByEmbeddedFrame = JNFCallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO;
 502         (*env)->DeleteLocalRef(env, platformWindow);
 503     }
 504 
 505     return isSimpleWindowOwnedByEmbeddedFrame;
 506 }
 507 
 508 // Tests whether the corresponding Java platform window is visible or not
 509 + (BOOL) isJavaPlatformWindowVisible:(NSWindow *)window {
 510     BOOL isVisible = NO;
 511 
 512     if ([AWTWindow isAWTWindow:window] && [window delegate] != nil) {
 513         AWTWindow *awtWindow = (AWTWindow *)[window delegate];
 514         [AWTToolkit eventCountPlusPlus];
 515 
 516         JNIEnv *env = [ThreadUtilities getJNIEnv];
 517         jobject platformWindow = [awtWindow.javaPlatformWindow jObjectWithEnv:env];
 518         if (platformWindow != NULL) {
 519             static JNF_MEMBER_CACHE(jm_isVisible, jc_CPlatformWindow, "isVisible", "()Z");
 520             isVisible = JNFCallBooleanMethod(env, platformWindow, jm_isVisible) == JNI_TRUE ? YES : NO;
 521             (*env)->DeleteLocalRef(env, platformWindow);
 522 
 523         }
 524     }
 525     return isVisible;
 526 }
 527 
 528 // Orders window's childs based on the current focus state
 529 - (void) orderChildWindows:(BOOL)focus {
 530 AWT_ASSERT_APPKIT_THREAD;
 531 
 532     if (self.isMinimizing || [self isBlocked]) {
 533         // Do not perform any ordering, if iconify is in progress
 534         // or the window is blocked by a modal window
 535         return;
 536     }
 537 
 538     NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator];
 539     NSWindow *window;
 540     while ((window = [windowEnumerator nextObject]) != nil) {
 541         if ([AWTWindow isJavaPlatformWindowVisible:window]) {
 542             AWTWindow *awtWindow = (AWTWindow *)[window delegate];
 543             AWTWindow *owner = awtWindow.ownerWindow;
 544             if (IS(awtWindow.styleBits, ALWAYS_ON_TOP)) {
 545                 // Do not order 'always on top' windows
 546                 continue;
 547             }
 548             while (awtWindow.ownerWindow != nil) {
 549                 if (awtWindow.ownerWindow == self) {
 550                     if (focus) {
 551                         // Move the childWindow to floating level
 552                         // so it will appear in front of its
 553                         // parent which owns the focus
 554                         [window setLevel:NSFloatingWindowLevel];
 555                     } else {
 556                         // Focus owner has changed, move the childWindow
 557                         // back to normal window level
 558                         [window setLevel:NSNormalWindowLevel];
 559                     }
 560                     // The childWindow should be displayed in front of
 561                     // its nearest parentWindow
 562                     [window orderWindow:NSWindowAbove relativeTo:[owner.nsWindow windowNumber]];
 563                     break;
 564                 }
 565                 awtWindow = awtWindow.ownerWindow;
 566             }
 567         }
 568     }
 569 }
 570 
 571 // NSWindow overrides
 572 - (BOOL) canBecomeKeyWindow {
 573 AWT_ASSERT_APPKIT_THREAD;
 574     return self.isEnabled && (IS(self.styleBits, SHOULD_BECOME_KEY) || [self isSimpleWindowOwnedByEmbeddedFrame]);
 575 }
 576 
 577 - (BOOL) canBecomeMainWindow {
 578 AWT_ASSERT_APPKIT_THREAD;
 579     if (!self.isEnabled) {
 580         // Native system can bring up the NSWindow to
 581         // the top even if the window is not main.
 582         // We should bring up the modal dialog manually
 583         [AWTToolkit eventCountPlusPlus];
 584 
 585         JNIEnv *env = [ThreadUtilities getJNIEnv];
 586         jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 587         if (platformWindow != NULL) {
 588             static JNF_MEMBER_CACHE(jm_checkBlockingAndOrder, jc_CPlatformWindow,
 589                                     "checkBlockingAndOrder", "()Z");
 590             JNFCallBooleanMethod(env, platformWindow, jm_checkBlockingAndOrder);
 591             (*env)->DeleteLocalRef(env, platformWindow);
 592         }
 593     }
 594 
 595     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN);
 596 }
 597 
 598 - (BOOL) worksWhenModal {
 599 AWT_ASSERT_APPKIT_THREAD;
 600     return IS(self.styleBits, MODAL_EXCLUDED);
 601 }
 602 
 603 
 604 // NSWindowDelegate methods
 605 
 606 - (void) _deliverMoveResizeEvent {
 607 AWT_ASSERT_APPKIT_THREAD;
 608 
 609     // deliver the event if this is a user-initiated live resize or as a side-effect
 610     // of a Java initiated resize, because AppKit can override the bounds and force
 611     // the bounds of the window to avoid the Dock or remain on screen.
 612     [AWTToolkit eventCountPlusPlus];
 613     JNIEnv *env = [ThreadUtilities getJNIEnv];
 614     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 615     if (platformWindow == NULL) {
 616         // TODO: create generic AWT assert
 617     }
 618 
 619     NSRect frame = ConvertNSScreenRect(env, [self.nsWindow frame]);
 620 
 621     static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIIIZ)V");
 622     JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
 623                       (jint)frame.origin.x,
 624                       (jint)frame.origin.y,
 625                       (jint)frame.size.width,
 626                       (jint)frame.size.height,
 627                       (jboolean)[self.nsWindow inLiveResize]);
 628     (*env)->DeleteLocalRef(env, platformWindow);
 629 
 630     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 631 }
 632 
 633 - (void)windowDidMove:(NSNotification *)notification {
 634 AWT_ASSERT_APPKIT_THREAD;
 635 
 636     [self _deliverMoveResizeEvent];
 637 }
 638 
 639 - (void)windowDidResize:(NSNotification *)notification {
 640 AWT_ASSERT_APPKIT_THREAD;
 641 
 642     [self _deliverMoveResizeEvent];
 643 }
 644 
 645 - (void)windowDidExpose:(NSNotification *)notification {
 646 AWT_ASSERT_APPKIT_THREAD;
 647 
 648     [AWTToolkit eventCountPlusPlus];
 649     // TODO: don't see this callback invoked anytime so we track
 650     // window exposing in _setVisible:(BOOL)
 651 }
 652 
 653 - (NSRect)windowWillUseStandardFrame:(NSWindow *)window
 654                         defaultFrame:(NSRect)newFrame {
 655 
 656     return NSEqualSizes(NSZeroSize, [self standardFrame].size)
 657                 ? newFrame
 658                 : [self standardFrame];
 659 }
 660 
 661 // Hides/shows window's childs during iconify/de-iconify operation
 662 - (void) iconifyChildWindows:(BOOL)iconify {
 663 AWT_ASSERT_APPKIT_THREAD;
 664 
 665     NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator];
 666     NSWindow *window;
 667     while ((window = [windowEnumerator nextObject]) != nil) {
 668         if ([AWTWindow isJavaPlatformWindowVisible:window]) {
 669             AWTWindow *awtWindow = (AWTWindow *)[window delegate];
 670             while (awtWindow.ownerWindow != nil) {
 671                 if (awtWindow.ownerWindow == self) {
 672                     if (iconify) {
 673                         [window orderOut:window];
 674                     } else {
 675                         [window orderFront:window];
 676                     }
 677                     break;
 678                 }
 679                 awtWindow = awtWindow.ownerWindow;
 680             }
 681         }
 682     }
 683 }
 684 
 685 - (void) _deliverIconify:(BOOL)iconify {
 686 AWT_ASSERT_APPKIT_THREAD;
 687 
 688     [AWTToolkit eventCountPlusPlus];
 689     JNIEnv *env = [ThreadUtilities getJNIEnv];
 690     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 691     if (platformWindow != NULL) {
 692         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 693         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 694         (*env)->DeleteLocalRef(env, platformWindow);
 695     }
 696 }
 697 
 698 - (void)windowWillMiniaturize:(NSNotification *)notification {
 699 AWT_ASSERT_APPKIT_THREAD;
 700 
 701     self.isMinimizing = YES;
 702 
 703     JNIEnv *env = [ThreadUtilities getJNIEnv];
 704     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 705     if (platformWindow != NULL) {
 706         static JNF_MEMBER_CACHE(jm_windowWillMiniaturize, jc_CPlatformWindow, "windowWillMiniaturize", "()V");
 707         JNFCallVoidMethod(env, platformWindow, jm_windowWillMiniaturize);
 708         (*env)->DeleteLocalRef(env, platformWindow);
 709     }
 710     // Explicitly make myself a key window to avoid possible
 711     // negative visual effects during iconify operation
 712     [self.nsWindow makeKeyAndOrderFront:self.nsWindow];
 713     [self iconifyChildWindows:YES];
 714 }
 715 
 716 - (void)windowDidMiniaturize:(NSNotification *)notification {
 717 AWT_ASSERT_APPKIT_THREAD;
 718 
 719     [self _deliverIconify:JNI_TRUE];
 720     self.isMinimizing = NO;
 721 }
 722 
 723 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 724 AWT_ASSERT_APPKIT_THREAD;
 725 
 726     [self _deliverIconify:JNI_FALSE];
 727     [self iconifyChildWindows:NO];
 728 }
 729 
 730 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
 731 //AWT_ASSERT_APPKIT_THREAD;
 732     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 733     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 734     if (platformWindow != NULL) {
 735         jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env];
 736 
 737         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
 738         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
 739         (*env)->DeleteLocalRef(env, platformWindow);
 740         (*env)->DeleteLocalRef(env, oppositeWindow);
 741     }
 742 }
 743 
 744 - (void) windowDidBecomeMain: (NSNotification *) notification {
 745 AWT_ASSERT_APPKIT_THREAD;
 746     [AWTToolkit eventCountPlusPlus];
 747 #ifdef DEBUG
 748     NSLog(@"became main: %d %@ %@ %d", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow], keyNotificationRecd);
 749 #endif
 750 
 751     // if for some reason, no KEY notification is received but this main window is also a key window
 752     // then we need to execute the KEY notification functionality.
 753     if(keyNotificationRecd != true && [self.nsWindow isKeyWindow]) {
 754         [self doWindowDidBecomeKey];
 755     }
 756     keyNotificationRecd = false;
 757 
 758     if (![self.nsWindow isKeyWindow]) {
 759         [self activateWindowMenuBar];
 760     }
 761 
 762     JNIEnv *env = [ThreadUtilities getJNIEnv];
 763     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 764     if (platformWindow != NULL) {
 765         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 766         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 767         (*env)->DeleteLocalRef(env, platformWindow);
 768     }
 769 }
 770 
 771 - (void) windowDidBecomeKey: (NSNotification *) notification {
 772 AWT_ASSERT_APPKIT_THREAD;
 773     [AWTToolkit eventCountPlusPlus];
 774 #ifdef DEBUG
 775     NSLog(@"became key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
 776 #endif
 777     [self doWindowDidBecomeKey];
 778     keyNotificationRecd = true;
 779 }
 780 
 781 - (void) doWindowDidBecomeKey {
 782 AWT_ASSERT_APPKIT_THREAD;
 783     AWTWindow *opposite = [AWTWindow lastKeyWindow];
 784 
 785     if (![self.nsWindow isMainWindow]) {
 786         [self activateWindowMenuBar];
 787     }
 788 
 789     [AWTWindow setLastKeyWindow:nil];
 790 
 791     [self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
 792     [self orderChildWindows:YES];
 793 }
 794 
 795 - (void) activateWindowMenuBar {
 796 AWT_ASSERT_APPKIT_THREAD;
 797     // Finds appropriate menubar in our hierarchy
 798     AWTWindow *awtWindow = self;
 799     while (awtWindow.ownerWindow != nil) {
 800         awtWindow = awtWindow.ownerWindow;
 801     }
 802 
 803     CMenuBar *menuBar = nil;
 804     BOOL isDisabled = NO;
 805     if ([awtWindow.nsWindow isVisible]){
 806         menuBar = awtWindow.javaMenuBar;
 807         isDisabled = !awtWindow.isEnabled;
 808     }
 809 
 810     if (menuBar == nil) {
 811         menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 812         isDisabled = NO;
 813     }
 814 
 815     [CMenuBar activate:menuBar modallyDisabled:isDisabled];
 816 }
 817 
 818 #ifdef DEBUG
 819 - (CMenuBar *) menuBarForWindow {
 820 AWT_ASSERT_APPKIT_THREAD;
 821     AWTWindow *awtWindow = self;
 822     while (awtWindow.ownerWindow != nil) {
 823         awtWindow = awtWindow.ownerWindow;
 824     }
 825     return awtWindow.javaMenuBar;
 826 }
 827 #endif
 828 
 829 - (void) windowDidResignKey: (NSNotification *) notification {
 830     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 831 AWT_ASSERT_APPKIT_THREAD;
 832     [AWTToolkit eventCountPlusPlus];
 833 #ifdef DEBUG
 834     NSLog(@"resigned key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
 835 #endif
 836     if (![self.nsWindow isMainWindow]) {
 837         [self deactivateWindow];
 838     }
 839 }
 840 
 841 - (void) windowDidResignMain: (NSNotification *) notification {
 842 AWT_ASSERT_APPKIT_THREAD;
 843     [AWTToolkit eventCountPlusPlus];
 844 #ifdef DEBUG
 845     NSLog(@"resigned main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]);
 846 #endif
 847     if (![self.nsWindow isKeyWindow]) {
 848         [self deactivateWindow];
 849     }
 850 }
 851 
 852 - (void) deactivateWindow {
 853 AWT_ASSERT_APPKIT_THREAD;
 854 #ifdef DEBUG
 855     NSLog(@"deactivating window: %@", [self.nsWindow title]);
 856 #endif
 857     [self.javaMenuBar deactivate];
 858 
 859     // the new key window
 860     NSWindow *keyWindow = [NSApp keyWindow];
 861     AWTWindow *opposite = nil;
 862     if ([AWTWindow isAWTWindow: keyWindow]) {
 863         opposite = (AWTWindow *)[keyWindow delegate];
 864         [AWTWindow setLastKeyWindow: self];
 865     } else {
 866         [AWTWindow setLastKeyWindow: nil];
 867     }
 868 
 869     [self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
 870     [self orderChildWindows:NO];
 871 }
 872 
 873 - (BOOL)windowShouldClose:(id)sender {
 874 AWT_ASSERT_APPKIT_THREAD;
 875     [AWTToolkit eventCountPlusPlus];
 876     JNIEnv *env = [ThreadUtilities getJNIEnv];
 877     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 878     if (platformWindow != NULL) {
 879         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 880         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 881         (*env)->DeleteLocalRef(env, platformWindow);
 882     }
 883     // The window will be closed (if allowed) as result of sending Java event
 884     return NO;
 885 }
 886 
 887 
 888 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 889     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 890     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 891     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 892     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 893     if (platformWindow != NULL) {
 894         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 895         if (awtWindow != NULL) {
 896             JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op);
 897             (*env)->DeleteLocalRef(env, awtWindow);
 898         }
 899         (*env)->DeleteLocalRef(env, platformWindow);
 900     }
 901 }
 902 
 903 
 904 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
 905     static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
 906     JNIEnv *env = [ThreadUtilities getJNIEnv];
 907     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 908     if (platformWindow != NULL) {
 909         JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
 910         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
 911         (*env)->DeleteLocalRef(env, platformWindow);
 912     }
 913 }
 914 
 915 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
 916     static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
 917     JNIEnv *env = [ThreadUtilities getJNIEnv];
 918     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 919     if (platformWindow != NULL) {
 920         JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
 921         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
 922         (*env)->DeleteLocalRef(env, platformWindow);
 923     }
 924     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 925 }
 926 
 927 - (void)windowWillExitFullScreen:(NSNotification *)notification {
 928     static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
 929     JNIEnv *env = [ThreadUtilities getJNIEnv];
 930     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 931     if (platformWindow != NULL) {
 932         JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
 933         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
 934         (*env)->DeleteLocalRef(env, platformWindow);
 935     }
 936 }
 937 
 938 - (void)windowDidExitFullScreen:(NSNotification *)notification {
 939     static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
 940     JNIEnv *env = [ThreadUtilities getJNIEnv];
 941     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 942     if (platformWindow != NULL) {
 943         JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
 944         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 945         (*env)->DeleteLocalRef(env, platformWindow);
 946     }
 947     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 948 }
 949 
 950 - (void)sendEvent:(NSEvent *)event {
 951         if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
 952             if ([self isBlocked]) {
 953                 // Move parent windows to front and make sure that a child window is displayed
 954                 // in front of its nearest parent.
 955                 if (self.ownerWindow != nil) {
 956                     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 957                     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 958                     if (platformWindow != NULL) {
 959                         static JNF_MEMBER_CACHE(jm_orderAboveSiblings, jc_CPlatformWindow, "orderAboveSiblings", "()V");
 960                         JNFCallVoidMethod(env,platformWindow, jm_orderAboveSiblings);
 961                         (*env)->DeleteLocalRef(env, platformWindow);
 962                     }
 963                 }
 964                 [self orderChildWindows:YES];
 965             }
 966 
 967             NSPoint p = [NSEvent mouseLocation];
 968             NSRect frame = [self.nsWindow frame];
 969             NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame];
 970 
 971             // Check if the click happened in the non-client area (title bar)
 972             if (p.y >= (frame.origin.y + contentRect.size.height)) {
 973                 JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 974                 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 975                 if (platformWindow != NULL) {
 976                     // Currently, no need to deliver the whole NSEvent.
 977                     static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
 978                     JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
 979                     (*env)->DeleteLocalRef(env, platformWindow);
 980                 }
 981             }
 982         }
 983 }
 984 
 985 - (void)constrainSize:(NSSize*)size {
 986     float minWidth = 0.f, minHeight = 0.f;
 987 
 988     if (IS(self.styleBits, DECORATED)) {
 989         NSRect frame = [self.nsWindow frame];
 990         NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self.nsWindow styleMask]];
 991 
 992         float top = frame.size.height - contentRect.size.height;
 993         float left = contentRect.origin.x - frame.origin.x;
 994         float bottom = contentRect.origin.y - frame.origin.y;
 995         float right = frame.size.width - (contentRect.size.width + left);
 996 
 997         // Speculative estimation: 80 - enough for window decorations controls
 998         minWidth += left + right + 80;
 999         minHeight += top + bottom;
1000     }
1001 
1002     minWidth = MAX(1.f, minWidth);
1003     minHeight = MAX(1.f, minHeight);
1004 
1005     size->width = MAX(size->width, minWidth);
1006     size->height = MAX(size->height, minHeight);
1007 }
1008 
1009 - (void) setEnabled: (BOOL)flag {
1010     self.isEnabled = flag;
1011 
1012     if (IS(self.styleBits, CLOSEABLE)) {
1013         [[self.nsWindow standardWindowButton:NSWindowCloseButton] setEnabled: flag];
1014     }
1015 
1016     if (IS(self.styleBits, MINIMIZABLE)) {
1017         [[self.nsWindow standardWindowButton:NSWindowMiniaturizeButton] setEnabled: flag];
1018     }
1019 
1020     if (IS(self.styleBits, ZOOMABLE)) {
1021         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled: flag];
1022     }
1023 
1024     if (IS(self.styleBits, RESIZABLE)) {
1025         [self updateMinMaxSize:flag];
1026         [self.nsWindow setShowsResizeIndicator:flag];
1027     }
1028 }
1029 
1030 + (void) setLastKeyWindow:(AWTWindow *)window {
1031     [window retain];
1032     [lastKeyWindow release];
1033     lastKeyWindow = window;
1034 }
1035 
1036 + (AWTWindow *) lastKeyWindow {
1037     return lastKeyWindow;
1038 }
1039 
1040 @end // AWTWindow
1041 
1042 
1043 /*
1044  * Class:     sun_lwawt_macosx_CPlatformWindow
1045  * Method:    nativeCreateNSWindow
1046  * Signature: (JJIIII)J
1047  */
1048 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow
1049 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong ownerPtr, jlong styleBits, jdouble x, jdouble y, jdouble w, jdouble h)
1050 {
1051     __block AWTWindow *window = nil;
1052 
1053 JNF_COCOA_ENTER(env);
1054 
1055     JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
1056     NSView *contentView = OBJC(contentViewPtr);
1057     NSRect frameRect = NSMakeRect(x, y, w, h);
1058     AWTWindow *owner = [OBJC(ownerPtr) delegate];
1059     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1060 
1061         window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow
1062                                                ownerWindow:owner
1063                                                  styleBits:styleBits
1064                                                  frameRect:frameRect
1065                                                contentView:contentView];
1066         // the window is released is CPlatformWindow.nativeDispose()
1067 
1068         if (window) [window.nsWindow retain];
1069     }];
1070 
1071 JNF_COCOA_EXIT(env);
1072 
1073     return ptr_to_jlong(window ? window.nsWindow : nil);
1074 }
1075 
1076 /*
1077  * Class:     sun_lwawt_macosx_CPlatformWindow
1078  * Method:    nativeSetNSWindowStyleBits
1079  * Signature: (JII)V
1080  */
1081 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStyleBits
1082 (JNIEnv *env, jclass clazz, jlong windowPtr, jint mask, jint bits)
1083 {
1084 JNF_COCOA_ENTER(env);
1085 
1086     NSWindow *nsWindow = OBJC(windowPtr);
1087     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1088 
1089         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1090 
1091         // scans the bit field, and only updates the values requested by the mask
1092         // (this implicity handles the _CALLBACK_PROP_BITMASK case, since those are passive reads)
1093         jint newBits = window.styleBits & ~mask | bits & mask;
1094 
1095         // resets the NSWindow's style mask if the mask intersects any of those bits
1096         if (mask & MASK(_STYLE_PROP_BITMASK)) {
1097             [nsWindow setStyleMask:[AWTWindow styleMaskForStyleBits:newBits]];
1098         }
1099 
1100         // calls methods on NSWindow to change other properties, based on the mask
1101         if (mask & MASK(_METHOD_PROP_BITMASK)) {
1102             [window setPropertiesForStyleBits:newBits mask:mask];
1103         }
1104 
1105         window.styleBits = newBits;
1106     }];
1107 
1108 JNF_COCOA_EXIT(env);
1109 }
1110 
1111 /*
1112  * Class:     sun_lwawt_macosx_CPlatformWindow
1113  * Method:    nativeSetNSWindowMenuBar
1114  * Signature: (JJ)V
1115  */
1116 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
1117 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
1118 {
1119 JNF_COCOA_ENTER(env);
1120 
1121     NSWindow *nsWindow = OBJC(windowPtr);
1122     CMenuBar *menuBar = OBJC(menuBarPtr);
1123     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1124 
1125         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1126 
1127         if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) {
1128             [window.javaMenuBar deactivate];
1129         }
1130 
1131         window.javaMenuBar = menuBar;
1132 
1133         CMenuBar* actualMenuBar = menuBar;
1134         if (actualMenuBar == nil) {
1135             actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
1136         }
1137 
1138         if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) {
1139             [CMenuBar activate:actualMenuBar modallyDisabled:NO];
1140         }
1141     }];
1142 
1143 JNF_COCOA_EXIT(env);
1144 }
1145 
1146 /*
1147  * Class:     sun_lwawt_macosx_CPlatformWindow
1148  * Method:    nativeGetNSWindowInsets
1149  * Signature: (J)Ljava/awt/Insets;
1150  */
1151 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
1152 (JNIEnv *env, jclass clazz, jlong windowPtr)
1153 {
1154     jobject ret = NULL;
1155 
1156 JNF_COCOA_ENTER(env);
1157 
1158     NSWindow *nsWindow = OBJC(windowPtr);
1159     __block NSRect contentRect = NSZeroRect;
1160     __block NSRect frame = NSZeroRect;
1161 
1162     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
1163 
1164         frame = [nsWindow frame];
1165         contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[nsWindow styleMask]];
1166     }];
1167 
1168     jint top = (jint)(frame.size.height - contentRect.size.height);
1169     jint left = (jint)(contentRect.origin.x - frame.origin.x);
1170     jint bottom = (jint)(contentRect.origin.y - frame.origin.y);
1171     jint right = (jint)(frame.size.width - (contentRect.size.width + left));
1172 
1173     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
1174     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
1175     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
1176 
1177 JNF_COCOA_EXIT(env);
1178     return ret;
1179 }
1180 
1181 /*
1182  * Class:     sun_lwawt_macosx_CPlatformWindow
1183  * Method:    nativeSetNSWindowBounds
1184  * Signature: (JDDDD)V
1185  */
1186 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowBounds
1187 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, jdouble width, jdouble height)
1188 {
1189 JNF_COCOA_ENTER(env);
1190 
1191     NSRect jrect = NSMakeRect(originX, originY, width, height);
1192 
1193     // TODO: not sure we need displayIfNeeded message in our view
1194     NSWindow *nsWindow = OBJC(windowPtr);
1195     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1196 
1197         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1198 
1199         NSRect rect = ConvertNSScreenRect(NULL, jrect);
1200         [window constrainSize:&rect.size];
1201 
1202         [nsWindow setFrame:rect display:YES];
1203 
1204         // only start tracking events if pointer is above the toplevel
1205         // TODO: should post an Entered event if YES.
1206         NSPoint mLocation = [NSEvent mouseLocation];
1207         [nsWindow setAcceptsMouseMovedEvents:NSPointInRect(mLocation, rect)];
1208 
1209         // ensure we repaint the whole window after the resize operation
1210         // (this will also re-enable screen updates, which were disabled above)
1211         // TODO: send PaintEvent
1212     }];
1213 
1214 JNF_COCOA_EXIT(env);
1215 }
1216 
1217 /*
1218  * Class:     sun_lwawt_macosx_CPlatformWindow
1219  * Method:    nativeSetNSWindowStandardFrame
1220  * Signature: (JDDDD)V
1221  */
1222 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStandardFrame
1223 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY,
1224      jdouble width, jdouble height)
1225 {
1226     JNF_COCOA_ENTER(env);
1227 
1228     NSRect jrect = NSMakeRect(originX, originY, width, height);
1229 
1230     NSWindow *nsWindow = OBJC(windowPtr);
1231     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1232 
1233         NSRect rect = ConvertNSScreenRect(NULL, jrect);
1234         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1235         window.standardFrame = rect;
1236     }];
1237 
1238     JNF_COCOA_EXIT(env);
1239 }
1240 
1241 /*
1242  * Class:     sun_lwawt_macosx_CPlatformWindow
1243  * Method:    nativeSetNSWindowLocationByPlatform
1244  * Signature: (J)V
1245  */
1246 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowLocationByPlatform
1247 (JNIEnv *env, jclass clazz, jlong windowPtr)
1248 {
1249     JNF_COCOA_ENTER(env);
1250 
1251     NSWindow *nsWindow = OBJC(windowPtr);
1252     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1253 
1254         if (NSEqualPoints(lastTopLeftPoint, NSZeroPoint)) {
1255             // This is the first usage of lastTopLeftPoint. So invoke cascadeTopLeftFromPoint
1256             // twice to avoid positioning the window's top left to zero-point, since it may
1257             // cause negative user experience.
1258             lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint];
1259         }
1260         lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint];
1261     }];
1262 
1263     JNF_COCOA_EXIT(env);
1264 }
1265 
1266 /*
1267  * Class:     sun_lwawt_macosx_CPlatformWindow
1268  * Method:    nativeSetNSWindowMinMax
1269  * Signature: (JDDDD)V
1270  */
1271 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax
1272 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH)
1273 {
1274 JNF_COCOA_ENTER(env);
1275 
1276     if (minW < 1) minW = 1;
1277     if (minH < 1) minH = 1;
1278     if (maxW < 1) maxW = 1;
1279     if (maxH < 1) maxH = 1;
1280 
1281     NSWindow *nsWindow = OBJC(windowPtr);
1282     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1283 
1284         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1285 
1286         NSSize min = { minW, minH };
1287         NSSize max = { maxW, maxH };
1288 
1289         [window constrainSize:&min];
1290         [window constrainSize:&max];
1291 
1292         window.javaMinSize = min;
1293         window.javaMaxSize = max;
1294         [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)];
1295     }];
1296 
1297 JNF_COCOA_EXIT(env);
1298 }
1299 
1300 /*
1301  * Class:     sun_lwawt_macosx_CPlatformWindow
1302  * Method:    nativePushNSWindowToBack
1303  * Signature: (J)V
1304  */
1305 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack
1306 (JNIEnv *env, jclass clazz, jlong windowPtr)
1307 {
1308 JNF_COCOA_ENTER(env);
1309 
1310     NSWindow *nsWindow = OBJC(windowPtr);
1311     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1312         [nsWindow orderBack:nil];
1313         // Order parent windows
1314         AWTWindow *awtWindow = (AWTWindow*)[nsWindow delegate];
1315         while (awtWindow.ownerWindow != nil) {
1316             awtWindow = awtWindow.ownerWindow;
1317             if ([AWTWindow isJavaPlatformWindowVisible:awtWindow.nsWindow]) {
1318                 [awtWindow.nsWindow orderBack:nil];
1319             }
1320         }
1321         // Order child windows
1322         [(AWTWindow*)[nsWindow delegate] orderChildWindows:NO];
1323     }];
1324 
1325 JNF_COCOA_EXIT(env);
1326 }
1327 
1328 /*
1329  * Class:     sun_lwawt_macosx_CPlatformWindow
1330  * Method:    nativePushNSWindowToFront
1331  * Signature: (J)V
1332  */
1333 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront
1334 (JNIEnv *env, jclass clazz, jlong windowPtr)
1335 {
1336 JNF_COCOA_ENTER(env);
1337 
1338     NSWindow *nsWindow = OBJC(windowPtr);
1339     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1340 
1341         if (![nsWindow isKeyWindow]) {
1342             [nsWindow makeKeyAndOrderFront:nsWindow];
1343         } else {
1344             [nsWindow orderFront:nsWindow];
1345         }
1346     }];
1347 
1348 JNF_COCOA_EXIT(env);
1349 }
1350 
1351 /*
1352  * Class:     sun_lwawt_macosx_CPlatformWindow
1353  * Method:    nativeSetNSWindowTitle
1354  * Signature: (JLjava/lang/String;)V
1355  */
1356 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle
1357 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle)
1358 {
1359 JNF_COCOA_ENTER(env);
1360 
1361     NSWindow *nsWindow = OBJC(windowPtr);
1362     [nsWindow performSelectorOnMainThread:@selector(setTitle:)
1363                               withObject:JNFJavaToNSString(env, jtitle)
1364                            waitUntilDone:NO];
1365 
1366 JNF_COCOA_EXIT(env);
1367 }
1368 
1369 /*
1370  * Class:     sun_lwawt_macosx_CPlatformWindow
1371  * Method:    nativeRevalidateNSWindowShadow
1372  * Signature: (J)V
1373  */
1374 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow
1375 (JNIEnv *env, jclass clazz, jlong windowPtr)
1376 {
1377 JNF_COCOA_ENTER(env);
1378 
1379     NSWindow *nsWindow = OBJC(windowPtr);
1380     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1381         [nsWindow invalidateShadow];
1382     }];
1383 
1384 JNF_COCOA_EXIT(env);
1385 }
1386 
1387 /*
1388  * Class:     sun_lwawt_macosx_CPlatformWindow
1389  * Method:    nativeScreenOn_AppKitThread
1390  * Signature: (J)I
1391  */
1392 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread
1393 (JNIEnv *env, jclass clazz, jlong windowPtr)
1394 {
1395     jint ret = 0;
1396 
1397 JNF_COCOA_ENTER(env);
1398 AWT_ASSERT_APPKIT_THREAD;
1399 
1400     NSWindow *nsWindow = OBJC(windowPtr);
1401     NSDictionary *props = [[nsWindow screen] deviceDescription];
1402     ret = [[props objectForKey:@"NSScreenNumber"] intValue];
1403 
1404 JNF_COCOA_EXIT(env);
1405 
1406     return ret;
1407 }
1408 
1409 /*
1410  * Class:     sun_lwawt_macosx_CPlatformWindow
1411  * Method:    nativeSetNSWindowMinimizedIcon
1412  * Signature: (JJ)V
1413  */
1414 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon
1415 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr)
1416 {
1417 JNF_COCOA_ENTER(env);
1418 
1419     NSWindow *nsWindow = OBJC(windowPtr);
1420     NSImage *image = OBJC(nsImagePtr);
1421     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1422         [nsWindow setMiniwindowImage:image];
1423     }];
1424 
1425 JNF_COCOA_EXIT(env);
1426 }
1427 
1428 /*
1429  * Class:     sun_lwawt_macosx_CPlatformWindow
1430  * Method:    nativeSetNSWindowRepresentedFilename
1431  * Signature: (JLjava/lang/String;)V
1432  */
1433 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename
1434 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename)
1435 {
1436 JNF_COCOA_ENTER(env);
1437 
1438     NSWindow *nsWindow = OBJC(windowPtr);
1439     NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)];
1440     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1441         [nsWindow setRepresentedURL:url];
1442     }];
1443 
1444 JNF_COCOA_EXIT(env);
1445 }
1446 
1447 /*
1448  * Class:     sun_lwawt_macosx_CPlatformWindow
1449  * Method:    nativeGetTopmostPlatformWindowUnderMouse
1450  * Signature: (J)V
1451  */
1452 JNIEXPORT jobject
1453 JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetTopmostPlatformWindowUnderMouse
1454 (JNIEnv *env, jclass clazz)
1455 {
1456     __block jobject topmostWindowUnderMouse = nil;
1457 
1458     JNF_COCOA_ENTER(env);
1459 
1460     [ThreadUtilities performOnMainThreadWaiting:YES block:^{
1461         AWTWindow *awtWindow = [AWTWindow getTopmostWindowUnderMouse];
1462         if (awtWindow != nil) {
1463             topmostWindowUnderMouse = [awtWindow.javaPlatformWindow jObject];
1464         }
1465     }];
1466 
1467     JNF_COCOA_EXIT(env);
1468 
1469     return topmostWindowUnderMouse;
1470 }
1471 
1472 /*
1473  * Class:     sun_lwawt_macosx_CPlatformWindow
1474  * Method:    nativeSynthesizeMouseEnteredExitedEvents
1475  * Signature: ()V
1476  */
1477 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents__
1478 (JNIEnv *env, jclass clazz)
1479 {
1480     JNF_COCOA_ENTER(env);
1481 
1482     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1483         [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
1484     }];
1485 
1486     JNF_COCOA_EXIT(env);
1487 }
1488 
1489 /*
1490  * Class:     sun_lwawt_macosx_CPlatformWindow
1491  * Method:    nativeSynthesizeMouseEnteredExitedEvents
1492  * Signature: (JI)V
1493  */
1494 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents__JI
1495 (JNIEnv *env, jclass clazz, jlong windowPtr, jint eventType)
1496 {
1497 JNF_COCOA_ENTER(env);
1498 
1499     if (eventType == NSMouseEntered || eventType == NSMouseExited) {
1500         NSWindow *nsWindow = OBJC(windowPtr);
1501 
1502         [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1503             [AWTWindow synthesizeMouseEnteredExitedEvents:nsWindow withType:eventType];
1504         }];
1505     } else {
1506         [JNFException raise:env as:kIllegalArgumentException reason:"unknown event type"];
1507     }
1508 
1509 JNF_COCOA_EXIT(env);
1510 }
1511 
1512 /*
1513  * Class:     sun_lwawt_macosx_CPlatformWindow
1514  * Method:    _toggleFullScreenMode
1515  * Signature: (J)V
1516  */
1517 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode
1518 (JNIEnv *env, jobject peer, jlong windowPtr)
1519 {
1520 JNF_COCOA_ENTER(env);
1521 
1522     NSWindow *nsWindow = OBJC(windowPtr);
1523     SEL toggleFullScreenSelector = @selector(toggleFullScreen:);
1524     if (![nsWindow respondsToSelector:toggleFullScreenSelector]) return;
1525 
1526     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1527         [nsWindow performSelector:toggleFullScreenSelector withObject:nil];
1528     }];
1529 
1530 JNF_COCOA_EXIT(env);
1531 }
1532 
1533 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetEnabled
1534 (JNIEnv *env, jclass clazz, jlong windowPtr, jboolean isEnabled)
1535 {
1536 JNF_COCOA_ENTER(env);
1537 
1538     NSWindow *nsWindow = OBJC(windowPtr);
1539     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1540         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1541 
1542         [window setEnabled: isEnabled];
1543     }];
1544 
1545 JNF_COCOA_EXIT(env);
1546 }
1547 
1548 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeDispose
1549 (JNIEnv *env, jclass clazz, jlong windowPtr)
1550 {
1551 JNF_COCOA_ENTER(env);
1552 
1553     NSWindow *nsWindow = OBJC(windowPtr);
1554     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1555         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1556 
1557         if ([AWTWindow lastKeyWindow] == window) {
1558             [AWTWindow setLastKeyWindow: nil];
1559         }
1560 
1561         // AWTWindow holds a reference to the NSWindow in its nsWindow
1562         // property. Unsetting the delegate allows it to be deallocated
1563         // which releases the reference. This, in turn, allows the window
1564         // itself be deallocated.
1565         [nsWindow setDelegate: nil];
1566 
1567         [window release];
1568     }];
1569 
1570 JNF_COCOA_EXIT(env);
1571 }
1572 
1573 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeEnterFullScreenMode
1574 (JNIEnv *env, jclass clazz, jlong windowPtr)
1575 {
1576 JNF_COCOA_ENTER(env);
1577 
1578     NSWindow *nsWindow = OBJC(windowPtr);
1579     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1580         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1581         NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow];
1582         CGDirectDisplayID aID = [screenID intValue];
1583 
1584         if (CGDisplayCapture(aID) == kCGErrorSuccess) {
1585             // remove window decoration
1586             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1587             [nsWindow setStyleMask:(styleMask & ~NSTitledWindowMask) | NSBorderlessWindowMask];
1588 
1589             int shieldLevel = CGShieldingWindowLevel();
1590             window.preFullScreenLevel = [nsWindow level];
1591             [nsWindow setLevel: shieldLevel];
1592 
1593             NSRect screenRect = [[nsWindow screen] frame];
1594             [nsWindow setFrame:screenRect display:YES];
1595         } else {
1596             [JNFException raise:[ThreadUtilities getJNIEnv]
1597                              as:kRuntimeException
1598                          reason:"Failed to enter full screen."];
1599         }
1600     }];
1601 
1602 JNF_COCOA_EXIT(env);
1603 }
1604 
1605 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeExitFullScreenMode
1606 (JNIEnv *env, jclass clazz, jlong windowPtr)
1607 {
1608 JNF_COCOA_ENTER(env);
1609 
1610     NSWindow *nsWindow = OBJC(windowPtr);
1611     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1612         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1613         NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow];
1614         CGDirectDisplayID aID = [screenID intValue];
1615 
1616         if (CGDisplayRelease(aID) == kCGErrorSuccess) {
1617             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1618             [nsWindow setStyleMask:styleMask];
1619             [nsWindow setLevel: window.preFullScreenLevel];
1620 
1621             // GraphicsDevice takes care of restoring pre full screen bounds
1622         } else {
1623             [JNFException raise:[ThreadUtilities getJNIEnv]
1624                              as:kRuntimeException
1625                          reason:"Failed to exit full screen."];
1626         }
1627     }];
1628 
1629 JNF_COCOA_EXIT(env);
1630 }
1631