1 /* 2 * Copyright (c) 2011, 2013, 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.*; 29 import java.awt.Dialog.ModalityType; 30 import java.awt.event.*; 31 import java.awt.peer.WindowPeer; 32 import java.beans.*; 33 import java.lang.reflect.InvocationTargetException; 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.util.logging.PlatformLogger; 43 44 import com.apple.laf.*; 45 import com.apple.laf.ClientPropertyApplicator.Property; 46 import com.sun.awt.AWTUtilities; 47 48 public class CPlatformWindow extends CFRetainedResource implements PlatformWindow { 49 private native long nativeCreateNSWindow(long nsViewPtr, long styleBits, double x, double y, double w, double h); 50 private static native void nativeSetNSWindowStyleBits(long nsWindowPtr, int mask, int data); 51 private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr); 52 private static native Insets nativeGetNSWindowInsets(long nsWindowPtr); 53 private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h); 54 private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH); 55 private static native void nativePushNSWindowToBack(long nsWindowPtr); 56 private static native void nativePushNSWindowToFront(long nsWindowPtr); 57 private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title); 58 private static native void nativeRevalidateNSWindowShadow(long nsWindowPtr); 59 private static native void nativeSetNSWindowMinimizedIcon(long nsWindowPtr, long nsImage); 60 private static native void nativeSetNSWindowRepresentedFilename(long nsWindowPtr, String representedFilename); 61 private static native void nativeSetEnabled(long nsWindowPtr, boolean isEnabled); 62 private static native void nativeSynthesizeMouseEnteredExitedEvents(); 63 private static native void nativeDispose(long nsWindowPtr); 64 private static native CPlatformWindow nativeGetTopmostPlatformWindowUnderMouse(); 65 66 // Loger to report issues happened during execution but that do not affect functionality 67 private static final PlatformLogger logger = PlatformLogger.getLogger("sun.lwawt.macosx.CPlatformWindow"); 68 private static final PlatformLogger focusLogger = PlatformLogger.getLogger("sun.lwawt.macosx.focus.CPlatformWindow"); 69 70 // for client properties 71 public static final String WINDOW_BRUSH_METAL_LOOK = "apple.awt.brushMetalLook"; 72 public static final String WINDOW_DRAGGABLE_BACKGROUND = "apple.awt.draggableWindowBackground"; 73 74 public static final String WINDOW_ALPHA = "Window.alpha"; 75 public static final String WINDOW_SHADOW = "Window.shadow"; 76 77 public static final String WINDOW_STYLE = "Window.style"; 78 public static final String WINDOW_SHADOW_REVALIDATE_NOW = "apple.awt.windowShadow.revalidateNow"; 79 80 public static final String WINDOW_DOCUMENT_MODIFIED = "Window.documentModified"; 81 public static final String WINDOW_DOCUMENT_FILE = "Window.documentFile"; 82 83 public static final String WINDOW_CLOSEABLE = "Window.closeable"; 84 public static final String WINDOW_MINIMIZABLE = "Window.minimizable"; 85 public static final String WINDOW_ZOOMABLE = "Window.zoomable"; 86 public static final String WINDOW_HIDES_ON_DEACTIVATE="Window.hidesOnDeactivate"; 87 88 public static final String WINDOW_DOC_MODAL_SHEET = "apple.awt.documentModalSheet"; 89 public static final String WINDOW_FADE_DELEGATE = "apple.awt._windowFadeDelegate"; 90 public static final String WINDOW_FADE_IN = "apple.awt._windowFadeIn"; 91 public static final String WINDOW_FADE_OUT = "apple.awt._windowFadeOut"; 92 public static final String WINDOW_FULLSCREENABLE = "apple.awt.fullscreenable"; 93 94 95 // Yeah, I know. But it's easier to deal with ints from JNI 96 static final int MODELESS = 0; 97 static final int DOCUMENT_MODAL = 1; 98 static final int APPLICATION_MODAL = 2; 99 static final int TOOLKIT_MODAL = 3; 100 101 // window style bits 102 static final int _RESERVED_FOR_DATA = 1 << 0; 103 104 // corresponds to native style mask bits 105 static final int DECORATED = 1 << 1; 106 static final int TEXTURED = 1 << 2; 107 static final int UNIFIED = 1 << 3; 108 static final int UTILITY = 1 << 4; 109 static final int HUD = 1 << 5; 110 static final int SHEET = 1 << 6; 111 112 static final int CLOSEABLE = 1 << 7; 113 static final int MINIMIZABLE = 1 << 8; 114 115 static final int RESIZABLE = 1 << 9; // both a style bit and prop bit 116 static final int NONACTIVATING = 1 << 24; 117 static final int IS_DIALOG = 1 << 25; 118 static final int IS_MODAL = 1 << 26; 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 = new Rectangle(0, 0, 0, 0); 201 private volatile boolean isFullScreenMode; 202 private boolean isFullScreenAnimationOn; 203 204 private Window target; 205 private LWWindowPeer peer; 206 private CPlatformView contentView; 207 private CPlatformWindow owner; 208 private boolean visible = false; // visibility status from native perspective 209 private boolean undecorated; // initialized in getInitialStyleBits() 210 private Rectangle normalBounds = null; // not-null only for undecorated maximized windows 211 private CPlatformResponder responder; 212 private volatile boolean zoomed = false; // from native perspective 213 214 public CPlatformWindow() { 215 super(0, true); 216 } 217 218 /* 219 * Delegate initialization (create native window and all the 220 * related resources). 221 */ 222 @Override // PlatformWindow 223 public void initialize(Window _target, LWWindowPeer _peer, PlatformWindow _owner) { 224 initializeBase(_target, _peer, _owner, new CPlatformView()); 225 226 final int styleBits = getInitialStyleBits(); 227 228 // TODO: handle these misc properties 229 final long parentNSWindowPtr = (owner != null ? owner.getNSWindowPtr() : 0); 230 String warningString = target.getWarningString(); 231 232 responder = new CPlatformResponder(peer, false); 233 contentView.initialize(peer, responder); 234 235 final long nativeWindowPtr = nativeCreateNSWindow(contentView.getAWTView(), styleBits, 0, 0, 0, 0); 236 setPtr(nativeWindowPtr); 237 238 // TODO: implement on top of JObjC bridged class 239 // NSWindow window = JObjC.getInstance().AppKit().NSWindow().getInstance(nativeWindowPtr, JObjCRuntime.getInstance()); 240 241 if (target instanceof javax.swing.RootPaneContainer) { 242 final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 243 if (rootpane != null) rootpane.addPropertyChangeListener("ancestor", new PropertyChangeListener() { 244 public void propertyChange(final PropertyChangeEvent evt) { 245 CLIENT_PROPERTY_APPLICATOR.attachAndApplyClientProperties(rootpane); 246 rootpane.removePropertyChangeListener("ancestor", this); 247 } 248 }); 249 } 250 251 validateSurface(); 252 } 253 254 protected void initializeBase(Window target, LWWindowPeer peer, PlatformWindow owner, CPlatformView view) { 255 this.peer = peer; 256 this.target = target; 257 if (owner instanceof CPlatformWindow) { 258 this.owner = (CPlatformWindow)owner; 259 } 260 this.contentView = view; 261 } 262 263 private int getInitialStyleBits() { 264 // defaults style bits 265 int styleBits = DECORATED | HAS_SHADOW | CLOSEABLE | MINIMIZABLE | ZOOMABLE | RESIZABLE; 266 267 if (isNativelyFocusableWindow()) { 268 styleBits = SET(styleBits, SHOULD_BECOME_KEY, true); 269 styleBits = SET(styleBits, SHOULD_BECOME_MAIN, true); 270 } 271 272 final boolean isFrame = (target instanceof Frame); 273 final boolean isDialog = (target instanceof Dialog); 274 final boolean isPopup = (target.getType() == Window.Type.POPUP); 275 if (isDialog) { 276 styleBits = SET(styleBits, MINIMIZABLE, false); 277 } 278 279 // Either java.awt.Frame or java.awt.Dialog can be undecorated, however java.awt.Window always is undecorated. 280 { 281 this.undecorated = isFrame ? ((Frame)target).isUndecorated() : (isDialog ? ((Dialog)target).isUndecorated() : true); 282 if (this.undecorated) styleBits = SET(styleBits, DECORATED, false); 283 } 284 285 // Either java.awt.Frame or java.awt.Dialog can be resizable, however java.awt.Window is never resizable 286 { 287 final boolean resizable = isFrame ? ((Frame)target).isResizable() : (isDialog ? ((Dialog)target).isResizable() : false); 288 styleBits = SET(styleBits, RESIZABLE, resizable); 289 if (!resizable) { 290 styleBits = SET(styleBits, ZOOMABLE, false); 291 } 292 } 293 294 if (target.isAlwaysOnTop()) { 295 styleBits = SET(styleBits, ALWAYS_ON_TOP, true); 296 } 297 298 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 299 styleBits = SET(styleBits, MODAL_EXCLUDED, true); 300 } 301 302 // If the target is a dialog, popup or tooltip we want it to ignore the brushed metal look. 303 if (isPopup) { 304 styleBits = SET(styleBits, TEXTURED, false); 305 // Popups in applets don't activate applet's process 306 styleBits = SET(styleBits, NONACTIVATING, true); 307 } 308 309 if (Window.Type.UTILITY.equals(target.getType())) { 310 styleBits = SET(styleBits, UTILITY, 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 if (isDialog) { 379 styleBits = SET(styleBits, IS_DIALOG, true); 380 if (((Dialog) target).isModal()) { 381 styleBits = SET(styleBits, IS_MODAL, true); 382 } 383 } 384 385 peer.setTextured(IS(TEXTURED, styleBits)); 386 387 return styleBits; 388 } 389 390 // this is the counter-point to -[CWindow _nativeSetStyleBit:] 391 private void setStyleBits(final int mask, final boolean value) { 392 nativeSetNSWindowStyleBits(getNSWindowPtr(), mask, value ? mask : 0); 393 } 394 395 private native void _toggleFullScreenMode(final long model); 396 397 public void toggleFullScreen() { 398 _toggleFullScreenMode(getNSWindowPtr()); 399 } 400 401 @Override // PlatformWindow 402 public void setMenuBar(MenuBar mb) { 403 final long nsWindowPtr = getNSWindowPtr(); 404 CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb); 405 if (mbPeer != null) { 406 nativeSetNSWindowMenuBar(nsWindowPtr, mbPeer.getModel()); 407 } else { 408 nativeSetNSWindowMenuBar(nsWindowPtr, 0); 409 } 410 } 411 412 @Override // PlatformWindow 413 public void dispose() { 414 if (owner != null) { 415 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), getNSWindowPtr()); 416 } 417 contentView.dispose(); 418 nativeDispose(getNSWindowPtr()); 419 CPlatformWindow.super.dispose(); 420 } 421 422 @Override // PlatformWindow 423 public FontMetrics getFontMetrics(Font f) { 424 // TODO: not implemented 425 (new RuntimeException("unimplemented")).printStackTrace(); 426 return null; 427 } 428 429 @Override // PlatformWindow 430 public Insets getInsets() { 431 if (!isFullScreenMode) { 432 return nativeGetNSWindowInsets(getNSWindowPtr()); 433 } 434 return new Insets(0, 0, 0, 0); 435 } 436 437 @Override // PlatformWindow 438 public Point getLocationOnScreen() { 439 return new Point(nativeBounds.x, nativeBounds.y); 440 } 441 442 @Override 443 public GraphicsDevice getGraphicsDevice() { 444 return contentView.getGraphicsDevice(); 445 } 446 447 @Override // PlatformWindow 448 public SurfaceData getScreenSurface() { 449 // TODO: not implemented 450 return null; 451 } 452 453 @Override // PlatformWindow 454 public SurfaceData replaceSurfaceData() { 455 return contentView.replaceSurfaceData(); 456 } 457 458 @Override // PlatformWindow 459 public void setBounds(int x, int y, int w, int h) { 460 // assert CThreading.assertEventQueue(); 461 nativeSetNSWindowBounds(getNSWindowPtr(), x, y, w, h); 462 } 463 464 private boolean isMaximized() { 465 return undecorated ? this.normalBounds != null : zoomed; 466 } 467 468 private void maximize() { 469 if (isMaximized()) { 470 return; 471 } 472 if (!undecorated) { 473 zoomed = true; 474 CWrapper.NSWindow.zoom(getNSWindowPtr()); 475 } else { 476 deliverZoom(true); 477 478 this.normalBounds = peer.getBounds(); 479 long screen = CWrapper.NSWindow.screen(getNSWindowPtr()); 480 Rectangle toBounds = CWrapper.NSScreen.visibleFrame(screen).getBounds(); 481 // Flip the y coordinate 482 Rectangle frame = CWrapper.NSScreen.frame(screen).getBounds(); 483 toBounds.y = frame.height - toBounds.y - toBounds.height; 484 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 485 } 486 } 487 488 private void unmaximize() { 489 if (!isMaximized()) { 490 return; 491 } 492 if (!undecorated) { 493 zoomed = false; 494 CWrapper.NSWindow.zoom(getNSWindowPtr()); 495 } else { 496 deliverZoom(false); 497 498 Rectangle toBounds = this.normalBounds; 499 this.normalBounds = null; 500 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 501 } 502 } 503 504 private boolean isVisible() { 505 return this.visible; 506 } 507 508 @Override // PlatformWindow 509 public void setVisible(boolean visible) { 510 final long nsWindowPtr = getNSWindowPtr(); 511 512 // Process parent-child relationship when hiding 513 if (!visible) { 514 // 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 // Unparent myself 526 if (owner != null && owner.isVisible()) { 527 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), nsWindowPtr); 528 } 529 } 530 531 // Configure stuff 532 updateIconImages(); 533 updateFocusabilityForAutoRequestFocus(false); 534 535 // Actually show or hide the window 536 LWWindowPeer blocker = peer.getBlocker(); 537 if (blocker == null || !visible) { 538 // If it ain't blocked, or is being hidden, go regular way 539 if (visible) { 540 CWrapper.NSWindow.makeFirstResponder(nsWindowPtr, contentView.getAWTView()); 541 542 boolean isPopup = (target.getType() == Window.Type.POPUP); 543 if (isPopup) { 544 // Popups in applets don't activate applet's process 545 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 546 } else { 547 CWrapper.NSWindow.orderFront(nsWindowPtr); 548 } 549 550 boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(nsWindowPtr); 551 if (!isKeyWindow) { 552 CWrapper.NSWindow.makeKeyWindow(nsWindowPtr); 553 } 554 } else { 555 CWrapper.NSWindow.orderOut(nsWindowPtr); 556 } 557 } else { 558 // otherwise, put it in a proper z-order 559 CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowBelow, 560 ((CPlatformWindow)blocker.getPlatformWindow()).getNSWindowPtr()); 561 } 562 this.visible = visible; 563 564 // Manage the extended state when showing 565 if (visible) { 566 // Apply the extended state as expected in shared code 567 if (target instanceof Frame) { 568 switch (((Frame)target).getExtendedState()) { 569 case Frame.ICONIFIED: 570 CWrapper.NSWindow.miniaturize(nsWindowPtr); 571 break; 572 case Frame.MAXIMIZED_BOTH: 573 maximize(); 574 break; 575 default: // NORMAL 576 unmaximize(); // in case it was maximized, otherwise this is a no-op 577 break; 578 } 579 } 580 } 581 582 nativeSynthesizeMouseEnteredExitedEvents(); 583 584 // Configure stuff #2 585 updateFocusabilityForAutoRequestFocus(true); 586 587 // Manage parent-child relationship when showing 588 if (visible) { 589 // Add myself as a child 590 if (owner != null && owner.isVisible()) { 591 CWrapper.NSWindow.addChildWindow(owner.getNSWindowPtr(), nsWindowPtr, CWrapper.NSWindow.NSWindowAbove); 592 if (target.isAlwaysOnTop()) { 593 CWrapper.NSWindow.setLevel(nsWindowPtr, CWrapper.NSWindow.NSFloatingWindowLevel); 594 } 595 } 596 597 // Add my own children to myself 598 for (Window w : target.getOwnedWindows()) { 599 WindowPeer p = (WindowPeer)w.getPeer(); 600 if (p instanceof LWWindowPeer) { 601 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 602 if (pw != null && pw.isVisible()) { 603 CWrapper.NSWindow.addChildWindow(nsWindowPtr, pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove); 604 if (w.isAlwaysOnTop()) { 605 CWrapper.NSWindow.setLevel(pw.getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 606 } 607 } 608 } 609 } 610 } 611 612 // Deal with the blocker of the window being shown 613 if (blocker != null && visible) { 614 // Make sure the blocker is above its siblings 615 ((CPlatformWindow)blocker.getPlatformWindow()).orderAboveSiblings(); 616 } 617 } 618 619 @Override // PlatformWindow 620 public void setTitle(String title) { 621 nativeSetNSWindowTitle(getNSWindowPtr(), title); 622 } 623 624 // Should be called on every window key property change. 625 @Override // PlatformWindow 626 public void updateIconImages() { 627 final long nsWindowPtr = getNSWindowPtr(); 628 final CImage cImage = getImageForTarget(); 629 nativeSetNSWindowMinimizedIcon(nsWindowPtr, cImage == null ? 0L : cImage.ptr); 630 } 631 632 public long getNSWindowPtr() { 633 final long nsWindowPtr = ptr; 634 if (nsWindowPtr == 0L) { 635 if(logger.isLoggable(PlatformLogger.FINE)) { 636 logger.fine("NSWindow already disposed?", new Exception("Pointer to native NSWindow is invalid.")); 637 } 638 } 639 return nsWindowPtr; 640 } 641 642 public SurfaceData getSurfaceData() { 643 return contentView.getSurfaceData(); 644 } 645 646 @Override // PlatformWindow 647 public void toBack() { 648 final long nsWindowPtr = getNSWindowPtr(); 649 nativePushNSWindowToBack(nsWindowPtr); 650 } 651 652 @Override // PlatformWindow 653 public void toFront() { 654 final long nsWindowPtr = getNSWindowPtr(); 655 updateFocusabilityForAutoRequestFocus(false); 656 nativePushNSWindowToFront(nsWindowPtr); 657 updateFocusabilityForAutoRequestFocus(true); 658 } 659 660 @Override 661 public void setResizable(final boolean resizable) { 662 setStyleBits(RESIZABLE, resizable); 663 } 664 665 @Override 666 public void setSizeConstraints(int minW, int minH, int maxW, int maxH) { 667 nativeSetNSWindowMinMax(getNSWindowPtr(), minW, minH, maxW, maxH); 668 } 669 670 @Override 671 public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { 672 // Cross-app activation requests are not allowed. 673 if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && 674 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) 675 { 676 focusLogger.fine("the app is inactive, so the request is rejected"); 677 return true; 678 } 679 return false; 680 } 681 682 @Override 683 public boolean requestWindowFocus() { 684 685 long ptr = getNSWindowPtr(); 686 if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) { 687 CWrapper.NSWindow.makeMainWindow(ptr); 688 } 689 CWrapper.NSWindow.makeKeyAndOrderFront(ptr); 690 return true; 691 } 692 693 @Override 694 public boolean isActive() { 695 long ptr = getNSWindowPtr(); 696 return CWrapper.NSWindow.isKeyWindow(ptr); 697 } 698 699 @Override 700 public void updateFocusableWindowState() { 701 final boolean isFocusable = isNativelyFocusableWindow(); 702 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 703 } 704 705 @Override 706 public Graphics transformGraphics(Graphics g) { 707 // is this where we can inject a transform for HiDPI? 708 return g; 709 } 710 711 @Override 712 public void setAlwaysOnTop(boolean isAlwaysOnTop) { 713 setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop); 714 } 715 716 public PlatformWindow getTopmostPlatformWindowUnderMouse(){ 717 return CPlatformWindow.nativeGetTopmostPlatformWindowUnderMouse(); 718 } 719 720 @Override 721 public void setOpacity(float opacity) { 722 CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity); 723 } 724 725 @Override 726 public void setOpaque(boolean isOpaque) { 727 CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque); 728 if (!isOpaque && !peer.isTextured()) { 729 long clearColor = CWrapper.NSColor.clearColor(); 730 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), clearColor); 731 } 732 733 //This is a temporary workaround. Looks like after 7124236 will be fixed 734 //the correct place for invalidateShadow() is CGLayer.drawInCGLContext. 735 SwingUtilities.invokeLater(new Runnable() { 736 @Override 737 public void run() { 738 invalidateShadow(); 739 } 740 }); 741 } 742 743 @Override 744 public void enterFullScreenMode() { 745 isFullScreenMode = true; 746 contentView.enterFullScreenMode(); 747 // the move/size notification from the underlying system comes 748 // but it contains a bounds smaller than the whole screen 749 // and therefore we need to create the synthetic notifications 750 Rectangle screenBounds; 751 final long screenPtr = CWrapper.NSWindow.screen(getNSWindowPtr()); 752 try { 753 screenBounds = CWrapper.NSScreen.frame(screenPtr).getBounds(); 754 } finally { 755 CWrapper.NSObject.release(screenPtr); 756 } 757 peer.notifyReshape(screenBounds.x, screenBounds.y, screenBounds.width, 758 screenBounds.height); 759 } 760 761 @Override 762 public void exitFullScreenMode() { 763 contentView.exitFullScreenMode(); 764 isFullScreenMode = false; 765 } 766 767 @Override 768 public void setWindowState(int windowState) { 769 if (!peer.isVisible()) { 770 // setVisible() applies the state 771 return; 772 } 773 774 int prevWindowState = peer.getState(); 775 if (prevWindowState == windowState) return; 776 777 final long nsWindowPtr = getNSWindowPtr(); 778 switch (windowState) { 779 case Frame.ICONIFIED: 780 if (prevWindowState == Frame.MAXIMIZED_BOTH) { 781 // let's return into the normal states first 782 // the zoom call toggles between the normal and the max states 783 unmaximize(); 784 } 785 CWrapper.NSWindow.miniaturize(nsWindowPtr); 786 break; 787 case Frame.MAXIMIZED_BOTH: 788 if (prevWindowState == Frame.ICONIFIED) { 789 // let's return into the normal states first 790 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 791 } 792 maximize(); 793 break; 794 case Frame.NORMAL: 795 if (prevWindowState == Frame.ICONIFIED) { 796 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 797 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) { 798 // the zoom call toggles between the normal and the max states 799 unmaximize(); 800 } 801 break; 802 default: 803 throw new RuntimeException("Unknown window state: " + windowState); 804 } 805 806 nativeSynthesizeMouseEnteredExitedEvents(); 807 808 // NOTE: the SWP.windowState field gets updated to the newWindowState 809 // value when the native notification comes to us 810 } 811 812 @Override 813 public void setModalBlocked(boolean blocked) { 814 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 815 return; 816 } 817 818 nativeSetEnabled(getNSWindowPtr(), !blocked); 819 } 820 821 public final void invalidateShadow(){ 822 nativeRevalidateNSWindowShadow(getNSWindowPtr()); 823 } 824 825 // ---------------------------------------------------------------------- 826 // UTILITY METHODS 827 // ---------------------------------------------------------------------- 828 829 /* 830 * Find image to install into Title or into Application icon. 831 * First try icons installed for toplevel. If there is no icon 832 * use default Duke image. 833 * This method shouldn't return null. 834 */ 835 private CImage getImageForTarget() { 836 List<Image> icons = target.getIconImages(); 837 if (icons == null || icons.size() == 0) { 838 return null; 839 } 840 return CImage.getCreator().createFromImages(icons); 841 } 842 843 /* 844 * Returns LWWindowPeer associated with this delegate. 845 */ 846 @Override 847 public LWWindowPeer getPeer() { 848 return peer; 849 } 850 851 @Override 852 public boolean isUnderMouse() { 853 return contentView.isUnderMouse(); 854 } 855 856 public CPlatformView getContentView() { 857 return contentView; 858 } 859 860 @Override 861 public long getLayerPtr() { 862 return contentView.getWindowLayerPtr(); 863 } 864 865 private void validateSurface() { 866 SurfaceData surfaceData = getSurfaceData(); 867 if (surfaceData instanceof CGLSurfaceData) { 868 ((CGLSurfaceData)surfaceData).validate(); 869 } 870 } 871 872 private void flushBuffers() { 873 if (isVisible() && !nativeBounds.isEmpty()) { 874 try { 875 LWCToolkit.invokeAndWait(new Runnable() { 876 @Override 877 public void run() { 878 //Posting an empty to flush the EventQueue without blocking the main thread 879 } 880 }, target); 881 } catch (InterruptedException | InvocationTargetException e) { 882 e.printStackTrace(); 883 } 884 } 885 } 886 887 /************************************************************* 888 * Callbacks from the AWTWindow and AWTView objc classes. 889 *************************************************************/ 890 private void deliverWindowFocusEvent(boolean gained, CPlatformWindow opposite){ 891 // Fix for 7150349: ingore "gained" notifications when the app is inactive. 892 if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) { 893 focusLogger.fine("the app is inactive, so the notification is ignored"); 894 return; 895 } 896 897 LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer(); 898 responder.handleWindowFocusEvent(gained, oppositePeer); 899 } 900 901 private void deliverMoveResizeEvent(int x, int y, int width, int height, 902 boolean byUser) { 903 // when the content view enters the full-screen mode, the native 904 // move/resize notifications contain a bounds smaller than 905 // the whole screen and therefore we ignore the native notifications 906 // and the content view itself creates correct synthetic notifications 907 if (isFullScreenMode) { 908 return; 909 } 910 911 final Rectangle oldB = nativeBounds; 912 nativeBounds = new Rectangle(x, y, width, height); 913 peer.notifyReshape(x, y, width, height); 914 if ((byUser && !oldB.getSize().equals(nativeBounds.getSize())) 915 || isFullScreenAnimationOn) { 916 flushBuffers(); 917 } 918 } 919 920 private void deliverWindowClosingEvent() { 921 if (peer.getBlocker() == null) { 922 peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING)); 923 } 924 } 925 926 private void deliverIconify(final boolean iconify) { 927 peer.notifyIconify(iconify); 928 } 929 930 private void deliverZoom(final boolean isZoomed) { 931 peer.notifyZoom(isZoomed); 932 } 933 934 private void deliverNCMouseDown() { 935 peer.notifyNCMouseDown(); 936 } 937 938 /* 939 * Our focus model is synthetic and only non-simple window 940 * may become natively focusable window. 941 */ 942 private boolean isNativelyFocusableWindow() { 943 return !peer.isSimpleWindow() && target.getFocusableWindowState(); 944 } 945 946 /* 947 * An utility method for the support of the auto request focus. 948 * Updates the focusable state of the window under certain 949 * circumstances. 950 */ 951 private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) { 952 if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return; 953 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 954 } 955 956 private boolean checkBlocking() { 957 LWWindowPeer blocker = peer.getBlocker(); 958 if (blocker == null) { 959 return false; 960 } 961 962 if (blocker instanceof CPrinterDialogPeer) { 963 return true; 964 } 965 966 CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow(); 967 968 pWindow.orderAboveSiblings(); 969 970 final long nsWindowPtr = pWindow.getNSWindowPtr(); 971 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 972 CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr); 973 CWrapper.NSWindow.makeMainWindow(nsWindowPtr); 974 975 return true; 976 } 977 978 private void orderAboveSiblings() { 979 if (owner == null) { 980 return; 981 } 982 983 // NOTE: the logic will fail if we have a hierarchy like: 984 // visible root owner 985 // invisible owner 986 // visible dialog 987 // However, this is an unlikely scenario for real life apps 988 if (owner.isVisible()) { 989 // Recursively pop up the windows from the very bottom so that only 990 // the very top-most one becomes the main window 991 owner.orderAboveSiblings(); 992 993 // Order the window to front of the stack of child windows 994 final long nsWindowSelfPtr = getNSWindowPtr(); 995 final long nsWindowOwnerPtr = owner.getNSWindowPtr(); 996 CWrapper.NSWindow.removeChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr); 997 CWrapper.NSWindow.addChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr, CWrapper.NSWindow.NSWindowAbove); 998 } 999 1000 if (target.isAlwaysOnTop()) { 1001 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 1002 } 1003 } 1004 1005 // ---------------------------------------------------------------------- 1006 // NATIVE CALLBACKS 1007 // ---------------------------------------------------------------------- 1008 1009 private void windowDidBecomeMain() { 1010 assert CThreading.assertAppKit(); 1011 1012 if (checkBlocking()) return; 1013 // If it's not blocked, make sure it's above its siblings 1014 orderAboveSiblings(); 1015 } 1016 1017 private void windowWillEnterFullScreen() { 1018 isFullScreenAnimationOn = true; 1019 } 1020 1021 private void windowDidEnterFullScreen() { 1022 isFullScreenAnimationOn = false; 1023 } 1024 1025 private void windowWillExitFullScreen() { 1026 isFullScreenAnimationOn = true; 1027 } 1028 1029 private void windowDidExitFullScreen() { 1030 isFullScreenAnimationOn = false; 1031 } 1032 }