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 {                                                               \
  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 
 188 - (void) updateMinMaxSize:(BOOL)resizable {
 189     if (resizable) {
 190         [self.nsWindow setMinSize:self.javaMinSize];
 191         [self.nsWindow setMaxSize:self.javaMaxSize];
 192     } else {
 193         NSRect currentFrame = [self.nsWindow frame];
 194         [self.nsWindow setMinSize:currentFrame.size];
 195         [self.nsWindow setMaxSize:currentFrame.size];
 196     }
 197 }
 198 
 199 // creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits
 200 + (NSUInteger) styleMaskForStyleBits:(jint)styleBits {
 201     NSUInteger type = 0;
 202     if (IS(styleBits, DECORATED)) {
 203         type |= NSTitledWindowMask;
 204         if (IS(styleBits, CLOSEABLE))   type |= NSClosableWindowMask;
 205         if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask;
 206         if (IS(styleBits, RESIZABLE))   type |= NSResizableWindowMask;
 207     } else {
 208         type |= NSBorderlessWindowMask;
 209     }
 210 
 211     if (IS(styleBits, TEXTURED))      type |= NSTexturedBackgroundWindowMask;
 212     if (IS(styleBits, UNIFIED))       type |= NSUnifiedTitleAndToolbarWindowMask;
 213     if (IS(styleBits, UTILITY))       type |= NSUtilityWindowMask;
 214     if (IS(styleBits, HUD))           type |= NSHUDWindowMask;
 215     if (IS(styleBits, SHEET))         type |= NSDocModalWindowMask;
 216     if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask;
 217 
 218     return type;
 219 }
 220 
 221 // updates _METHOD_PROP_BITMASK based properties on the window
 222 - (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask {
 223     if (IS(mask, RESIZABLE)) {
 224         BOOL resizable = IS(bits, RESIZABLE);
 225         [self updateMinMaxSize:resizable];
 226         [self.nsWindow setShowsResizeIndicator:resizable];
 227         // Zoom button should be disabled, if the window is not resizable,
 228         // otherwise button should be restored to initial state.
 229         BOOL zoom = resizable && IS(bits, ZOOMABLE);
 230         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:zoom];
 231     }
 232 
 233     if (IS(mask, HAS_SHADOW)) {
 234         [self.nsWindow setHasShadow:IS(bits, HAS_SHADOW)];
 235     }
 236 
 237     if (IS(mask, ZOOMABLE)) {
 238         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)];
 239     }
 240 
 241     if (IS(mask, ALWAYS_ON_TOP)) {
 242         [self.nsWindow setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel];
 243     }
 244 
 245     if (IS(mask, HIDES_ON_DEACTIVATE)) {
 246         [self.nsWindow setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)];
 247     }
 248 
 249     if (IS(mask, DRAGGABLE_BACKGROUND)) {
 250         [self.nsWindow setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)];
 251     }
 252 
 253     if (IS(mask, DOCUMENT_MODIFIED)) {
 254         [self.nsWindow setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)];
 255     }
 256 
 257     if (IS(mask, FULLSCREENABLE) && [self.nsWindow respondsToSelector:@selector(toggleFullScreen:)]) {
 258         if (IS(bits, FULLSCREENABLE)) {
 259             [self.nsWindow setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/];
 260         } else {
 261             [self.nsWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault];
 262         }
 263     }
 264 
 265 }
 266 
 267 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow
 268                   ownerWindow:owner
 269                     styleBits:(jint)bits
 270                     frameRect:(NSRect)rect
 271                   contentView:(NSView *)view
 272 {
 273 AWT_ASSERT_APPKIT_THREAD;
 274 
 275     NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits];
 276     NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
 277     if (contentRect.size.width <= 0.0) {
 278         contentRect.size.width = 1.0;
 279     }
 280     if (contentRect.size.height <= 0.0) {
 281         contentRect.size.height = 1.0;
 282     }
 283 
 284     self = [super init];
 285 
 286     if (self == nil) return nil; // no hope
 287 
 288     if (IS(bits, UTILITY) ||
 289         IS(bits, NONACTIVATING) ||
 290         IS(bits, HUD) ||
 291         IS(bits, HIDES_ON_DEACTIVATE))
 292     {
 293         self.nsWindow = [[AWTWindow_Panel alloc] initWithDelegate:self
 294                             frameRect:contentRect
 295                             styleMask:styleMask
 296                           contentView:view];
 297     }
 298     else
 299     {
 300         // These windows will appear in the window list in the dock icon menu
 301         self.nsWindow = [[AWTWindow_Normal alloc] initWithDelegate:self
 302                             frameRect:contentRect
 303                             styleMask:styleMask
 304                           contentView:view];
 305     }
 306 
 307     if (self.nsWindow == nil) return nil; // no hope either
 308     [self.nsWindow release]; // the property retains the object already
 309 
 310     self.isEnabled = YES;
 311     self.javaPlatformWindow = platformWindow;
 312     self.styleBits = bits;
 313     self.ownerWindow = owner;
 314     [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
 315 
 316     if (IS(self.styleBits, IS_POPUP)) {
 317         [self.nsWindow setCollectionBehavior:(1 << 8) /*NSWindowCollectionBehaviorFullScreenAuxiliary*/]; 
 318     }
 319 
 320     return self;
 321 }
 322 
 323 + (BOOL) isAWTWindow:(NSWindow *)window {
 324     return [window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]];
 325 }
 326 
 327 // returns id for the topmost window under mouse
 328 + (NSInteger) getTopmostWindowUnderMouseID {
 329     NSInteger result = -1;
 330     
 331     NSRect screenRect = [[NSScreen mainScreen] frame];
 332     NSPoint nsMouseLocation = [NSEvent mouseLocation];
 333     CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y);
 334 
 335     NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
 336 
 337     for (NSDictionary *window in windows) {
 338         NSInteger layer = [[window objectForKey:(id)kCGWindowLayer] integerValue];
 339         if (layer == 0) {
 340             CGRect rect;
 341             CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect);
 342             if (CGRectContainsPoint(rect, cgMouseLocation)) {
 343                 result = [[window objectForKey:(id)kCGWindowNumber] integerValue];
 344                 break;
 345             }
 346         }
 347     }
 348     [windows release];
 349     return result;
 350 }
 351 
 352 // checks that this window is under the mouse cursor and this point is not overlapped by others windows
 353 - (BOOL) isTopmostWindowUnderMouse {
 354     return [self.nsWindow windowNumber] == [AWTWindow getTopmostWindowUnderMouseID];
 355 }
 356 
 357 + (AWTWindow *) getTopmostWindowUnderMouse {
 358     NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator];
 359     NSWindow *window;
 360 
 361     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 362 
 363     while ((window = [windowEnumerator nextObject]) != nil) {
 364         if ([window windowNumber] == topmostWindowUnderMouseID) {
 365             BOOL isAWTWindow = [AWTWindow isAWTWindow: window];
 366             return isAWTWindow ? (AWTWindow *) [window delegate] : nil;
 367         }
 368     }
 369     return nil;
 370 }
 371 
 372 + (void) synthesizeMouseEnteredExitedEvents:(NSWindow*)window withType:(NSEventType)eventType {
 373 
 374     NSPoint screenLocation = [NSEvent mouseLocation];
 375     NSPoint windowLocation = [window convertScreenToBase: screenLocation];
 376     int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask;
 377 
 378     NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType
 379                                                  location: windowLocation
 380                                             modifierFlags: modifierFlags
 381                                                 timestamp: 0
 382                                              windowNumber: [window windowNumber]
 383                                                   context: nil
 384                                               eventNumber: 0
 385                                            trackingNumber: 0
 386                                                  userData: nil
 387                            ];
 388 
 389     [[window contentView] deliverJavaMouseEvent: mouseEvent];
 390 }
 391 
 392 + (void) synthesizeMouseEnteredExitedEventsForAllWindows {
 393 
 394     NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
 395     NSArray *windows = [NSApp windows];
 396     NSWindow *window;
 397 
 398     NSEnumerator *windowEnumerator = [windows objectEnumerator];
 399     while ((window = [windowEnumerator nextObject]) != nil) {
 400         if ([AWTWindow isAWTWindow: window]) {
 401             BOOL isUnderMouse = ([window windowNumber] == topmostWindowUnderMouseID);
 402             BOOL mouseIsOver = [[window contentView] mouseIsOver];
 403             if (isUnderMouse && !mouseIsOver) {
 404                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseEntered];
 405             } else if (!isUnderMouse && mouseIsOver) {
 406                 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseExited];
 407             }
 408         }
 409     }
 410 }
 411 
 412 + (NSNumber *) getNSWindowDisplayID_AppKitThread:(NSWindow *)window {
 413     AWT_ASSERT_APPKIT_THREAD;
 414     NSScreen *screen = [window screen];
 415     NSDictionary *deviceDescription = [screen deviceDescription];
 416     return [deviceDescription objectForKey:@"NSScreenNumber"];
 417 }
 418 
 419 - (void) dealloc {
 420 AWT_ASSERT_APPKIT_THREAD;
 421 
 422     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 423     [self.javaPlatformWindow setJObject:nil withEnv:env];
 424 
 425     self.nsWindow = nil;
 426     self.ownerWindow = nil;
 427     [super dealloc];
 428 }
 429 
 430 // NSWindow overrides
 431 - (BOOL) canBecomeKeyWindow {
 432 AWT_ASSERT_APPKIT_THREAD;
 433     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_KEY);
 434 }
 435 
 436 - (BOOL) canBecomeMainWindow {
 437 AWT_ASSERT_APPKIT_THREAD;
 438     if (!self.isEnabled) {
 439         // Native system can bring up the NSWindow to
 440         // the top even if the window is not main.
 441         // We should bring up the modal dialog manually
 442         [AWTToolkit eventCountPlusPlus];
 443 
 444         JNIEnv *env = [ThreadUtilities getJNIEnv];
 445         jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 446         if (platformWindow != NULL) {
 447             static JNF_MEMBER_CACHE(jm_checkBlockingAndOrder, jc_CPlatformWindow,
 448                                     "checkBlockingAndOrder", "()Z");
 449             JNFCallBooleanMethod(env, platformWindow, jm_checkBlockingAndOrder);
 450             (*env)->DeleteLocalRef(env, platformWindow);
 451         }
 452     }
 453 
 454     return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN);
 455 }
 456 
 457 - (BOOL) worksWhenModal {
 458 AWT_ASSERT_APPKIT_THREAD;
 459     return IS(self.styleBits, MODAL_EXCLUDED);
 460 }
 461 
 462 
 463 // NSWindowDelegate methods
 464 
 465 - (void) _deliverMoveResizeEvent {
 466 AWT_ASSERT_APPKIT_THREAD;
 467 
 468     // deliver the event if this is a user-initiated live resize or as a side-effect
 469     // of a Java initiated resize, because AppKit can override the bounds and force
 470     // the bounds of the window to avoid the Dock or remain on screen.
 471     [AWTToolkit eventCountPlusPlus];
 472     JNIEnv *env = [ThreadUtilities getJNIEnv];
 473     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 474     if (platformWindow == NULL) {
 475         // TODO: create generic AWT assert
 476     }
 477 
 478     NSRect frame = ConvertNSScreenRect(env, [self.nsWindow frame]);
 479 
 480     static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIIIZ)V");
 481     JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
 482                       (jint)frame.origin.x,
 483                       (jint)frame.origin.y,
 484                       (jint)frame.size.width,
 485                       (jint)frame.size.height,
 486                       (jboolean)[self.nsWindow inLiveResize]);
 487     (*env)->DeleteLocalRef(env, platformWindow);
 488 
 489     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 490 }
 491 
 492 - (void)windowDidMove:(NSNotification *)notification {
 493 AWT_ASSERT_APPKIT_THREAD;
 494 
 495     [self _deliverMoveResizeEvent];
 496 }
 497 
 498 - (void)windowDidResize:(NSNotification *)notification {
 499 AWT_ASSERT_APPKIT_THREAD;
 500 
 501     [self _deliverMoveResizeEvent];
 502 }
 503 
 504 - (void)windowDidExpose:(NSNotification *)notification {
 505 AWT_ASSERT_APPKIT_THREAD;
 506 
 507     [AWTToolkit eventCountPlusPlus];
 508     // TODO: don't see this callback invoked anytime so we track
 509     // window exposing in _setVisible:(BOOL)
 510 }
 511 






 512 - (void) _deliverIconify:(BOOL)iconify {
 513 AWT_ASSERT_APPKIT_THREAD;
 514 
 515     [AWTToolkit eventCountPlusPlus];
 516     JNIEnv *env = [ThreadUtilities getJNIEnv];
 517     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 518     if (platformWindow != NULL) {
 519         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 520         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 521         (*env)->DeleteLocalRef(env, platformWindow);
 522     }
 523 }
 524 
 525 - (void)windowDidMiniaturize:(NSNotification *)notification {
 526 AWT_ASSERT_APPKIT_THREAD;
 527 
 528     [self _deliverIconify:JNI_TRUE];
 529 }
 530 
 531 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 532 AWT_ASSERT_APPKIT_THREAD;
 533 
 534     [self _deliverIconify:JNI_FALSE];
 535 }
 536 
 537 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
 538 //AWT_ASSERT_APPKIT_THREAD;
 539     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 540     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 541     if (platformWindow != NULL) {
 542         jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env];
 543 
 544         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
 545         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
 546         (*env)->DeleteLocalRef(env, platformWindow);
 547         (*env)->DeleteLocalRef(env, oppositeWindow);
 548     }
 549 }
 550 
 551 
 552 - (void) windowDidBecomeKey: (NSNotification *) notification {
 553 AWT_ASSERT_APPKIT_THREAD;
 554     [AWTToolkit eventCountPlusPlus];
 555     AWTWindow *opposite = [AWTWindow lastKeyWindow];
 556 
 557     // Finds appropriate menubar in our hierarchy,
 558     AWTWindow *awtWindow = self;
 559     while (awtWindow.ownerWindow != nil) {
 560         awtWindow = awtWindow.ownerWindow;
 561     }
 562 
 563     CMenuBar *menuBar = nil;
 564     BOOL isDisabled = NO;
 565     if ([awtWindow.nsWindow isVisible]){
 566         menuBar = awtWindow.javaMenuBar;
 567         isDisabled = !awtWindow.isEnabled;
 568     }
 569 
 570     if (menuBar == nil) {
 571         menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 572         isDisabled = NO;
 573     }
 574 
 575     [CMenuBar activate:menuBar modallyDisabled:isDisabled];
 576 
 577     [AWTWindow setLastKeyWindow:nil];
 578 
 579     [self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
 580 }
 581 
 582 - (void) windowDidResignKey: (NSNotification *) notification {
 583     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 584 AWT_ASSERT_APPKIT_THREAD;
 585     [AWTToolkit eventCountPlusPlus];
 586     [self.javaMenuBar deactivate];
 587 
 588     // In theory, this might cause flickering if the window gaining focus
 589     // has its own menu. However, I couldn't reproduce it on practice, so
 590     // perhaps this is a non issue.
 591     CMenuBar* defaultMenu = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 592     if (defaultMenu != nil) {
 593         [CMenuBar activate:defaultMenu modallyDisabled:NO];
 594     }
 595 
 596     // the new key window
 597     NSWindow *keyWindow = [NSApp keyWindow];
 598     AWTWindow *opposite = nil;
 599     if ([AWTWindow isAWTWindow: keyWindow]) {
 600         opposite = (AWTWindow *)[keyWindow delegate];
 601         [AWTWindow setLastKeyWindow: self];
 602     } else {
 603         [AWTWindow setLastKeyWindow: nil];
 604     }
 605 
 606     [self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
 607 }
 608 
 609 - (void) windowDidBecomeMain: (NSNotification *) notification {
 610 AWT_ASSERT_APPKIT_THREAD;
 611     [AWTToolkit eventCountPlusPlus];
 612 
 613     JNIEnv *env = [ThreadUtilities getJNIEnv];
 614     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 615     if (platformWindow != NULL) {
 616         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 617         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 618         (*env)->DeleteLocalRef(env, platformWindow);
 619     }
 620 }
 621 
 622 - (BOOL)windowShouldClose:(id)sender {
 623 AWT_ASSERT_APPKIT_THREAD;
 624     [AWTToolkit eventCountPlusPlus];
 625     JNIEnv *env = [ThreadUtilities getJNIEnv];
 626     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 627     if (platformWindow != NULL) {
 628         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 629         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 630         (*env)->DeleteLocalRef(env, platformWindow);
 631     }
 632     // The window will be closed (if allowed) as result of sending Java event
 633     return NO;
 634 }
 635 
 636 
 637 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 638     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 639     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 640     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 641     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 642     if (platformWindow != NULL) {
 643         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 644         if (awtWindow != NULL) {
 645             JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op);
 646             (*env)->DeleteLocalRef(env, awtWindow);
 647         }
 648         (*env)->DeleteLocalRef(env, platformWindow);
 649     }
 650 }
 651 
 652 
 653 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
 654     static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
 655     JNIEnv *env = [ThreadUtilities getJNIEnv];
 656     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 657     if (platformWindow != NULL) {
 658         JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
 659         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
 660         (*env)->DeleteLocalRef(env, platformWindow);
 661     }
 662 }
 663 
 664 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
 665     static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
 666     JNIEnv *env = [ThreadUtilities getJNIEnv];
 667     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 668     if (platformWindow != NULL) {
 669         JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
 670         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
 671         (*env)->DeleteLocalRef(env, platformWindow);
 672     }
 673     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 674 }
 675 
 676 - (void)windowWillExitFullScreen:(NSNotification *)notification {
 677     static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
 678     JNIEnv *env = [ThreadUtilities getJNIEnv];
 679     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 680     if (platformWindow != NULL) {
 681         JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
 682         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
 683         (*env)->DeleteLocalRef(env, platformWindow);
 684     }
 685 }
 686 
 687 - (void)windowDidExitFullScreen:(NSNotification *)notification {
 688     static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
 689     JNIEnv *env = [ThreadUtilities getJNIEnv];
 690     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 691     if (platformWindow != NULL) {
 692         JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
 693         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 694         (*env)->DeleteLocalRef(env, platformWindow);
 695     }
 696     [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
 697 }
 698 
 699 - (void)sendEvent:(NSEvent *)event {
 700         if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
 701 
 702             NSPoint p = [NSEvent mouseLocation];
 703             NSRect frame = [self.nsWindow frame];
 704             NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame];
 705 
 706             // Check if the click happened in the non-client area (title bar)
 707             if (p.y >= (frame.origin.y + contentRect.size.height)) {
 708                 JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 709                 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 710                 // Currently, no need to deliver the whole NSEvent.
 711                 static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
 712                 JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
 713             }
 714         }
 715 }
 716 
 717 - (void)constrainSize:(NSSize*)size {
 718     float minWidth = 0.f, minHeight = 0.f;
 719 
 720     if (IS(self.styleBits, DECORATED)) {
 721         NSRect frame = [self.nsWindow frame];
 722         NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self.nsWindow styleMask]];
 723 
 724         float top = frame.size.height - contentRect.size.height;
 725         float left = contentRect.origin.x - frame.origin.x;
 726         float bottom = contentRect.origin.y - frame.origin.y;
 727         float right = frame.size.width - (contentRect.size.width + left);
 728 
 729         // Speculative estimation: 80 - enough for window decorations controls
 730         minWidth += left + right + 80;
 731         minHeight += top + bottom;
 732     }
 733 
 734     minWidth = MAX(1.f, minWidth);
 735     minHeight = MAX(1.f, minHeight);
 736 
 737     size->width = MAX(size->width, minWidth);
 738     size->height = MAX(size->height, minHeight);
 739 }
 740 
 741 - (void) setEnabled: (BOOL)flag {
 742     self.isEnabled = flag;
 743 
 744     if (IS(self.styleBits, CLOSEABLE)) {
 745         [[self.nsWindow standardWindowButton:NSWindowCloseButton] setEnabled: flag];
 746     }
 747 
 748     if (IS(self.styleBits, MINIMIZABLE)) {
 749         [[self.nsWindow standardWindowButton:NSWindowMiniaturizeButton] setEnabled: flag];
 750     }
 751 
 752     if (IS(self.styleBits, ZOOMABLE)) {
 753         [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled: flag];
 754     }
 755 
 756     if (IS(self.styleBits, RESIZABLE)) {
 757         [self updateMinMaxSize:flag];
 758         [self.nsWindow setShowsResizeIndicator:flag];
 759     }
 760 }
 761 
 762 + (void) setLastKeyWindow:(AWTWindow *)window {
 763     [window retain];
 764     [lastKeyWindow release];
 765     lastKeyWindow = window;
 766 }
 767 
 768 + (AWTWindow *) lastKeyWindow {
 769     return lastKeyWindow;
 770 }
 771 
 772 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame {
 773     return !NSEqualSizes(self.nsWindow.frame.size, newFrame.size);
 774 }
 775 
 776 
 777 @end // AWTWindow
 778 
 779 
 780 /*
 781  * Class:     sun_lwawt_macosx_CPlatformWindow
 782  * Method:    nativeCreateNSWindow
 783  * Signature: (JJIIII)J
 784  */
 785 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow
 786 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong ownerPtr, jlong styleBits, jdouble x, jdouble y, jdouble w, jdouble h)
 787 {
 788     __block AWTWindow *window = nil;
 789 
 790 JNF_COCOA_ENTER(env);
 791 
 792     JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
 793     NSView *contentView = OBJC(contentViewPtr);
 794     NSRect frameRect = NSMakeRect(x, y, w, h);
 795     AWTWindow *owner = [OBJC(ownerPtr) delegate];
 796     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 797 
 798         window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow
 799                                                ownerWindow:owner
 800                                                  styleBits:styleBits
 801                                                  frameRect:frameRect
 802                                                contentView:contentView];
 803         // the window is released is CPlatformWindow.nativeDispose()
 804 
 805         if (window) [window.nsWindow retain];
 806     }];
 807 
 808 JNF_COCOA_EXIT(env);
 809 
 810     return ptr_to_jlong(window ? window.nsWindow : nil);
 811 }
 812 
 813 /*
 814  * Class:     sun_lwawt_macosx_CPlatformWindow
 815  * Method:    nativeSetNSWindowStyleBits
 816  * Signature: (JII)V
 817  */
 818 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStyleBits
 819 (JNIEnv *env, jclass clazz, jlong windowPtr, jint mask, jint bits)
 820 {
 821 JNF_COCOA_ENTER(env);
 822 
 823     NSWindow *nsWindow = OBJC(windowPtr);
 824     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 825 
 826         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
 827 
 828         // scans the bit field, and only updates the values requested by the mask
 829         // (this implicity handles the _CALLBACK_PROP_BITMASK case, since those are passive reads)
 830         jint newBits = window.styleBits & ~mask | bits & mask;
 831 
 832         // resets the NSWindow's style mask if the mask intersects any of those bits
 833         if (mask & MASK(_STYLE_PROP_BITMASK)) {
 834             [nsWindow setStyleMask:[AWTWindow styleMaskForStyleBits:newBits]];
 835         }
 836 
 837         // calls methods on NSWindow to change other properties, based on the mask
 838         if (mask & MASK(_METHOD_PROP_BITMASK)) {
 839             [window setPropertiesForStyleBits:newBits mask:mask];
 840         }
 841 
 842         window.styleBits = newBits;
 843     }];
 844 
 845 JNF_COCOA_EXIT(env);
 846 }
 847 
 848 /*
 849  * Class:     sun_lwawt_macosx_CPlatformWindow
 850  * Method:    nativeSetNSWindowMenuBar
 851  * Signature: (JJ)V
 852  */
 853 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
 854 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
 855 {
 856 JNF_COCOA_ENTER(env);
 857 
 858     NSWindow *nsWindow = OBJC(windowPtr);
 859     CMenuBar *menuBar = OBJC(menuBarPtr);
 860     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 861 
 862         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
 863 
 864         if ([nsWindow isKeyWindow]) {
 865             [window.javaMenuBar deactivate];
 866         }
 867 
 868         window.javaMenuBar = menuBar;
 869 
 870         CMenuBar* actualMenuBar = menuBar;
 871         if (actualMenuBar == nil) {
 872             actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
 873         }
 874 
 875         if ([nsWindow isKeyWindow]) {
 876             [CMenuBar activate:actualMenuBar modallyDisabled:NO];
 877         }
 878     }];
 879 
 880 JNF_COCOA_EXIT(env);
 881 }
 882 
 883 /*
 884  * Class:     sun_lwawt_macosx_CPlatformWindow
 885  * Method:    nativeGetNSWindowInsets
 886  * Signature: (J)Ljava/awt/Insets;
 887  */
 888 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
 889 (JNIEnv *env, jclass clazz, jlong windowPtr)
 890 {
 891     jobject ret = NULL;
 892 
 893 JNF_COCOA_ENTER(env);
 894 
 895     NSWindow *nsWindow = OBJC(windowPtr);
 896     __block NSRect contentRect = NSZeroRect;
 897     __block NSRect frame = NSZeroRect;
 898 
 899     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 900 
 901         frame = [nsWindow frame];
 902         contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[nsWindow styleMask]];
 903     }];
 904 
 905     jint top = (jint)(frame.size.height - contentRect.size.height);
 906     jint left = (jint)(contentRect.origin.x - frame.origin.x);
 907     jint bottom = (jint)(contentRect.origin.y - frame.origin.y);
 908     jint right = (jint)(frame.size.width - (contentRect.size.width + left));
 909 
 910     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
 911     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
 912     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
 913 
 914 JNF_COCOA_EXIT(env);
 915     return ret;
 916 }
 917 
 918 /*
 919  * Class:     sun_lwawt_macosx_CPlatformWindow
 920  * Method:    nativeSetNSWindowBounds
 921  * Signature: (JDDDD)V
 922  */
 923 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowBounds
 924 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, jdouble width, jdouble height)
 925 {
 926 JNF_COCOA_ENTER(env);
 927 
 928     NSRect jrect = NSMakeRect(originX, originY, width, height);
 929 
 930     // TODO: not sure we need displayIfNeeded message in our view
 931     NSWindow *nsWindow = OBJC(windowPtr);
 932     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 933 
 934         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
 935 
 936         NSRect rect = ConvertNSScreenRect(NULL, jrect);
 937         [window constrainSize:&rect.size];
 938 
 939         [nsWindow setFrame:rect display:YES];
 940 
 941         // only start tracking events if pointer is above the toplevel
 942         // TODO: should post an Entered event if YES.
 943         NSPoint mLocation = [NSEvent mouseLocation];
 944         [nsWindow setAcceptsMouseMovedEvents:NSPointInRect(mLocation, rect)];
 945 
 946         // ensure we repaint the whole window after the resize operation
 947         // (this will also re-enable screen updates, which were disabled above)
 948         // TODO: send PaintEvent
 949     }];
 950 
 951 JNF_COCOA_EXIT(env);
 952 }
 953 
 954 /*
 955  * Class:     sun_lwawt_macosx_CPlatformWindow
























 956  * Method:    nativeSetNSWindowMinMax
 957  * Signature: (JDDDD)V
 958  */
 959 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax
 960 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH)
 961 {
 962 JNF_COCOA_ENTER(env);
 963 
 964     if (minW < 1) minW = 1;
 965     if (minH < 1) minH = 1;
 966     if (maxW < 1) maxW = 1;
 967     if (maxH < 1) maxH = 1;
 968 
 969     NSWindow *nsWindow = OBJC(windowPtr);
 970     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 971 
 972         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
 973 
 974         NSSize min = { minW, minH };
 975         NSSize max = { maxW, maxH };
 976 
 977         [window constrainSize:&min];
 978         [window constrainSize:&max];
 979 
 980         window.javaMinSize = min;
 981         window.javaMaxSize = max;
 982         [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)];
 983     }];
 984 
 985 JNF_COCOA_EXIT(env);
 986 }
 987 
 988 /*
 989  * Class:     sun_lwawt_macosx_CPlatformWindow
 990  * Method:    nativePushNSWindowToBack
 991  * Signature: (J)V
 992  */
 993 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack
 994 (JNIEnv *env, jclass clazz, jlong windowPtr)
 995 {
 996 JNF_COCOA_ENTER(env);
 997 
 998     NSWindow *nsWindow = OBJC(windowPtr);
 999     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1000         [nsWindow orderBack:nil];
1001     }];
1002 
1003 JNF_COCOA_EXIT(env);
1004 }
1005 
1006 /*
1007  * Class:     sun_lwawt_macosx_CPlatformWindow
1008  * Method:    nativePushNSWindowToFront
1009  * Signature: (J)V
1010  */
1011 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront
1012 (JNIEnv *env, jclass clazz, jlong windowPtr)
1013 {
1014 JNF_COCOA_ENTER(env);
1015 
1016     NSWindow *nsWindow = OBJC(windowPtr);
1017     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1018 
1019         if (![nsWindow isKeyWindow]) {
1020             [nsWindow makeKeyAndOrderFront:nsWindow];
1021         } else {
1022             [nsWindow orderFront:nsWindow];
1023         }
1024     }];
1025 
1026 JNF_COCOA_EXIT(env);
1027 }
1028 
1029 /*
1030  * Class:     sun_lwawt_macosx_CPlatformWindow
1031  * Method:    nativeSetNSWindowTitle
1032  * Signature: (JLjava/lang/String;)V
1033  */
1034 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle
1035 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle)
1036 {
1037 JNF_COCOA_ENTER(env);
1038 
1039     NSWindow *nsWindow = OBJC(windowPtr);
1040     [nsWindow performSelectorOnMainThread:@selector(setTitle:)
1041                               withObject:JNFJavaToNSString(env, jtitle)
1042                            waitUntilDone:NO];
1043 
1044 JNF_COCOA_EXIT(env);
1045 }
1046 
1047 /*
1048  * Class:     sun_lwawt_macosx_CPlatformWindow
1049  * Method:    nativeRevalidateNSWindowShadow
1050  * Signature: (J)V
1051  */
1052 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow
1053 (JNIEnv *env, jclass clazz, jlong windowPtr)
1054 {
1055 JNF_COCOA_ENTER(env);
1056 
1057     NSWindow *nsWindow = OBJC(windowPtr);
1058     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1059         [nsWindow invalidateShadow];
1060     }];
1061 
1062 JNF_COCOA_EXIT(env);
1063 }
1064 
1065 /*
1066  * Class:     sun_lwawt_macosx_CPlatformWindow
1067  * Method:    nativeScreenOn_AppKitThread
1068  * Signature: (J)I
1069  */
1070 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread
1071 (JNIEnv *env, jclass clazz, jlong windowPtr)
1072 {
1073     jint ret = 0;
1074 
1075 JNF_COCOA_ENTER(env);
1076 AWT_ASSERT_APPKIT_THREAD;
1077 
1078     NSWindow *nsWindow = OBJC(windowPtr);
1079     NSDictionary *props = [[nsWindow screen] deviceDescription];
1080     ret = [[props objectForKey:@"NSScreenNumber"] intValue];
1081 
1082 JNF_COCOA_EXIT(env);
1083 
1084     return ret;
1085 }
1086 
1087 /*
1088  * Class:     sun_lwawt_macosx_CPlatformWindow
1089  * Method:    nativeSetNSWindowMinimizedIcon
1090  * Signature: (JJ)V
1091  */
1092 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon
1093 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr)
1094 {
1095 JNF_COCOA_ENTER(env);
1096 
1097     NSWindow *nsWindow = OBJC(windowPtr);
1098     NSImage *image = OBJC(nsImagePtr);
1099     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1100         [nsWindow setMiniwindowImage:image];
1101     }];
1102 
1103 JNF_COCOA_EXIT(env);
1104 }
1105 
1106 /*
1107  * Class:     sun_lwawt_macosx_CPlatformWindow
1108  * Method:    nativeSetNSWindowRepresentedFilename
1109  * Signature: (JLjava/lang/String;)V
1110  */
1111 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename
1112 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename)
1113 {
1114 JNF_COCOA_ENTER(env);
1115 
1116     NSWindow *nsWindow = OBJC(windowPtr);
1117     NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)];
1118     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1119         [nsWindow setRepresentedURL:url];
1120     }];
1121 
1122 JNF_COCOA_EXIT(env);
1123 }
1124 
1125 /*
1126  * Class:     sun_lwawt_macosx_CPlatformWindow
1127  * Method:    nativeGetTopmostPlatformWindowUnderMouse
1128  * Signature: (J)V
1129  */
1130 JNIEXPORT jobject
1131 JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetTopmostPlatformWindowUnderMouse
1132 (JNIEnv *env, jclass clazz)
1133 {
1134     jobject topmostWindowUnderMouse = nil;
1135 
1136     JNF_COCOA_ENTER(env);
1137     AWT_ASSERT_APPKIT_THREAD;
1138 
1139     AWTWindow *awtWindow = [AWTWindow getTopmostWindowUnderMouse];
1140     if (awtWindow != nil) {
1141         topmostWindowUnderMouse = [awtWindow.javaPlatformWindow jObject];
1142     }
1143 
1144     JNF_COCOA_EXIT(env);
1145 
1146     return topmostWindowUnderMouse;
1147 }
1148 
1149 /*
1150  * Class:     sun_lwawt_macosx_CPlatformWindow
1151  * Method:    nativeSynthesizeMouseEnteredExitedEvents
1152  * Signature: (J)V
1153  */
1154 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents
1155 (JNIEnv *env, jclass clazz)
1156 {
1157     JNF_COCOA_ENTER(env);
1158 
1159     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1160         [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
1161     }];
1162 
1163     JNF_COCOA_EXIT(env);
1164 }
1165 
1166 /*
1167  * Class:     sun_lwawt_macosx_CPlatformWindow
1168  * Method:    _toggleFullScreenMode
1169  * Signature: (J)V
1170  */
1171 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode
1172 (JNIEnv *env, jobject peer, jlong windowPtr)
1173 {
1174 JNF_COCOA_ENTER(env);
1175 
1176     NSWindow *nsWindow = OBJC(windowPtr);
1177     SEL toggleFullScreenSelector = @selector(toggleFullScreen:);
1178     if (![nsWindow respondsToSelector:toggleFullScreenSelector]) return;
1179 
1180     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1181         [nsWindow performSelector:toggleFullScreenSelector withObject:nil];
1182     }];
1183 
1184 JNF_COCOA_EXIT(env);
1185 }
1186 
1187 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetEnabled
1188 (JNIEnv *env, jclass clazz, jlong windowPtr, jboolean isEnabled)
1189 {
1190 JNF_COCOA_ENTER(env);
1191 
1192     NSWindow *nsWindow = OBJC(windowPtr);
1193     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1194         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1195 
1196         [window setEnabled: isEnabled];
1197     }];
1198 
1199 JNF_COCOA_EXIT(env);
1200 }
1201 
1202 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeDispose
1203 (JNIEnv *env, jclass clazz, jlong windowPtr)
1204 {
1205 JNF_COCOA_ENTER(env);
1206 
1207     NSWindow *nsWindow = OBJC(windowPtr);
1208     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1209         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1210 
1211         if ([AWTWindow lastKeyWindow] == window) {
1212             [AWTWindow setLastKeyWindow: nil];
1213         }
1214 
1215         // AWTWindow holds a reference to the NSWindow in its nsWindow
1216         // property. Unsetting the delegate allows it to be deallocated
1217         // which releases the reference. This, in turn, allows the window
1218         // itself be deallocated.
1219         [nsWindow setDelegate: nil];
1220 
1221         [window release];
1222     }];
1223 
1224 JNF_COCOA_EXIT(env);
1225 }
1226 
1227 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeEnterFullScreenMode
1228 (JNIEnv *env, jclass clazz, jlong windowPtr)
1229 {
1230 JNF_COCOA_ENTER(env);
1231 
1232     NSWindow *nsWindow = OBJC(windowPtr);
1233     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
1234         AWTWindow *window = (AWTWindow*)[nsWindow delegate];
1235         NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow];
1236         CGDirectDisplayID aID = [screenID intValue];
1237 
1238         if (CGDisplayCapture(aID) == kCGErrorSuccess) {
1239             // remove window decoration
1240             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1241             [nsWindow setStyleMask:(styleMask & ~NSTitledWindowMask) | NSBorderlessWindowMask];
1242 
1243             int shieldLevel = CGShieldingWindowLevel();
1244             window.preFullScreenLevel = [nsWindow level];
1245             [nsWindow setLevel: shieldLevel];
1246 
1247             NSRect screenRect = [[nsWindow screen] frame];
1248             [nsWindow setFrame:screenRect display:YES];
1249         } else {
1250             [JNFException raise:[ThreadUtilities getJNIEnv]
1251                              as:kRuntimeException
1252                          reason:"Failed to enter full screen."];
1253         }
1254     }];
1255 
1256 JNF_COCOA_EXIT(env);
1257 }
1258 
1259 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeExitFullScreenMode
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 (CGDisplayRelease(aID) == kCGErrorSuccess) {
1271             NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits];
1272             [nsWindow setStyleMask:styleMask]; 
1273             [nsWindow setLevel: window.preFullScreenLevel];
1274 
1275             // GraphicsDevice takes care of restoring pre full screen bounds
1276         } else {
1277             [JNFException raise:[ThreadUtilities getJNIEnv]
1278                              as:kRuntimeException
1279                          reason:"Failed to exit full screen."];
1280         }
1281     }];
1282 
1283 JNF_COCOA_EXIT(env);
1284 }
1285 
--- EOF ---