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