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