1 /* 2 * Copyright (c) 2011, 2018, 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 29 #import "sun_lwawt_macosx_CPlatformWindow.h" 30 #import "com_apple_eawt_event_GestureHandler.h" 31 #import "com_apple_eawt_FullScreenHandler.h" 32 #import "ApplicationDelegate.h" 33 34 #import "AWTWindow.h" 35 #import "AWTView.h" 36 #import "GeomUtilities.h" 37 #import "ThreadUtilities.h" 38 39 #define MASK(KEY) \ 40 (sun_lwawt_macosx_CPlatformWindow_ ## KEY) 41 42 #define IS(BITS, KEY) \ 43 ((BITS & MASK(KEY)) != 0) 44 45 #define SET(BITS, KEY, VALUE) \ 46 BITS = VALUE ? BITS | MASK(KEY) : BITS & ~MASK(KEY) 47 48 static JNF_CLASS_CACHE(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow"); 49 50 // Cocoa windowDidBecomeKey/windowDidResignKey notifications 51 // doesn't provide information about "opposite" window, so we 52 // have to do a bit of tracking. This variable points to a window 53 // which had been the key window just before a new key window 54 // was set. It would be nil if the new key window isn't an AWT 55 // window or the app currently has no key window. 56 static AWTWindow* lastKeyWindow = nil; 57 58 // This variable contains coordinates of a window's top left 59 // which was positioned via java.awt.Window.setLocationByPlatform. 60 // It would be NSZeroPoint if 'Location by Platform' is not used. 61 static NSPoint lastTopLeftPoint; 62 63 // -------------------------------------------------------------- 64 // NSWindow/NSPanel descendants implementation 65 #define AWT_NS_WINDOW_IMPLEMENTATION \ 66 - (id) initWithDelegate:(AWTWindow *)delegate \ 67 frameRect:(NSRect)contectRect \ 68 styleMask:(NSUInteger)styleMask \ 69 contentView:(NSView *)view \ 70 { \ 71 self = [super initWithContentRect:contectRect \ 72 styleMask:styleMask \ 73 backing:NSBackingStoreBuffered \ 74 defer:NO]; \ 75 \ 76 if (self == nil) return nil; \ 77 \ 78 [self setDelegate:delegate]; \ 79 [self setContentView:view]; \ 80 [self setInitialFirstResponder:view]; \ 81 [self setReleasedWhenClosed:NO]; \ 82 [self setPreservesContentDuringLiveResize:YES]; \ 83 \ 84 return self; \ 85 } \ 86 \ 87 /* NSWindow overrides */ \ 88 - (BOOL) canBecomeKeyWindow { \ 89 return [(AWTWindow*)[self delegate] canBecomeKeyWindow]; \ 90 } \ 91 \ 92 - (BOOL) canBecomeMainWindow { \ 93 return [(AWTWindow*)[self delegate] canBecomeMainWindow]; \ 94 } \ 95 \ 96 - (BOOL) worksWhenModal { \ 97 return [(AWTWindow*)[self delegate] worksWhenModal]; \ 98 } \ 99 \ 100 - (void)sendEvent:(NSEvent *)event { \ 101 [(AWTWindow*)[self delegate] sendEvent:event]; \ 102 [super sendEvent:event]; \ 103 } 104 105 @implementation AWTWindow_Normal 106 AWT_NS_WINDOW_IMPLEMENTATION 107 108 // Gesture support 109 - (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b { 110 AWT_ASSERT_APPKIT_THREAD; 111 112 JNIEnv *env = [ThreadUtilities getJNIEnv]; 113 jobject platformWindow = [((AWTWindow *)self.delegate).javaPlatformWindow jObjectWithEnv:env]; 114 if (platformWindow != NULL) { 115 // extract the target AWT Window object out of the CPlatformWindow 116 static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;"); 117 jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target); 118 if (awtWindow != NULL) { 119 // translate the point into Java coordinates 120 NSPoint loc = [event locationInWindow]; 121 loc.y = [self frame].size.height - loc.y; 122 123 // send up to the GestureHandler to recursively dispatch on the AWT event thread 124 static JNF_CLASS_CACHE(jc_GestureHandler, "com/apple/eawt/event/GestureHandler"); 125 static JNF_STATIC_MEMBER_CACHE(sjm_handleGestureFromNative, jc_GestureHandler, "handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V"); 126 JNFCallStaticVoidMethod(env, sjm_handleGestureFromNative, awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b); 127 (*env)->DeleteLocalRef(env, awtWindow); 128 } 129 (*env)->DeleteLocalRef(env, platformWindow); 130 } 131 } 132 133 - (void)beginGestureWithEvent:(NSEvent *)event { 134 [self postGesture:event 135 as:com_apple_eawt_event_GestureHandler_PHASE 136 a:-1.0 137 b:0.0]; 138 } 139 140 - (void)endGestureWithEvent:(NSEvent *)event { 141 [self postGesture:event 142 as:com_apple_eawt_event_GestureHandler_PHASE 143 a:1.0 144 b:0.0]; 145 } 146 147 - (void)magnifyWithEvent:(NSEvent *)event { 148 [self postGesture:event 149 as:com_apple_eawt_event_GestureHandler_MAGNIFY 150 a:[event magnification] 151 b:0.0]; 152 } 153 154 - (void)rotateWithEvent:(NSEvent *)event { 155 [self postGesture:event 156 as:com_apple_eawt_event_GestureHandler_ROTATE 157 a:[event rotation] 158 b:0.0]; 159 } 160 161 - (void)swipeWithEvent:(NSEvent *)event { 162 [self postGesture:event 163 as:com_apple_eawt_event_GestureHandler_SWIPE 164 a:[event deltaX] 165 b:[event deltaY]]; 166 } 167 168 @end 169 @implementation AWTWindow_Panel 170 AWT_NS_WINDOW_IMPLEMENTATION 171 @end 172 // END of NSWindow/NSPanel descendants implementation 173 // -------------------------------------------------------------- 174 175 176 @implementation AWTWindow 177 178 @synthesize nsWindow; 179 @synthesize javaPlatformWindow; 180 @synthesize javaMenuBar; 181 @synthesize javaMinSize; 182 @synthesize javaMaxSize; 183 @synthesize styleBits; 184 @synthesize isEnabled; 185 @synthesize ownerWindow; 186 @synthesize preFullScreenLevel; 187 @synthesize standardFrame; 188 @synthesize isMinimizing; 189 190 - (void) updateMinMaxSize:(BOOL)resizable { 191 if (resizable) { 192 [self.nsWindow setMinSize:self.javaMinSize]; 193 [self.nsWindow setMaxSize:self.javaMaxSize]; 194 } else { 195 NSRect currentFrame = [self.nsWindow frame]; 196 [self.nsWindow setMinSize:currentFrame.size]; 197 [self.nsWindow setMaxSize:currentFrame.size]; 198 } 199 } 200 201 // creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits 202 + (NSUInteger) styleMaskForStyleBits:(jint)styleBits { 203 NSUInteger type = 0; 204 if (IS(styleBits, DECORATED)) { 205 type |= NSTitledWindowMask; 206 if (IS(styleBits, CLOSEABLE)) type |= NSClosableWindowMask; 207 if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask; 208 if (IS(styleBits, RESIZABLE)) type |= NSResizableWindowMask; 209 } else { 210 type |= NSBorderlessWindowMask; 211 } 212 213 if (IS(styleBits, TEXTURED)) type |= NSTexturedBackgroundWindowMask; 214 if (IS(styleBits, UNIFIED)) type |= NSUnifiedTitleAndToolbarWindowMask; 215 if (IS(styleBits, UTILITY)) type |= NSUtilityWindowMask; 216 if (IS(styleBits, HUD)) type |= NSHUDWindowMask; 217 if (IS(styleBits, SHEET)) type |= NSDocModalWindowMask; 218 if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask; 219 220 return type; 221 } 222 223 // updates _METHOD_PROP_BITMASK based properties on the window 224 - (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask { 225 if (IS(mask, RESIZABLE)) { 226 BOOL resizable = IS(bits, RESIZABLE); 227 [self updateMinMaxSize:resizable]; 228 [self.nsWindow setShowsResizeIndicator:resizable]; 229 // Zoom button should be disabled, if the window is not resizable, 230 // otherwise button should be restored to initial state. 231 BOOL zoom = resizable && IS(bits, ZOOMABLE); 232 [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:zoom]; 233 } 234 235 if (IS(mask, HAS_SHADOW)) { 236 [self.nsWindow setHasShadow:IS(bits, HAS_SHADOW)]; 237 } 238 239 if (IS(mask, ZOOMABLE)) { 240 [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)]; 241 } 242 243 if (IS(mask, ALWAYS_ON_TOP)) { 244 [self.nsWindow setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel]; 245 } 246 247 if (IS(mask, HIDES_ON_DEACTIVATE)) { 248 [self.nsWindow setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)]; 249 } 250 251 if (IS(mask, DRAGGABLE_BACKGROUND)) { 252 [self.nsWindow setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)]; 253 } 254 255 if (IS(mask, DOCUMENT_MODIFIED)) { 256 [self.nsWindow setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)]; 257 } 258 259 if (IS(mask, FULLSCREENABLE) && [self.nsWindow respondsToSelector:@selector(toggleFullScreen:)]) { 260 if (IS(bits, FULLSCREENABLE)) { 261 [self.nsWindow setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/]; 262 } else { 263 [self.nsWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault]; 264 } 265 } 266 } 267 268 - (id) initWithPlatformWindow:(JNFWeakJObjectWrapper *)platformWindow 269 ownerWindow:owner 270 styleBits:(jint)bits 271 frameRect:(NSRect)rect 272 contentView:(NSView *)view 273 { 274 AWT_ASSERT_APPKIT_THREAD; 275 276 NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits]; 277 NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask]; 278 if (contentRect.size.width <= 0.0) { 279 contentRect.size.width = 1.0; 280 } 281 if (contentRect.size.height <= 0.0) { 282 contentRect.size.height = 1.0; 283 } 284 285 self = [super init]; 286 287 if (self == nil) return nil; // no hope 288 289 if (IS(bits, UTILITY) || 290 IS(bits, NONACTIVATING) || 291 IS(bits, HUD) || 292 IS(bits, HIDES_ON_DEACTIVATE)) 293 { 294 self.nsWindow = [[AWTWindow_Panel alloc] initWithDelegate:self 295 frameRect:contentRect 296 styleMask:styleMask 297 contentView:view]; 298 } 299 else 300 { 301 // These windows will appear in the window list in the dock icon menu 302 self.nsWindow = [[AWTWindow_Normal alloc] initWithDelegate:self 303 frameRect:contentRect 304 styleMask:styleMask 305 contentView:view]; 306 } 307 308 if (self.nsWindow == nil) return nil; // no hope either 309 [self.nsWindow release]; // the property retains the object already 310 311 self.isEnabled = YES; 312 self.isMinimizing = NO; 313 self.javaPlatformWindow = platformWindow; 314 self.styleBits = bits; 315 self.ownerWindow = owner; 316 [self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)]; 317 318 if (IS(self.styleBits, IS_POPUP)) { 319 [self.nsWindow setCollectionBehavior:(1 << 8) /*NSWindowCollectionBehaviorFullScreenAuxiliary*/]; 320 } 321 322 return self; 323 } 324 325 + (BOOL) isAWTWindow:(NSWindow *)window { 326 return [window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]]; 327 } 328 329 // Retrieves the list of possible window layers (levels) 330 + (NSArray*) getWindowLayers { 331 static NSArray *windowLayers; 332 static dispatch_once_t token; 333 334 // Initialize the list of possible window layers 335 dispatch_once(&token, ^{ 336 // The layers are ordered from front to back, (i.e. the toppest one is the first) 337 windowLayers = [NSArray arrayWithObjects: 338 [NSNumber numberWithInt:CGWindowLevelForKey(kCGPopUpMenuWindowLevelKey)], 339 [NSNumber numberWithInt:CGWindowLevelForKey(kCGFloatingWindowLevelKey)], 340 [NSNumber numberWithInt:CGWindowLevelForKey(kCGNormalWindowLevelKey)], 341 nil 342 ]; 343 [windowLayers retain]; 344 }); 345 return windowLayers; 346 } 347 348 // returns id for the topmost window under mouse 349 + (NSInteger) getTopmostWindowUnderMouseID { 350 NSInteger result = -1; 351 352 NSArray *windowLayers = [AWTWindow getWindowLayers]; 353 // Looking for the window under mouse starting from the toppest layer 354 for (NSNumber *layer in windowLayers) { 355 result = [AWTWindow getTopmostWindowUnderMouseIDImpl:[layer integerValue]]; 356 if (result != -1) { 357 break; 358 } 359 } 360 return result; 361 } 362 363 + (NSInteger) getTopmostWindowUnderMouseIDImpl:(NSInteger)windowLayer { 364 NSInteger result = -1; 365 366 NSRect screenRect = [[NSScreen mainScreen] frame]; 367 NSPoint nsMouseLocation = [NSEvent mouseLocation]; 368 CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y); 369 370 NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID); 371 372 for (NSDictionary *window in windows) { 373 NSInteger layer = [[window objectForKey:(id)kCGWindowLayer] integerValue]; 374 if (layer == windowLayer) { 375 CGRect rect; 376 CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect); 377 if (CGRectContainsPoint(rect, cgMouseLocation)) { 378 result = [[window objectForKey:(id)kCGWindowNumber] integerValue]; 379 break; 380 } 381 } 382 } 383 [windows release]; 384 return result; 385 } 386 387 // checks that this window is under the mouse cursor and this point is not overlapped by others windows 388 - (BOOL) isTopmostWindowUnderMouse { 389 return [self.nsWindow windowNumber] == [AWTWindow getTopmostWindowUnderMouseID]; 390 } 391 392 + (AWTWindow *) getTopmostWindowUnderMouse { 393 NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator]; 394 NSWindow *window; 395 396 NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID]; 397 398 while ((window = [windowEnumerator nextObject]) != nil) { 399 if ([window windowNumber] == topmostWindowUnderMouseID) { 400 BOOL isAWTWindow = [AWTWindow isAWTWindow: window]; 401 return isAWTWindow ? (AWTWindow *) [window delegate] : nil; 402 } 403 } 404 return nil; 405 } 406 407 + (void) synthesizeMouseEnteredExitedEvents:(NSWindow*)window withType:(NSEventType)eventType { 408 409 NSPoint screenLocation = [NSEvent mouseLocation]; 410 NSPoint windowLocation = [window convertScreenToBase: screenLocation]; 411 int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask; 412 413 NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType 414 location: windowLocation 415 modifierFlags: modifierFlags 416 timestamp: 0 417 windowNumber: [window windowNumber] 418 context: nil 419 eventNumber: 0 420 trackingNumber: 0 421 userData: nil 422 ]; 423 424 [[window contentView] deliverJavaMouseEvent: mouseEvent]; 425 } 426 427 + (void) synthesizeMouseEnteredExitedEventsForAllWindows { 428 429 NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID]; 430 NSArray *windows = [NSApp windows]; 431 NSWindow *window; 432 433 NSEnumerator *windowEnumerator = [windows objectEnumerator]; 434 while ((window = [windowEnumerator nextObject]) != nil) { 435 if ([AWTWindow isAWTWindow: window]) { 436 BOOL isUnderMouse = ([window windowNumber] == topmostWindowUnderMouseID); 437 BOOL mouseIsOver = [[window contentView] mouseIsOver]; 438 if (isUnderMouse && !mouseIsOver) { 439 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseEntered]; 440 } else if (!isUnderMouse && mouseIsOver) { 441 [AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseExited]; 442 } 443 } 444 } 445 } 446 447 + (NSNumber *) getNSWindowDisplayID_AppKitThread:(NSWindow *)window { 448 AWT_ASSERT_APPKIT_THREAD; 449 NSScreen *screen = [window screen]; 450 NSDictionary *deviceDescription = [screen deviceDescription]; 451 return [deviceDescription objectForKey:@"NSScreenNumber"]; 452 } 453 454 - (void) dealloc { 455 AWT_ASSERT_APPKIT_THREAD; 456 457 JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; 458 [self.javaPlatformWindow setJObject:nil withEnv:env]; 459 460 self.nsWindow = nil; 461 self.ownerWindow = nil; 462 [super dealloc]; 463 } 464 465 // Tests whether window is blocked by modal dialog/window 466 - (BOOL) isBlocked { 467 BOOL isBlocked = NO; 468 469 JNIEnv *env = [ThreadUtilities getJNIEnv]; 470 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 471 if (platformWindow != NULL) { 472 static JNF_MEMBER_CACHE(jm_isBlocked, jc_CPlatformWindow, "isBlocked", "()Z"); 473 isBlocked = JNFCallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO; 474 (*env)->DeleteLocalRef(env, platformWindow); 475 } 476 477 return isBlocked; 478 } 479 480 // Test whether window is simple window and owned by embedded frame 481 - (BOOL) isSimpleWindowOwnedByEmbeddedFrame { 482 BOOL isSimpleWindowOwnedByEmbeddedFrame = NO; 483 484 JNIEnv *env = [ThreadUtilities getJNIEnv]; 485 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 486 if (platformWindow != NULL) { 487 static JNF_MEMBER_CACHE(jm_isBlocked, jc_CPlatformWindow, "isSimpleWindowOwnedByEmbeddedFrame", "()Z"); 488 isSimpleWindowOwnedByEmbeddedFrame = JNFCallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO; 489 (*env)->DeleteLocalRef(env, platformWindow); 490 } 491 492 return isSimpleWindowOwnedByEmbeddedFrame; 493 } 494 495 // Tests whether the corresponding Java platform window is visible or not 496 + (BOOL) isJavaPlatformWindowVisible:(NSWindow *)window { 497 BOOL isVisible = NO; 498 499 if ([AWTWindow isAWTWindow:window] && [window delegate] != nil) { 500 AWTWindow *awtWindow = (AWTWindow *)[window delegate]; 501 [AWTToolkit eventCountPlusPlus]; 502 503 JNIEnv *env = [ThreadUtilities getJNIEnv]; 504 jobject platformWindow = [awtWindow.javaPlatformWindow jObjectWithEnv:env]; 505 if (platformWindow != NULL) { 506 static JNF_MEMBER_CACHE(jm_isVisible, jc_CPlatformWindow, "isVisible", "()Z"); 507 isVisible = JNFCallBooleanMethod(env, platformWindow, jm_isVisible) == JNI_TRUE ? YES : NO; 508 (*env)->DeleteLocalRef(env, platformWindow); 509 510 } 511 } 512 return isVisible; 513 } 514 515 // Orders window's childs based on the current focus state 516 - (void) orderChildWindows:(BOOL)focus { 517 AWT_ASSERT_APPKIT_THREAD; 518 519 if (self.isMinimizing || [self isBlocked]) { 520 // Do not perform any ordering, if iconify is in progress 521 // or the window is blocked by a modal window 522 return; 523 } 524 525 NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator]; 526 NSWindow *window; 527 while ((window = [windowEnumerator nextObject]) != nil) { 528 if ([AWTWindow isJavaPlatformWindowVisible:window]) { 529 AWTWindow *awtWindow = (AWTWindow *)[window delegate]; 530 AWTWindow *owner = awtWindow.ownerWindow; 531 if (IS(awtWindow.styleBits, ALWAYS_ON_TOP)) { 532 // Do not order 'always on top' windows 533 continue; 534 } 535 while (awtWindow.ownerWindow != nil) { 536 if (awtWindow.ownerWindow == self) { 537 if (focus) { 538 // Move the childWindow to floating level 539 // so it will appear in front of its 540 // parent which owns the focus 541 [window setLevel:NSFloatingWindowLevel]; 542 } else { 543 // Focus owner has changed, move the childWindow 544 // back to normal window level 545 [window setLevel:NSNormalWindowLevel]; 546 } 547 // The childWindow should be displayed in front of 548 // its nearest parentWindow 549 [window orderWindow:NSWindowAbove relativeTo:[owner.nsWindow windowNumber]]; 550 break; 551 } 552 awtWindow = awtWindow.ownerWindow; 553 } 554 } 555 } 556 } 557 558 // NSWindow overrides 559 - (BOOL) canBecomeKeyWindow { 560 AWT_ASSERT_APPKIT_THREAD; 561 return self.isEnabled && (IS(self.styleBits, SHOULD_BECOME_KEY) || [self isSimpleWindowOwnedByEmbeddedFrame]); 562 } 563 564 - (BOOL) canBecomeMainWindow { 565 AWT_ASSERT_APPKIT_THREAD; 566 if (!self.isEnabled) { 567 // Native system can bring up the NSWindow to 568 // the top even if the window is not main. 569 // We should bring up the modal dialog manually 570 [AWTToolkit eventCountPlusPlus]; 571 572 JNIEnv *env = [ThreadUtilities getJNIEnv]; 573 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 574 if (platformWindow != NULL) { 575 static JNF_MEMBER_CACHE(jm_checkBlockingAndOrder, jc_CPlatformWindow, 576 "checkBlockingAndOrder", "()Z"); 577 JNFCallBooleanMethod(env, platformWindow, jm_checkBlockingAndOrder); 578 (*env)->DeleteLocalRef(env, platformWindow); 579 } 580 } 581 582 return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN); 583 } 584 585 - (BOOL) worksWhenModal { 586 AWT_ASSERT_APPKIT_THREAD; 587 return IS(self.styleBits, MODAL_EXCLUDED); 588 } 589 590 591 // NSWindowDelegate methods 592 593 - (void) _deliverMoveResizeEvent { 594 AWT_ASSERT_APPKIT_THREAD; 595 596 // deliver the event if this is a user-initiated live resize or as a side-effect 597 // of a Java initiated resize, because AppKit can override the bounds and force 598 // the bounds of the window to avoid the Dock or remain on screen. 599 [AWTToolkit eventCountPlusPlus]; 600 JNIEnv *env = [ThreadUtilities getJNIEnv]; 601 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 602 if (platformWindow == NULL) { 603 // TODO: create generic AWT assert 604 } 605 606 NSRect frame = ConvertNSScreenRect(env, [self.nsWindow frame]); 607 608 static JNF_MEMBER_CACHE(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIIIZ)V"); 609 JNFCallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent, 610 (jint)frame.origin.x, 611 (jint)frame.origin.y, 612 (jint)frame.size.width, 613 (jint)frame.size.height, 614 (jboolean)[self.nsWindow inLiveResize]); 615 (*env)->DeleteLocalRef(env, platformWindow); 616 617 [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows]; 618 } 619 620 - (void)windowDidMove:(NSNotification *)notification { 621 AWT_ASSERT_APPKIT_THREAD; 622 623 [self _deliverMoveResizeEvent]; 624 } 625 626 - (void)windowDidResize:(NSNotification *)notification { 627 AWT_ASSERT_APPKIT_THREAD; 628 629 [self _deliverMoveResizeEvent]; 630 } 631 632 - (void)windowDidExpose:(NSNotification *)notification { 633 AWT_ASSERT_APPKIT_THREAD; 634 635 [AWTToolkit eventCountPlusPlus]; 636 // TODO: don't see this callback invoked anytime so we track 637 // window exposing in _setVisible:(BOOL) 638 } 639 640 - (NSRect)windowWillUseStandardFrame:(NSWindow *)window 641 defaultFrame:(NSRect)newFrame { 642 643 return NSEqualSizes(NSZeroSize, [self standardFrame].size) 644 ? newFrame 645 : [self standardFrame]; 646 } 647 648 // Hides/shows window's childs during iconify/de-iconify operation 649 - (void) iconifyChildWindows:(BOOL)iconify { 650 AWT_ASSERT_APPKIT_THREAD; 651 652 NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator]; 653 NSWindow *window; 654 while ((window = [windowEnumerator nextObject]) != nil) { 655 if ([AWTWindow isJavaPlatformWindowVisible:window]) { 656 AWTWindow *awtWindow = (AWTWindow *)[window delegate]; 657 while (awtWindow.ownerWindow != nil) { 658 if (awtWindow.ownerWindow == self) { 659 if (iconify) { 660 [window orderOut:window]; 661 } else { 662 [window orderFront:window]; 663 } 664 break; 665 } 666 awtWindow = awtWindow.ownerWindow; 667 } 668 } 669 } 670 } 671 672 - (void) _deliverIconify:(BOOL)iconify { 673 AWT_ASSERT_APPKIT_THREAD; 674 675 [AWTToolkit eventCountPlusPlus]; 676 JNIEnv *env = [ThreadUtilities getJNIEnv]; 677 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 678 if (platformWindow != NULL) { 679 static JNF_MEMBER_CACHE(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V"); 680 JNFCallVoidMethod(env, platformWindow, jm_deliverIconify, iconify); 681 (*env)->DeleteLocalRef(env, platformWindow); 682 } 683 } 684 685 - (void)windowWillMiniaturize:(NSNotification *)notification { 686 AWT_ASSERT_APPKIT_THREAD; 687 688 self.isMinimizing = YES; 689 690 JNIEnv *env = [ThreadUtilities getJNIEnv]; 691 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 692 if (platformWindow != NULL) { 693 static JNF_MEMBER_CACHE(jm_windowWillMiniaturize, jc_CPlatformWindow, "windowWillMiniaturize", "()V"); 694 JNFCallVoidMethod(env, platformWindow, jm_windowWillMiniaturize); 695 (*env)->DeleteLocalRef(env, platformWindow); 696 } 697 // Explicitly make myself a key window to avoid possible 698 // negative visual effects during iconify operation 699 [self.nsWindow makeKeyAndOrderFront:self.nsWindow]; 700 [self iconifyChildWindows:YES]; 701 } 702 703 - (void)windowDidMiniaturize:(NSNotification *)notification { 704 AWT_ASSERT_APPKIT_THREAD; 705 706 [self _deliverIconify:JNI_TRUE]; 707 self.isMinimizing = NO; 708 } 709 710 - (void)windowDidDeminiaturize:(NSNotification *)notification { 711 AWT_ASSERT_APPKIT_THREAD; 712 713 [self _deliverIconify:JNI_FALSE]; 714 [self iconifyChildWindows:NO]; 715 } 716 717 - (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite { 718 //AWT_ASSERT_APPKIT_THREAD; 719 JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; 720 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 721 if (platformWindow != NULL) { 722 jobject oppositeWindow = [opposite.javaPlatformWindow jObjectWithEnv:env]; 723 724 static JNF_MEMBER_CACHE(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V"); 725 JNFCallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow); 726 (*env)->DeleteLocalRef(env, platformWindow); 727 (*env)->DeleteLocalRef(env, oppositeWindow); 728 } 729 } 730 731 - (void) windowDidBecomeMain: (NSNotification *) notification { 732 AWT_ASSERT_APPKIT_THREAD; 733 [AWTToolkit eventCountPlusPlus]; 734 #ifdef DEBUG 735 NSLog(@"became main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]); 736 #endif 737 738 if (![self.nsWindow isKeyWindow]) { 739 [self activateWindowMenuBar]; 740 } 741 742 JNIEnv *env = [ThreadUtilities getJNIEnv]; 743 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 744 if (platformWindow != NULL) { 745 static JNF_MEMBER_CACHE(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V"); 746 JNFCallVoidMethod(env, platformWindow, jm_windowDidBecomeMain); 747 (*env)->DeleteLocalRef(env, platformWindow); 748 } 749 } 750 751 - (void) windowDidBecomeKey: (NSNotification *) notification { 752 AWT_ASSERT_APPKIT_THREAD; 753 [AWTToolkit eventCountPlusPlus]; 754 #ifdef DEBUG 755 NSLog(@"became key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]); 756 #endif 757 AWTWindow *opposite = [AWTWindow lastKeyWindow]; 758 759 if (![self.nsWindow isMainWindow]) { 760 [self activateWindowMenuBar]; 761 } 762 763 [AWTWindow setLastKeyWindow:nil]; 764 765 [self _deliverWindowFocusEvent:YES oppositeWindow: opposite]; 766 [self orderChildWindows:YES]; 767 } 768 769 - (void) activateWindowMenuBar { 770 AWT_ASSERT_APPKIT_THREAD; 771 // Finds appropriate menubar in our hierarchy 772 AWTWindow *awtWindow = self; 773 while (awtWindow.ownerWindow != nil) { 774 awtWindow = awtWindow.ownerWindow; 775 } 776 777 CMenuBar *menuBar = nil; 778 BOOL isDisabled = NO; 779 if ([awtWindow.nsWindow isVisible]){ 780 menuBar = awtWindow.javaMenuBar; 781 isDisabled = !awtWindow.isEnabled; 782 } 783 784 if (menuBar == nil) { 785 menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar]; 786 isDisabled = NO; 787 } 788 789 [CMenuBar activate:menuBar modallyDisabled:isDisabled]; 790 } 791 792 #ifdef DEBUG 793 - (CMenuBar *) menuBarForWindow { 794 AWT_ASSERT_APPKIT_THREAD; 795 AWTWindow *awtWindow = self; 796 while (awtWindow.ownerWindow != nil) { 797 awtWindow = awtWindow.ownerWindow; 798 } 799 return awtWindow.javaMenuBar; 800 } 801 #endif 802 803 - (void) windowDidResignKey: (NSNotification *) notification { 804 // TODO: check why sometimes at start is invoked *not* on AppKit main thread. 805 AWT_ASSERT_APPKIT_THREAD; 806 [AWTToolkit eventCountPlusPlus]; 807 #ifdef DEBUG 808 NSLog(@"resigned key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]); 809 #endif 810 if (![self.nsWindow isMainWindow]) { 811 [self deactivateWindow]; 812 } 813 } 814 815 - (void) windowDidResignMain: (NSNotification *) notification { 816 AWT_ASSERT_APPKIT_THREAD; 817 [AWTToolkit eventCountPlusPlus]; 818 #ifdef DEBUG 819 NSLog(@"resigned main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]); 820 #endif 821 if (![self.nsWindow isKeyWindow]) { 822 [self deactivateWindow]; 823 } 824 } 825 826 - (void) deactivateWindow { 827 AWT_ASSERT_APPKIT_THREAD; 828 #ifdef DEBUG 829 NSLog(@"deactivating window: %@", [self.nsWindow title]); 830 #endif 831 [self.javaMenuBar deactivate]; 832 833 // the new key window 834 NSWindow *keyWindow = [NSApp keyWindow]; 835 AWTWindow *opposite = nil; 836 if ([AWTWindow isAWTWindow: keyWindow]) { 837 opposite = (AWTWindow *)[keyWindow delegate]; 838 [AWTWindow setLastKeyWindow: self]; 839 } else { 840 [AWTWindow setLastKeyWindow: nil]; 841 } 842 843 [self _deliverWindowFocusEvent:NO oppositeWindow: opposite]; 844 [self orderChildWindows:NO]; 845 } 846 847 - (BOOL)windowShouldClose:(id)sender { 848 AWT_ASSERT_APPKIT_THREAD; 849 [AWTToolkit eventCountPlusPlus]; 850 JNIEnv *env = [ThreadUtilities getJNIEnv]; 851 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 852 if (platformWindow != NULL) { 853 static JNF_MEMBER_CACHE(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V"); 854 JNFCallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent); 855 (*env)->DeleteLocalRef(env, platformWindow); 856 } 857 // The window will be closed (if allowed) as result of sending Java event 858 return NO; 859 } 860 861 862 - (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env { 863 static JNF_CLASS_CACHE(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler"); 864 static JNF_STATIC_MEMBER_CACHE(jm_notifyFullScreenOperation, jc_FullScreenHandler, "handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V"); 865 static JNF_MEMBER_CACHE(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;"); 866 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 867 if (platformWindow != NULL) { 868 jobject awtWindow = JNFGetObjectField(env, platformWindow, jf_target); 869 if (awtWindow != NULL) { 870 JNFCallStaticVoidMethod(env, jm_notifyFullScreenOperation, awtWindow, op); 871 (*env)->DeleteLocalRef(env, awtWindow); 872 } 873 (*env)->DeleteLocalRef(env, platformWindow); 874 } 875 } 876 877 878 - (void)windowWillEnterFullScreen:(NSNotification *)notification { 879 static JNF_MEMBER_CACHE(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V"); 880 JNIEnv *env = [ThreadUtilities getJNIEnv]; 881 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 882 if (platformWindow != NULL) { 883 JNFCallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen); 884 [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env]; 885 (*env)->DeleteLocalRef(env, platformWindow); 886 } 887 } 888 889 - (void)windowDidEnterFullScreen:(NSNotification *)notification { 890 static JNF_MEMBER_CACHE(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V"); 891 JNIEnv *env = [ThreadUtilities getJNIEnv]; 892 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 893 if (platformWindow != NULL) { 894 JNFCallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen); 895 [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env]; 896 (*env)->DeleteLocalRef(env, platformWindow); 897 } 898 [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows]; 899 } 900 901 - (void)windowWillExitFullScreen:(NSNotification *)notification { 902 static JNF_MEMBER_CACHE(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V"); 903 JNIEnv *env = [ThreadUtilities getJNIEnv]; 904 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 905 if (platformWindow != NULL) { 906 JNFCallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen); 907 [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env]; 908 (*env)->DeleteLocalRef(env, platformWindow); 909 } 910 } 911 912 - (void)windowDidExitFullScreen:(NSNotification *)notification { 913 static JNF_MEMBER_CACHE(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V"); 914 JNIEnv *env = [ThreadUtilities getJNIEnv]; 915 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 916 if (platformWindow != NULL) { 917 JNFCallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen); 918 [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env]; 919 (*env)->DeleteLocalRef(env, platformWindow); 920 } 921 [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows]; 922 } 923 924 - (void)sendEvent:(NSEvent *)event { 925 if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) { 926 if ([self isBlocked]) { 927 // Move parent windows to front and make sure that a child window is displayed 928 // in front of its nearest parent. 929 if (self.ownerWindow != nil) { 930 JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; 931 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 932 if (platformWindow != NULL) { 933 static JNF_MEMBER_CACHE(jm_orderAboveSiblings, jc_CPlatformWindow, "orderAboveSiblings", "()V"); 934 JNFCallVoidMethod(env,platformWindow, jm_orderAboveSiblings); 935 (*env)->DeleteLocalRef(env, platformWindow); 936 } 937 } 938 [self orderChildWindows:YES]; 939 } 940 941 NSPoint p = [NSEvent mouseLocation]; 942 NSRect frame = [self.nsWindow frame]; 943 NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame]; 944 945 // Check if the click happened in the non-client area (title bar) 946 if (p.y >= (frame.origin.y + contentRect.size.height)) { 947 JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; 948 jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env]; 949 if (platformWindow != NULL) { 950 // Currently, no need to deliver the whole NSEvent. 951 static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V"); 952 JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown); 953 // Deliver double click on title bar 954 if ([event clickCount] > 1) { 955 static JNF_MEMBER_CACHE(jm_deliverDoubleClickOnTitlebar, jc_CPlatformWindow, "deliverDoubleClickOnTitlebar", "()V"); 956 JNFCallVoidMethod(env, platformWindow, jm_deliverDoubleClickOnTitlebar); 957 } 958 (*env)->DeleteLocalRef(env, platformWindow); 959 } 960 } 961 } 962 } 963 964 - (void)constrainSize:(NSSize*)size { 965 float minWidth = 0.f, minHeight = 0.f; 966 967 if (IS(self.styleBits, DECORATED)) { 968 NSRect frame = [self.nsWindow frame]; 969 NSRect contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[self.nsWindow styleMask]]; 970 971 float top = frame.size.height - contentRect.size.height; 972 float left = contentRect.origin.x - frame.origin.x; 973 float bottom = contentRect.origin.y - frame.origin.y; 974 float right = frame.size.width - (contentRect.size.width + left); 975 976 // Speculative estimation: 80 - enough for window decorations controls 977 minWidth += left + right + 80; 978 minHeight += top + bottom; 979 } 980 981 minWidth = MAX(1.f, minWidth); 982 minHeight = MAX(1.f, minHeight); 983 984 size->width = MAX(size->width, minWidth); 985 size->height = MAX(size->height, minHeight); 986 } 987 988 - (void) setEnabled: (BOOL)flag { 989 self.isEnabled = flag; 990 991 if (IS(self.styleBits, CLOSEABLE)) { 992 [[self.nsWindow standardWindowButton:NSWindowCloseButton] setEnabled: flag]; 993 } 994 995 if (IS(self.styleBits, MINIMIZABLE)) { 996 [[self.nsWindow standardWindowButton:NSWindowMiniaturizeButton] setEnabled: flag]; 997 } 998 999 if (IS(self.styleBits, ZOOMABLE)) { 1000 [[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled: flag]; 1001 } 1002 1003 if (IS(self.styleBits, RESIZABLE)) { 1004 [self updateMinMaxSize:flag]; 1005 [self.nsWindow setShowsResizeIndicator:flag]; 1006 } 1007 } 1008 1009 + (void) setLastKeyWindow:(AWTWindow *)window { 1010 [window retain]; 1011 [lastKeyWindow release]; 1012 lastKeyWindow = window; 1013 } 1014 1015 + (AWTWindow *) lastKeyWindow { 1016 return lastKeyWindow; 1017 } 1018 1019 @end // AWTWindow 1020 1021 1022 /* 1023 * Class: sun_lwawt_macosx_CPlatformWindow 1024 * Method: nativeCreateNSWindow 1025 * Signature: (JJIIII)J 1026 */ 1027 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeCreateNSWindow 1028 (JNIEnv *env, jobject obj, jlong contentViewPtr, jlong ownerPtr, jlong styleBits, jdouble x, jdouble y, jdouble w, jdouble h) 1029 { 1030 __block AWTWindow *window = nil; 1031 1032 JNF_COCOA_ENTER(env); 1033 1034 JNFWeakJObjectWrapper *platformWindow = [JNFWeakJObjectWrapper wrapperWithJObject:obj withEnv:env]; 1035 NSView *contentView = OBJC(contentViewPtr); 1036 NSRect frameRect = NSMakeRect(x, y, w, h); 1037 AWTWindow *owner = [OBJC(ownerPtr) delegate]; 1038 [ThreadUtilities performOnMainThreadWaiting:YES block:^(){ 1039 1040 window = [[AWTWindow alloc] initWithPlatformWindow:platformWindow 1041 ownerWindow:owner 1042 styleBits:styleBits 1043 frameRect:frameRect 1044 contentView:contentView]; 1045 // the window is released is CPlatformWindow.nativeDispose() 1046 1047 if (window) [window.nsWindow retain]; 1048 }]; 1049 1050 JNF_COCOA_EXIT(env); 1051 1052 return ptr_to_jlong(window ? window.nsWindow : nil); 1053 } 1054 1055 /* 1056 * Class: sun_lwawt_macosx_CPlatformWindow 1057 * Method: nativeSetNSWindowStyleBits 1058 * Signature: (JII)V 1059 */ 1060 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStyleBits 1061 (JNIEnv *env, jclass clazz, jlong windowPtr, jint mask, jint bits) 1062 { 1063 JNF_COCOA_ENTER(env); 1064 1065 NSWindow *nsWindow = OBJC(windowPtr); 1066 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1067 1068 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1069 1070 // scans the bit field, and only updates the values requested by the mask 1071 // (this implicity handles the _CALLBACK_PROP_BITMASK case, since those are passive reads) 1072 jint newBits = window.styleBits & ~mask | bits & mask; 1073 1074 // resets the NSWindow's style mask if the mask intersects any of those bits 1075 if (mask & MASK(_STYLE_PROP_BITMASK)) { 1076 [nsWindow setStyleMask:[AWTWindow styleMaskForStyleBits:newBits]]; 1077 } 1078 1079 // calls methods on NSWindow to change other properties, based on the mask 1080 if (mask & MASK(_METHOD_PROP_BITMASK)) { 1081 [window setPropertiesForStyleBits:newBits mask:mask]; 1082 } 1083 1084 window.styleBits = newBits; 1085 }]; 1086 1087 JNF_COCOA_EXIT(env); 1088 } 1089 1090 /* 1091 * Class: sun_lwawt_macosx_CPlatformWindow 1092 * Method: nativeSetNSWindowMenuBar 1093 * Signature: (JJ)V 1094 */ 1095 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMenuBar 1096 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong menuBarPtr) 1097 { 1098 JNF_COCOA_ENTER(env); 1099 1100 NSWindow *nsWindow = OBJC(windowPtr); 1101 CMenuBar *menuBar = OBJC(menuBarPtr); 1102 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1103 1104 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1105 1106 if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) { 1107 [window.javaMenuBar deactivate]; 1108 } 1109 1110 window.javaMenuBar = menuBar; 1111 1112 CMenuBar* actualMenuBar = menuBar; 1113 if (actualMenuBar == nil) { 1114 actualMenuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar]; 1115 } 1116 1117 if ([nsWindow isKeyWindow] || [nsWindow isMainWindow]) { 1118 [CMenuBar activate:actualMenuBar modallyDisabled:NO]; 1119 } 1120 }]; 1121 1122 JNF_COCOA_EXIT(env); 1123 } 1124 1125 /* 1126 * Class: sun_lwawt_macosx_CPlatformWindow 1127 * Method: nativeGetNSWindowInsets 1128 * Signature: (J)Ljava/awt/Insets; 1129 */ 1130 JNIEXPORT jobject JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetNSWindowInsets 1131 (JNIEnv *env, jclass clazz, jlong windowPtr) 1132 { 1133 jobject ret = NULL; 1134 1135 JNF_COCOA_ENTER(env); 1136 1137 NSWindow *nsWindow = OBJC(windowPtr); 1138 __block NSRect contentRect = NSZeroRect; 1139 __block NSRect frame = NSZeroRect; 1140 1141 [ThreadUtilities performOnMainThreadWaiting:YES block:^(){ 1142 1143 frame = [nsWindow frame]; 1144 contentRect = [NSWindow contentRectForFrameRect:frame styleMask:[nsWindow styleMask]]; 1145 }]; 1146 1147 jint top = (jint)(frame.size.height - contentRect.size.height); 1148 jint left = (jint)(contentRect.origin.x - frame.origin.x); 1149 jint bottom = (jint)(contentRect.origin.y - frame.origin.y); 1150 jint right = (jint)(frame.size.width - (contentRect.size.width + left)); 1151 1152 static JNF_CLASS_CACHE(jc_Insets, "java/awt/Insets"); 1153 static JNF_CTOR_CACHE(jc_Insets_ctor, jc_Insets, "(IIII)V"); 1154 ret = JNFNewObject(env, jc_Insets_ctor, top, left, bottom, right); 1155 1156 JNF_COCOA_EXIT(env); 1157 return ret; 1158 } 1159 1160 /* 1161 * Class: sun_lwawt_macosx_CPlatformWindow 1162 * Method: nativeSetNSWindowBounds 1163 * Signature: (JDDDD)V 1164 */ 1165 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowBounds 1166 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, jdouble width, jdouble height) 1167 { 1168 JNF_COCOA_ENTER(env); 1169 1170 NSRect jrect = NSMakeRect(originX, originY, width, height); 1171 1172 // TODO: not sure we need displayIfNeeded message in our view 1173 NSWindow *nsWindow = OBJC(windowPtr); 1174 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1175 1176 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1177 1178 NSRect rect = ConvertNSScreenRect(NULL, jrect); 1179 [window constrainSize:&rect.size]; 1180 1181 [nsWindow setFrame:rect display:YES]; 1182 1183 // only start tracking events if pointer is above the toplevel 1184 // TODO: should post an Entered event if YES. 1185 NSPoint mLocation = [NSEvent mouseLocation]; 1186 [nsWindow setAcceptsMouseMovedEvents:NSPointInRect(mLocation, rect)]; 1187 1188 // ensure we repaint the whole window after the resize operation 1189 // (this will also re-enable screen updates, which were disabled above) 1190 // TODO: send PaintEvent 1191 }]; 1192 1193 JNF_COCOA_EXIT(env); 1194 } 1195 1196 /* 1197 * Class: sun_lwawt_macosx_CPlatformWindow 1198 * Method: nativeSetNSWindowStandardFrame 1199 * Signature: (JDDDD)V 1200 */ 1201 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowStandardFrame 1202 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble originX, jdouble originY, 1203 jdouble width, jdouble height) 1204 { 1205 JNF_COCOA_ENTER(env); 1206 1207 NSRect jrect = NSMakeRect(originX, originY, width, height); 1208 1209 NSWindow *nsWindow = OBJC(windowPtr); 1210 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1211 1212 NSRect rect = ConvertNSScreenRect(NULL, jrect); 1213 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1214 window.standardFrame = rect; 1215 }]; 1216 1217 JNF_COCOA_EXIT(env); 1218 } 1219 1220 /* 1221 * Class: sun_lwawt_macosx_CPlatformWindow 1222 * Method: nativeSetNSWindowLocationByPlatform 1223 * Signature: (J)V 1224 */ 1225 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowLocationByPlatform 1226 (JNIEnv *env, jclass clazz, jlong windowPtr) 1227 { 1228 JNF_COCOA_ENTER(env); 1229 1230 NSWindow *nsWindow = OBJC(windowPtr); 1231 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1232 1233 if (NSEqualPoints(lastTopLeftPoint, NSZeroPoint)) { 1234 // This is the first usage of lastTopLeftPoint. So invoke cascadeTopLeftFromPoint 1235 // twice to avoid positioning the window's top left to zero-point, since it may 1236 // cause negative user experience. 1237 lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint]; 1238 } 1239 lastTopLeftPoint = [nsWindow cascadeTopLeftFromPoint:lastTopLeftPoint]; 1240 }]; 1241 1242 JNF_COCOA_EXIT(env); 1243 } 1244 1245 /* 1246 * Class: sun_lwawt_macosx_CPlatformWindow 1247 * Method: nativeSetNSWindowMinMax 1248 * Signature: (JDDDD)V 1249 */ 1250 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinMax 1251 (JNIEnv *env, jclass clazz, jlong windowPtr, jdouble minW, jdouble minH, jdouble maxW, jdouble maxH) 1252 { 1253 JNF_COCOA_ENTER(env); 1254 1255 if (minW < 1) minW = 1; 1256 if (minH < 1) minH = 1; 1257 if (maxW < 1) maxW = 1; 1258 if (maxH < 1) maxH = 1; 1259 1260 NSWindow *nsWindow = OBJC(windowPtr); 1261 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1262 1263 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1264 1265 NSSize min = { minW, minH }; 1266 NSSize max = { maxW, maxH }; 1267 1268 [window constrainSize:&min]; 1269 [window constrainSize:&max]; 1270 1271 window.javaMinSize = min; 1272 window.javaMaxSize = max; 1273 [window updateMinMaxSize:IS(window.styleBits, RESIZABLE)]; 1274 }]; 1275 1276 JNF_COCOA_EXIT(env); 1277 } 1278 1279 /* 1280 * Class: sun_lwawt_macosx_CPlatformWindow 1281 * Method: nativePushNSWindowToBack 1282 * Signature: (J)V 1283 */ 1284 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToBack 1285 (JNIEnv *env, jclass clazz, jlong windowPtr) 1286 { 1287 JNF_COCOA_ENTER(env); 1288 1289 NSWindow *nsWindow = OBJC(windowPtr); 1290 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1291 [nsWindow orderBack:nil]; 1292 // Order parent windows 1293 AWTWindow *awtWindow = (AWTWindow*)[nsWindow delegate]; 1294 while (awtWindow.ownerWindow != nil) { 1295 awtWindow = awtWindow.ownerWindow; 1296 if ([AWTWindow isJavaPlatformWindowVisible:awtWindow.nsWindow]) { 1297 [awtWindow.nsWindow orderBack:nil]; 1298 } 1299 } 1300 // Order child windows 1301 [(AWTWindow*)[nsWindow delegate] orderChildWindows:NO]; 1302 }]; 1303 1304 JNF_COCOA_EXIT(env); 1305 } 1306 1307 /* 1308 * Class: sun_lwawt_macosx_CPlatformWindow 1309 * Method: nativePushNSWindowToFront 1310 * Signature: (J)V 1311 */ 1312 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativePushNSWindowToFront 1313 (JNIEnv *env, jclass clazz, jlong windowPtr) 1314 { 1315 JNF_COCOA_ENTER(env); 1316 1317 NSWindow *nsWindow = OBJC(windowPtr); 1318 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1319 1320 if (![nsWindow isKeyWindow]) { 1321 [nsWindow makeKeyAndOrderFront:nsWindow]; 1322 } else { 1323 [nsWindow orderFront:nsWindow]; 1324 } 1325 }]; 1326 1327 JNF_COCOA_EXIT(env); 1328 } 1329 1330 /* 1331 * Class: sun_lwawt_macosx_CPlatformWindow 1332 * Method: nativeSetNSWindowTitle 1333 * Signature: (JLjava/lang/String;)V 1334 */ 1335 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowTitle 1336 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring jtitle) 1337 { 1338 JNF_COCOA_ENTER(env); 1339 1340 NSWindow *nsWindow = OBJC(windowPtr); 1341 [nsWindow performSelectorOnMainThread:@selector(setTitle:) 1342 withObject:JNFJavaToNSString(env, jtitle) 1343 waitUntilDone:NO]; 1344 1345 JNF_COCOA_EXIT(env); 1346 } 1347 1348 /* 1349 * Class: sun_lwawt_macosx_CPlatformWindow 1350 * Method: nativeRevalidateNSWindowShadow 1351 * Signature: (J)V 1352 */ 1353 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeRevalidateNSWindowShadow 1354 (JNIEnv *env, jclass clazz, jlong windowPtr) 1355 { 1356 JNF_COCOA_ENTER(env); 1357 1358 NSWindow *nsWindow = OBJC(windowPtr); 1359 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1360 [nsWindow invalidateShadow]; 1361 }]; 1362 1363 JNF_COCOA_EXIT(env); 1364 } 1365 1366 /* 1367 * Class: sun_lwawt_macosx_CPlatformWindow 1368 * Method: nativeScreenOn_AppKitThread 1369 * Signature: (J)I 1370 */ 1371 JNIEXPORT jint JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeScreenOn_1AppKitThread 1372 (JNIEnv *env, jclass clazz, jlong windowPtr) 1373 { 1374 jint ret = 0; 1375 1376 JNF_COCOA_ENTER(env); 1377 AWT_ASSERT_APPKIT_THREAD; 1378 1379 NSWindow *nsWindow = OBJC(windowPtr); 1380 NSDictionary *props = [[nsWindow screen] deviceDescription]; 1381 ret = [[props objectForKey:@"NSScreenNumber"] intValue]; 1382 1383 JNF_COCOA_EXIT(env); 1384 1385 return ret; 1386 } 1387 1388 /* 1389 * Class: sun_lwawt_macosx_CPlatformWindow 1390 * Method: nativeSetNSWindowMinimizedIcon 1391 * Signature: (JJ)V 1392 */ 1393 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowMinimizedIcon 1394 (JNIEnv *env, jclass clazz, jlong windowPtr, jlong nsImagePtr) 1395 { 1396 JNF_COCOA_ENTER(env); 1397 1398 NSWindow *nsWindow = OBJC(windowPtr); 1399 NSImage *image = OBJC(nsImagePtr); 1400 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1401 [nsWindow setMiniwindowImage:image]; 1402 }]; 1403 1404 JNF_COCOA_EXIT(env); 1405 } 1406 1407 /* 1408 * Class: sun_lwawt_macosx_CPlatformWindow 1409 * Method: nativeSetNSWindowRepresentedFilename 1410 * Signature: (JLjava/lang/String;)V 1411 */ 1412 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetNSWindowRepresentedFilename 1413 (JNIEnv *env, jclass clazz, jlong windowPtr, jstring filename) 1414 { 1415 JNF_COCOA_ENTER(env); 1416 1417 NSWindow *nsWindow = OBJC(windowPtr); 1418 NSURL *url = (filename == NULL) ? nil : [NSURL fileURLWithPath:JNFNormalizedNSStringForPath(env, filename)]; 1419 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1420 [nsWindow setRepresentedURL:url]; 1421 }]; 1422 1423 JNF_COCOA_EXIT(env); 1424 } 1425 1426 /* 1427 * Class: sun_lwawt_macosx_CPlatformWindow 1428 * Method: nativeGetTopmostPlatformWindowUnderMouse 1429 * Signature: (J)V 1430 */ 1431 JNIEXPORT jobject 1432 JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeGetTopmostPlatformWindowUnderMouse 1433 (JNIEnv *env, jclass clazz) 1434 { 1435 __block jobject topmostWindowUnderMouse = nil; 1436 1437 JNF_COCOA_ENTER(env); 1438 1439 [ThreadUtilities performOnMainThreadWaiting:YES block:^{ 1440 AWTWindow *awtWindow = [AWTWindow getTopmostWindowUnderMouse]; 1441 if (awtWindow != nil) { 1442 topmostWindowUnderMouse = [awtWindow.javaPlatformWindow jObject]; 1443 } 1444 }]; 1445 1446 JNF_COCOA_EXIT(env); 1447 1448 return topmostWindowUnderMouse; 1449 } 1450 1451 /* 1452 * Class: sun_lwawt_macosx_CPlatformWindow 1453 * Method: nativeSynthesizeMouseEnteredExitedEvents 1454 * Signature: ()V 1455 */ 1456 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents__ 1457 (JNIEnv *env, jclass clazz) 1458 { 1459 JNF_COCOA_ENTER(env); 1460 1461 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1462 [AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows]; 1463 }]; 1464 1465 JNF_COCOA_EXIT(env); 1466 } 1467 1468 /* 1469 * Class: sun_lwawt_macosx_CPlatformWindow 1470 * Method: nativeSynthesizeMouseEnteredExitedEvents 1471 * Signature: (JI)V 1472 */ 1473 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSynthesizeMouseEnteredExitedEvents__JI 1474 (JNIEnv *env, jclass clazz, jlong windowPtr, jint eventType) 1475 { 1476 JNF_COCOA_ENTER(env); 1477 1478 if (eventType == NSMouseEntered || eventType == NSMouseExited) { 1479 NSWindow *nsWindow = OBJC(windowPtr); 1480 1481 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1482 [AWTWindow synthesizeMouseEnteredExitedEvents:nsWindow withType:eventType]; 1483 }]; 1484 } else { 1485 [JNFException raise:env as:kIllegalArgumentException reason:"unknown event type"]; 1486 } 1487 1488 JNF_COCOA_EXIT(env); 1489 } 1490 1491 /* 1492 * Class: sun_lwawt_macosx_CPlatformWindow 1493 * Method: _toggleFullScreenMode 1494 * Signature: (J)V 1495 */ 1496 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow__1toggleFullScreenMode 1497 (JNIEnv *env, jobject peer, jlong windowPtr) 1498 { 1499 JNF_COCOA_ENTER(env); 1500 1501 NSWindow *nsWindow = OBJC(windowPtr); 1502 SEL toggleFullScreenSelector = @selector(toggleFullScreen:); 1503 if (![nsWindow respondsToSelector:toggleFullScreenSelector]) return; 1504 1505 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1506 [nsWindow performSelector:toggleFullScreenSelector withObject:nil]; 1507 }]; 1508 1509 JNF_COCOA_EXIT(env); 1510 } 1511 1512 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeSetEnabled 1513 (JNIEnv *env, jclass clazz, jlong windowPtr, jboolean isEnabled) 1514 { 1515 JNF_COCOA_ENTER(env); 1516 1517 NSWindow *nsWindow = OBJC(windowPtr); 1518 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1519 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1520 1521 [window setEnabled: isEnabled]; 1522 }]; 1523 1524 JNF_COCOA_EXIT(env); 1525 } 1526 1527 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeDispose 1528 (JNIEnv *env, jclass clazz, jlong windowPtr) 1529 { 1530 JNF_COCOA_ENTER(env); 1531 1532 NSWindow *nsWindow = OBJC(windowPtr); 1533 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1534 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1535 1536 if ([AWTWindow lastKeyWindow] == window) { 1537 [AWTWindow setLastKeyWindow: nil]; 1538 } 1539 1540 // AWTWindow holds a reference to the NSWindow in its nsWindow 1541 // property. Unsetting the delegate allows it to be deallocated 1542 // which releases the reference. This, in turn, allows the window 1543 // itself be deallocated. 1544 [nsWindow setDelegate: nil]; 1545 1546 [window release]; 1547 }]; 1548 1549 JNF_COCOA_EXIT(env); 1550 } 1551 1552 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeEnterFullScreenMode 1553 (JNIEnv *env, jclass clazz, jlong windowPtr) 1554 { 1555 JNF_COCOA_ENTER(env); 1556 1557 NSWindow *nsWindow = OBJC(windowPtr); 1558 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1559 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1560 NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow]; 1561 CGDirectDisplayID aID = [screenID intValue]; 1562 1563 if (CGDisplayCapture(aID) == kCGErrorSuccess) { 1564 // remove window decoration 1565 NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits]; 1566 [nsWindow setStyleMask:(styleMask & ~NSTitledWindowMask) | NSBorderlessWindowMask]; 1567 1568 int shieldLevel = CGShieldingWindowLevel(); 1569 window.preFullScreenLevel = [nsWindow level]; 1570 [nsWindow setLevel: shieldLevel]; 1571 1572 NSRect screenRect = [[nsWindow screen] frame]; 1573 [nsWindow setFrame:screenRect display:YES]; 1574 } else { 1575 [JNFException raise:[ThreadUtilities getJNIEnv] 1576 as:kRuntimeException 1577 reason:"Failed to enter full screen."]; 1578 } 1579 }]; 1580 1581 JNF_COCOA_EXIT(env); 1582 } 1583 1584 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CPlatformWindow_nativeExitFullScreenMode 1585 (JNIEnv *env, jclass clazz, jlong windowPtr) 1586 { 1587 JNF_COCOA_ENTER(env); 1588 1589 NSWindow *nsWindow = OBJC(windowPtr); 1590 [ThreadUtilities performOnMainThreadWaiting:NO block:^(){ 1591 AWTWindow *window = (AWTWindow*)[nsWindow delegate]; 1592 NSNumber* screenID = [AWTWindow getNSWindowDisplayID_AppKitThread: nsWindow]; 1593 CGDirectDisplayID aID = [screenID intValue]; 1594 1595 if (CGDisplayRelease(aID) == kCGErrorSuccess) { 1596 NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:window.styleBits]; 1597 [nsWindow setStyleMask:styleMask]; 1598 [nsWindow setLevel: window.preFullScreenLevel]; 1599 1600 // GraphicsDevice takes care of restoring pre full screen bounds 1601 } else { 1602 [JNFException raise:[ThreadUtilities getJNIEnv] 1603 as:kRuntimeException 1604 reason:"Failed to exit full screen."]; 1605 } 1606 }]; 1607 1608 JNF_COCOA_EXIT(env); 1609 } 1610 --- EOF ---