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