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.util.List; 35 36 import javax.swing.*; 37 38 import sun.awt.*; 39 import sun.java2d.SurfaceData; 40 import sun.java2d.opengl.CGLSurfaceData; 41 import sun.lwawt.*; 42 import sun.lwawt.LWWindowPeer.PeerType; 43 import sun.util.logging.PlatformLogger; 44 45 import com.apple.laf.*; 46 import com.apple.laf.ClientPropertyApplicator.Property; 47 import com.sun.awt.AWTUtilities; 48 49 public class CPlatformWindow extends CFRetainedResource implements PlatformWindow { 50 private native long nativeCreateNSWindow(long nsViewPtr, long styleBits, double x, double y, double w, double h); 51 private static native void nativeSetNSWindowStyleBits(long nsWindowPtr, int mask, int data); 52 private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr); 53 private static native Insets nativeGetNSWindowInsets(long nsWindowPtr); 54 private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h); 55 private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH); 56 private static native void nativePushNSWindowToBack(long nsWindowPtr); 57 private static native void nativePushNSWindowToFront(long nsWindowPtr); 58 private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title); 59 private static native void nativeSetNSWindowAlpha(long nsWindowPtr, float alpha); 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 nativeSetNSWindowSecurityWarningPositioning(long nsWindowPtr, double x, double y, float biasX, float biasY); 64 private static native void nativeModallyBlocked(long nsWindowPtr); 65 private static native void nativeModallyUnblocked(long nsWindowPtr); 66 67 private static native int nativeGetScreenNSWindowIsOn_AppKitThread(long nsWindowPtr); 68 69 // Loger to report issues happened during execution but that do not affect functionality 70 private static final PlatformLogger logger = PlatformLogger.getLogger("sun.lwawt.macosx.CPlatformWindow"); 71 private static final PlatformLogger focusLogger = PlatformLogger.getLogger("sun.lwawt.macosx.focus.CPlatformWindow"); 72 73 // for client properties 74 public static final String WINDOW_BRUSH_METAL_LOOK = "apple.awt.brushMetalLook"; 75 public static final String WINDOW_DRAGGABLE_BACKGROUND = "apple.awt.draggableWindowBackground"; 76 77 public static final String WINDOW_ALPHA = "Window.alpha"; 78 public static final String WINDOW_SHADOW = "Window.shadow"; 79 80 public static final String WINDOW_STYLE = "Window.style"; 81 public static final String WINDOW_SHADOW_REVALIDATE_NOW = "apple.awt.windowShadow.revalidateNow"; 82 83 public static final String WINDOW_DOCUMENT_MODIFIED = "Window.documentModified"; 84 public static final String WINDOW_DOCUMENT_FILE = "Window.documentFile"; 85 86 public static final String WINDOW_CLOSEABLE = "Window.closeable"; 87 public static final String WINDOW_MINIMIZABLE = "Window.minimizable"; 88 public static final String WINDOW_ZOOMABLE = "Window.zoomable"; 89 public static final String WINDOW_HIDES_ON_DEACTIVATE="Window.hidesOnDeactivate"; 90 91 public static final String WINDOW_DOC_MODAL_SHEET = "apple.awt.documentModalSheet"; 92 public static final String WINDOW_FADE_DELEGATE = "apple.awt._windowFadeDelegate"; 93 public static final String WINDOW_FADE_IN = "apple.awt._windowFadeIn"; 94 public static final String WINDOW_FADE_OUT = "apple.awt._windowFadeOut"; 95 public static final String WINDOW_FULLSCREENABLE = "apple.awt.fullscreenable"; 96 97 98 // Yeah, I know. But it's easier to deal with ints from JNI 99 static final int MODELESS = 0; 100 static final int DOCUMENT_MODAL = 1; 101 static final int APPLICATION_MODAL = 2; 102 static final int TOOLKIT_MODAL = 3; 103 104 // window style bits 105 static final int _RESERVED_FOR_DATA = 1 << 0; 106 107 // corresponds to native style mask bits 108 static final int DECORATED = 1 << 1; 109 static final int TEXTURED = 1 << 2; 110 static final int UNIFIED = 1 << 3; 111 static final int UTILITY = 1 << 4; 112 static final int HUD = 1 << 5; 113 static final int SHEET = 1 << 6; 114 115 static final int CLOSEABLE = 1 << 7; 116 static final int MINIMIZABLE = 1 << 8; 117 118 static final int RESIZABLE = 1 << 9; // both a style bit and prop bit 119 static final int NONACTIVATING = 1 << 24; 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; 202 private volatile boolean isFullScreenMode = false; 203 204 private Window target; 205 private LWWindowPeer peer; 206 private CPlatformView contentView; 207 private CPlatformWindow owner; 208 private boolean visible = false; // visibility status from native perspective 209 private boolean undecorated; // initialized in getInitialStyleBits() 210 private Rectangle normalBounds = null; // not-null only for undecorated maximized windows 211 212 public CPlatformWindow(final PeerType peerType) { 213 super(0, true); 214 assert (peerType == PeerType.SIMPLEWINDOW || peerType == PeerType.DIALOG || peerType == PeerType.FRAME); 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 contentView = new CPlatformView(); 236 contentView.initialize(peer); 237 238 final long nativeWindowPtr = nativeCreateNSWindow(contentView.getAWTView(), 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 // Since JDK7 we have standard way to set opacity, so we should not pick 245 // background's alpha. 246 // TODO: set appropriate opacity value 247 // this.opacity = target.getOpacity(); 248 // this.setOpacity(this.opacity); 249 250 final float windowAlpha = target.getOpacity(); 251 if (windowAlpha != 1.0f) { 252 nativeSetNSWindowAlpha(nativeWindowPtr, windowAlpha); 253 } 254 255 if (target instanceof javax.swing.RootPaneContainer) { 256 final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 257 if (rootpane != null) rootpane.addPropertyChangeListener("ancestor", new PropertyChangeListener() { 258 public void propertyChange(final PropertyChangeEvent evt) { 259 CLIENT_PROPERTY_APPLICATOR.attachAndApplyClientProperties(rootpane); 260 rootpane.removePropertyChangeListener("ancestor", this); 261 } 262 }); 263 } 264 265 validateSurface(); 266 } 267 268 protected int getInitialStyleBits() { 269 // defaults style bits 270 int styleBits = DECORATED | HAS_SHADOW | CLOSEABLE | MINIMIZABLE | ZOOMABLE | RESIZABLE; 271 272 if (isNativelyFocusableWindow()) { 273 styleBits = SET(styleBits, SHOULD_BECOME_KEY, true); 274 styleBits = SET(styleBits, SHOULD_BECOME_MAIN, true); 275 } 276 277 final boolean isFrame = (target instanceof Frame); 278 final boolean isDialog = (target instanceof Dialog); 279 final boolean isPopup = (target.getType() == Window.Type.POPUP); 280 if (isDialog) { 281 styleBits = SET(styleBits, MINIMIZABLE, false); 282 } 283 284 // Either java.awt.Frame or java.awt.Dialog can be undecorated, however java.awt.Window always is undecorated. 285 { 286 this.undecorated = isFrame ? ((Frame)target).isUndecorated() : (isDialog ? ((Dialog)target).isUndecorated() : true); 287 if (this.undecorated) styleBits = SET(styleBits, DECORATED, false); 288 } 289 290 // Either java.awt.Frame or java.awt.Dialog can be resizable, however java.awt.Window is never resizable 291 { 292 final boolean resizable = isFrame ? ((Frame)target).isResizable() : (isDialog ? ((Dialog)target).isResizable() : false); 293 styleBits = SET(styleBits, RESIZABLE, resizable); 294 if (!resizable) { 295 styleBits = SET(styleBits, RESIZABLE, false); 296 styleBits = SET(styleBits, ZOOMABLE, false); 297 } 298 } 299 300 if (target.isAlwaysOnTop()) { 301 styleBits = SET(styleBits, ALWAYS_ON_TOP, true); 302 } 303 304 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 305 styleBits = SET(styleBits, MODAL_EXCLUDED, true); 306 } 307 308 // If the target is a dialog, popup or tooltip we want it to ignore the brushed metal look. 309 if (isPopup) { 310 styleBits = SET(styleBits, TEXTURED, true); 311 // Popups in applets don't activate applet's process 312 styleBits = SET(styleBits, NONACTIVATING, true); 313 } 314 315 if (target instanceof javax.swing.RootPaneContainer) { 316 javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 317 Object prop = null; 318 319 prop = rootpane.getClientProperty(WINDOW_BRUSH_METAL_LOOK); 320 if (prop != null) { 321 styleBits = SET(styleBits, TEXTURED, Boolean.parseBoolean(prop.toString())); 322 } 323 324 if (isDialog && ((Dialog)target).getModalityType() == ModalityType.DOCUMENT_MODAL) { 325 prop = rootpane.getClientProperty(WINDOW_DOC_MODAL_SHEET); 326 if (prop != null) { 327 styleBits = SET(styleBits, SHEET, Boolean.parseBoolean(prop.toString())); 328 } 329 } 330 331 prop = rootpane.getClientProperty(WINDOW_STYLE); 332 if (prop != null) { 333 if ("small".equals(prop)) { 334 styleBits = SET(styleBits, UTILITY, true); 335 if (target.isAlwaysOnTop() && rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE) == null) { 336 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, true); 337 } 338 } 339 if ("textured".equals(prop)) styleBits = SET(styleBits, TEXTURED, true); 340 if ("unified".equals(prop)) styleBits = SET(styleBits, UNIFIED, true); 341 if ("hud".equals(prop)) styleBits = SET(styleBits, HUD, true); 342 } 343 344 prop = rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE); 345 if (prop != null) { 346 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, Boolean.parseBoolean(prop.toString())); 347 } 348 349 prop = rootpane.getClientProperty(WINDOW_CLOSEABLE); 350 if (prop != null) { 351 styleBits = SET(styleBits, CLOSEABLE, Boolean.parseBoolean(prop.toString())); 352 } 353 354 prop = rootpane.getClientProperty(WINDOW_MINIMIZABLE); 355 if (prop != null) { 356 styleBits = SET(styleBits, MINIMIZABLE, Boolean.parseBoolean(prop.toString())); 357 } 358 359 prop = rootpane.getClientProperty(WINDOW_ZOOMABLE); 360 if (prop != null) { 361 styleBits = SET(styleBits, ZOOMABLE, Boolean.parseBoolean(prop.toString())); 362 } 363 364 prop = rootpane.getClientProperty(WINDOW_FULLSCREENABLE); 365 if (prop != null) { 366 styleBits = SET(styleBits, FULLSCREENABLE, Boolean.parseBoolean(prop.toString())); 367 } 368 369 prop = rootpane.getClientProperty(WINDOW_SHADOW); 370 if (prop != null) { 371 styleBits = SET(styleBits, HAS_SHADOW, Boolean.parseBoolean(prop.toString())); 372 } 373 374 prop = rootpane.getClientProperty(WINDOW_DRAGGABLE_BACKGROUND); 375 if (prop != null) { 376 styleBits = SET(styleBits, DRAGGABLE_BACKGROUND, Boolean.parseBoolean(prop.toString())); 377 } 378 } 379 380 return styleBits; 381 } 382 383 // this is the counter-point to -[CWindow _nativeSetStyleBit:] 384 protected void setStyleBits(final int mask, final boolean value) { 385 nativeSetNSWindowStyleBits(getNSWindowPtr(), mask, value ? mask : 0); 386 } 387 388 private native void _toggleFullScreenMode(final long model); 389 390 public void toggleFullScreen() { 391 _toggleFullScreenMode(getNSWindowPtr()); 392 } 393 394 @Override // PlatformWindow 395 public void setMenuBar(MenuBar mb) { 396 final long nsWindowPtr = getNSWindowPtr(); 397 CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb); 398 if (mbPeer != null) { 399 nativeSetNSWindowMenuBar(nsWindowPtr, mbPeer.getModel()); 400 } else { 401 nativeSetNSWindowMenuBar(nsWindowPtr, 0); 402 } 403 } 404 405 @Override // PlatformWindow 406 public Image createBackBuffer() { 407 return contentView.createBackBuffer(); 408 } 409 410 @Override // PlatformWindow 411 public void dispose() { 412 if (owner != null) { 413 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), getNSWindowPtr()); 414 } 415 // Make sure window is ordered out before it is disposed, we could order it out right here or 416 // we could postpone the disposal, I think postponing is probably better. 417 EventQueue.invokeLater(new Runnable() { 418 public void run() { 419 contentView.dispose(); 420 CPlatformWindow.super.dispose(); 421 } 422 }); 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 final Insets insets = nativeGetNSWindowInsets(getNSWindowPtr()); 441 return insets; 442 } 443 444 @Override // PlatformWindow 445 public Point getLocationOnScreen() { 446 return new Point(nativeBounds.x, nativeBounds.y); 447 } 448 449 @Override // PlatformWindow 450 public int getScreenImOn() { 451 // REMIND: we could also acquire screenID from the 452 // graphicsConfig.getDevice().getCoreGraphicsScreen() 453 // which might look a bit less natural but don't 454 // require new native accessor. 455 return nativeGetScreenNSWindowIsOn_AppKitThread(getNSWindowPtr()); 456 } 457 458 @Override // PlatformWindow 459 public SurfaceData getScreenSurface() { 460 // TODO: not implemented 461 return null; 462 } 463 464 @Override // PlatformWindow 465 public SurfaceData replaceSurfaceData() { 466 return contentView.replaceSurfaceData(); 467 } 468 469 @Override // PlatformWindow 470 public void setBounds(int x, int y, int w, int h) { 471 // assert CThreading.assertEventQueue(); 472 nativeSetNSWindowBounds(getNSWindowPtr(), x, y, w, h); 473 } 474 475 private void zoom() { 476 if (!undecorated) { 477 CWrapper.NSWindow.zoom(getNSWindowPtr()); 478 } else { 479 // OS X handles -zoom incorrectly for undecorated windows 480 final boolean isZoomed = this.normalBounds == null; 481 deliverZoom(isZoomed); 482 483 Rectangle toBounds; 484 if (isZoomed) { 485 this.normalBounds = peer.getBounds(); 486 long screen = CWrapper.NSWindow.screen(getNSWindowPtr()); 487 toBounds = CWrapper.NSScreen.visibleFrame(screen).getBounds(); 488 // Flip the y coordinate 489 Rectangle frame = CWrapper.NSScreen.frame(screen).getBounds(); 490 toBounds.y = frame.height - toBounds.y - toBounds.height; 491 } else { 492 toBounds = normalBounds; 493 this.normalBounds = null; 494 } 495 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 496 } 497 } 498 499 private boolean isVisible() { 500 return this.visible; 501 } 502 503 @Override // PlatformWindow 504 public void setVisible(boolean visible) { 505 final long nsWindowPtr = getNSWindowPtr(); 506 507 // 1. Process parent-child relationship when hiding 508 if (!visible) { 509 // 1a. Unparent my children 510 for (Window w : target.getOwnedWindows()) { 511 WindowPeer p = (WindowPeer)w.getPeer(); 512 if (p instanceof LWWindowPeer) { 513 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 514 if (pw != null && pw.isVisible()) { 515 CWrapper.NSWindow.removeChildWindow(nsWindowPtr, pw.getNSWindowPtr()); 516 } 517 } 518 } 519 520 // 1b. Unparent myself 521 if (owner != null && owner.isVisible()) { 522 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), nsWindowPtr); 523 } 524 } 525 526 // 2. Configure stuff 527 updateIconImages(); 528 updateFocusabilityForAutoRequestFocus(false); 529 530 // 3. Manage the extended state when hiding 531 if (!visible) { 532 // Cancel out the current native state of the window 533 switch (peer.getState()) { 534 case Frame.ICONIFIED: 535 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 536 break; 537 case Frame.MAXIMIZED_BOTH: 538 zoom(); 539 break; 540 } 541 } 542 543 // 4. Actually show or hide the window 544 LWWindowPeer blocker = peer.getBlocker(); 545 if (blocker == null || !visible) { 546 // If it ain't blocked, or is being hidden, go regular way 547 if (visible) { 548 CWrapper.NSWindow.makeFirstResponder(nsWindowPtr, contentView.getAWTView()); 549 550 boolean isPopup = (target.getType() == Window.Type.POPUP); 551 if (isPopup) { 552 // Popups in applets don't activate applet's process 553 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 554 } else { 555 CWrapper.NSWindow.orderFront(nsWindowPtr); 556 } 557 558 boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(nsWindowPtr); 559 if (!isKeyWindow) { 560 CWrapper.NSWindow.makeKeyWindow(nsWindowPtr); 561 } 562 } else { 563 CWrapper.NSWindow.orderOut(nsWindowPtr); 564 } 565 } else { 566 // otherwise, put it in a proper z-order 567 CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowBelow, 568 ((CPlatformWindow)blocker.getPlatformWindow()).getNSWindowPtr()); 569 } 570 this.visible = visible; 571 572 // 5. Manage the extended state when showing 573 if (visible) { 574 // Re-apply the extended state as expected in shared code 575 if (target instanceof Frame) { 576 switch (((Frame)target).getExtendedState()) { 577 case Frame.ICONIFIED: 578 CWrapper.NSWindow.miniaturize(nsWindowPtr); 579 break; 580 case Frame.MAXIMIZED_BOTH: 581 zoom(); 582 break; 583 } 584 } 585 } 586 587 // 6. Configure stuff #2 588 updateFocusabilityForAutoRequestFocus(true); 589 590 // 7. Manage parent-child relationship when showing 591 if (visible) { 592 // 7a. Add myself as a child 593 if (owner != null && owner.isVisible()) { 594 CWrapper.NSWindow.addChildWindow(owner.getNSWindowPtr(), nsWindowPtr, CWrapper.NSWindow.NSWindowAbove); 595 if (target.isAlwaysOnTop()) { 596 CWrapper.NSWindow.setLevel(nsWindowPtr, CWrapper.NSWindow.NSFloatingWindowLevel); 597 } 598 } 599 600 // 7b. Add my own children to myself 601 for (Window w : target.getOwnedWindows()) { 602 WindowPeer p = (WindowPeer)w.getPeer(); 603 if (p instanceof LWWindowPeer) { 604 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 605 if (pw != null && pw.isVisible()) { 606 CWrapper.NSWindow.addChildWindow(nsWindowPtr, pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove); 607 if (w.isAlwaysOnTop()) { 608 CWrapper.NSWindow.setLevel(pw.getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 609 } 610 } 611 } 612 } 613 } 614 615 // 8. Deal with the blocker of the window being shown 616 if (blocker != null && visible) { 617 // Make sure the blocker is above its siblings 618 ((CPlatformWindow)blocker.getPlatformWindow()).orderAboveSiblings(); 619 } 620 } 621 622 @Override // PlatformWindow 623 public void setTitle(String title) { 624 nativeSetNSWindowTitle(getNSWindowPtr(), title); 625 } 626 627 // Should be called on every window key property change. 628 @Override // PlatformWindow 629 public void updateIconImages() { 630 final long nsWindowPtr = getNSWindowPtr(); 631 final CImage cImage = getImageForTarget(); 632 nativeSetNSWindowMinimizedIcon(nsWindowPtr, cImage == null ? 0L : cImage.ptr); 633 } 634 635 public long getNSWindowPtr() { 636 final long nsWindowPtr = ptr; 637 if (nsWindowPtr == 0L) { 638 if(logger.isLoggable(PlatformLogger.FINE)) { 639 logger.fine("NSWindow already disposed?", new Exception("Pointer to native NSWindow is invalid.")); 640 } 641 } 642 return nsWindowPtr; 643 } 644 645 public SurfaceData getSurfaceData() { 646 return contentView.getSurfaceData(); 647 } 648 649 @Override // PlatformWindow 650 public void toBack() { 651 final long nsWindowPtr = getNSWindowPtr(); 652 nativePushNSWindowToBack(nsWindowPtr); 653 } 654 655 @Override // PlatformWindow 656 public void toFront() { 657 final long nsWindowPtr = getNSWindowPtr(); 658 updateFocusabilityForAutoRequestFocus(false); 659 nativePushNSWindowToFront(nsWindowPtr); 660 updateFocusabilityForAutoRequestFocus(true); 661 } 662 663 @Override 664 public void setResizable(boolean resizable) { 665 setStyleBits(RESIZABLE, resizable); 666 667 // Re-apply the size constraints and the size to ensure the space 668 // occupied by the grow box is counted properly 669 setMinimumSize(1, 1); // the method ignores its arguments 670 671 Rectangle bounds = peer.getBounds(); 672 setBounds(bounds.x, bounds.y, bounds.width, bounds.height); 673 } 674 675 @Override 676 public void setMinimumSize(int width, int height) { 677 //TODO width, height should be used 678 //NOTE: setResizable() calls setMinimumSize(1,1) relaying on the logic below 679 final long nsWindowPtr = getNSWindowPtr(); 680 final Dimension min = target.getMinimumSize(); 681 final Dimension max = target.getMaximumSize(); 682 nativeSetNSWindowMinMax(nsWindowPtr, min.getWidth(), min.getHeight(), max.getWidth(), max.getHeight()); 683 } 684 685 @Override 686 public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { 687 // Cross-app activation requests are not allowed. 688 if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && 689 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) 690 { 691 focusLogger.fine("the app is inactive, so the request is rejected"); 692 return true; 693 } 694 return false; 695 } 696 697 @Override 698 public boolean requestWindowFocus() { 699 700 long ptr = getNSWindowPtr(); 701 if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) { 702 CWrapper.NSWindow.makeMainWindow(ptr); 703 } 704 CWrapper.NSWindow.makeKeyAndOrderFront(ptr); 705 return true; 706 } 707 708 @Override 709 public boolean isActive() { 710 long ptr = getNSWindowPtr(); 711 return CWrapper.NSWindow.isKeyWindow(ptr); 712 } 713 714 @Override 715 public void updateFocusableWindowState() { 716 final boolean isFocusable = isNativelyFocusableWindow(); 717 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 718 } 719 720 @Override 721 public Graphics transformGraphics(Graphics g) { 722 // is this where we can inject a transform for HiDPI? 723 return g; 724 } 725 726 @Override 727 public void setAlwaysOnTop(boolean isAlwaysOnTop) { 728 setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop); 729 } 730 731 @Override 732 public void setOpacity(float opacity) { 733 CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity); 734 } 735 736 @Override 737 public void setOpaque(boolean isOpaque) { 738 CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque); 739 if (!isOpaque) { 740 long clearColor = CWrapper.NSColor.clearColor(); 741 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), clearColor); 742 } 743 } 744 745 @Override 746 public void enterFullScreenMode() { 747 isFullScreenMode = true; 748 contentView.enterFullScreenMode(getNSWindowPtr()); 749 } 750 751 @Override 752 public void exitFullScreenMode() { 753 contentView.exitFullScreenMode(); 754 isFullScreenMode = false; 755 } 756 757 @Override 758 public void setWindowState(int windowState) { 759 if (!peer.isVisible()) { 760 // setVisible() applies the state 761 return; 762 } 763 764 int prevWindowState = peer.getState(); 765 if (prevWindowState == windowState) return; 766 767 final long nsWindowPtr = getNSWindowPtr(); 768 switch (windowState) { 769 case Frame.ICONIFIED: 770 if (prevWindowState == Frame.MAXIMIZED_BOTH) { 771 // let's return into the normal states first 772 // the zoom call toggles between the normal and the max states 773 zoom(); 774 } 775 CWrapper.NSWindow.miniaturize(nsWindowPtr); 776 break; 777 case Frame.MAXIMIZED_BOTH: 778 if (prevWindowState == Frame.ICONIFIED) { 779 // let's return into the normal states first 780 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 781 } 782 zoom(); 783 break; 784 case Frame.NORMAL: 785 if (prevWindowState == Frame.ICONIFIED) { 786 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 787 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) { 788 // the zoom call toggles between the normal and the max states 789 zoom(); 790 } 791 break; 792 default: 793 throw new RuntimeException("Unknown window state: " + windowState); 794 } 795 796 // NOTE: the SWP.windowState field gets updated to the newWindowState 797 // value when the native notification comes to us 798 } 799 800 @Override 801 public void modallyBlocked() { 802 nativeModallyBlocked(getNSWindowPtr()); 803 } 804 805 @Override 806 public void modallyUnblocked() { 807 nativeModallyUnblocked(getNSWindowPtr()); 808 } 809 810 // ---------------------------------------------------------------------- 811 // UTILITY METHODS 812 // ---------------------------------------------------------------------- 813 814 /* 815 * Find image to install into Title or into Application icon. 816 * First try icons installed for toplevel. If there is no icon 817 * use default Duke image. 818 * This method shouldn't return null. 819 */ 820 private CImage getImageForTarget() { 821 List<Image> icons = target.getIconImages(); 822 if (icons == null || icons.size() == 0) { 823 return null; 824 } 825 return CImage.getCreator().createFromImages(icons); 826 } 827 828 /* 829 * Returns LWWindowPeer associated with this delegate. 830 */ 831 @Override 832 public LWWindowPeer getPeer() { 833 return peer; 834 } 835 836 public CPlatformView getContentView() { 837 return contentView; 838 } 839 840 @Override 841 public long getLayerPtr() { 842 return contentView.getWindowLayerPtr(); 843 } 844 845 private void validateSurface() { 846 SurfaceData surfaceData = getSurfaceData(); 847 if (surfaceData instanceof CGLSurfaceData) { 848 ((CGLSurfaceData)surfaceData).validate(); 849 } 850 } 851 852 /************************************************************* 853 * Callbacks from the AWTWindow and AWTView objc classes. 854 *************************************************************/ 855 private void deliverWindowFocusEvent(boolean gained){ 856 // Fix for 7150349: ingore "gained" notifications when the app is inactive. 857 if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) { 858 focusLogger.fine("the app is inactive, so the notification is ignored"); 859 return; 860 } 861 peer.notifyActivation(gained); 862 } 863 864 private void deliverMoveResizeEvent(int x, int y, int width, int height) { 865 // when the content view enters the full-screen mode, the native 866 // move/resize notifications contain a bounds smaller than 867 // the whole screen and therefore we ignore the native notifications 868 // and the content view itself creates correct synthetic notifications 869 if (isFullScreenMode) return; 870 871 nativeBounds = new Rectangle(x, y, width, height); 872 peer.notifyReshape(x, y, width, height); 873 //TODO validateSurface already called from notifyReshape 874 validateSurface(); 875 } 876 877 private void deliverWindowClosingEvent() { 878 if (peer.getBlocker() == null) { 879 peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING)); 880 } 881 } 882 883 private void deliverIconify(final boolean iconify) { 884 peer.notifyIconify(iconify); 885 } 886 887 private void deliverZoom(final boolean isZoomed) { 888 peer.notifyZoom(isZoomed); 889 } 890 891 private void deliverNCMouseDown() { 892 peer.notifyNCMouseDown(); 893 } 894 895 /* 896 * Our focus model is synthetic and only non-simple window 897 * may become natively focusable window. 898 */ 899 private boolean isNativelyFocusableWindow() { 900 return !peer.isSimpleWindow() && target.getFocusableWindowState(); 901 } 902 903 /* 904 * An utility method for the support of the auto request focus. 905 * Updates the focusable state of the window under certain 906 * circumstances. 907 */ 908 private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) { 909 if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return; 910 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 911 } 912 913 private boolean checkBlocking() { 914 LWWindowPeer blocker = peer.getBlocker(); 915 if (blocker == null) { 916 return false; 917 } 918 919 CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow(); 920 921 pWindow.orderAboveSiblings(); 922 923 final long nsWindowPtr = pWindow.getNSWindowPtr(); 924 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 925 CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr); 926 CWrapper.NSWindow.makeMainWindow(nsWindowPtr); 927 928 return true; 929 } 930 931 private void orderAboveSiblings() { 932 if (owner == null) { 933 return; 934 } 935 936 // NOTE: the logic will fail if we have a hierarchy like: 937 // visible root owner 938 // invisible owner 939 // visible dialog 940 // However, this is an unlikely scenario for real life apps 941 if (owner.isVisible()) { 942 // Recursively pop up the windows from the very bottom so that only 943 // the very top-most one becomes the main window 944 owner.orderAboveSiblings(); 945 946 // Order the window to front of the stack of child windows 947 final long nsWindowSelfPtr = getNSWindowPtr(); 948 final long nsWindowOwnerPtr = owner.getNSWindowPtr(); 949 CWrapper.NSWindow.removeChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr); 950 CWrapper.NSWindow.addChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr, CWrapper.NSWindow.NSWindowAbove); 951 } 952 953 if (target.isAlwaysOnTop()) { 954 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 955 } 956 } 957 958 // ---------------------------------------------------------------------- 959 // NATIVE CALLBACKS 960 // ---------------------------------------------------------------------- 961 962 private void windowDidBecomeMain() { 963 assert CThreading.assertAppKit(); 964 965 if (checkBlocking()) return; 966 // If it's not blocked, make sure it's above its siblings 967 orderAboveSiblings(); 968 } 969 970 private void updateDisplay() { 971 EventQueue.invokeLater(new Runnable() { 972 public void run() { 973 validateSurface(); 974 } 975 }); 976 } 977 978 private void updateWindowContent() { 979 ComponentEvent resizeEvent = new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED); 980 SunToolkit.postEvent(SunToolkit.targetToAppContext(target), resizeEvent); 981 } 982 983 private void windowWillEnterFullScreen() { 984 updateWindowContent(); 985 } 986 private void windowDidEnterFullScreen() { 987 updateDisplay(); 988 } 989 private void windowWillExitFullScreen() { 990 updateWindowContent(); 991 } 992 private void windowDidExitFullScreen() {} 993 }