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