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