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