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