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