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