1 /*
   2  * Copyright (c) 2011, 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 
  34 #import "AWTWindow.h"
  35 #import "AWTView.h"
  36 #import "CMenu.h"
  37 #import "CMenuBar.h"
  38 #import "LWCToolkit.h"
  39 #import "GeomUtilities.h"
  40 #import "ThreadUtilities.h"
  41 #import "OSVersion.h"
  42 
  43 #define MASK(KEY) \
  44     (sun_lwawt_macosx_CPlatformWindow_ ## KEY)
  45 
  46 #define IS(BITS, KEY) \
  47     ((BITS & MASK(KEY)) != 0)
  48 
  49 #define SET(BITS, KEY, VALUE) \
  50     BITS = VALUE ? BITS | MASK(KEY) : BITS & ~MASK(KEY)
  51 
  52 static JNF_CLASS_CACHE(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow");
  53 
  54 @implementation AWTWindow
  55 
  56 @synthesize javaPlatformWindow;
  57 @synthesize javaMenuBar;
  58 @synthesize javaMinSize;
  59 @synthesize javaMaxSize;
  60 @synthesize styleBits;
  61 
  62 - (void) updateMinMaxSize:(BOOL)resizable {
  63     if (resizable) {
  64         [self setMinSize:self.javaMinSize];
  65         [self setMaxSize:self.javaMaxSize];
  66     } else {
  67         NSRect currentFrame = [self frame];
  68         [self setMinSize:currentFrame.size];
  69         [self setMaxSize:currentFrame.size];
  70     }
  71 }
  72 
  73 // creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits
  74 + (NSUInteger) styleMaskForStyleBits:(jint)styleBits {
  75     NSUInteger type = 0;
  76     if (IS(styleBits, DECORATED)) {
  77         type |= NSTitledWindowMask;
  78         if (IS(styleBits, CLOSEABLE))   type |= NSClosableWindowMask;
  79         if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask;
  80         if (IS(styleBits, RESIZABLE))   type |= NSResizableWindowMask;
  81     } else {
  82         type |= NSBorderlessWindowMask;
  83     }
  84 
  85     if (IS(styleBits, TEXTURED))      type |= NSTexturedBackgroundWindowMask;
  86     if (IS(styleBits, UNIFIED))       type |= NSUnifiedTitleAndToolbarWindowMask;
  87     if (IS(styleBits, UTILITY))       type |= NSUtilityWindowMask;
  88     if (IS(styleBits, HUD))           type |= NSHUDWindowMask;
  89     if (IS(styleBits, SHEET))         type |= NSDocModalWindowMask;
  90     if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask;
  91 
  92     return type;
  93 }
  94 
  95 // updates _METHOD_PROP_BITMASK based properties on the window
  96 - (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask {
  97     if (IS(mask, RESIZABLE)) {
  98         BOOL resizable = IS(bits, RESIZABLE);
  99         [self updateMinMaxSize:resizable];
 100         [self setShowsResizeIndicator:resizable];
 101     }
 102 
 103     if (IS(mask, HAS_SHADOW)) {
 104         [self setHasShadow:IS(bits, HAS_SHADOW)];
 105     }
 106 
 107     if (IS(mask, ZOOMABLE)) {
 108         [[self standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)];
 109     }
 110 
 111     if (IS(mask, ALWAYS_ON_TOP)) {
 112         [self setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel];
 113     }
 114 
 115     if (IS(mask, HIDES_ON_DEACTIVATE)) {
 116         [self setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)];
 117     }
 118 
 119     if (IS(mask, DRAGGABLE_BACKGROUND)) {
 120         [self setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)];
 121     }
 122 
 123     if (IS(mask, DOCUMENT_MODIFIED)) {
 124         [self setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)];
 125     }
 126 
 127     if ([self respondsToSelector:@selector(toggleFullScreen:)]) {
 128         if (IS(mask, FULLSCREENABLE)) {
 129             [self setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/];
 130         } else {
 131             [self setCollectionBehavior:NSWindowCollectionBehaviorDefault];
 132         }
 133     }
 134 
 135 }
 136 
 137 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow
 138                     styleBits:(jint)bits
 139                     frameRect:(NSRect)rect
 140                   contentView:(NSView *)view
 141 {
 142 AWT_ASSERT_APPKIT_THREAD;
 143 
 144     NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits];
 145     NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
 146     if (contentRect.size.width <= 0.0) {
 147         contentRect.size.width = 1.0;
 148     }
 149     if (contentRect.size.height <= 0.0) {
 150         contentRect.size.height = 1.0;
 151     }
 152 
 153     self = [super initWithContentRect:contentRect
 154                             styleMask:styleMask
 155                               backing:NSBackingStoreBuffered
 156                                 defer:NO];
 157 
 158     if (self == nil) return nil; // no hope
 159 
 160     self.javaPlatformWindow = platformWindow;
 161     self.styleBits = bits;
 162     [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
 163 
 164     [self setDelegate:self];
 165     [self setContentView:view];
 166     [self setInitialFirstResponder:view];
 167     [self setReleasedWhenClosed:NO];
 168     [self setPreservesContentDuringLiveResize:YES];
 169 
 170     return self;
 171 }
 172 
 173 - (void) dealloc {
 174 AWT_ASSERT_APPKIT_THREAD;
 175 
 176     JNIEnv *env = [ThreadUtilities getJNIEnv];
 177     [self.javaPlatformWindow setJObject:nil withEnv:env];
 178 
 179     [super dealloc];
 180 }
 181 
 182 
 183 // NSWindow overrides
 184 - (BOOL) canBecomeKeyWindow {
 185 AWT_ASSERT_APPKIT_THREAD;
 186     return IS(self.styleBits, SHOULD_BECOME_KEY);
 187 }
 188 
 189 - (BOOL) canBecomeMainWindow {
 190 AWT_ASSERT_APPKIT_THREAD;
 191     return IS(self.styleBits, SHOULD_BECOME_MAIN);
 192 }
 193 
 194 - (BOOL) worksWhenModal {
 195 AWT_ASSERT_APPKIT_THREAD;
 196     return IS(self.styleBits, MODAL_EXCLUDED);
 197 }
 198 
 199 
 200 // Gesture support
 201 - (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b {
 202 AWT_ASSERT_APPKIT_THREAD;
 203 
 204     JNIEnv *env = [ThreadUtilities getJNIEnv];
 205     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 206     if (platformWindow != NULL) {
 207         // extract the target AWT Window object out of the CPlatformWindow
 208         static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 209         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 210         if (awtWindow != NULL) {
 211             // translate the point into Java coordinates
 212             NSPoint loc = [event locationInWindow];
 213             loc.y = [self frame].size.height - loc.y;
 214 
 215             // send up to the GestureHandler to recursively dispatch on the AWT event thread
 216             static JNF_CLASS_CACHE(jc_GestureHandler, "com/apple/eawt/event/GestureHandler");
 217             static JNF_STATIC_MEMBER_CACHE(sjm_handleGestureFromNative, jc_GestureHandler, "handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V");
 218             JNFCallStaticVoidMethod(env, sjm_handleGestureFromNative, awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b);
 219             (*env)->DeleteLocalRef(env, awtWindow);
 220         }
 221         (*env)->DeleteLocalRef(env, platformWindow);
 222     }
 223 }
 224 
 225 - (void)beginGestureWithEvent:(NSEvent *)event {
 226     [self postGesture:event
 227                    as:com_apple_eawt_event_GestureHandler_PHASE
 228                     a:-1.0
 229                     b:0.0];
 230 }
 231 
 232 - (void)endGestureWithEvent:(NSEvent *)event {
 233     [self postGesture:event
 234                    as:com_apple_eawt_event_GestureHandler_PHASE
 235                     a:1.0
 236                     b:0.0];
 237 }
 238 
 239 - (void)magnifyWithEvent:(NSEvent *)event {
 240     [self postGesture:event
 241                    as:com_apple_eawt_event_GestureHandler_MAGNIFY
 242                     a:[event magnification]
 243                     b:0.0];
 244 }
 245 
 246 - (void)rotateWithEvent:(NSEvent *)event {
 247     [self postGesture:event
 248                    as:com_apple_eawt_event_GestureHandler_ROTATE
 249                     a:[event rotation]
 250                     b:0.0];
 251 }
 252 
 253 - (void)swipeWithEvent:(NSEvent *)event {
 254     [self postGesture:event
 255                    as:com_apple_eawt_event_GestureHandler_SWIPE
 256                     a:[event deltaX]
 257                     b:[event deltaY]];
 258 }
 259 
 260 
 261 // NSWindowDelegate methods
 262 
 263 - (void) _deliverMoveResizeEvent {
 264 AWT_ASSERT_APPKIT_THREAD;
 265 
 266     // deliver the event if this is a user-initiated live resize or as a side-effect
 267     // of a Java initiated resize, because AppKit can override the bounds and force
 268     // the bounds of the window to avoid the Dock or remain on screen.
 269     [AWTToolkit eventCountPlusPlus];
 270     JNIEnv *env = [ThreadUtilities getJNIEnv];
 271     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 272     if (platformWindow == NULL) {
 273         // TODO: create generic AWT assert
 274     }
 275 
 276     NSRect frame = ConvertNSScreenRect(env, [self frame]);
 277 
 278     static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIII)V");
 279     JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
 280                       (jint)frame.origin.x,
 281                       (jint)frame.origin.y,
 282                       (jint)frame.size.width,
 283                       (jint)frame.size.height);
 284     (*env)->DeleteLocalRef(env, platformWindow);
 285 }
 286 
 287 - (void)windowDidMove:(NSNotification *)notification {
 288 AWT_ASSERT_APPKIT_THREAD;
 289 
 290     [self _deliverMoveResizeEvent];
 291 }
 292 
 293 - (void)windowDidResize:(NSNotification *)notification {
 294 AWT_ASSERT_APPKIT_THREAD;
 295 
 296     [self _deliverMoveResizeEvent];
 297 }
 298 
 299 - (void)windowDidExpose:(NSNotification *)notification {
 300 AWT_ASSERT_APPKIT_THREAD;
 301 
 302     [AWTToolkit eventCountPlusPlus];
 303     // TODO: don't see this callback invoked anytime so we track
 304     // window exposing in _setVisible:(BOOL)
 305 }
 306 
 307 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)proposedFrame {
 308 AWT_ASSERT_APPKIT_THREAD;
 309 
 310     [AWTToolkit eventCountPlusPlus];
 311     JNIEnv *env = [ThreadUtilities getJNIEnv];
 312     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 313     if (platformWindow != NULL) {
 314         static JNF_MEMBER_CACHE(jm_deliverZoom, jc_CPlatformWindow, "deliverZoom", "(Z)V");
 315         JNFCallVoidMethod(env, platformWindow, jm_deliverZoom, ![window isZoomed]);
 316         (*env)->DeleteLocalRef(env, platformWindow);
 317     }
 318     return YES;
 319 }
 320 
 321 - (void) _deliverIconify:(BOOL)iconify {
 322 AWT_ASSERT_APPKIT_THREAD;
 323 
 324     [AWTToolkit eventCountPlusPlus];
 325     JNIEnv *env = [ThreadUtilities getJNIEnv];
 326     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 327     if (platformWindow != NULL) {
 328         static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
 329         JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
 330         (*env)->DeleteLocalRef(env, platformWindow);
 331     }
 332 }
 333 
 334 - (void)windowDidMiniaturize:(NSNotification *)notification {
 335 AWT_ASSERT_APPKIT_THREAD;
 336 
 337     [self _deliverIconify:JNI_TRUE];
 338 }
 339 
 340 - (void)windowDidDeminiaturize:(NSNotification *)notification {
 341 AWT_ASSERT_APPKIT_THREAD;
 342 
 343     [self _deliverIconify:JNI_FALSE];
 344 }
 345 
 346 - (void) _deliverWindowFocusEvent:(BOOL)focused {
 347 //AWT_ASSERT_APPKIT_THREAD;
 348 
 349     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 350     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 351     if (platformWindow != NULL) {
 352         static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(Z)V");
 353         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused);
 354         (*env)->DeleteLocalRef(env, platformWindow);
 355     }
 356 }
 357 
 358 
 359 - (void) windowDidBecomeKey: (NSNotification *) notification {
 360 AWT_ASSERT_APPKIT_THREAD;
 361     [AWTToolkit eventCountPlusPlus];
 362     [CMenuBar activate:self.javaMenuBar modallyDisabled:NO];
 363     [self _deliverWindowFocusEvent:YES];
 364 }
 365 
 366 - (void) windowDidResignKey: (NSNotification *) notification {
 367     // TODO: check why sometimes at start is invoked *not* on AppKit main thread.
 368 AWT_ASSERT_APPKIT_THREAD;
 369     [AWTToolkit eventCountPlusPlus];
 370     [self.javaMenuBar deactivate];
 371     [self _deliverWindowFocusEvent:NO];
 372 }
 373 
 374 - (void) windowDidBecomeMain: (NSNotification *) notification {
 375 AWT_ASSERT_APPKIT_THREAD;
 376     [AWTToolkit eventCountPlusPlus];
 377 
 378     JNIEnv *env = [ThreadUtilities getJNIEnv];
 379     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 380     if (platformWindow != NULL) {
 381         static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
 382         JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
 383         (*env)->DeleteLocalRef(env, platformWindow);
 384     }
 385 }
 386 
 387 - (BOOL)windowShouldClose:(id)sender {
 388 AWT_ASSERT_APPKIT_THREAD;
 389     [AWTToolkit eventCountPlusPlus];
 390     JNIEnv *env = [ThreadUtilities getJNIEnv];
 391     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 392     if (platformWindow != NULL) {
 393         static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V");
 394         JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
 395         (*env)->DeleteLocalRef(env, platformWindow);
 396     }
 397     // The window will be closed (if allowed) as result of sending Java event
 398     return NO;
 399 }
 400 
 401 
 402 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
 403     static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
 404     static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
 405     static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
 406     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 407     if (platformWindow != NULL) {
 408         jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target);
 409         if (awtWindow != NULL) {
 410             JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op);
 411             (*env)->DeleteLocalRef(env, awtWindow);
 412         }
 413         (*env)->DeleteLocalRef(env, platformWindow);
 414     }
 415 }
 416 
 417 
 418 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
 419     static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
 420     JNIEnv *env = [ThreadUtilities getJNIEnv];
 421     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 422     if (platformWindow != NULL) {
 423         JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
 424         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
 425         (*env)->DeleteLocalRef(env, platformWindow);
 426     }
 427 }
 428 
 429 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
 430     static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
 431     JNIEnv *env = [ThreadUtilities getJNIEnv];
 432     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 433     if (platformWindow != NULL) {
 434         JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
 435         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
 436         (*env)->DeleteLocalRef(env, platformWindow);
 437     }
 438 }
 439 
 440 - (void)windowWillExitFullScreen:(NSNotification *)notification {
 441     static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
 442     JNIEnv *env = [ThreadUtilities getJNIEnv];
 443     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 444     if (platformWindow != NULL) {
 445         JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
 446         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
 447         (*env)->DeleteLocalRef(env, platformWindow);
 448     }
 449 }
 450 
 451 - (void)windowDidExitFullScreen:(NSNotification *)notification {
 452     static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
 453     JNIEnv *env = [ThreadUtilities getJNIEnv];
 454     jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 455     if (platformWindow != NULL) {
 456         JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
 457         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 458         (*env)->DeleteLocalRef(env, platformWindow);
 459     }
 460 }
 461 
 462 - (void)sendEvent:(NSEvent *)event {
 463         if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
 464 
 465             NSPoint p = [NSEvent mouseLocation];
 466             NSRect frame = [self frame];
 467             NSRect contentRect = [self contentRectForFrameRect:frame];
 468 
 469             // Check if the click happened in the non-client area (title bar)
 470             if (p.y >= (frame.origin.y + contentRect.size.height)) {
 471                 JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 472                 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
 473                 // Currently, no need to deliver the whole NSEvent.
 474                 static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
 475                 JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
 476             }
 477         }
 478         [super sendEvent:event];
 479 }
 480 
 481 - (void)constrainSize:(NSSize*)size {
 482     float minWidth = 0.f, minHeight = 0.f;
 483 
 484     if (IS(self.styleBits, DECORATED)) {
 485         NSRect frame = [self frame];
 486         NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self styleMask]];
 487 
 488         float top = frame.size.height - contentRect.size.height;
 489         float left = contentRect.origin.x - frame.origin.x;
 490         float bottom = contentRect.origin.y - frame.origin.y;
 491         float right = frame.size.width - (contentRect.size.width + left);
 492 
 493         // Speculative estimation: 80 - enough for window decorations controls
 494         minWidth += left + right + 80;
 495         minHeight += top + bottom;
 496     }
 497 
 498     minWidth = MAX(1.f, minWidth);
 499     minHeight = MAX(1.f, minHeight);
 500 
 501     size->width = MAX(size->width, minWidth);
 502     size->height = MAX(size->height, minHeight);
 503 }
 504 
 505 @end // AWTWindow
 506 
 507 
 508 /*
 509  * Class:     sun_lwawt_macosx_CPlatformWindow
 510  * Method:    nativeCreateNSWindow
 511  * Signature: (JJIIII)J
 512  */
 513 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow
 514 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong styleBits, jdouble x, jdouble y, jdouble w, jdouble h)
 515 {
 516     __block AWTWindow *window = nil;
 517 
 518 JNF_COCOA_ENTER(env);
 519 AWT_ASSERT_NOT_APPKIT_THREAD;
 520 
 521     JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env];
 522     NSView *contentView = OBJC(contentViewPtr);
 523     NSRect frameRect = NSMakeRect(x, y, w, h);
 524 
 525     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 526         AWT_ASSERT_APPKIT_THREAD;
 527 
 528         window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow
 529                                                   styleBits:styleBits
 530                                                   frameRect:frameRect
 531                                                 contentView:contentView];
 532 
 533         if (window) CFRetain(window);
 534         [window release]; // GC
 535     }];
 536 
 537 JNF_COCOA_EXIT(env);
 538 
 539     return ptr_to_jlong(window);
 540 }
 541 
 542 /*
 543  * Class:     sun_lwawt_macosx_CPlatformWindow
 544  * Method:    nativeSetNSWindowStyleBits
 545  * Signature: (JII)V
 546  */
 547 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStyleBits
 548 (JNIEnv *env, jclass clazz, jlong windowPtr, jint mask, jint bits)
 549 {
 550 JNF_COCOA_ENTER(env);
 551 AWT_ASSERT_NOT_APPKIT_THREAD;
 552 
 553     AWTWindow *window = OBJC(windowPtr);
 554     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 555         AWT_ASSERT_APPKIT_THREAD;
 556 
 557         // scans the bit field, and only updates the values requested by the mask
 558         // (this implicity handles the _CALLBACK_PROP_BITMASK case, since those are passive reads)
 559         jint newBits = window.styleBits & ~mask | bits & mask;
 560 
 561         // resets the NSWindow's style mask if the mask intersects any of those bits
 562         if (mask & MASK(_STYLE_PROP_BITMASK)) {
 563             [window setStyleMask:[AWTWindow styleMaskForStyleBits:newBits]];
 564         }
 565 
 566         // calls methods on NSWindow to change other properties, based on the mask
 567         if (mask & MASK(_METHOD_PROP_BITMASK)) {
 568             [window setPropertiesForStyleBits:bits mask:mask];
 569         }
 570 
 571         window.styleBits = newBits;
 572     }];
 573 
 574 JNF_COCOA_EXIT(env);
 575 }
 576 
 577 /*
 578  * Class:     sun_lwawt_macosx_CPlatformWindow
 579  * Method:    nativeSetNSWindowMenuBar
 580  * Signature: (JJ)V
 581  */
 582 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar
 583 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr)
 584 {
 585 JNF_COCOA_ENTER(env);
 586 AWT_ASSERT_NOT_APPKIT_THREAD;
 587 
 588     AWTWindow *window = OBJC(windowPtr);
 589     CMenuBar *menuBar = OBJC(menuBarPtr);
 590     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 591         AWT_ASSERT_APPKIT_THREAD;
 592 
 593         if ([window isKeyWindow]) [window.javaMenuBar deactivate];
 594         window.javaMenuBar = menuBar;
 595 
 596         // if ([self isKeyWindow]) {
 597         [CMenuBar activate:window.javaMenuBar modallyDisabled:NO];
 598         // }
 599     }];
 600 
 601 JNF_COCOA_EXIT(env);
 602 }
 603 
 604 /*
 605  * Class:     sun_lwawt_macosx_CPlatformWindow
 606  * Method:    nativeGetNSWindowInsets
 607  * Signature: (J)Ljava/awt/Insets;
 608  */
 609 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets
 610 (JNIEnv *env, jclass clazz, jlong windowPtr)
 611 {
 612     jobject ret = NULL;
 613 
 614 JNF_COCOA_ENTER(env);
 615 AWT_ASSERT_NOT_APPKIT_THREAD;
 616 
 617     AWTWindow *window = OBJC(windowPtr);
 618     __block NSRect contentRect = NSZeroRect;
 619     __block NSRect frame = NSZeroRect;
 620 
 621     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^(){
 622         AWT_ASSERT_APPKIT_THREAD;
 623 
 624         frame = [window frame];
 625         contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[window styleMask]];
 626     }];
 627 
 628     jint top = (jint)(frame.size.height - contentRect.size.height);
 629     jint left = (jint)(contentRect.origin.x - frame.origin.x);
 630     jint bottom = (jint)(contentRect.origin.y - frame.origin.y);
 631     jint right = (jint)(frame.size.width - (contentRect.size.width + left));
 632 
 633     static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets");
 634     static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V");
 635     ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right);
 636 
 637 JNF_COCOA_EXIT(env);
 638     return ret;
 639 }
 640 
 641 /*
 642  * Class:     sun_lwawt_macosx_CPlatformWindow
 643  * Method:    nativeSetNSWindowBounds
 644  * Signature: (JDDDD)V
 645  */
 646 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowBounds
 647 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, jdouble width, jdouble height)
 648 {
 649 JNF_COCOA_ENTER(env);
 650 AWT_ASSERT_NOT_APPKIT_THREAD;
 651 
 652     NSRect jrect = NSMakeRect(originX, originY, width, height);
 653 
 654     // TODO: not sure we need displayIfNeeded message in our view
 655     AWTWindow *window = OBJC(windowPtr);
 656     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 657         AWT_ASSERT_APPKIT_THREAD;
 658 
 659         NSRect rect = ConvertNSScreenRect(NULL, jrect);
 660         [window constrainSize:&rect.size];
 661 
 662         [window setFrame:rect display:YES];
 663 
 664         // only start tracking events if pointer is above the toplevel
 665         // TODO: should post an Entered event if YES.
 666         NSPoint mLocation = [NSEvent mouseLocation];
 667         [window setAcceptsMouseMovedEvents:NSPointInRect(mLocation, rect)];
 668 
 669         // ensure we repaint the whole window after the resize operation
 670         // (this will also re-enable screen updates, which were disabled above)
 671         // TODO: send PaintEvent
 672     }];
 673 
 674 JNF_COCOA_EXIT(env);
 675 }
 676 
 677 /*
 678  * Class:     sun_lwawt_macosx_CPlatformWindow
 679  * Method:    nativeSetNSWindowMinMax
 680  * Signature: (JDDDD)V
 681  */
 682 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax
 683 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH)
 684 {
 685 JNF_COCOA_ENTER(env);
 686 AWT_ASSERT_NOT_APPKIT_THREAD;
 687 
 688     if (minW < 1) minW = 1;
 689     if (minH < 1) minH = 1;
 690     if (maxW < 1) maxW = 1;
 691     if (maxH < 1) maxH = 1;
 692 
 693     AWTWindow *window = OBJC(windowPtr);
 694     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 695         AWT_ASSERT_APPKIT_THREAD;
 696 
 697         NSSize min = { minW, minH };
 698         NSSize max = { maxW, maxH };
 699 
 700         [window constrainSize:&min];
 701         [window constrainSize:&max];
 702 
 703         window.javaMinSize = min;
 704         window.javaMaxSize = max;
 705         [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)];
 706     }];
 707 
 708 JNF_COCOA_EXIT(env);
 709 }
 710 
 711 /*
 712  * Class:     sun_lwawt_macosx_CPlatformWindow
 713  * Method:    nativePushNSWindowToBack
 714  * Signature: (J)V
 715  */
 716 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack
 717 (JNIEnv *env, jclass clazz, jlong windowPtr)
 718 {
 719 JNF_COCOA_ENTER(env);
 720 AWT_ASSERT_NOT_APPKIT_THREAD;
 721 
 722     AWTWindow *window = OBJC(windowPtr);
 723     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 724         AWT_ASSERT_APPKIT_THREAD;
 725 
 726         [window orderBack:nil];
 727     }];
 728 
 729 JNF_COCOA_EXIT(env);
 730 }
 731 
 732 /*
 733  * Class:     sun_lwawt_macosx_CPlatformWindow
 734  * Method:    nativePushNSWindowToFront
 735  * Signature: (J)V
 736  */
 737 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront
 738 (JNIEnv *env, jclass clazz, jlong windowPtr)
 739 {
 740 JNF_COCOA_ENTER(env);
 741 AWT_ASSERT_NOT_APPKIT_THREAD;
 742 
 743     AWTWindow *window = OBJC(windowPtr);
 744     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 745         AWT_ASSERT_APPKIT_THREAD;
 746 
 747         if (![window isKeyWindow]) {
 748             [window makeKeyAndOrderFront:window];
 749         } else {
 750             [window orderFront:window];
 751         }
 752     }];
 753 
 754 JNF_COCOA_EXIT(env);
 755 }
 756 
 757 /*
 758  * Class:     sun_lwawt_macosx_CPlatformWindow
 759  * Method:    nativeSetNSWindowTitle
 760  * Signature: (JLjava/lang/String;)V
 761  */
 762 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle
 763 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle)
 764 {
 765 JNF_COCOA_ENTER(env);
 766 AWT_ASSERT_NOT_APPKIT_THREAD;
 767 
 768     AWTWindow *window = OBJC(windowPtr);
 769     [window performSelectorOnMainThread:@selector(setTitle:)
 770                               withObject:JNFJavaToNSString(env, jtitle)
 771                            waitUntilDone:NO];
 772 
 773 JNF_COCOA_EXIT(env);
 774 }
 775 
 776 /*
 777  * Class:     sun_lwawt_macosx_CPlatformWindow
 778  * Method:    nativeSetNSWindowAlpha
 779  * Signature: (JF)V
 780  */
 781 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowAlpha
 782 (JNIEnv *env, jclass clazz, jlong windowPtr, jfloat alpha)
 783 {
 784 JNF_COCOA_ENTER(env);
 785 AWT_ASSERT_NOT_APPKIT_THREAD;
 786 
 787     AWTWindow *window = OBJC(windowPtr);
 788     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 789         AWT_ASSERT_APPKIT_THREAD;
 790 
 791         [window setAlphaValue:alpha];
 792     }];
 793 
 794 JNF_COCOA_EXIT(env);
 795 }
 796 
 797 /*
 798  * Class:     sun_lwawt_macosx_CPlatformWindow
 799  * Method:    nativeRevalidateNSWindowShadow
 800  * Signature: (J)V
 801  */
 802 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow
 803 (JNIEnv *env, jclass clazz, jlong windowPtr)
 804 {
 805 JNF_COCOA_ENTER(env);
 806 AWT_ASSERT_NOT_APPKIT_THREAD;
 807 
 808     AWTWindow *window = OBJC(windowPtr);
 809     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 810         AWT_ASSERT_APPKIT_THREAD;
 811 
 812         [window invalidateShadow];
 813     }];
 814 
 815 JNF_COCOA_EXIT(env);
 816 }
 817 
 818 /*
 819  * Class:     sun_lwawt_macosx_CPlatformWindow
 820  * Method:    nativeScreenOn_AppKitThread
 821  * Signature: (J)I
 822  */
 823 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread
 824 (JNIEnv *env, jclass clazz, jlong windowPtr)
 825 {
 826     jint ret = 0;
 827 
 828 JNF_COCOA_ENTER(env);
 829 AWT_ASSERT_APPKIT_THREAD;
 830 
 831     AWTWindow *window = OBJC(windowPtr);
 832     NSDictionary *props = [[window screen] deviceDescription];
 833     ret = [[props objectForKey:@"NSScreenNumber"] intValue];
 834 
 835 JNF_COCOA_EXIT(env);
 836 
 837     return ret;
 838 }
 839 
 840 /*
 841  * Class:     sun_lwawt_macosx_CPlatformWindow
 842  * Method:    nativeSetNSWindowMinimizedIcon
 843  * Signature: (JJ)V
 844  */
 845 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon
 846 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr)
 847 {
 848 JNF_COCOA_ENTER(env);
 849 AWT_ASSERT_NOT_APPKIT_THREAD;
 850 
 851     AWTWindow *window = OBJC(windowPtr);
 852     NSImage *image = OBJC(nsImagePtr);
 853     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 854         AWT_ASSERT_APPKIT_THREAD;
 855 
 856         [window setMiniwindowImage:image];
 857     }];
 858 
 859 JNF_COCOA_EXIT(env);
 860 }
 861 
 862 /*
 863  * Class:     sun_lwawt_macosx_CPlatformWindow
 864  * Method:    nativeSetNSWindowRepresentedFilename
 865  * Signature: (JLjava/lang/String;)V
 866  */
 867 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename
 868 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename)
 869 {
 870 JNF_COCOA_ENTER(env);
 871 AWT_ASSERT_NOT_APPKIT_THREAD;
 872 
 873     AWTWindow *window = OBJC(windowPtr);
 874     NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)];
 875     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 876         AWT_ASSERT_APPKIT_THREAD;
 877 
 878         [window setRepresentedURL:url];
 879     }];
 880 
 881 JNF_COCOA_EXIT(env);
 882 }
 883 
 884 /*
 885  * Class:     sun_lwawt_macosx_CPlatformWindow
 886  * Method:    nativeSetNSWindowSecurityWarningPositioning
 887  * Signature: (JDDFF)V
 888  */
 889 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowSecurityWarningPositioning
 890 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble x, jdouble y, jfloat biasX, jfloat biasY)
 891 {
 892 JNF_COCOA_ENTER(env);
 893 AWT_ASSERT_NOT_APPKIT_THREAD;
 894 
 895     [JNFException raise:env as:kRuntimeException reason:"unimplemented"];
 896 
 897 JNF_COCOA_EXIT(env);
 898 }
 899 
 900 /*
 901  * Class:     sun_lwawt_macosx_CPlatformWindow
 902  * Method:    nativeGetScreenNSWindowIsOn_AppKitThread
 903  * Signature: (J)I
 904  */
 905 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetScreenNSWindowIsOn_1AppKitThread
 906 (JNIEnv *env, jclass clazz, jlong windowPtr)
 907 {
 908     jint index = -1;
 909 
 910 JNF_COCOA_ENTER(env);
 911 AWT_ASSERT_APPKIT_THREAD;
 912 
 913     AWTWindow *window = OBJC(windowPtr);
 914     NSScreen* screen = [window screen];
 915 
 916     //+++gdb NOTE: This is using a linear search of the screens. If it should
 917     //  prove to be a bottleneck, this can definitely be improved. However,
 918     //  many screens should prove to be the exception, rather than the rule.
 919     NSArray* screens = [NSScreen screens];
 920     NSUInteger i;
 921     for (i = 0; i < [screens count]; i++)
 922     {
 923         if ([[screens objectAtIndex:i] isEqualTo:screen])
 924         {
 925             index = i;
 926             break;
 927         }
 928     }
 929 
 930 JNF_COCOA_EXIT(env);
 931     return 1;
 932 }
 933 
 934 
 935 /*
 936  * Class:     sun_lwawt_macosx_CPlatformWindow
 937  * Method:    _toggleFullScreenMode
 938  * Signature: (J)V
 939  */
 940 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode
 941 (JNIEnv *env, jobject peer, jlong windowPtr)
 942 {
 943 JNF_COCOA_ENTER(env);
 944 
 945     AWTWindow *window = OBJC(windowPtr);
 946     SEL toggleFullScreenSelector = @selector(toggleFullScreen:);
 947     if (![window respondsToSelector:toggleFullScreenSelector]) return;
 948 
 949     [JNFRunLoop performOnMainThreadWaiting:NO withBlock:^(){
 950         [window performSelector:toggleFullScreenSelector withObject:nil];
 951     }];
 952 
 953 JNF_COCOA_EXIT(env);
 954 }
 955 
 956 JNIEXPORT jboolean JNICALL Java_sun_lwawt_macosx_CMouseInfoPeer_nativeIsWindowUnderMouse
 957 (JNIEnv *env, jclass clazz, jlong windowPtr)
 958 {
 959     __block jboolean underMouse = JNI_FALSE;
 960 
 961 JNF_COCOA_ENTER(env);
 962 AWT_ASSERT_NOT_APPKIT_THREAD;
 963 
 964     AWTWindow *aWindow = OBJC(windowPtr);
 965     [JNFRunLoop performOnMainThreadWaiting:YES withBlock:^() {
 966         AWT_ASSERT_APPKIT_THREAD;
 967 
 968         NSPoint pt = [aWindow mouseLocationOutsideOfEventStream];
 969         underMouse = [[aWindow contentView] hitTest:pt] != nil;
 970     }];
 971 
 972 JNF_COCOA_EXIT(env);
 973 
 974     return underMouse;
 975 }