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