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