1 /*
   2  * Copyright (c) 2011, 2014, 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             zoomed:(BOOL)zoomed                                 \
  71 {                                                               \
  72     self = [super initWithContentRect:contectRect               \
  73                             styleMask:styleMask                 \
  74                               backing:NSBackingStoreBuffered    \
  75                                 defer:NO];                      \
  76                                                                 \
  77     if (self == nil) return nil;                                \
  78                                                                 \
  79     if (zoomed) [self zoom:nil];                                \
  80     [self setDelegate:delegate];                                \
  81     [self setContentView:view];                                 \
  82     [self setInitialFirstResponder:view];                       \
  83     [self setReleasedWhenClosed:NO];                            \
  84     [self setPreservesContentDuringLiveResize:YES];             \
  85                                                                 \
  86     return self;                                                \
  87 }                                                               \
  88                                                                 \
  89 /* NSWindow overrides */                                        \
  90 - (BOOL) canBecomeKeyWindow {                                   \
  91     return [(AWTWindow*)[self delegate] canBecomeKeyWindow];    \
  92 }                                                               \
  93                                                                 \
  94 - (BOOL) canBecomeMainWindow {                                  \
  95     return [(AWTWindow*)[self delegate] canBecomeMainWindow];   \
  96 }                                                               \
  97                                                                 \
  98 - (BOOL) worksWhenModal {                                       \
  99     return [(AWTWindow*)[self delegate] worksWhenModal];        \
 100 }                                                               \
 101                                                                 \
 102 - (void)sendEvent:(NSEvent *)event {                            \
 103     [(AWTWindow*)[self delegate] sendEvent:event];              \
 104     [super sendEvent:event];                                    \
 105 }
 106 
 107 @implementation AWTWindow_Normal
 108 AWT_NS_WINDOW_IMPLEMENTATION
 109 
 110 // Gesture support
 111 - (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b {
 112     AWT_ASSERT_APPKIT_THREAD;
 113 
 114     JNIEnv *env = [ThreadUtilities getJNIEnv];
 115     jobject platformWindow = [((AWTWindow *)self.delegate).javaPlatformWindow jObjectWithEnv:env];
 116     if (platformWindow != NULL) {
 117         // extract the target AWT Window object out of the CPlatformWindow
 118         static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 119         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 120         if (awtWindow != NULL) {
 121             // translate the point into Java coordinates
 122             NSPoint loc = [event locationInWindow];
 123             loc.y = [self frame].size.height - loc.y;
 124 
 125             // send up to the GestureHandler to recursively dispatch on the AWT event thread
 126             static JNF_CLASS_CACHE(jc_GestureHandler, "com/apple/eawt/event/GestureHandler");
 127             static JNF_STATIC_MEMBER_CACHE(sjm_handleGestureFromNative, jc_GestureHandler, "handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V");
 128             JNFCallStaticVoidMethod(env, sjm_handleGestureFromNative, awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b);
 129             (*env)->DeleteLocalRef(env, awtWindow);
 130         }
 131         (*env)->DeleteLocalRef(env, platformWindow);
 132     }
 133 }
 134 
 135 - (void)beginGestureWithEvent:(NSEvent *)event {
 136     [self postGesture:event
 137                    as:com_apple_eawt_event_GestureHandler_PHASE
 138                     a:-1.0
 139                     b:0.0];
 140 }
 141 
 142 - (void)endGestureWithEvent:(NSEvent *)event {
 143     [self postGesture:event
 144                    as:com_apple_eawt_event_GestureHandler_PHASE
 145                     a:1.0
 146                     b:0.0];
 147 }
 148 
 149 - (void)magnifyWithEvent:(NSEvent *)event {
 150     [self postGesture:event
 151                    as:com_apple_eawt_event_GestureHandler_MAGNIFY
 152                     a:[event magnification]
 153                     b:0.0];
 154 }
 155 
 156 - (void)rotateWithEvent:(NSEvent *)event {
 157     [self postGesture:event
 158                    as:com_apple_eawt_event_GestureHandler_ROTATE
 159                     a:[event rotation]
 160                     b:0.0];
 161 }
 162 
 163 - (void)swipeWithEvent:(NSEvent *)event {
 164     [self postGesture:event
 165                    as:com_apple_eawt_event_GestureHandler_SWIPE
 166                     a:[event deltaX]
 167                     b:[event deltaY]];
 168 }
 169 
 170 @end
 171 @implementation AWTWindow_Panel
 172 AWT_NS_WINDOW_IMPLEMENTATION
 173 @end
 174 // END of NSWindow/NSPanel descendants implementation
 175 // --------------------------------------------------------------
 176 
 177 
 178 @implementation AWTWindow
 179 
 180 @synthesize nsWindow;
 181 @synthesize javaPlatformWindow;
 182 @synthesize javaMenuBar;
 183 @synthesize javaMinSize;
 184 @synthesize javaMaxSize;
 185 @synthesize styleBits;
 186 @synthesize isEnabled;
 187 @synthesize ownerWindow;
 188 @synthesize preFullScreenLevel;
 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                   zoomed:(BOOL)zoomed
 275 {
 276 AWT_ASSERT_APPKIT_THREAD;
 277 
 278     NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits];
 279     NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
 280     if (contentRect.size.width <= 0.0) {
 281         contentRect.size.width = 1.0;
 282     }
 283     if (contentRect.size.height <= 0.0) {
 284         contentRect.size.height = 1.0;
 285     }
 286 
 287     self = [super init];
 288 
 289     if (self == nil) return nil; // no hope
 290 
 291     if (IS(bits, UTILITY) ||
 292         IS(bits, NONACTIVATING) ||
 293         IS(bits, HUD) ||
 294         IS(bits, HIDES_ON_DEACTIVATE))
 295     {
 296         self.nsWindow = [[AWTWindow_Panel alloc] initWithDelegate:self
 297                             frameRect:contentRect
 298                             styleMask:styleMask
 299                           contentView:view
 300                                zoomed:NO];
 301     }
 302     else
 303     {
 304         // These windows will appear in the window list in the dock icon menu
 305         self.nsWindow = [[AWTWindow_Normal alloc] initWithDelegate:self
 306                             frameRect:contentRect
 307                             styleMask:styleMask
 308                             contentView:view
 309                             zoomed: zoomed];
 310     }
 311 
 312     if (self.nsWindow == nil) return nil; // no hope either
 313     [self.nsWindow release]; // the property retains the object already
 314 
 315     self.isEnabled = YES;
 316     self.javaPlatformWindow = platformWindow;
 317     self.styleBits = bits;
 318     self.ownerWindow = owner;
 319     [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
 320 
 321     if (IS(self.styleBits, IS_POPUP)) {
 322         [self.nsWindow setCollectionBehavior:(1 << 8) /*NSWindowCollectionBehaviorFullScreenAuxiliary*/]; 
 323     }
 324 
 325     return self;
 326 }
 327 
 328 + (BOOL) isAWTWindow:(NSWindow *)window {
 329     return [window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]];
 330 }
 331 
 332 // returns id for the topmost window under mouse
 333 + (NSInteger) getTopmostWindowUnderMouseID {
 334     NSInteger result = -1;
 335     
 336     NSRect screenRect = [[NSScreen mainScreen] frame];
 337     NSPoint nsMouseLocation = [NSEvent mouseLocation];
 338     CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y);
 339 
 340     NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
 341 
 342     for (NSDictionary *window in windows) {
 343         NSInteger layer = [[window objectForKey:(id)kCGWindowLayer] integerValue];
 344         if (layer == 0) {
 345             CGRect rect;
 346             CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect);
 347             if (CGRectContainsPoint(rect, cgMouseLocation)) {
 348                 result = [[window objectForKey:(id)kCGWindowNumber] integerValue];
 349                 break;
 350             }
 351         }
 352     }
 353     [windows release];
 354     return result;
 355 }
 356 
 357 // checks that this window is under the mouse cursor and this point is not overlapped by others windows
 358 - (BOOL) isTopmostWindowUnderMouse {
 359     return [self.nsWindow windowNumber] == [AWTWindow getTopmostWindowUnderMouseID];
 360 }
 361 
 362 + (AWTWindow *) getTopmostWindowUnderMouse {
 363     NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator];
 364     NSWindow *window;
 365 
 366     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 367 
 368     while ((window = [windowEnumerator nextObject]) != nil) {
 369         if ([window windowNumber] == topmostWindowUnderMouseID) {
 370             BOOL isAWTWindow = [AWTWindow isAWTWindow: window];
 371             return isAWTWindow ? (AWTWindow *) [window delegate] : nil;
 372         }
 373     }
 374     return nil;
 375 }
 376 
 377 + (void) synthesizeMouseEnteredExitedEvents:(NSWindow*)window withType:(NSEventType)eventType {
 378 
 379     NSPoint screenLocation = [NSEvent mouseLocation];
 380     NSPoint windowLocation = [window convertScreenToBase: screenLocation];
 381     int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask;
 382 
 383     NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType
 384                                                  location: windowLocation
 385                                             modifierFlags: modifierFlags
 386                                                 timestamp: 0
 387                                              windowNumber: [window windowNumber]
 388                                                   context: nil
 389                                               eventNumber: 0
 390                                            trackingNumber: 0
 391                                                  userData: nil
 392                            ];
 393 
 394     [[window contentView] deliverJavaMouseEvent: mouseEvent];
 395 }
 396 
 397 + (void) synthesizeMouseEnteredExitedEventsForAllWindows {
 398 
 399     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 400     NSArray *windows = [NSApp windows];
 401     NSWindow *window;
 402 
 403     NSEnumerator *windowEnumerator = [windows objectEnumerator];
 404     while ((window = [windowEnumerator nextObject]) != nil) {
 405         if ([AWTWindow isAWTWindow: window]) {
 406             BOOL isUnderMouse = ([window windowNumber] == topmostWindowUnderMouseID);
 407             BOOL mouseIsOver = [[window contentView] mouseIsOver];
 408             if (isUnderMouse && !mouseIsOver) {
 409                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseEntered];
 410             } else if (!isUnderMouse && mouseIsOver) {
 411                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseExited];
 412             }
 413         }
 414     }
 415 }
 416 
 417 + (NSNumber *) getNSWindowDisplayID_AppKitThread:(NSWindow *)window {
 418     AWT_ASSERT_APPKIT_THREAD;
 419     NSScreen *screen = [window screen];
 420     NSDictionary *deviceDescription = [screen deviceDescription];
 421     return [deviceDescription objectForKey:@"NSScreenNumber"];
 422 }
 423 
 424 - (void) dealloc {
 425 AWT_ASSERT_APPKIT_THREAD;
 426 
 427     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 428     [self.javaPlatformWindow setJObject:nil withEnv:env];
 429 
 430     self.nsWindow = nil;
 431     self.ownerWindow = nil;
 432     [super dealloc];
 433 }
 434 
 435 // NSWindow overrides
 436 - (BOOL) canBecomeKeyWindow {
 437 AWT_ASSERT_APPKIT_THREAD;
 438     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_KEY);
 439 }
 440 
 441 - (BOOL) canBecomeMainWindow {
 442 AWT_ASSERT_APPKIT_THREAD;
 443     if (!self.isEnabled) {
 444         // Native system can bring up the NSWindow to
 445         // the top even if the window is not main.
 446         // We should bring up the modal dialog manually
 447         [AWTToolkit eventCountPlusPlus];
 448 
 449         JNIEnv *env = [ThreadUtilities getJNIEnv];
 450         jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 451         if (platformWindow != NULL) {
 452             static JNF_MEMBER_CACHE(jm_checkBlockingAndOrder, jc_CPlatformWindow,
 453                                     "checkBlockingAndOrder", "()Z");
 454             JNFCallBooleanMethod(env, platformWindow, jm_checkBlockingAndOrder);
 455             (*env)->DeleteLocalRef(env, platformWindow);
 456         }
 457     }
 458 
 459     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN);
 460 }
 461 
 462 - (BOOL) worksWhenModal {
 463 AWT_ASSERT_APPKIT_THREAD;
 464     return IS(self.styleBits, MODAL_EXCLUDED);
 465 }
 466 
 467 
 468 // NSWindowDelegate methods
 469 
 470 - (void) _deliverMoveResizeEvent {
 471 AWT_ASSERT_APPKIT_THREAD;
 472 
 473     // deliver the event if this is a user-initiated live resize or as a side-effect
 474     // of a Java initiated resize, because AppKit can override the bounds and force
 475     // the bounds of the window to avoid the Dock or remain on screen.
 476     [AWTToolkit eventCountPlusPlus];
 477     JNIEnv *env = [ThreadUtilities getJNIEnv];
 478     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 479     if (platformWindow == NULL) {
 480         // TODO: create generic AWT assert
 481     }
 482 
 483     NSRect frame = ConvertNSScreenRect(env, [self.nsWindow frame]);
 484 
 485     static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIIIZ)V");
 486     JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
 487                       (jint)frame.origin.x,
 488                       (jint)frame.origin.y,
 489                       (jint)frame.size.width,
 490                       (jint)frame.size.height,
 491                       (jboolean)[self.nsWindow inLiveResize]);
 492     (*env)->DeleteLocalRef(env, platformWindow);
 493 
 494     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 495 }
 496 
 497 - (void)windowDidMove:(NSNotification *)notification {
 498 AWT_ASSERT_APPKIT_THREAD;
 499 
 500     [self _deliverMoveResizeEvent];
 501 }
 502 
 503 - (void)windowDidResize:(NSNotification *)notification {
 504 AWT_ASSERT_APPKIT_THREAD;
 505 
 506     [self _deliverMoveResizeEvent];
 507 }
 508 
 509 - (void)windowDidExpose:(NSNotification *)notification {
 510 AWT_ASSERT_APPKIT_THREAD;
 511 
 512     [AWTToolkit eventCountPlusPlus];
 513     // TODO: don't see this callback invoked anytime so we track
 514     // window exposing in _setVisible:(BOOL)
 515 }
 516 
 517 - (void) _deliverIconify:(BOOL)iconify {
 518 AWT_ASSERT_APPKIT_THREAD;
 519 
 520     [AWTToolkit eventCountPlusPlus];
 521     JNIEnv *env = [ThreadUtilities getJNIEnv];
 522     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 523     if (platformWindow != NULL) {
 524         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 525         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 526         (*env)->DeleteLocalRef(env, platformWindow);
 527     }
 528 }
 529 
 530 - (void)windowDidMiniaturize:(NSNotification *)notification {
 531 AWT_ASSERT_APPKIT_THREAD;
 532 
 533     [self _deliverIconify:JNI_TRUE];
 534 }
 535 
 536 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 537 AWT_ASSERT_APPKIT_THREAD;
 538 
 539     [self _deliverIconify:JNI_FALSE];
 540 }
 541 
 542 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
 543 //AWT_ASSERT_APPKIT_THREAD;
 544     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 545     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 546     if (platformWindow != NULL) {
 547         jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env];
 548 
 549         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
 550         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
 551         (*env)->DeleteLocalRef(env, platformWindow);
 552         (*env)->DeleteLocalRef(env, oppositeWindow);
 553     }
 554 }
 555 
 556 
 557 - (void) windowDidBecomeKey: (NSNotification *) notification {
 558 AWT_ASSERT_APPKIT_THREAD;
 559     [AWTToolkit eventCountPlusPlus];
 560     AWTWindow *opposite = [AWTWindow lastKeyWindow];
 561 
 562     // Finds appropriate menubar in our hierarchy,
 563     AWTWindow *awtWindow = self;
 564     while (awtWindow.ownerWindow != nil) {
 565         awtWindow = awtWindow.ownerWindow;
 566     }
 567 
 568     CMenuBar *menuBar = nil;
 569     BOOL isDisabled = NO;
 570     if ([awtWindow.nsWindow isVisible]){
 571         menuBar = awtWindow.javaMenuBar;
 572         isDisabled = !awtWindow.isEnabled;
 573     }
 574 
 575     if (menuBar == nil) {
 576         menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 577         isDisabled = NO;
 578     }
 579 
 580     [CMenuBar activate:menuBar modallyDisabled:isDisabled];
 581 
 582     [AWTWindow setLastKeyWindow:nil];
 583 
 584     [self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
 585 }
 586 
 587 - (void) windowDidResignKey: (NSNotification *) notification {
 588     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 589 AWT_ASSERT_APPKIT_THREAD;
 590     [AWTToolkit eventCountPlusPlus];
 591     [self.javaMenuBar deactivate];
 592 
 593     // In theory, this might cause flickering if the window gaining focus
 594     // has its own menu. However, I couldn't reproduce it on practice, so
 595     // perhaps this is a non issue.
 596     CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 597     if (defaultMenu != nil) {
 598         [CMenuBar activate:defaultMenu modallyDisabled:NO];
 599     }
 600 
 601     // the new key window
 602     NSWindow *keyWindow = [NSApp keyWindow];
 603     AWTWindow *opposite = nil;
 604     if ([AWTWindow isAWTWindow: keyWindow]) {
 605         opposite = (AWTWindow *)[keyWindow delegate];
 606         [AWTWindow setLastKeyWindow: self];
 607     } else {
 608         [AWTWindow setLastKeyWindow: nil];
 609     }
 610 
 611     [self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
 612 }
 613 
 614 - (void) windowDidBecomeMain: (NSNotification *) notification {
 615 AWT_ASSERT_APPKIT_THREAD;
 616     [AWTToolkit eventCountPlusPlus];
 617 
 618     JNIEnv *env = [ThreadUtilities getJNIEnv];
 619     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 620     if (platformWindow != NULL) {
 621         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 622         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 623         (*env)->DeleteLocalRef(env, platformWindow);
 624     }
 625 }
 626 
 627 - (BOOL)windowShouldClose:(id)sender {
 628 AWT_ASSERT_APPKIT_THREAD;
 629     [AWTToolkit eventCountPlusPlus];
 630     JNIEnv *env = [ThreadUtilities getJNIEnv];
 631     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 632     if (platformWindow != NULL) {
 633         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 634         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 635         (*env)->DeleteLocalRef(env, platformWindow);
 636     }
 637     // The window will be closed (if allowed) as result of sending Java event
 638     return NO;
 639 }
 640 
 641 
 642 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 643     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 644     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 645     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 646     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 647     if (platformWindow != NULL) {
 648         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 649         if (awtWindow != NULL) {
 650             JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op);
 651             (*env)->DeleteLocalRef(env, awtWindow);
 652         }
 653         (*env)->DeleteLocalRef(env, platformWindow);
 654     }
 655 }
 656 
 657 
 658 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
 659     static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
 660     JNIEnv *env = [ThreadUtilities getJNIEnv];
 661     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 662     if (platformWindow != NULL) {
 663         JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
 664         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
 665         (*env)->DeleteLocalRef(env, platformWindow);
 666     }
 667 }
 668 
 669 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
 670     static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
 671     JNIEnv *env = [ThreadUtilities getJNIEnv];
 672     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 673     if (platformWindow != NULL) {
 674         JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
 675         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
 676         (*env)->DeleteLocalRef(env, platformWindow);
 677     }
 678     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 679 }
 680 
 681 - (void)windowWillExitFullScreen:(NSNotification *)notification {
 682     static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
 683     JNIEnv *env = [ThreadUtilities getJNIEnv];
 684     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 685     if (platformWindow != NULL) {
 686         JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
 687         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
 688         (*env)->DeleteLocalRef(env, platformWindow);
 689     }
 690 }
 691 
 692 - (void)windowDidExitFullScreen:(NSNotification *)notification {
 693     static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
 694     JNIEnv *env = [ThreadUtilities getJNIEnv];
 695     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 696     if (platformWindow != NULL) {
 697         JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
 698         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 699         (*env)->DeleteLocalRef(env, platformWindow);
 700     }
 701     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 702 }
 703 
 704 - (void)sendEvent:(NSEvent *)event {
 705         if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
 706 
 707             NSPoint p = [NSEvent mouseLocation];
 708             NSRect frame = [self.nsWindow frame];
 709             NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame];
 710 
 711             // Check if the click happened in the non-client area (title bar)
 712             if (p.y >= (frame.origin.y + contentRect.size.height)) {
 713                 JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 714                 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 715                 // Currently, no need to deliver the whole NSEvent.
 716                 static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
 717                 JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
 718             }
 719         }
 720 }
 721 
 722 - (void)constrainSize:(NSSize*)size {
 723     float minWidth = 0.f, minHeight = 0.f;
 724 
 725     if (IS(self.styleBits, DECORATED)) {
 726         NSRect frame = [self.nsWindow frame];
 727         NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self.nsWindow styleMask]];
 728 
 729         float top = frame.size.height - contentRect.size.height;
 730         float left = contentRect.origin.x - frame.origin.x;
 731         float bottom = contentRect.origin.y - frame.origin.y;
 732         float right = frame.size.width - (contentRect.size.width + left);
 733 
 734         // Speculative estimation: 80 - enough for window decorations controls
 735         minWidth += left + right + 80;
 736         minHeight += top + bottom;
 737     }
 738 
 739     minWidth = MAX(1.f, minWidth);
 740     minHeight = MAX(1.f, minHeight);
 741 
 742     size->width = MAX(size->width, minWidth);
 743     size->height = MAX(size->height, minHeight);
 744 }
 745 
 746 - (void) setEnabled: (BOOL)flag {
 747     self.isEnabled = flag;
 748 
 749     if (IS(self.styleBits, CLOSEABLE)) {
 750         [[self.nsWindow standardWindowButton:NSWindowCloseButton] setEnabled: flag];
 751     }
 752 
 753     if (IS(self.styleBits, MINIMIZABLE)) {
 754         [[self.nsWindow standardWindowButton:NSWindowMiniaturizeButton] setEnabled: flag];
 755     }
 756 
 757     if (IS(self.styleBits, ZOOMABLE)) {
 758         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled: flag];
 759     }
 760 
 761     if (IS(self.styleBits, RESIZABLE)) {
 762         [self updateMinMaxSize:flag];
 763         [self.nsWindow setShowsResizeIndicator:flag];
 764     }
 765 }
 766 
 767 + (void) setLastKeyWindow:(AWTWindow *)window {
 768     [window retain];
 769     [lastKeyWindow release];
 770     lastKeyWindow = window;
 771 }
 772 
 773 + (AWTWindow *) lastKeyWindow {
 774     return lastKeyWindow;
 775 }
 776 
 777 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame {
 778     return !NSEqualSizes(self.nsWindow.frame.size, newFrame.size);
 779 }
 780 
 781 
 782 @end // AWTWindow
 783 
 784 
 785 /*
 786  * Class:     sun_lwawt_macosx_CPlatformWindow
 787  * Method:    nativeCreateNSWindow
 788  * Signature: (JJIIII)J
 789  */
 790 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow
 791 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong ownerPtr, jlong styleBits,
 792    jdouble x, jdouble y, jdouble w, jdouble h, jboolean zoomed)
 793 {
 794     __block AWTWindow *window = nil;
 795 
 796 JNF_COCOA_ENTER(env);
 797 
 798     JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
 799     NSView *contentView = OBJC(contentViewPtr);
 800     NSRect frameRect = NSMakeRect(x, y, w, h);
 801     AWTWindow *owner = [OBJC(ownerPtr) delegate];
 802     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 803 
 804         window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow
 805                                                ownerWindow:owner
 806                                                  styleBits:styleBits
 807                                                  frameRect:frameRect
 808                                                contentView:contentView
 809                                                     zoomed: zoomed ? YES : NO];
 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:    nativeSetNSWindowMinMax
 964  * Signature: (JDDDD)V
 965  */
 966 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax
 967 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH)
 968 {
 969 JNF_COCOA_ENTER(env);
 970 
 971     if (minW < 1) minW = 1;
 972     if (minH < 1) minH = 1;
 973     if (maxW < 1) maxW = 1;
 974     if (maxH < 1) maxH = 1;
 975 
 976     NSWindow *nsWindow = OBJC(windowPtr);
 977     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 978 
 979         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
 980 
 981         NSSize min = { minW, minH };
 982         NSSize max = { maxW, maxH };
 983 
 984         [window constrainSize:&min];
 985         [window constrainSize:&max];
 986 
 987         window.javaMinSize = min;
 988         window.javaMaxSize = max;
 989         [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)];
 990     }];
 991 
 992 JNF_COCOA_EXIT(env);
 993 }
 994 
 995 /*
 996  * Class:     sun_lwawt_macosx_CPlatformWindow
 997  * Method:    nativePushNSWindowToBack
 998  * Signature: (J)V
 999  */
