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