1 /* 2 * Copyright (c) 2011, 2016, 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 package sun.lwawt.macosx; 27 28 import com.apple.eawt.FullScreenAdapter; 29 import com.apple.eawt.FullScreenUtilities; 30 import com.apple.eawt.event.FullScreenEvent; 31 import java.awt.*; 32 import java.awt.Dialog.ModalityType; 33 import java.awt.event.*; 34 import java.beans.*; 35 import java.lang.reflect.InvocationTargetException; 36 37 import javax.swing.*; 38 39 import sun.awt.*; 40 import sun.awt.AWTAccessor.ComponentAccessor; 41 import sun.java2d.SurfaceData; 42 import sun.java2d.opengl.CGLSurfaceData; 43 import sun.lwawt.*; 44 import sun.util.logging.PlatformLogger; 45 46 import com.apple.laf.*; 47 import com.apple.laf.ClientPropertyApplicator.Property; 48 import com.sun.awt.AWTUtilities; 49 import sun.lwawt.LWWindowPeer.PeerType; 50 51 public class CPlatformWindow extends CFRetainedResource implements PlatformWindow { 52 private native long nativeCreateNSWindow(long nsViewPtr,long ownerPtr, long styleBits, double x, double y, double w, double h); 53 private static native void nativeSetNSWindowStyleBits(long nsWindowPtr, int mask, int data); 54 private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr); 55 private static native Insets nativeGetNSWindowInsets(long nsWindowPtr); 56 private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h); 57 private static native void nativeSetNSWindowLocationByPlatform(long nsWindowPtr); 58 private static native void nativeSetNSWindowStandardFrame(long nsWindowPtr, 59 double x, double y, double w, double h); 60 private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH); 61 private static native void nativePushNSWindowToBack(long nsWindowPtr); 62 private static native void nativePushNSWindowToFront(long nsWindowPtr); 63 private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title); 64 private static native void nativeRevalidateNSWindowShadow(long nsWindowPtr); 65 private static native void nativeSetNSWindowMinimizedIcon(long nsWindowPtr, long nsImage); 66 private static native void nativeSetNSWindowRepresentedFilename(long nsWindowPtr, String representedFilename); 67 private static native void nativeSetEnabled(long nsWindowPtr, boolean isEnabled); 68 private static native void nativeSynthesizeMouseEnteredExitedEvents(); 69 private static native void nativeSynthesizeMouseEnteredExitedEvents(long nsWindowPtr, int eventType); 70 private static native void nativeDispose(long nsWindowPtr); 71 private static native void nativeEnterFullScreenMode(long nsWindowPtr); 72 private static native void nativeExitFullScreenMode(long nsWindowPtr); 73 static native CPlatformWindow nativeGetTopmostPlatformWindowUnderMouse(); 74 75 // Loger to report issues happened during execution but that do not affect functionality 76 private static final PlatformLogger logger = PlatformLogger.getLogger("sun.lwawt.macosx.CPlatformWindow"); 77 private static final PlatformLogger focusLogger = PlatformLogger.getLogger("sun.lwawt.macosx.focus.CPlatformWindow"); 78 79 // for client properties 80 public static final String WINDOW_BRUSH_METAL_LOOK = "apple.awt.brushMetalLook"; 81 public static final String WINDOW_DRAGGABLE_BACKGROUND = "apple.awt.draggableWindowBackground"; 82 83 public static final String WINDOW_ALPHA = "Window.alpha"; 84 public static final String WINDOW_SHADOW = "Window.shadow"; 85 86 public static final String WINDOW_STYLE = "Window.style"; 87 public static final String WINDOW_SHADOW_REVALIDATE_NOW = "apple.awt.windowShadow.revalidateNow"; 88 89 public static final String WINDOW_DOCUMENT_MODIFIED = "Window.documentModified"; 90 public static final String WINDOW_DOCUMENT_FILE = "Window.documentFile"; 91 92 public static final String WINDOW_CLOSEABLE = "Window.closeable"; 93 public static final String WINDOW_MINIMIZABLE = "Window.minimizable"; 94 public static final String WINDOW_ZOOMABLE = "Window.zoomable"; 95 public static final String WINDOW_HIDES_ON_DEACTIVATE="Window.hidesOnDeactivate"; 96 97 public static final String WINDOW_DOC_MODAL_SHEET = "apple.awt.documentModalSheet"; 98 public static final String WINDOW_FADE_DELEGATE = "apple.awt._windowFadeDelegate"; 99 public static final String WINDOW_FADE_IN = "apple.awt._windowFadeIn"; 100 public static final String WINDOW_FADE_OUT = "apple.awt._windowFadeOut"; 101 public static final String WINDOW_FULLSCREENABLE = "apple.awt.fullscreenable"; 102 103 104 // Yeah, I know. But it's easier to deal with ints from JNI 105 static final int MODELESS = 0; 106 static final int DOCUMENT_MODAL = 1; 107 static final int APPLICATION_MODAL = 2; 108 static final int TOOLKIT_MODAL = 3; 109 110 // window style bits 111 static final int _RESERVED_FOR_DATA = 1 << 0; 112 113 // corresponds to native style mask bits 114 static final int DECORATED = 1 << 1; 115 static final int TEXTURED = 1 << 2; 116 static final int UNIFIED = 1 << 3; 117 static final int UTILITY = 1 << 4; 118 static final int HUD = 1 << 5; 119 static final int SHEET = 1 << 6; 120 121 static final int CLOSEABLE = 1 << 7; 122 static final int MINIMIZABLE = 1 << 8; 123 124 static final int RESIZABLE = 1 << 9; // both a style bit and prop bit 125 static final int NONACTIVATING = 1 << 24; 126 static final int IS_DIALOG = 1 << 25; 127 static final int IS_MODAL = 1 << 26; 128 static final int IS_POPUP = 1 << 27; 129 130 static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE | MINIMIZABLE | RESIZABLE; 131 132 // corresponds to method-based properties 133 static final int HAS_SHADOW = 1 << 10; 134 static final int ZOOMABLE = 1 << 11; 135 136 static final int ALWAYS_ON_TOP = 1 << 15; 137 static final int HIDES_ON_DEACTIVATE = 1 << 17; 138 static final int DRAGGABLE_BACKGROUND = 1 << 19; 139 static final int DOCUMENT_MODIFIED = 1 << 21; 140 static final int FULLSCREENABLE = 1 << 23; 141 142 static final int _METHOD_PROP_BITMASK = RESIZABLE | HAS_SHADOW | ZOOMABLE | ALWAYS_ON_TOP | HIDES_ON_DEACTIVATE | DRAGGABLE_BACKGROUND | DOCUMENT_MODIFIED | FULLSCREENABLE; 143 144 // corresponds to callback-based properties 145 static final int SHOULD_BECOME_KEY = 1 << 12; 146 static final int SHOULD_BECOME_MAIN = 1 << 13; 147 static final int MODAL_EXCLUDED = 1 << 16; 148 149 static final int _CALLBACK_PROP_BITMASK = SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN | MODAL_EXCLUDED; 150 151 static int SET(final int bits, final int mask, final boolean value) { 152 if (value) return (bits | mask); 153 return bits & ~mask; 154 } 155 156 static boolean IS(final int bits, final int mask) { 157 return (bits & mask) != 0; 158 } 159 160 @SuppressWarnings({"unchecked", "rawtypes"}) 161 static ClientPropertyApplicator<JRootPane, CPlatformWindow> CLIENT_PROPERTY_APPLICATOR = new ClientPropertyApplicator<JRootPane, CPlatformWindow>(new Property[] { 162 new Property<CPlatformWindow>(WINDOW_DOCUMENT_MODIFIED) { public void applyProperty(final CPlatformWindow c, final Object value) { 163 c.setStyleBits(DOCUMENT_MODIFIED, value == null ? false : Boolean.parseBoolean(value.toString())); 164 }}, 165 new Property<CPlatformWindow>(WINDOW_BRUSH_METAL_LOOK) { public void applyProperty(final CPlatformWindow c, final Object value) { 166 c.setStyleBits(TEXTURED, Boolean.parseBoolean(value.toString())); 167 }}, 168 new Property<CPlatformWindow>(WINDOW_ALPHA) { public void applyProperty(final CPlatformWindow c, final Object value) { 169 AWTUtilities.setWindowOpacity(c.target, value == null ? 1.0f : Float.parseFloat(value.toString())); 170 }}, 171 new Property<CPlatformWindow>(WINDOW_SHADOW) { public void applyProperty(final CPlatformWindow c, final Object value) { 172 c.setStyleBits(HAS_SHADOW, value == null ? true : Boolean.parseBoolean(value.toString())); 173 }}, 174 new Property<CPlatformWindow>(WINDOW_MINIMIZABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 175 c.setStyleBits(MINIMIZABLE, Boolean.parseBoolean(value.toString())); 176 }}, 177 new Property<CPlatformWindow>(WINDOW_CLOSEABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 178 c.setStyleBits(CLOSEABLE, Boolean.parseBoolean(value.toString())); 179 }}, 180 new Property<CPlatformWindow>(WINDOW_ZOOMABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 181 c.setStyleBits(ZOOMABLE, Boolean.parseBoolean(value.toString())); 182 }}, 183 new Property<CPlatformWindow>(WINDOW_FULLSCREENABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 184 c.setStyleBits(FULLSCREENABLE, Boolean.parseBoolean(value.toString())); 185 }}, 186 new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) { 187 nativeRevalidateNSWindowShadow(c.getNSWindowPtr()); 188 }}, 189 new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) { 190 if (value == null || !(value instanceof java.io.File)) { 191 nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), null); 192 return; 193 } 194 195 final String filename = ((java.io.File)value).getAbsolutePath(); 196 nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), filename); 197 }} 198 }) { 199 @SuppressWarnings("deprecation") 200 public CPlatformWindow convertJComponentToTarget(final JRootPane p) { 201 Component root = SwingUtilities.getRoot(p); 202 final ComponentAccessor acc = AWTAccessor.getComponentAccessor(); 203 if (root == null || acc.getPeer(root) == null) return null; 204 return (CPlatformWindow)((LWWindowPeer)acc.getPeer(root)).getPlatformWindow(); 205 } 206 }; 207 208 // Bounds of the native widget but in the Java coordinate system. 209 // In order to keep it up-to-date we will update them on 210 // 1) setting native bounds via nativeSetBounds() call 211 // 2) getting notification from the native level via deliverMoveResizeEvent() 212 private Rectangle nativeBounds = new Rectangle(0, 0, 0, 0); 213 private volatile boolean isFullScreenMode; 214 private boolean isFullScreenAnimationOn; 215 216 private volatile boolean isInFullScreen; 217 218 private Window target; 219 private LWWindowPeer peer; 220 protected CPlatformView contentView; 221 protected CPlatformWindow owner; 222 protected boolean visible = false; // visibility status from native perspective 223 private boolean undecorated; // initialized in getInitialStyleBits() 224 private Rectangle normalBounds = null; // not-null only for undecorated maximized windows 225 private CPlatformResponder responder; 226 227 public CPlatformWindow() { 228 super(0, true); 229 } 230 231 /* 232 * Delegate initialization (create native window and all the 233 * related resources). 234 */ 235 @Override // PlatformWindow 236 public void initialize(Window _target, LWWindowPeer _peer, PlatformWindow _owner) { 237 initializeBase(_target, _peer, _owner, new CPlatformView()); 238 239 final int styleBits = getInitialStyleBits(); 240 241 responder = createPlatformResponder(); 242 contentView = createContentView(); 243 contentView.initialize(peer, responder); 244 245 final long ownerPtr = owner != null ? owner.getNSWindowPtr() : 0L; 246 Rectangle bounds; 247 if (!IS(DECORATED, styleBits)) { 248 // For undecorated frames the move/resize event does not come if the frame is centered on the screen 249 // so we need to set a stub location to force an initial move/resize. Real bounds would be set later. 250 bounds = new Rectangle(0, 0, 1, 1); 251 } else { 252 bounds = _peer.constrainBounds(_target.getBounds()); 253 } 254 final long nativeWindowPtr = nativeCreateNSWindow(contentView.getAWTView(), 255 ownerPtr, styleBits, bounds.x, bounds.y, bounds.width, bounds.height); 256 setPtr(nativeWindowPtr); 257 258 if (target instanceof javax.swing.RootPaneContainer) { 259 final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 260 if (rootpane != null) rootpane.addPropertyChangeListener("ancestor", new PropertyChangeListener() { 261 public void propertyChange(final PropertyChangeEvent evt) { 262 CLIENT_PROPERTY_APPLICATOR.attachAndApplyClientProperties(rootpane); 263 rootpane.removePropertyChangeListener("ancestor", this); 264 } 265 }); 266 if (getPeer().getPeerType() == PeerType.FRAME) { 267 if (IS(RESIZABLE, styleBits)) { 268 setCanFullscreen(true); 269 } 270 } 271 } 272 273 validateSurface(); 274 } 275 276 protected void initializeBase(Window target, LWWindowPeer peer, PlatformWindow owner, CPlatformView view) { 277 this.peer = peer; 278 this.target = target; 279 if (owner instanceof CPlatformWindow) { 280 this.owner = (CPlatformWindow)owner; 281 } 282 this.contentView = view; 283 } 284 285 protected CPlatformResponder createPlatformResponder() { 286 return new CPlatformResponder(peer, false); 287 } 288 289 protected CPlatformView createContentView() { 290 return new CPlatformView(); 291 } 292 293 protected int getInitialStyleBits() { 294 // defaults style bits 295 int styleBits = DECORATED | HAS_SHADOW | CLOSEABLE | MINIMIZABLE | ZOOMABLE | RESIZABLE; 296 297 if (isNativelyFocusableWindow()) { 298 styleBits = SET(styleBits, SHOULD_BECOME_KEY, true); 299 styleBits = SET(styleBits, SHOULD_BECOME_MAIN, true); 300 } 301 302 final boolean isFrame = (target instanceof Frame); 303 final boolean isDialog = (target instanceof Dialog); 304 final boolean isPopup = (target.getType() == Window.Type.POPUP); 305 if (isDialog) { 306 styleBits = SET(styleBits, MINIMIZABLE, false); 307 } 308 309 // Either java.awt.Frame or java.awt.Dialog can be undecorated, however java.awt.Window always is undecorated. 310 { 311 this.undecorated = isFrame ? ((Frame)target).isUndecorated() : (isDialog ? ((Dialog)target).isUndecorated() : true); 312 if (this.undecorated) styleBits = SET(styleBits, DECORATED, false); 313 } 314 315 // Either java.awt.Frame or java.awt.Dialog can be resizable, however java.awt.Window is never resizable 316 { 317 final boolean resizable = isFrame ? ((Frame)target).isResizable() : (isDialog ? ((Dialog)target).isResizable() : false); 318 styleBits = SET(styleBits, RESIZABLE, resizable); 319 if (!resizable) { 320 styleBits = SET(styleBits, ZOOMABLE, false); 321 } 322 } 323 324 if (target.isAlwaysOnTop()) { 325 styleBits = SET(styleBits, ALWAYS_ON_TOP, true); 326 } 327 328 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 329 styleBits = SET(styleBits, MODAL_EXCLUDED, true); 330 } 331 332 // If the target is a dialog, popup or tooltip we want it to ignore the brushed metal look. 333 if (isPopup) { 334 styleBits = SET(styleBits, TEXTURED, false); 335 // Popups in applets don't activate applet's process 336 styleBits = SET(styleBits, NONACTIVATING, true); 337 styleBits = SET(styleBits, IS_POPUP, true); 338 } 339 340 if (Window.Type.UTILITY.equals(target.getType())) { 341 styleBits = SET(styleBits, UTILITY, true); 342 } 343 344 if (target instanceof javax.swing.RootPaneContainer) { 345 javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 346 Object prop = null; 347 348 prop = rootpane.getClientProperty(WINDOW_BRUSH_METAL_LOOK); 349 if (prop != null) { 350 styleBits = SET(styleBits, TEXTURED, Boolean.parseBoolean(prop.toString())); 351 } 352 353 if (isDialog && ((Dialog)target).getModalityType() == ModalityType.DOCUMENT_MODAL) { 354 prop = rootpane.getClientProperty(WINDOW_DOC_MODAL_SHEET); 355 if (prop != null) { 356 styleBits = SET(styleBits, SHEET, Boolean.parseBoolean(prop.toString())); 357 } 358 } 359 360 prop = rootpane.getClientProperty(WINDOW_STYLE); 361 if (prop != null) { 362 if ("small".equals(prop)) { 363 styleBits = SET(styleBits, UTILITY, true); 364 if (target.isAlwaysOnTop() && rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE) == null) { 365 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, true); 366 } 367 } 368 if ("textured".equals(prop)) styleBits = SET(styleBits, TEXTURED, true); 369 if ("unified".equals(prop)) styleBits = SET(styleBits, UNIFIED, true); 370 if ("hud".equals(prop)) styleBits = SET(styleBits, HUD, true); 371 } 372 373 prop = rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE); 374 if (prop != null) { 375 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, Boolean.parseBoolean(prop.toString())); 376 } 377 378 prop = rootpane.getClientProperty(WINDOW_CLOSEABLE); 379 if (prop != null) { 380 styleBits = SET(styleBits, CLOSEABLE, Boolean.parseBoolean(prop.toString())); 381 } 382 383 prop = rootpane.getClientProperty(WINDOW_MINIMIZABLE); 384 if (prop != null) { 385 styleBits = SET(styleBits, MINIMIZABLE, Boolean.parseBoolean(prop.toString())); 386 } 387 388 prop = rootpane.getClientProperty(WINDOW_ZOOMABLE); 389 if (prop != null) { 390 styleBits = SET(styleBits, ZOOMABLE, Boolean.parseBoolean(prop.toString())); 391 } 392 393 prop = rootpane.getClientProperty(WINDOW_FULLSCREENABLE); 394 if (prop != null) { 395 styleBits = SET(styleBits, FULLSCREENABLE, Boolean.parseBoolean(prop.toString())); 396 } 397 398 prop = rootpane.getClientProperty(WINDOW_SHADOW); 399 if (prop != null) { 400 styleBits = SET(styleBits, HAS_SHADOW, Boolean.parseBoolean(prop.toString())); 401 } 402 403 prop = rootpane.getClientProperty(WINDOW_DRAGGABLE_BACKGROUND); 404 if (prop != null) { 405 styleBits = SET(styleBits, DRAGGABLE_BACKGROUND, Boolean.parseBoolean(prop.toString())); 406 } 407 } 408 409 if (isDialog) { 410 styleBits = SET(styleBits, IS_DIALOG, true); 411 if (((Dialog) target).isModal()) { 412 styleBits = SET(styleBits, IS_MODAL, true); 413 } 414 } 415 416 peer.setTextured(IS(TEXTURED, styleBits)); 417 418 return styleBits; 419 } 420 421 // this is the counter-point to -[CWindow _nativeSetStyleBit:] 422 private void setStyleBits(final int mask, final boolean value) { 423 nativeSetNSWindowStyleBits(getNSWindowPtr(), mask, value ? mask : 0); 424 } 425 426 private native void _toggleFullScreenMode(final long model); 427 428 public void toggleFullScreen() { 429 _toggleFullScreenMode(getNSWindowPtr()); 430 } 431 432 @Override // PlatformWindow 433 public void setMenuBar(MenuBar mb) { 434 final long nsWindowPtr = getNSWindowPtr(); 435 CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb); 436 if (mbPeer != null) { 437 nativeSetNSWindowMenuBar(nsWindowPtr, mbPeer.getModel()); 438 } else { 439 nativeSetNSWindowMenuBar(nsWindowPtr, 0); 440 } 441 } 442 443 @Override // PlatformWindow 444 public void dispose() { 445 contentView.dispose(); 446 nativeDispose(getNSWindowPtr()); 447 CPlatformWindow.super.dispose(); 448 } 449 450 @Override // PlatformWindow 451 public FontMetrics getFontMetrics(Font f) { 452 // TODO: not implemented 453 (new RuntimeException("unimplemented")).printStackTrace(); 454 return null; 455 } 456 457 @Override // PlatformWindow 458 public Insets getInsets() { 459 return nativeGetNSWindowInsets(getNSWindowPtr()); 460 } 461 462 @Override // PlatformWindow 463 public Point getLocationOnScreen() { 464 return new Point(nativeBounds.x, nativeBounds.y); 465 } 466 467 @Override 468 public GraphicsDevice getGraphicsDevice() { 469 return contentView.getGraphicsDevice(); 470 } 471 472 @Override // PlatformWindow 473 public SurfaceData getScreenSurface() { 474 // TODO: not implemented 475 return null; 476 } 477 478 @Override // PlatformWindow 479 public SurfaceData replaceSurfaceData() { 480 return contentView.replaceSurfaceData(); 481 } 482 483 @Override // PlatformWindow 484 public void setBounds(int x, int y, int w, int h) { 485 nativeSetNSWindowBounds(getNSWindowPtr(), x, y, w, h); 486 } 487 488 public void setMaximizedBounds(int x, int y, int w, int h) { 489 nativeSetNSWindowStandardFrame(getNSWindowPtr(), x, y, w, h); 490 } 491 492 private boolean isMaximized() { 493 return undecorated ? this.normalBounds != null 494 : CWrapper.NSWindow.isZoomed(getNSWindowPtr()); 495 } 496 497 private void maximize() { 498 if (peer == null || isMaximized()) { 499 return; 500 } 501 if (!undecorated) { 502 CWrapper.NSWindow.zoom(getNSWindowPtr()); 503 } else { 504 deliverZoom(true); 505 506 // We need an up to date size of the peer, so we flush the native events 507 // to be sure that there are no setBounds requests in the queue. 508 LWCToolkit.flushNativeSelectors(); 509 this.normalBounds = peer.getBounds(); 510 Rectangle maximizedBounds = peer.getMaximizedBounds(); 511 setBounds(maximizedBounds.x, maximizedBounds.y, 512 maximizedBounds.width, maximizedBounds.height); 513 } 514 } 515 516 private void unmaximize() { 517 if (!isMaximized()) { 518 return; 519 } 520 if (!undecorated) { 521 CWrapper.NSWindow.zoom(getNSWindowPtr()); 522 } else { 523 deliverZoom(false); 524 525 Rectangle toBounds = this.normalBounds; 526 this.normalBounds = null; 527 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 528 } 529 } 530 531 public boolean isVisible() { 532 return this.visible; 533 } 534 535 @Override // PlatformWindow 536 public void setVisible(boolean visible) { 537 final long nsWindowPtr = getNSWindowPtr(); 538 539 // Configure stuff 540 updateIconImages(); 541 updateFocusabilityForAutoRequestFocus(false); 542 543 boolean wasMaximized = isMaximized(); 544 545 if (visible && target.isLocationByPlatform()) { 546 nativeSetNSWindowLocationByPlatform(getNSWindowPtr()); 547 } 548 549 // Actually show or hide the window 550 LWWindowPeer blocker = (peer == null)? null : peer.getBlocker(); 551 if (blocker == null || !visible) { 552 // If it ain't blocked, or is being hidden, go regular way 553 if (visible) { 554 CWrapper.NSWindow.makeFirstResponder(nsWindowPtr, contentView.getAWTView()); 555 556 boolean isPopup = (target.getType() == Window.Type.POPUP); 557 if (isPopup) { 558 // Popups in applets don't activate applet's process 559 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 560 } else { 561 CWrapper.NSWindow.orderFront(nsWindowPtr); 562 } 563 564 boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(nsWindowPtr); 565 if (!isKeyWindow) { 566 CWrapper.NSWindow.makeKeyWindow(nsWindowPtr); 567 } 568 } else { 569 // immediately hide the window 570 CWrapper.NSWindow.orderOut(nsWindowPtr); 571 // process the close 572 CWrapper.NSWindow.close(nsWindowPtr); 573 } 574 } else { 575 // otherwise, put it in a proper z-order 576 CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowBelow, 577 ((CPlatformWindow)blocker.getPlatformWindow()).getNSWindowPtr()); 578 } 579 this.visible = visible; 580 581 // Manage the extended state when showing 582 if (visible) { 583 // Apply the extended state as expected in shared code 584 if (target instanceof Frame) { 585 if (!wasMaximized && isMaximized()) { 586 // setVisible could have changed the native maximized state 587 deliverZoom(true); 588 } else { 589 int frameState = ((Frame)target).getExtendedState(); 590 if ((frameState & Frame.ICONIFIED) != 0) { 591 // Treat all state bit masks with ICONIFIED bit as ICONIFIED state. 592 frameState = Frame.ICONIFIED; 593 } 594 switch (frameState) { 595 case Frame.ICONIFIED: 596 CWrapper.NSWindow.miniaturize(nsWindowPtr); 597 break; 598 case Frame.MAXIMIZED_BOTH: 599 maximize(); 600 break; 601 default: // NORMAL 602 unmaximize(); // in case it was maximized, otherwise this is a no-op 603 break; 604 } 605 } 606 } 607 } 608 609 nativeSynthesizeMouseEnteredExitedEvents(); 610 611 // Configure stuff #2 612 updateFocusabilityForAutoRequestFocus(true); 613 614 // Manage parent-child relationship when showing 615 final ComponentAccessor acc = AWTAccessor.getComponentAccessor(); 616 617 if (visible) { 618 // Order myself above my parent 619 if (owner != null && owner.isVisible()) { 620 CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowAbove, owner.getNSWindowPtr()); 621 applyWindowLevel(target); 622 } 623 624 // Order my own children above myself 625 for (Window w : target.getOwnedWindows()) { 626 final Object p = acc.getPeer(w); 627 if (p instanceof LWWindowPeer) { 628 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 629 if (pw != null && pw.isVisible()) { 630 CWrapper.NSWindow.orderWindow(pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove, nsWindowPtr); 631 pw.applyWindowLevel(w); 632 } 633 } 634 } 635 } 636 637 // Deal with the blocker of the window being shown 638 if (blocker != null && visible) { 639 // Make sure the blocker is above its siblings 640 ((CPlatformWindow)blocker.getPlatformWindow()).orderAboveSiblings(); 641 } 642 } 643 644 @Override // PlatformWindow 645 public void setTitle(String title) { 646 nativeSetNSWindowTitle(getNSWindowPtr(), title); 647 } 648 649 // Should be called on every window key property change. 650 @Override // PlatformWindow 651 public void updateIconImages() { 652 final long nsWindowPtr = getNSWindowPtr(); 653 final CImage cImage = getImageForTarget(); 654 nativeSetNSWindowMinimizedIcon(nsWindowPtr, cImage == null ? 0L : cImage.ptr); 655 } 656 657 public long getNSWindowPtr() { 658 final long nsWindowPtr = ptr; 659 if (nsWindowPtr == 0L) { 660 if(logger.isLoggable(PlatformLogger.Level.FINE)) { 661 logger.fine("NSWindow already disposed?", new Exception("Pointer to native NSWindow is invalid.")); 662 } 663 } 664 return nsWindowPtr; 665 } 666 667 public SurfaceData getSurfaceData() { 668 return contentView.getSurfaceData(); 669 } 670 671 @Override // PlatformWindow 672 public void toBack() { 673 final long nsWindowPtr = getNSWindowPtr(); 674 nativePushNSWindowToBack(nsWindowPtr); 675 } 676 677 @Override // PlatformWindow 678 public void toFront() { 679 final long nsWindowPtr = getNSWindowPtr(); 680 LWCToolkit lwcToolkit = (LWCToolkit) Toolkit.getDefaultToolkit(); 681 Window w = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 682 final ComponentAccessor acc = AWTAccessor.getComponentAccessor(); 683 if( w != null && acc.getPeer(w) != null 684 && ((LWWindowPeer)acc.getPeer(w)).getPeerType() == LWWindowPeer.PeerType.EMBEDDED_FRAME 685 && !lwcToolkit.isApplicationActive()) { 686 lwcToolkit.activateApplicationIgnoringOtherApps(); 687 } 688 updateFocusabilityForAutoRequestFocus(false); 689 nativePushNSWindowToFront(nsWindowPtr); 690 updateFocusabilityForAutoRequestFocus(true); 691 } 692 693 private void setCanFullscreen(final boolean canFullScreen) { 694 if (target instanceof RootPaneContainer 695 && getPeer().getPeerType() == PeerType.FRAME) { 696 697 if (isInFullScreen && !canFullScreen) { 698 toggleFullScreen(); 699 } 700 701 final RootPaneContainer rpc = (RootPaneContainer) target; 702 rpc.getRootPane().putClientProperty( 703 CPlatformWindow.WINDOW_FULLSCREENABLE, canFullScreen); 704 } 705 } 706 707 @Override 708 public void setResizable(final boolean resizable) { 709 setCanFullscreen(resizable); 710 setStyleBits(RESIZABLE, resizable); 711 setStyleBits(ZOOMABLE, resizable); 712 } 713 714 @Override 715 public void setSizeConstraints(int minW, int minH, int maxW, int maxH) { 716 nativeSetNSWindowMinMax(getNSWindowPtr(), minW, minH, maxW, maxH); 717 } 718 719 @Override 720 public boolean rejectFocusRequest(FocusEvent.Cause cause) { 721 // Cross-app activation requests are not allowed. 722 if (cause != FocusEvent.Cause.MOUSE_EVENT && 723 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) 724 { 725 focusLogger.fine("the app is inactive, so the request is rejected"); 726 return true; 727 } 728 return false; 729 } 730 731 @Override 732 public boolean requestWindowFocus() { 733 734 long ptr = getNSWindowPtr(); 735 if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) { 736 CWrapper.NSWindow.makeMainWindow(ptr); 737 } 738 CWrapper.NSWindow.makeKeyAndOrderFront(ptr); 739 return true; 740 } 741 742 @Override 743 public boolean isActive() { 744 long ptr = getNSWindowPtr(); 745 return CWrapper.NSWindow.isKeyWindow(ptr); 746 } 747 748 @Override 749 public void updateFocusableWindowState() { 750 final boolean isFocusable = isNativelyFocusableWindow(); 751 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 752 } 753 754 @Override 755 public void setAlwaysOnTop(boolean isAlwaysOnTop) { 756 setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop); 757 } 758 759 @Override 760 public void setOpacity(float opacity) { 761 CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity); 762 } 763 764 @Override 765 public void setOpaque(boolean isOpaque) { 766 CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque); 767 boolean isTextured = (peer == null) ? false : peer.isTextured(); 768 if (!isTextured) { 769 if (!isOpaque) { 770 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), 0); 771 } else if (peer != null) { 772 Color color = peer.getBackground(); 773 if (color != null) { 774 int rgb = color.getRGB(); 775 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), rgb); 776 } 777 } 778 } 779 780 //This is a temporary workaround. Looks like after 7124236 will be fixed 781 //the correct place for invalidateShadow() is CGLayer.drawInCGLContext. 782 SwingUtilities.invokeLater(this::invalidateShadow); 783 } 784 785 @Override 786 public void enterFullScreenMode() { 787 isFullScreenMode = true; 788 nativeEnterFullScreenMode(getNSWindowPtr()); 789 } 790 791 @Override 792 public void exitFullScreenMode() { 793 nativeExitFullScreenMode(getNSWindowPtr()); 794 isFullScreenMode = false; 795 } 796 797 @Override 798 public boolean isFullScreenMode() { 799 return isFullScreenMode; 800 } 801 802 @Override 803 public void setWindowState(int windowState) { 804 if (peer == null || !peer.isVisible()) { 805 // setVisible() applies the state 806 return; 807 } 808 809 int prevWindowState = peer.getState(); 810 if (prevWindowState == windowState) return; 811 812 final long nsWindowPtr = getNSWindowPtr(); 813 if ((windowState & Frame.ICONIFIED) != 0) { 814 // Treat all state bit masks with ICONIFIED bit as ICONIFIED state. 815 windowState = Frame.ICONIFIED; 816 } 817 switch (windowState) { 818 case Frame.ICONIFIED: 819 if (prevWindowState == Frame.MAXIMIZED_BOTH) { 820 // let's return into the normal states first 821 // the zoom call toggles between the normal and the max states 822 unmaximize(); 823 } 824 CWrapper.NSWindow.miniaturize(nsWindowPtr); 825 break; 826 case Frame.MAXIMIZED_BOTH: 827 if (prevWindowState == Frame.ICONIFIED) { 828 // let's return into the normal states first 829 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 830 } 831 maximize(); 832 break; 833 case Frame.NORMAL: 834 if (prevWindowState == Frame.ICONIFIED) { 835 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 836 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) { 837 // the zoom call toggles between the normal and the max states 838 unmaximize(); 839 } 840 break; 841 default: 842 throw new RuntimeException("Unknown window state: " + windowState); 843 } 844 845 // NOTE: the SWP.windowState field gets updated to the newWindowState 846 // value when the native notification comes to us 847 } 848 849 @Override 850 public void setModalBlocked(boolean blocked) { 851 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 852 return; 853 } 854 855 if (blocked) { 856 // We are going to show a modal window. Previously displayed window will be 857 // blocked/disabled. So we have to send mouse exited event to it now, since 858 // all mouse events are discarded for blocked/disabled windows. 859 nativeSynthesizeMouseEnteredExitedEvents(getNSWindowPtr(), CocoaConstants.NSMouseExited); 860 } 861 862 nativeSetEnabled(getNSWindowPtr(), !blocked); 863 checkBlockingAndOrder(); 864 } 865 866 public final void invalidateShadow(){ 867 nativeRevalidateNSWindowShadow(getNSWindowPtr()); 868 } 869 870 // ---------------------------------------------------------------------- 871 // UTILITY METHODS 872 // ---------------------------------------------------------------------- 873 874 /** 875 * Find image to install into Title or into Application icon. First try 876 * icons installed for toplevel. Null is returned, if there is no icon and 877 * default Duke image should be used. 878 */ 879 private CImage getImageForTarget() { 880 CImage icon = null; 881 try { 882 icon = CImage.getCreator().createFromImages(target.getIconImages()); 883 } catch (Exception ignored) { 884 // Perhaps the icon passed into Java is broken. Skipping this icon. 885 } 886 return icon; 887 } 888 889 /* 890 * Returns LWWindowPeer associated with this delegate. 891 */ 892 @Override 893 public LWWindowPeer getPeer() { 894 return peer; 895 } 896 897 @Override 898 public boolean isUnderMouse() { 899 return contentView.isUnderMouse(); 900 } 901 902 public CPlatformView getContentView() { 903 return contentView; 904 } 905 906 @Override 907 public long getLayerPtr() { 908 return contentView.getWindowLayerPtr(); 909 } 910 911 private void validateSurface() { 912 SurfaceData surfaceData = getSurfaceData(); 913 if (surfaceData instanceof CGLSurfaceData) { 914 ((CGLSurfaceData)surfaceData).validate(); 915 } 916 } 917 918 void flushBuffers() { 919 if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) { 920 try { 921 LWCToolkit.invokeAndWait(new Runnable() { 922 @Override 923 public void run() { 924 //Posting an empty to flush the EventQueue without blocking the main thread 925 } 926 }, target); 927 } catch (InvocationTargetException e) { 928 e.printStackTrace(); 929 } 930 } 931 } 932 933 /** 934 * Helper method to get a pointer to the native view from the PlatformWindow. 935 */ 936 static long getNativeViewPtr(PlatformWindow platformWindow) { 937 long nativePeer = 0L; 938 if (platformWindow instanceof CPlatformWindow) { 939 nativePeer = ((CPlatformWindow) platformWindow).getContentView().getAWTView(); 940 } else if (platformWindow instanceof CViewPlatformEmbeddedFrame){ 941 nativePeer = ((CViewPlatformEmbeddedFrame) platformWindow).getNSViewPtr(); 942 } 943 return nativePeer; 944 } 945 946 /************************************************************* 947 * Callbacks from the AWTWindow and AWTView objc classes. 948 *************************************************************/ 949 private void deliverWindowFocusEvent(boolean gained, CPlatformWindow opposite){ 950 // Fix for 7150349: ingore "gained" notifications when the app is inactive. 951 if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) { 952 focusLogger.fine("the app is inactive, so the notification is ignored"); 953 return; 954 } 955 956 LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer(); 957 responder.handleWindowFocusEvent(gained, oppositePeer); 958 } 959 960 protected void deliverMoveResizeEvent(int x, int y, int width, int height, 961 boolean byUser) { 962 checkZoom(); 963 964 final Rectangle oldB = nativeBounds; 965 nativeBounds = new Rectangle(x, y, width, height); 966 if (peer != null) { 967 peer.notifyReshape(x, y, width, height); 968 // System-dependent appearance optimization. 969 if ((byUser && !oldB.getSize().equals(nativeBounds.getSize())) 970 || isFullScreenAnimationOn) { 971 flushBuffers(); 972 } 973 } 974 } 975 976 private void deliverWindowClosingEvent() { 977 if (peer != null && peer.getBlocker() == null) { 978 peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING)); 979 } 980 } 981 982 private void deliverIconify(final boolean iconify) { 983 if (peer != null) { 984 peer.notifyIconify(iconify); 985 } 986 } 987 988 private void deliverZoom(final boolean isZoomed) { 989 if (peer != null) { 990 peer.notifyZoom(isZoomed); 991 } 992 } 993 994 private void checkZoom() { 995 if (peer != null) { 996 int state = peer.getState(); 997 if (state != Frame.MAXIMIZED_BOTH && isMaximized()) { 998 deliverZoom(true); 999 } else if (state == Frame.MAXIMIZED_BOTH && !isMaximized()) { 1000 deliverZoom(false); 1001 } 1002 } 1003 } 1004 1005 private void deliverNCMouseDown() { 1006 if (peer != null) { 1007 peer.notifyNCMouseDown(); 1008 } 1009 } 1010 1011 /* 1012 * Our focus model is synthetic and only non-simple window 1013 * may become natively focusable window. 1014 */ 1015 private boolean isNativelyFocusableWindow() { 1016 if (peer == null) { 1017 return false; 1018 } 1019 1020 return !peer.isSimpleWindow() && target.getFocusableWindowState(); 1021 } 1022 1023 /* 1024 * An utility method for the support of the auto request focus. 1025 * Updates the focusable state of the window under certain 1026 * circumstances. 1027 */ 1028 private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) { 1029 if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return; 1030 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 1031 } 1032 1033 private boolean checkBlockingAndOrder() { 1034 LWWindowPeer blocker = (peer == null)? null : peer.getBlocker(); 1035 if (blocker == null) { 1036 return false; 1037 } 1038 1039 if (blocker instanceof CPrinterDialogPeer) { 1040 return true; 1041 } 1042 1043 CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow(); 1044 1045 pWindow.orderAboveSiblings(); 1046 1047 final long nsWindowPtr = pWindow.getNSWindowPtr(); 1048 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 1049 CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr); 1050 CWrapper.NSWindow.makeMainWindow(nsWindowPtr); 1051 1052 return true; 1053 } 1054 1055 private void orderAboveSiblings() { 1056 if (owner == null) { 1057 return; 1058 } 1059 1060 // NOTE: the logic will fail if we have a hierarchy like: 1061 // visible root owner 1062 // invisible owner 1063 // visible dialog 1064 // However, this is an unlikely scenario for real life apps 1065 if (owner.isVisible()) { 1066 // Recursively pop up the windows from the very bottom so that only 1067 // the very top-most one becomes the main window 1068 owner.orderAboveSiblings(); 1069 1070 // Order the window to front of the stack of child windows 1071 final long nsWindowSelfPtr = getNSWindowPtr(); 1072 final long nsWindowOwnerPtr = owner.getNSWindowPtr(); 1073 CWrapper.NSWindow.orderFront(nsWindowOwnerPtr); 1074 CWrapper.NSWindow.orderWindow(nsWindowSelfPtr, CWrapper.NSWindow.NSWindowAbove, nsWindowOwnerPtr); 1075 } 1076 1077 applyWindowLevel(target); 1078 } 1079 1080 protected void applyWindowLevel(Window target) { 1081 if (target.isAlwaysOnTop() && target.getType() != Window.Type.POPUP) { 1082 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 1083 } else if (target.getType() == Window.Type.POPUP) { 1084 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSPopUpMenuWindowLevel); 1085 } 1086 } 1087 1088 // ---------------------------------------------------------------------- 1089 // NATIVE CALLBACKS 1090 // ---------------------------------------------------------------------- 1091 1092 private void windowDidBecomeMain() { 1093 if (checkBlockingAndOrder()) return; 1094 // If it's not blocked, make sure it's above its siblings 1095 orderAboveSiblings(); 1096 } 1097 1098 private void windowWillEnterFullScreen() { 1099 isFullScreenAnimationOn = true; 1100 } 1101 1102 private void windowDidEnterFullScreen() { 1103 isInFullScreen = true; 1104 isFullScreenAnimationOn = false; 1105 } 1106 1107 private void windowWillExitFullScreen() { 1108 isFullScreenAnimationOn = true; 1109 } 1110 1111 private void windowDidExitFullScreen() { 1112 isInFullScreen = false; 1113 isFullScreenAnimationOn = false; 1114 } 1115 }