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