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