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