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