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