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