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 666 @Override 667 public void setMinimumSize(int width, int height) { 668 //TODO width, height should be used 669 final long nsWindowPtr = getNSWindowPtr(); 670 final Dimension min = target.getMinimumSize(); 671 final Dimension max = target.getMaximumSize(); 672 nativeSetNSWindowMinMax(nsWindowPtr, min.getWidth(), min.getHeight(), max.getWidth(), max.getHeight()); 673 } 674 675 @Override 676 public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { 677 // Cross-app activation requests are not allowed. 678 if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && 679 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) 680 { 681 focusLogger.fine("the app is inactive, so the request is rejected"); 682 return true; 683 } 684 return false; 685 } 686 687 @Override 688 public boolean requestWindowFocus() { 689 690 long ptr = getNSWindowPtr(); 691 if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) { 692 CWrapper.NSWindow.makeMainWindow(ptr); 693 } 694 CWrapper.NSWindow.makeKeyAndOrderFront(ptr); 695 return true; 696 } 697 698 @Override 699 public boolean isActive() { 700 long ptr = getNSWindowPtr(); 701 return CWrapper.NSWindow.isKeyWindow(ptr); 702 } 703 704 @Override 705 public void updateFocusableWindowState() { 706 final boolean isFocusable = isNativelyFocusableWindow(); 707 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 708 } 709 710 @Override 711 public Graphics transformGraphics(Graphics g) { 712 // is this where we can inject a transform for HiDPI? 713 return g; 714 } 715 716 @Override 717 public void setAlwaysOnTop(boolean isAlwaysOnTop) { 718 setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop); 719 } 720 721 @Override 722 public void setOpacity(float opacity) { 723 CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity); 724 } 725 726 @Override 727 public void setOpaque(boolean isOpaque) { 728 CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque); 729 if (!isOpaque) { 730 long clearColor = CWrapper.NSColor.clearColor(); 731 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), clearColor); 732 } 733 } 734 735 @Override 736 public void enterFullScreenMode() { 737 isFullScreenMode = true; 738 contentView.enterFullScreenMode(getNSWindowPtr()); 739 } 740 741 @Override 742 public void exitFullScreenMode() { 743 contentView.exitFullScreenMode(); 744 isFullScreenMode = false; 745 } 746 747 @Override 748 public void setWindowState(int windowState) { 749 if (!peer.isVisible()) { 750 // setVisible() applies the state 751 return; 752 } 753 754 int prevWindowState = peer.getState(); 755 if (prevWindowState == windowState) return; 756 757 final long nsWindowPtr = getNSWindowPtr(); 758 switch (windowState) { 759 case Frame.ICONIFIED: 760 if (prevWindowState == Frame.MAXIMIZED_BOTH) { 761 // let's return into the normal states first 762 // the zoom call toggles between the normal and the max states 763 zoom(); 764 } 765 CWrapper.NSWindow.miniaturize(nsWindowPtr); 766 break; 767 case Frame.MAXIMIZED_BOTH: 768 if (prevWindowState == Frame.ICONIFIED) { 769 // let's return into the normal states first 770 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 771 } 772 zoom(); 773 break; 774 case Frame.NORMAL: 775 if (prevWindowState == Frame.ICONIFIED) { 776 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 777 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) { 778 // the zoom call toggles between the normal and the max states 779 zoom(); 780 } 781 break; 782 default: 783 throw new RuntimeException("Unknown window state: " + windowState); 784 } 785 786 // NOTE: the SWP.windowState field gets updated to the newWindowState 787 // value when the native notification comes to us 788 } 789 790 // ---------------------------------------------------------------------- 791 // UTILITY METHODS 792 // ---------------------------------------------------------------------- 793 794 /* 795 * Find image to install into Title or into Application icon. 796 * First try icons installed for toplevel. If there is no icon 797 * use default Duke image. 798 * This method shouldn't return null. 799 */ 800 private CImage getImageForTarget() { 801 List<Image> icons = target.getIconImages(); 802 if (icons == null || icons.size() == 0) { 803 return null; 804 } 805 806 // TODO: need a walk-through to find the best image. 807 // The best mean with higher resolution. Otherwise an icon looks bad. 808 final Image image = icons.get(0); 809 return CImage.getCreator().createFromImage(image); 810 } 811 812 /* 813 * Returns LWWindowPeer associated with this delegate. 814 */ 815 @Override 816 public LWWindowPeer getPeer() { 817 return peer; 818 } 819 820 public CPlatformView getContentView() { 821 return contentView; 822 } 823 824 @Override 825 public long getLayerPtr() { 826 return contentView.getWindowLayerPtr(); 827 } 828 829 private void validateSurface() { 830 SurfaceData surfaceData = getSurfaceData(); 831 if (surfaceData instanceof CGLSurfaceData) { 832 ((CGLSurfaceData)surfaceData).validate(); 833 } 834 } 835 836 /************************************************************* 837 * Callbacks from the AWTWindow and AWTView objc classes. 838 *************************************************************/ 839 private void deliverWindowFocusEvent(boolean gained){ 840 // Fix for 7150349: ingore "gained" notifications when the app is inactive. 841 if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) { 842 focusLogger.fine("the app is inactive, so the notification is ignored"); 843 return; 844 } 845 peer.notifyActivation(gained); 846 } 847 848 private void deliverMoveResizeEvent(int x, int y, int width, int height) { 849 // when the content view enters the full-screen mode, the native 850 // move/resize notifications contain a bounds smaller than 851 // the whole screen and therefore we ignore the native notifications 852 // and the content view itself creates correct synthetic notifications 853 if (isFullScreenMode) return; 854 855 nativeBounds = new Rectangle(x, y, width, height); 856 peer.notifyReshape(x, y, width, height); 857 //TODO validateSurface already called from notifyReshape 858 validateSurface(); 859 } 860 861 private void deliverWindowClosingEvent() { 862 if (peer.getBlocker() == null) { 863 peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING)); 864 } 865 } 866 867 private void deliverIconify(final boolean iconify) { 868 peer.notifyIconify(iconify); 869 } 870 871 private void deliverZoom(final boolean isZoomed) { 872 peer.notifyZoom(isZoomed); 873 } 874 875 private void deliverNCMouseDown() { 876 peer.notifyNCMouseDown(); 877 } 878 879 /* 880 * Our focus model is synthetic and only non-simple window 881 * may become natively focusable window. 882 */ 883 private boolean isNativelyFocusableWindow() { 884 return !peer.isSimpleWindow() && target.getFocusableWindowState(); 885 } 886 887 /* 888 * An utility method for the support of the auto request focus. 889 * Updates the focusable state of the window under certain 890 * circumstances. 891 */ 892 private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) { 893 if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return; 894 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 895 } 896 897 private boolean checkBlocking() { 898 LWWindowPeer blocker = peer.getBlocker(); 899 if (blocker == null) { 900 return false; 901 } 902 903 CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow(); 904 905 pWindow.orderAboveSiblings(); 906 907 final long nsWindowPtr = pWindow.getNSWindowPtr(); 908 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 909 CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr); 910 CWrapper.NSWindow.makeMainWindow(nsWindowPtr); 911 912 return true; 913 } 914 915 private void orderAboveSiblings() { 916 if (owner == null) { 917 return; 918 } 919 920 // NOTE: the logic will fail if we have a hierarchy like: 921 // visible root owner 922 // invisible owner 923 // visible dialog 924 // However, this is an unlikely scenario for real life apps 925 if (owner.isVisible()) { 926 // Recursively pop up the windows from the very bottom so that only 927 // the very top-most one becomes the main window 928 owner.orderAboveSiblings(); 929 930 // Order the window to front of the stack of child windows 931 final long nsWindowSelfPtr = getNSWindowPtr(); 932 final long nsWindowOwnerPtr = owner.getNSWindowPtr(); 933 CWrapper.NSWindow.removeChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr); 934 CWrapper.NSWindow.addChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr, CWrapper.NSWindow.NSWindowAbove); 935 } 936 937 if (target.isAlwaysOnTop()) { 938 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 939 } 940 } 941 942 // ---------------------------------------------------------------------- 943 // NATIVE CALLBACKS 944 // ---------------------------------------------------------------------- 945 946 private void windowDidBecomeMain() { 947 assert CThreading.assertAppKit(); 948 949 if (checkBlocking()) return; 950 // If it's not blocked, make sure it's above its siblings 951 orderAboveSiblings(); 952 } 953 954 private void updateDisplay() { 955 EventQueue.invokeLater(new Runnable() { 956 public void run() { 957 validateSurface(); 958 } 959 }); 960 } 961 962 private void updateWindowContent() { 963 ComponentEvent resizeEvent = new ComponentEvent(target, ComponentEvent.COMPONENT_RESIZED); 964 SunToolkit.postEvent(SunToolkit.targetToAppContext(target), resizeEvent); 965 } 966 967 private void windowWillEnterFullScreen() { 968 updateWindowContent(); 969 } 970 private void windowDidEnterFullScreen() { 971 updateDisplay(); 972 } 973 private void windowWillExitFullScreen() { 974 updateWindowContent(); 975 } 976 private void windowDidExitFullScreen() {} 977 }