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