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