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