1000 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack
1001 (JNIEnv *env, jclass clazz, jlong windowPtr)
1002 {
1003 JNF_COCOA_ENTER(env);
1004 
1005     NSWindow *nsWindow = OBJC(windowPtr);
1006     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1007         [nsWindow orderBack:nil];
1008     }];
1009 
1010 JNF_COCOA_EXIT(env);
1011 }
1012 
1013 /*
1014  * Class:     sun_lwawt_macosx_CPlatformWindow
1015  * Method:    nativePushNSWindowToFront
1016  * Signature: (J)V
1017  */
1018 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront
1019 (JNIEnv *env, jclass clazz, jlong windowPtr)
1020 {
1021 JNF_COCOA_ENTER(env);
1022 
1023     NSWindow *nsWindow = OBJC(windowPtr);
1024     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1025 
1026         if (![nsWindow isKeyWindow]) {
1027             [nsWindow makeKeyAndOrderFront:nsWindow];
1028         } else {
1029             [nsWindow orderFront:nsWindow];
1030         }
1031     }];
1032 
1033 JNF_COCOA_EXIT(env);
1034 }
1035 
1036 /*
1037  * Class:     sun_lwawt_macosx_CPlatformWindow
1038  * Method:    nativeSetNSWindowTitle
1039  * Signature: (JLjava/lang/String;)V
1040  */
1041 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle
1042 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle)
1043 {
1044 JNF_COCOA_ENTER(env);
1045 
1046     NSWindow *nsWindow = OBJC(windowPtr);
1047     [nsWindow performSelectorOnMainThread:@selector(setTitle:)
1048                               withObject:JNFJavaToNSString(env, jtitle)
1049                            waitUntilDone:NO];
1050 
1051 JNF_COCOA_EXIT(env);
1052 }
1053 
1054 /*
1055  * Class:     sun_lwawt_macosx_CPlatformWindow
1056  * Method:    nativeRevalidateNSWindowShadow
1057  * Signature: (J)V
1058  */
1059 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow
1060 (JNIEnv *env, jclass clazz, jlong windowPtr)
1061 {
1062 JNF_COCOA_ENTER(env);
1063 
1064     NSWindow *nsWindow = OBJC(windowPtr);
1065     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1066         [nsWindow invalidateShadow];
1067     }];
1068 
1069 JNF_COCOA_EXIT(env);
1070 }
1071 
1072 /*
1073  * Class:     sun_lwawt_macosx_CPlatformWindow
1074  * Method:    nativeScreenOn_AppKitThread
1075  * Signature: (J)I
1076  */
1077 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread
1078 (JNIEnv *env, jclass clazz, jlong windowPtr)
1079 {
1080     jint ret = 0;
1081 
1082 JNF_COCOA_ENTER(env);
1083 AWT_ASSERT_APPKIT_THREAD;
1084 
1085     NSWindow *nsWindow = OBJC(windowPtr);
1086     NSDictionary *props = [[nsWindow screen] deviceDescription];
1087     ret = [[props objectForKey:@"NSScreenNumber"] intValue];
1088 
1089 JNF_COCOA_EXIT(env);
1090 
1091     return ret;
1092 }
1093 
1094 /*
1095  * Class:     sun_lwawt_macosx_CPlatformWindow
1096  * Method:    nativeSetNSWindowMinimizedIcon
1097  * Signature: (JJ)V
1098  */
1099 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon
1100 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr)
1101 {
1102 JNF_COCOA_ENTER(env);
1103 
1104     NSWindow *nsWindow = OBJC(windowPtr);
1105     NSImage *image = OBJC(nsImagePtr);
1106     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1107         [nsWindow setMiniwindowImage:image];
1108     }];
1109 
1110 JNF_COCOA_EXIT(env);
1111 }
1112 
1113 /*
1114  * Class:     sun_lwawt_macosx_CPlatformWindow
1115  * Method:    nativeSetNSWindowRepresentedFilename
1116  * Signature: (JLjava/lang/String;)V
1117  */
1118 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename
1119 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename)
1120 {
1121 JNF_COCOA_ENTER(env);
1122 
1123     NSWindow *nsWindow = OBJC(windowPtr);
1124     NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)];
1125     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1126         [nsWindow setRepresentedURL:url];
1127     }];
1128 
1129 JNF_COCOA_EXIT(env);
1130 }
1131 
1132 /*
1133  * Class:     sun_lwawt_macosx_CPlatformWindow
1134  * Method:    nativeGetTopmostPlatformWindowUnderMouse
1135  * Signature: (J)V
1136  */
1137 JNIEXPORT jobject
1138 JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetTopmostPlatformWindowUnderMouse
1139 (JNIEnv *env, jclass clazz)
1140 {
1141     jobject topmostWindowUnderMouse = nil;
1142 
1143     JNF_COCOA_ENTER(env);
1144     AWT_ASSERT_APPKIT_THREAD;
1145 
1146     AWTWindow *awtWindow = [AWTWindow getTopmostWindowUnderMouse];
1147     if (awtWindow != nil) {
1148         topmostWindowUnderMouse = [awtWindow.javaPlatformWindow jObject];
1149     }
1150 
1151     JNF_COCOA_EXIT(env);
1152 
1153     return topmostWindowUnderMouse;
1154 }
1155 
1156 /*
1157  * Class:     sun_lwawt_macosx_CPlatformWindow
1158  * Method:    nativeSynthesizeMouseEnteredExitedEvents
1159  * Signature: (J)V
1160  */
1161 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents
1162 (JNIEnv *env, jclass clazz)
1163 {
1164     JNF_COCOA_ENTER(env);
1165 
1166     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1167         [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
1168     }];
1169 
1170     JNF_COCOA_EXIT(env);
1171 }
1172 
1173 /*
1174  * Class:     sun_lwawt_macosx_CPlatformWindow
1175  * Method:    _toggleFullScreenMode
1176  * Signature: (J)V
1177  */
1178 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode
1179 (JNIEnv *env, jobject peer, jlong windowPtr)
1180 {
1181 JNF_COCOA_ENTER(env);
1182 
1183     NSWindow *nsWindow = OBJC(windowPtr);
1184     SEL toggleFullScreenSelector = @selector(toggleFullScreen:);
1185     if (![nsWindow respondsToSelector:toggleFullScreenSelector]) return;
1186 
1187     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1188         [nsWindow performSelector:toggleFullScreenSelector withObject:nil];
1189     }];
1190 
1191 JNF_COCOA_EXIT(env);
1192 }
1193 
1194 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetEnabled
1195 (JNIEnv *env, jclass clazz, jlong windowPtr, jboolean isEnabled)
1196 {
1197 JNF_COCOA_ENTER(env);
1198 
1199     NSWindow *nsWindow = OBJC(windowPtr);
1200     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1201         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1202 
1203         [window setEnabled: isEnabled];
1204     }];
1205 
1206 JNF_COCOA_EXIT(env);
1207 }
1208 
1209 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeDispose
1210 (JNIEnv *env, jclass clazz, jlong windowPtr)
1211 {
1212 JNF_COCOA_ENTER(env);
1213 
1214     NSWindow *nsWindow = OBJC(windowPtr);
1215     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1216         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1217 
1218         if ([AWTWindow lastKeyWindow] == window) {
1219             [AWTWindow setLastKeyWindow: nil];
1220         }
1221 
1222         // AWTWindow holds a reference to the NSWindow in its nsWindow
1223         // property. Unsetting the delegate allows it to be deallocated
1224         // which releases the reference. This, in turn, allows the window
1225         // itself be deallocated.
1226         [nsWindow setDelegate: nil];
1227 
1228         [window release];
1229     }];
1230 
1231 JNF_COCOA_EXIT(env);
1232 }
1233 
1234 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeEnterFullScreenMode
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         NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow];
1243         CGDirectDisplayID aID = [screenID intValue];
1244 
1245         if (CGDisplayCapture(aID) == kCGErrorSuccess) {
1246             // remove window decoration
1247             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1248             [nsWindow setStyleMask:(styleMask & ~NSTitledWindowMask) | NSBorderlessWindowMask];
1249 
1250             int shieldLevel = CGShieldingWindowLevel();
1251             window.preFullScreenLevel = [nsWindow level];
1252             [nsWindow setLevel: shieldLevel];
1253 
1254             NSRect screenRect = [[nsWindow screen] frame];
1255             [nsWindow setFrame:screenRect display:YES];
1256         } else {
1257             [JNFException raise:[ThreadUtilities getJNIEnv]
1258                              as:kRuntimeException
1259                          reason:"Failed to enter full screen."];
1260         }
1261     }];
1262 
1263 JNF_COCOA_EXIT(env);
1264 }
1265 
1266 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeExitFullScreenMode
1267 (JNIEnv *env, jclass clazz, jlong windowPtr)
1268 {
1269 JNF_COCOA_ENTER(env);
1270 
1271     NSWindow *nsWindow = OBJC(windowPtr);
1272     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1273         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1274         NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow];
1275         CGDirectDisplayID aID = [screenID intValue];
1276 
1277         if (CGDisplayRelease(aID) == kCGErrorSuccess) {
1278             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1279             [nsWindow setStyleMask:styleMask]; 
1280             [nsWindow setLevel: window.preFullScreenLevel];
1281 
1282             // GraphicsDevice takes care of restoring pre full screen bounds
1283         } else {
1284             [JNFException raise:[ThreadUtilities getJNIEnv]
1285                              as:kRuntimeException
1286                          reason:"Failed to exit full screen."];
1287         }
1288     }];
1289 
1290 JNF_COCOA_EXIT(env);
1291 }
1292