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