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