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