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