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