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