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