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