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