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