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