1 /*
   2  * Copyright (c) 2011, 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 #import <JavaRuntimeSupport/JavaRuntimeSupport.h>
  29 
  30 #import "sun_lwawt_macosx_CPlatformWindow.h"
  31 #import "com_apple_eawt_event_GestureHandler.h"
  32 #import "com_apple_eawt_FullScreenHandler.h"
  33 
  34 #import "AWTWindow.h"
  35 #import "AWTView.h"
  36 #import "CMenu.h"
  37 #import "CMenuBar.h"
  38 #import "LWCToolkit.h"
  39 #import "GeomUtilities.h"
  40 #import "ThreadUtilities.h"
  41 #import "OSVersion.h"
  42 
  43 static const float GROW_BOX_SIZE = 12.f;
  44 
  45 #define MASK(KEY) \
  46     (sun_lwawt_macosx_CPlatformWindow_ ## KEY)
  47 
  48 #define IS(BITS, KEY) \
  49     ((BITS & MASK(KEY)) != 0)
  50 
  51 #define SET(BITS, KEY, VALUE) \
  52     BITS = VALUE ? BITS | MASK(KEY) : BITS & ~MASK(KEY)
  53 
  54 static JNF_CLASS_CACHE(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow");
  55 
  56 @interface JavaResizeGrowBoxOverlayWindow : NSWindow { }
  57 
  58 @end
  59 
  60 @implementation JavaResizeGrowBoxOverlayWindow
  61 
  62 - (BOOL) accessibilityIsIgnored
  63 {
  64     return YES;
  65 }
  66 
  67 - (NSArray *)accessibilityChildrenAttribute
  68 {
  69     return nil;
  70 }
  71 @end
  72 
  73 @implementation AWTWindow
  74 
  75 @synthesize javaPlatformWindow;
  76 @synthesize javaMenuBar;
  77 @synthesize growBoxWindow;
  78 @synthesize javaMinSize;
  79 @synthesize javaMaxSize;
  80 @synthesize styleBits;
  81 @synthesize isEnabled;
  82 
  83 - (void) updateMinMaxSize:(BOOL)resizable {
  84     if (resizable) {
  85         [self setMinSize:self.javaMinSize];
  86         [self setMaxSize:self.javaMaxSize];
  87     } else {
  88         NSRect currentFrame = [self frame];
  89         [self setMinSize:currentFrame.size];
  90         [self setMaxSize:currentFrame.size];
  91     }
  92 }
  93 
  94 // creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits
  95 + (NSUInteger) styleMaskForStyleBits:(jint)styleBits {
  96     NSUInteger type = 0;
  97     if (IS(styleBits, DECORATED)) {
  98         type |= NSTitledWindowMask;
  99         if (IS(styleBits, CLOSEABLE))   type |= NSClosableWindowMask;
 100         if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask;
 101         if (IS(styleBits, RESIZABLE))   type |= NSResizableWindowMask;
 102     } else {
 103         type |= NSBorderlessWindowMask;
 104     }
 105 
 106     if (IS(styleBits, TEXTURED))      type |= NSTexturedBackgroundWindowMask;
 107     if (IS(styleBits, UNIFIED))       type |= NSUnifiedTitleAndToolbarWindowMask;
 108     if (IS(styleBits, UTILITY))       type |= NSUtilityWindowMask;
 109     if (IS(styleBits, HUD))           type |= NSHUDWindowMask;
 110     if (IS(styleBits, SHEET))         type |= NSDocModalWindowMask;
 111     if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask;
 112 
 113     return type;
 114 }
 115 
 116 // updates _METHOD_PROP_BITMASK based properties on the window
 117 - (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask {
 118     if (IS(mask, RESIZABLE)) {
 119         BOOL resizable = IS(bits, RESIZABLE);
 120         [self updateMinMaxSize:resizable];
 121         [self setShowsResizeIndicator:resizable];
 122     }
 123 
 124     if (IS(mask, HAS_SHADOW)) {
 125         [self setHasShadow:IS(bits, HAS_SHADOW)];
 126     }
 127 
 128     if (IS(mask, ZOOMABLE)) {
 129         [[self standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)];
 130     }
 131 
 132     if (IS(mask, ALWAYS_ON_TOP)) {
 133         [self setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel];
 134     }
 135 
 136     if (IS(mask, HIDES_ON_DEACTIVATE)) {
 137         [self setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)];
 138     }
 139 
 140     if (IS(mask, DRAGGABLE_BACKGROUND)) {
 141         [self setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)];
 142     }
 143 
 144     if (IS(mask, DOCUMENT_MODIFIED)) {
 145         [self setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)];
 146     }
 147 
 148     if ([self respondsToSelector:@selector(toggleFullScreen:)]) {
 149         if (IS(mask, FULLSCREENABLE)) {
 150             [self setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/];
 151         } else {
 152             [self setCollectionBehavior:NSWindowCollectionBehaviorDefault];
 153         }
 154     }
 155 
 156 }
 157 
 158 - (BOOL) shouldShowGrowBox {
 159     return isSnowLeopardOrLower() && IS(self.styleBits, RESIZABLE);
 160 }
 161 
 162 - (NSImage *) createGrowBoxImage {
 163     NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(GROW_BOX_SIZE, GROW_BOX_SIZE)];
 164     JRSUIControlRef growBoxWidget = JRSUIControlCreate(FALSE);
 165     JRSUIControlSetWidget(growBoxWidget, kJRSUI_Widget_growBoxTextured);
 166     JRSUIControlSetWindowType(growBoxWidget, kJRSUI_WindowType_utility);
 167     JRSUIRendererRef renderer = JRSUIRendererCreate();
 168     [image lockFocus]; // sets current graphics context to that of the image
 169     JRSUIControlDraw(renderer, growBoxWidget, [[NSGraphicsContext currentContext] graphicsPort], CGRectMake(0, 1, GROW_BOX_SIZE - 1, GROW_BOX_SIZE - 1));
 170     [image unlockFocus];
 171     JRSUIRendererRelease(renderer);
 172     JRSUIControlRelease(growBoxWidget);
 173     return image;
 174 }
 175 
 176 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow
 177                     styleBits:(jint)bits
 178                     frameRect:(NSRect)rect
 179                   contentView:(NSView *)view
 180 {
 181 AWT_ASSERT_APPKIT_THREAD;
 182 
 183     NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits];
 184     NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
 185     if (contentRect.size.width <= 0.0) {
 186         contentRect.size.width = 1.0;
 187     }
 188     if (contentRect.size.height <= 0.0) {
 189         contentRect.size.height = 1.0;
 190     }
 191 
 192     self = [super initWithContentRect:contentRect
 193                             styleMask:styleMask
 194                               backing:NSBackingStoreBuffered
 195                                 defer:NO];
 196 
 197     if (self == nil) return nil; // no hope
 198 
 199     self.isEnabled = YES;
 200     self.javaPlatformWindow = platformWindow;
 201     self.styleBits = bits;
 202     [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
 203 
 204     [self setDelegate:self];
 205     [self setContentView:view];
 206     [self setInitialFirstResponder:view];
 207     [self setReleasedWhenClosed:NO];
 208     [self setPreservesContentDuringLiveResize:YES];
 209 
 210     if ([self shouldShowGrowBox]) {
 211         NSImage *growBoxImage = [self createGrowBoxImage];
 212         growBoxWindow = [[JavaResizeGrowBoxOverlayWindow alloc] initWithContentRect:NSMakeRect(0, 0, [growBoxImage size].width, [growBoxImage size].height) styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
 213         [self.growBoxWindow setIgnoresMouseEvents:YES];
 214         [self.growBoxWindow setOpaque:NO];
 215         [self.growBoxWindow setBackgroundColor:[NSColor clearColor]];
 216         [self.growBoxWindow setHasShadow:NO];
 217         [self.growBoxWindow setReleasedWhenClosed:NO];
 218 
 219         NSImageView *imageView = [[NSImageView alloc] initWithFrame:[self.growBoxWindow frame]];
 220         [imageView setEditable:NO];
 221         [imageView setAnimates:NO];
 222         [imageView setAllowsCutCopyPaste:NO];
 223         [self.growBoxWindow setContentView:imageView];
 224         [imageView setImage:growBoxImage];
 225         [growBoxImage release];
 226         [imageView release];
 227 
 228         [self addChildWindow:self.growBoxWindow ordered:NSWindowAbove];
 229         [self adjustGrowBoxWindow];
 230     } else growBoxWindow = nil;
 231 
 232     return self;
 233 }
 234 
 235 - (void) dealloc {
 236 AWT_ASSERT_APPKIT_THREAD;
 237 
 238     JNIEnv *env = [ThreadUtilities getJNIEnv];
 239     [self.javaPlatformWindow setJObject:nil withEnv:env];
 240     self.growBoxWindow = nil;
 241 
 242     [super dealloc];
 243 }
 244 
 245 // NSWindow overrides
 246 - (BOOL) canBecomeKeyWindow {
 247 AWT_ASSERT_APPKIT_THREAD;
 248     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_KEY);
 249 }
 250 
 251 - (BOOL) canBecomeMainWindow {
 252 AWT_ASSERT_APPKIT_THREAD;
 253     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN);
 254 }
 255 
 256 - (BOOL) worksWhenModal {
 257 AWT_ASSERT_APPKIT_THREAD;
 258     return IS(self.styleBits, MODAL_EXCLUDED);
 259 }
 260 
 261 
 262 // Gesture support
 263 - (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b {
 264 AWT_ASSERT_APPKIT_THREAD;
 265 
 266     JNIEnv *env = [ThreadUtilities getJNIEnv];
 267     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 268     if (platformWindow != NULL) {
 269         // extract the target AWT Window object out of the CPlatformWindow
 270         static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 271         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 272         if (awtWindow != NULL) {
 273             // translate the point into Java coordinates
 274             NSPoint loc = [event locationInWindow];
 275             loc.y = [self frame].size.height - loc.y;
 276 
 277             // send up to the GestureHandler to recursively dispatch on the AWT event thread
 278             static JNF_CLASS_CACHE(jc_GestureHandler, "com/apple/eawt/event/GestureHandler");
 279             static JNF_STATIC_MEMBER_CACHE(sjm_handleGestureFromNative, jc_GestureHandler, "handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V");
 280             JNFCallStaticVoidMethod(env, sjm_handleGestureFromNative, awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b);
 281             (*env)->DeleteLocalRef(env, awtWindow);
 282         }
 283         (*env)->DeleteLocalRef(env, platformWindow);
 284     }
 285 }
 286 
 287 - (void)beginGestureWithEvent:(NSEvent *)event {
 288     [self postGesture:event
 289                    as:com_apple_eawt_event_GestureHandler_PHASE
 290                     a:-1.0
 291                     b:0.0];
 292 }
 293 
 294 - (void)endGestureWithEvent:(NSEvent *)event {
 295     [self postGesture:event
 296                    as:com_apple_eawt_event_GestureHandler_PHASE
 297                     a:1.0
 298                     b:0.0];
 299 }
 300 
 301 - (void)magnifyWithEvent:(NSEvent *)event {
 302     [self postGesture:event
 303                    as:com_apple_eawt_event_GestureHandler_MAGNIFY
 304                     a:[event magnification]
 305                     b:0.0];
 306 }
 307 
 308 - (void)rotateWithEvent:(NSEvent *)event {
 309     [self postGesture:event
 310                    as:com_apple_eawt_event_GestureHandler_ROTATE
 311                     a:[event rotation]
 312                     b:0.0];
 313 }
 314 
 315 - (void)swipeWithEvent:(NSEvent *)event {
 316     [self postGesture:event
 317                    as:com_apple_eawt_event_GestureHandler_SWIPE
 318                     a:[event deltaX]
 319                     b:[event deltaY]];
 320 }
 321 
 322 
 323 // NSWindowDelegate methods
 324 
 325 - (void) adjustGrowBoxWindow {
 326     if (self.growBoxWindow != nil) {
 327         NSRect parentRect = [self frame];
 328         parentRect.origin.x += (parentRect.size.width - [self.growBoxWindow frame].size.width);
 329         [self.growBoxWindow setFrameOrigin:parentRect.origin];
 330     }
 331 }
 332 
 333 - (void) _deliverMoveResizeEvent {
 334 AWT_ASSERT_APPKIT_THREAD;
 335 
 336     // deliver the event if this is a user-initiated live resize or as a side-effect
 337     // of a Java initiated resize, because AppKit can override the bounds and force
 338     // the bounds of the window to avoid the Dock or remain on screen.
 339     [AWTToolkit eventCountPlusPlus];
 340     JNIEnv *env = [ThreadUtilities getJNIEnv];
 341     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 342     if (platformWindow == NULL) {
 343         // TODO: create generic AWT assert
 344     }
 345 
 346     [self adjustGrowBoxWindow];
 347 
 348     NSRect frame = ConvertNSScreenRect(env, [self frame]);
 349 
 350     static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIII)V");
 351     JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
 352                       (jint)frame.origin.x,
 353                       (jint)frame.origin.y,
 354                       (jint)frame.size.width,
 355                       (jint)frame.size.height);
 356     (*env)->DeleteLocalRef(env, platformWindow);
 357 }
 358 
 359 - (void)windowDidMove:(NSNotification *)notification {
 360 AWT_ASSERT_APPKIT_THREAD;
 361 
 362     [self _deliverMoveResizeEvent];
 363 }
 364 
 365 - (void)windowDidResize:(NSNotification *)notification {
 366 AWT_ASSERT_APPKIT_THREAD;
 367 
 368     [self _deliverMoveResizeEvent];
 369 }
 370 
 371 - (void)windowDidExpose:(NSNotification *)notification {
 372 AWT_ASSERT_APPKIT_THREAD;
 373 
 374     [AWTToolkit eventCountPlusPlus];
 375     // TODO: don't see this callback invoked anytime so we track
 376     // window exposing in _setVisible:(BOOL)
 377 }
 378 
 379 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)proposedFrame {
 380 AWT_ASSERT_APPKIT_THREAD;
 381 
 382     [AWTToolkit eventCountPlusPlus];
 383     JNIEnv *env = [ThreadUtilities getJNIEnv];
 384     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 385     if (platformWindow != NULL) {
 386         static JNF_MEMBER_CACHE(jm_deliverZoom, jc_CPlatformWindow, "deliverZoom", "(Z)V");
 387         JNFCallVoidMethod(env, platformWindow, jm_deliverZoom, ![window isZoomed]);
 388         (*env)->DeleteLocalRef(env, platformWindow);
 389     }
 390     return YES;
 391 }
 392 
 393 - (void) _deliverIconify:(BOOL)iconify {
 394 AWT_ASSERT_APPKIT_THREAD;
 395 
 396     [AWTToolkit eventCountPlusPlus];
 397     JNIEnv *env = [ThreadUtilities getJNIEnv];
 398     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 399     if (platformWindow != NULL) {
 400         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 401         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 402         (*env)->DeleteLocalRef(env, platformWindow);
 403     }
 404 }
 405 
 406 - (void)windowDidMiniaturize:(NSNotification *)notification {
 407 AWT_ASSERT_APPKIT_THREAD;
 408 
 409     [self _deliverIconify:JNI_TRUE];
 410 }
 411 
 412 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 413 AWT_ASSERT_APPKIT_THREAD;
 414 
 415     [self _deliverIconify:JNI_FALSE];
 416 }
 417 
 418 - (void) _deliverWindowFocusEvent:(BOOL)focused {
 419 //AWT_ASSERT_APPKIT_THREAD;
 420 
 421     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 422     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 423     if (platformWindow != NULL) {
 424         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(Z)V");
 425         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused);
 426         (*env)->DeleteLocalRef(env, platformWindow);
 427     }
 428 }
 429 
 430 
 431 - (void) windowDidBecomeKey: (NSNotification *) notification {
 432 AWT_ASSERT_APPKIT_THREAD;
 433     [AWTToolkit eventCountPlusPlus];
 434     [CMenuBar activate:self.javaMenuBar modallyDisabled:NO];
 435     [self _deliverWindowFocusEvent:YES];
 436 }
 437 
 438 - (void) windowDidResignKey: (NSNotification *) notification {
 439     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 440 AWT_ASSERT_APPKIT_THREAD;
 441     [AWTToolkit eventCountPlusPlus];
 442     [self.javaMenuBar deactivate];
 443     [self _deliverWindowFocusEvent:NO];
 444 }
 445 
 446 - (void) windowDidBecomeMain: (NSNotification *) notification {
 447 AWT_ASSERT_APPKIT_THREAD;
 448     [AWTToolkit eventCountPlusPlus];
 449 
 450     JNIEnv *env = [ThreadUtilities getJNIEnv];
 451     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 452     if (platformWindow != NULL) {
 453         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 454         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 455         (*env)->DeleteLocalRef(env, platformWindow);
 456     }
 457 }
 458 
 459 - (BOOL)windowShouldClose:(id)sender {
 460 AWT_ASSERT_APPKIT_THREAD;
 461     [AWTToolkit eventCountPlusPlus];
 462     JNIEnv *env = [ThreadUtilities getJNIEnv];
 463     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 464     if (platformWindow != NULL) {
 465         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 466         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 467         (*env)->DeleteLocalRef(env, platformWindow);
 468     }
 469     // The window will be closed (if allowed) as result of sending Java event
 470     return NO;
 471 }
 472 
 473 
 474 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 475     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 476     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 477     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 478     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 479     if (platformWindow != NULL) {
 480         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 481         if (awtWindow != NULL) {
 482             JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op);
 483             (*env)->DeleteLocalRef(env, awtWindow);
 484         }
 485         (*env)->DeleteLocalRef(env, platformWindow);
 486     }
 487 }
 488 
 489 
 490 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
 491     static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
 492     JNIEnv *env = [ThreadUtilities getJNIEnv];
 493     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 494     if (platformWindow != NULL) {
 495         JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
 496         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
 497         (*env)->DeleteLocalRef(env, platformWindow);
 498     }
 499 }
 500 
 501 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
 502     static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
 503     JNIEnv *env = [ThreadUtilities getJNIEnv];
 504     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 505     if (platformWindow != NULL) {
 506         JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
 507         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
 508         (*env)->DeleteLocalRef(env, platformWindow);
 509     }
 510 }
 511 
 512 - (void)windowWillExitFullScreen:(NSNotification *)notification {
 513     static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
 514     JNIEnv *env = [ThreadUtilities getJNIEnv];
 515     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 516     if (platformWindow != NULL) {
 517         JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
 518         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
 519         (*env)->DeleteLocalRef(env, platformWindow);
 520     }
 521 }
 522 
 523 - (void)windowDidExitFullScreen:(NSNotification *)notification {
 524     static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
 525     JNIEnv *env = [ThreadUtilities getJNIEnv];
 526     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 527     if (platformWindow != NULL) {
 528         JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
 529         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 530         (*env)->DeleteLocalRef(env, platformWindow);
 531     }
 532 }
 533 
 534 - (void)sendEvent:(NSEvent *)event {
 535         if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
 536 
 537             NSPoint p = [NSEvent mouseLocation];
 538             NSRect frame = [self frame];
 539             NSRect contentRect = [self contentRectForFrameRect:frame];
 540 
 541             // Check if the click happened in the non-client area (title bar)
 542             if (p.y >= (frame.origin.y + contentRect.size.height)) {
 543                 JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 544                 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 545                 // Currently, no need to deliver the whole NSEvent.
 546                 static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
 547                 JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
 548             }
 549         }
 550         [super sendEvent:event];
 551 }
 552 
 553 - (void)constrainSize:(NSSize*)size {
 554     float minWidth = 0.f, minHeight = 0.f;
 555 
 556     if (IS(self.styleBits, DECORATED)) {
 557         NSRect frame = [self frame];
 558         NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self styleMask]];
 559 
 560         float top = frame.size.height - contentRect.size.height;
 561         float left = contentRect.origin.x - frame.origin.x;
 562         float bottom = contentRect.origin.y - frame.origin.y;
 563         float right = frame.size.width - (contentRect.size.width + left);
 564 
 565         // Speculative estimation: 80 - enough for window decorations controls
 566         minWidth += left + right + 80;
 567         minHeight += top + bottom;
 568     }
 569 
 570     if ([self shouldShowGrowBox]) {
 571         minWidth = MAX(minWidth, GROW_BOX_SIZE);
 572         minHeight += GROW_BOX_SIZE;
 573     }
 574 
 575     minWidth = MAX(1.f, minWidth);
 576     minHeight = MAX(1.f, minHeight);
 577 
 578     size->width = MAX(size->width, minWidth);
 579     size->height = MAX(size->height, minHeight);
 580 }
 581 
 582 - (void) setEnabled: (BOOL)flag {
 583     self.isEnabled = flag;
 584 
 585     if (IS(self.styleBits, CLOSEABLE)) {
 586         [[self standardWindowButton:NSWindowCloseButton] setEnabled: flag];
 587     }
 588 
 589     if (IS(self.styleBits, MINIMIZABLE)) {        
 590         [[self standardWindowButton:NSWindowMiniaturizeButton] setEnabled: flag];
 591     }
 592 
 593     if (IS(self.styleBits, ZOOMABLE)) {
 594         [[self standardWindowButton:NSWindowZoomButton] setEnabled: flag];
 595     }
 596 
 597     if (IS(self.styleBits, RESIZABLE)) {
 598         [self updateMinMaxSize:flag];
 599         [self setShowsResizeIndicator:flag];
 600     }
 601 }
 602 
 603 @end // AWTWindow
 604 
 605 
 606 /*
 607  * Class:     sun_lwawt_macosx_CPlatformWindow
 608  * Method:    nativeCreateNSWindow
 609  * Signature: (JJIIII)J
 610  */
 611 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow
 612 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong styleBits, jdouble x, jdouble y, jdouble w, jdouble h)
 613 {
 614     __block AWTWindow *window = nil;
 615 
 616 JNF_COCOA_ENTER(env);
 617 AWT_ASSERT_NOT_APPKIT_THREAD;
 618 
 619     JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
 620     NSView *contentView = OBJC(contentViewPtr);
 621     NSRect frameRect = NSMakeRect(x, y, w, h);
 622 
 623     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 624         AWT_ASSERT_APPKIT_THREAD;
 625 
 626         window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow
 627                                                   styleBits:styleBits
 628                                                   frameRect:frameRect
 629                                                 contentView:contentView];
 630 
 631         if (window) CFRetain(window);
 632         [window release]; // GC
 633     }];
 634 
 635 JNF_COCOA_EXIT(env);
 636 
 637     return ptr_to_jlong(window);
 638 }
 639 
 640 /*
 641  * Class:     sun_lwawt_macosx_CPlatformWindow
 642  * Method:    nativeSetNSWindowStyleBits
 643  * Signature: (JII)V
 644  */
 645 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStyleBits
 646 (JNIEnv *env, jclass clazz, jlong windowPtr, jint mask, jint bits)
 647 {
 648 JNF_COCOA_ENTER(env);
 649 AWT_ASSERT_NOT_APPKIT_THREAD;
 650 
 651     AWTWindow *window = OBJC(windowPtr);
 652     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 653         AWT_ASSERT_APPKIT_THREAD;
 654 
 655         // scans the bit field, and only updates the values requested by the mask
 656         // (this implicity handles the _CALLBACK_PROP_BITMASK case, since those are passive reads)
 657         jint newBits = window.styleBits & ~mask | bits & mask;
 658 
 659         // resets the NSWindow's style mask if the mask intersects any of those bits
 660         if (mask & MASK(_STYLE_PROP_BITMASK)) {
 661             [window setStyleMask:[AWTWindow styleMaskForStyleBits:newBits]];
 662         }
 663 
 664         // calls methods on NSWindow to change other properties, based on the mask
 665         if (mask & MASK(_METHOD_PROP_BITMASK)) {
 666             [window setPropertiesForStyleBits:bits mask:mask];
 667         }
 668 
 669         window.styleBits = newBits;
 670     }];
 671 
 672 JNF_COCOA_EXIT(env);
 673 }
 674 
 675 /*
 676  * Class:     sun_lwawt_macosx_CPlatformWindow
 677  * Method:    nativeSetNSWindowMenuBar
 678  * Signature: (JJ)V
 679  */
 680 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
 681 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
 682 {
 683 JNF_COCOA_ENTER(env);
 684 AWT_ASSERT_NOT_APPKIT_THREAD;
 685 
 686     AWTWindow *window = OBJC(windowPtr);
 687     CMenuBar *menuBar = OBJC(menuBarPtr);
 688     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 689         AWT_ASSERT_APPKIT_THREAD;
 690 
 691         if ([window isKeyWindow]) [window.javaMenuBar deactivate];
 692         window.javaMenuBar = menuBar;
 693 
 694         // if ([self isKeyWindow]) {
 695         [CMenuBar activate:window.javaMenuBar modallyDisabled:NO];
 696         // }
 697     }];
 698 
 699 JNF_COCOA_EXIT(env);
 700 }
 701 
 702 /*
 703  * Class:     sun_lwawt_macosx_CPlatformWindow
 704  * Method:    nativeGetNSWindowInsets
 705  * Signature: (J)Ljava/awt/Insets;
 706  */
 707 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
 708 (JNIEnv *env, jclass clazz, jlong windowPtr)
 709 {
 710     jobject ret = NULL;
 711 
 712 JNF_COCOA_ENTER(env);
 713 AWT_ASSERT_NOT_APPKIT_THREAD;
 714 
 715     AWTWindow *window = OBJC(windowPtr);
 716     __block NSRect contentRect = NSZeroRect;
 717     __block NSRect frame = NSZeroRect;
 718 
 719     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 720         AWT_ASSERT_APPKIT_THREAD;
 721 
 722         frame = [window frame];
 723         contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[window styleMask]];
 724     }];
 725 
 726     jint top = (jint)(frame.size.height - contentRect.size.height);
 727     jint left = (jint)(contentRect.origin.x - frame.origin.x);
 728     jint bottom = (jint)(contentRect.origin.y - frame.origin.y);
 729     jint right = (jint)(frame.size.width - (contentRect.size.width + left));
 730 
 731     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
 732     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
 733     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
 734 
 735 JNF_COCOA_EXIT(env);
 736     return ret;
 737 }
 738 
 739 /*
 740  * Class:     sun_lwawt_macosx_CPlatformWindow
 741  * Method:    nativeSetNSWindowBounds
 742  * Signature: (JDDDD)V
 743  */
 744 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowBounds
 745 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, jdouble width, jdouble height)
 746 {
 747 JNF_COCOA_ENTER(env);
 748 AWT_ASSERT_NOT_APPKIT_THREAD;
 749 
 750     NSRect jrect = NSMakeRect(originX, originY, width, height);
 751 
 752     // TODO: not sure we need displayIfNeeded message in our view
 753     AWTWindow *window = OBJC(windowPtr);
 754     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 755         AWT_ASSERT_APPKIT_THREAD;
 756 
 757         NSRect rect = ConvertNSScreenRect(NULL, jrect);
 758         [window constrainSize:&rect.size];
 759 
 760         [window setFrame:rect display:YES];
 761 
 762         // only start tracking events if pointer is above the toplevel
 763         // TODO: should post an Entered event if YES.
 764         NSPoint mLocation = [NSEvent mouseLocation];
 765         [window setAcceptsMouseMovedEvents:NSPointInRect(mLocation, rect)];
 766 
 767         // ensure we repaint the whole window after the resize operation
 768         // (this will also re-enable screen updates, which were disabled above)
 769         // TODO: send PaintEvent
 770     }];
 771 
 772 JNF_COCOA_EXIT(env);
 773 }
 774 
 775 /*
 776  * Class:     sun_lwawt_macosx_CPlatformWindow
 777  * Method:    nativeSetNSWindowMinMax
 778  * Signature: (JDDDD)V
 779  */
 780 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax
 781 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH)
 782 {
 783 JNF_COCOA_ENTER(env);
 784 AWT_ASSERT_NOT_APPKIT_THREAD;
 785 
 786     if (minW < 1) minW = 1;
 787     if (minH < 1) minH = 1;
 788     if (maxW < 1) maxW = 1;
 789     if (maxH < 1) maxH = 1;
 790 
 791     AWTWindow *window = OBJC(windowPtr);
 792     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 793         AWT_ASSERT_APPKIT_THREAD;
 794 
 795         NSSize min = { minW, minH };
 796         NSSize max = { maxW, maxH };
 797 
 798         [window constrainSize:&min];
 799         [window constrainSize:&max];
 800 
 801         window.javaMinSize = min;
 802         window.javaMaxSize = max;
 803         [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)];
 804     }];
 805 
 806 JNF_COCOA_EXIT(env);
 807 }
 808 
 809 /*
 810  * Class:     sun_lwawt_macosx_CPlatformWindow
 811  * Method:    nativePushNSWindowToBack
 812  * Signature: (J)V
 813  */
 814 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack
 815 (JNIEnv *env, jclass clazz, jlong windowPtr)
 816 {
 817 JNF_COCOA_ENTER(env);
 818 AWT_ASSERT_NOT_APPKIT_THREAD;
 819 
 820     AWTWindow *window = OBJC(windowPtr);
 821     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 822         AWT_ASSERT_APPKIT_THREAD;
 823 
 824         [window orderBack:nil];
 825     }];
 826 
 827 JNF_COCOA_EXIT(env);
 828 }
 829 
 830 /*
 831  * Class:     sun_lwawt_macosx_CPlatformWindow
 832  * Method:    nativePushNSWindowToFront
 833  * Signature: (J)V
 834  */
 835 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront
 836 (JNIEnv *env, jclass clazz, jlong windowPtr)
 837 {
 838 JNF_COCOA_ENTER(env);
 839 AWT_ASSERT_NOT_APPKIT_THREAD;
 840 
 841     AWTWindow *window = OBJC(windowPtr);
 842     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 843         AWT_ASSERT_APPKIT_THREAD;
 844 
 845         if (![window isKeyWindow]) {
 846             [window makeKeyAndOrderFront:window];
 847         } else {
 848             [window orderFront:window];
 849         }
 850     }];
 851 
 852 JNF_COCOA_EXIT(env);
 853 }
 854 
 855 /*
 856  * Class:     sun_lwawt_macosx_CPlatformWindow
 857  * Method:    nativeSetNSWindowTitle
 858  * Signature: (JLjava/lang/String;)V
 859  */
 860 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle
 861 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle)
 862 {
 863 JNF_COCOA_ENTER(env);
 864 AWT_ASSERT_NOT_APPKIT_THREAD;
 865 
 866     AWTWindow *window = OBJC(windowPtr);
 867     [window performSelectorOnMainThread:@selector(setTitle:)
 868                               withObject:JNFJavaToNSString(env, jtitle)
 869                            waitUntilDone:NO];
 870 
 871 JNF_COCOA_EXIT(env);
 872 }
 873 
 874 /*
 875  * Class:     sun_lwawt_macosx_CPlatformWindow
 876  * Method:    nativeSetNSWindowAlpha
 877  * Signature: (JF)V
 878  */
 879 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowAlpha
 880 (JNIEnv *env, jclass clazz, jlong windowPtr, jfloat alpha)
 881 {
 882 JNF_COCOA_ENTER(env);
 883 AWT_ASSERT_NOT_APPKIT_THREAD;
 884 
 885     AWTWindow *window = OBJC(windowPtr);
 886     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 887         AWT_ASSERT_APPKIT_THREAD;
 888 
 889         [window setAlphaValue:alpha];
 890         [window.growBoxWindow setAlphaValue:alpha];
 891     }];
 892 
 893 JNF_COCOA_EXIT(env);
 894 }
 895 
 896 /*
 897  * Class:     sun_lwawt_macosx_CPlatformWindow
 898  * Method:    nativeRevalidateNSWindowShadow
 899  * Signature: (J)V
 900  */
 901 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow
 902 (JNIEnv *env, jclass clazz, jlong windowPtr)
 903 {
 904 JNF_COCOA_ENTER(env);
 905 AWT_ASSERT_NOT_APPKIT_THREAD;
 906 
 907     AWTWindow *window = OBJC(windowPtr);
 908     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 909         AWT_ASSERT_APPKIT_THREAD;
 910 
 911         [window invalidateShadow];
 912     }];
 913 
 914 JNF_COCOA_EXIT(env);
 915 }
 916 
 917 /*
 918  * Class:     sun_lwawt_macosx_CPlatformWindow
 919  * Method:    nativeScreenOn_AppKitThread
 920  * Signature: (J)I
 921  */
 922 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread
 923 (JNIEnv *env, jclass clazz, jlong windowPtr)
 924 {
 925     jint ret = 0;
 926 
 927 JNF_COCOA_ENTER(env);
 928 AWT_ASSERT_APPKIT_THREAD;
 929 
 930     AWTWindow *window = OBJC(windowPtr);
 931     NSDictionary *props = [[window screen] deviceDescription];
 932     ret = [[props objectForKey:@"NSScreenNumber"] intValue];
 933 
 934 JNF_COCOA_EXIT(env);
 935 
 936     return ret;
 937 }
 938 
 939 /*
 940  * Class:     sun_lwawt_macosx_CPlatformWindow
 941  * Method:    nativeSetNSWindowMinimizedIcon
 942  * Signature: (JJ)V
 943  */
 944 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon
 945 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr)
 946 {
 947 JNF_COCOA_ENTER(env);
 948 AWT_ASSERT_NOT_APPKIT_THREAD;
 949 
 950     AWTWindow *window = OBJC(windowPtr);
 951     NSImage *image = OBJC(nsImagePtr);
 952     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 953         AWT_ASSERT_APPKIT_THREAD;
 954 
 955         [window setMiniwindowImage:image];
 956     }];
 957 
 958 JNF_COCOA_EXIT(env);
 959 }
 960 
 961 /*
 962  * Class:     sun_lwawt_macosx_CPlatformWindow
 963  * Method:    nativeSetNSWindowRepresentedFilename
 964  * Signature: (JLjava/lang/String;)V
 965  */
 966 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename
 967 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename)
 968 {
 969 JNF_COCOA_ENTER(env);
 970 AWT_ASSERT_NOT_APPKIT_THREAD;
 971 
 972     AWTWindow *window = OBJC(windowPtr);
 973     NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)];
 974     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 975         AWT_ASSERT_APPKIT_THREAD;
 976 
 977         [window setRepresentedURL:url];
 978     }];
 979 
 980 JNF_COCOA_EXIT(env);
 981 }
 982 
 983 /*
 984  * Class:     sun_lwawt_macosx_CPlatformWindow
 985  * Method:    nativeSetNSWindowSecurityWarningPositioning
 986  * Signature: (JDDFF)V
 987  */
 988 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowSecurityWarningPositioning
 989 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble x, jdouble y, jfloat biasX, jfloat biasY)
 990 {
 991 JNF_COCOA_ENTER(env);
 992 AWT_ASSERT_NOT_APPKIT_THREAD;
 993 
 994     [JNFException raise:env as:kRuntimeException reason:"unimplemented"];
 995 
 996 JNF_COCOA_EXIT(env);
 997 }
 998 
 999 /*
1000  * Class:     sun_lwawt_macosx_CPlatformWindow
1001  * Method:    nativeGetScreenNSWindowIsOn_AppKitThread
1002  * Signature: (J)I
1003  */
1004 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetScreenNSWindowIsOn_1AppKitThread
1005 (JNIEnv *env, jclass clazz, jlong windowPtr)
1006 {
1007     jint index = -1;
1008 
1009 JNF_COCOA_ENTER(env);
1010 AWT_ASSERT_APPKIT_THREAD;
1011 
1012     AWTWindow *window = OBJC(windowPtr);
1013     NSScreen* screen = [window screen];
1014 
1015     //+++gdb NOTE: This is using a linear search of the screens. If it should
1016     //  prove to be a bottleneck, this can definitely be improved. However,
1017     //  many screens should prove to be the exception, rather than the rule.
1018     NSArray* screens = [NSScreen screens];
1019     NSUInteger i;
1020     for (i = 0; i < [screens count]; i++)
1021     {
1022         if ([[screens objectAtIndex:i] isEqualTo:screen])
1023         {
1024             index = i;
1025             break;
1026         }
1027     }
1028 
1029 JNF_COCOA_EXIT(env);
1030     return 1;
1031 }
1032 
1033 
1034 /*
1035  * Class:     sun_lwawt_macosx_CPlatformWindow
1036  * Method:    _toggleFullScreenMode
1037  * Signature: (J)V
1038  */
1039 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode
1040 (JNIEnv *env, jobject peer, jlong windowPtr)
1041 {
1042 JNF_COCOA_ENTER(env);
1043 
1044     AWTWindow *window = OBJC(windowPtr);
1045     SEL toggleFullScreenSelector = @selector(toggleFullScreen:);
1046     if (![window respondsToSelector:toggleFullScreenSelector]) return;
1047 
1048     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
1049         [window performSelector:toggleFullScreenSelector withObject:nil];
1050     }];
1051 
1052 JNF_COCOA_EXIT(env);
1053 }
1054 
1055 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CMouseInfoPeer_nativeIsWindowUnderMouse
1056 (JNIEnv *env, jclass clazz, jlong windowPtr)
1057 {
1058     __block jboolean underMouse = JNI_FALSE;
1059 
1060 JNF_COCOA_ENTER(env);
1061 AWT_ASSERT_NOT_APPKIT_THREAD;
1062 
1063     AWTWindow *aWindow = OBJC(windowPtr);
1064     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^() {
1065         AWT_ASSERT_APPKIT_THREAD;
1066 
1067         NSPoint pt = [aWindow mouseLocationOutsideOfEventStream];
1068         underMouse = [[aWindow contentView] hitTest:pt] != nil;
1069     }];
1070 
1071 JNF_COCOA_EXIT(env);
1072 
1073     return underMouse;
1074 }
1075 
1076 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetEnabled
1077 (JNIEnv *env, jclass clazz, jlong windowPtr, jboolean isEnabled)
1078 {
1079 JNF_COCOA_ENTER(env);
1080 
1081     AWTWindow *window = OBJC(windowPtr);
1082     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
1083         [window setEnabled: isEnabled];
1084     }];
1085 
1086 JNF_COCOA_EXIT(env);
1087 }
1088