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.*; 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 118 static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE | MINIMIZABLE | RESIZABLE; 119 120 // corresponds to method-based properties 121 static final int HAS_SHADOW = 1 << 10; 122 static final int ZOOMABLE = 1 << 11; 123 124 static final int ALWAYS_ON_TOP = 1 << 15; 125 static final int HIDES_ON_DEACTIVATE = 1 << 17; 126 static final int DRAGGABLE_BACKGROUND = 1 << 19; 127 static final int DOCUMENT_MODIFIED = 1 << 21; 128 static final int FULLSCREENABLE = 1 << 23; 129 130 static final int _METHOD_PROP_BITMASK = RESIZABLE | HAS_SHADOW | ZOOMABLE | ALWAYS_ON_TOP | HIDES_ON_DEACTIVATE | DRAGGABLE_BACKGROUND | DOCUMENT_MODIFIED | FULLSCREENABLE; 131 132 // corresponds to callback-based properties 133 static final int SHOULD_BECOME_KEY = 1 << 12; 134 static final int SHOULD_BECOME_MAIN = 1 << 13; 135 static final int MODAL_EXCLUDED = 1 << 16; 136 137 static final int _CALLBACK_PROP_BITMASK = SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN | MODAL_EXCLUDED; 138 139 static int SET(final int bits, final int mask, final boolean value) { 140 if (value) return (bits | mask); 141 return bits & ~mask; 142 } 143 144 static boolean IS(final int bits, final int mask) { 145 return (bits & mask) != 0; 146 } 147 148 @SuppressWarnings("unchecked") 149 static ClientPropertyApplicator<JRootPane, CPlatformWindow> CLIENT_PROPERTY_APPLICATOR = new ClientPropertyApplicator<JRootPane, CPlatformWindow>(new Property[] { 150 new Property<CPlatformWindow>(WINDOW_DOCUMENT_MODIFIED) { public void applyProperty(final CPlatformWindow c, final Object value) { 151 c.setStyleBits(DOCUMENT_MODIFIED, value == null ? false : Boolean.parseBoolean(value.toString())); 152 }}, 153 new Property<CPlatformWindow>(WINDOW_BRUSH_METAL_LOOK) { public void applyProperty(final CPlatformWindow c, final Object value) { 154 c.setStyleBits(TEXTURED, Boolean.parseBoolean(value.toString())); 155 }}, 156 new Property<CPlatformWindow>(WINDOW_ALPHA) { public void applyProperty(final CPlatformWindow c, final Object value) { 157 AWTUtilities.setWindowOpacity(c.target, value == null ? 1.0f : Float.parseFloat(value.toString())); 158 }}, 159 new Property<CPlatformWindow>(WINDOW_SHADOW) { public void applyProperty(final CPlatformWindow c, final Object value) { 160 c.setStyleBits(HAS_SHADOW, value == null ? true : Boolean.parseBoolean(value.toString())); 161 }}, 162 new Property<CPlatformWindow>(WINDOW_MINIMIZABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 163 c.setStyleBits(MINIMIZABLE, Boolean.parseBoolean(value.toString())); 164 }}, 165 new Property<CPlatformWindow>(WINDOW_CLOSEABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 166 c.setStyleBits(CLOSEABLE, Boolean.parseBoolean(value.toString())); 167 }}, 168 new Property<CPlatformWindow>(WINDOW_ZOOMABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 169 c.setStyleBits(ZOOMABLE, Boolean.parseBoolean(value.toString())); 170 }}, 171 new Property<CPlatformWindow>(WINDOW_FULLSCREENABLE) { public void applyProperty(final CPlatformWindow c, final Object value) { 172 c.setStyleBits(FULLSCREENABLE, Boolean.parseBoolean(value.toString())); 173 }}, 174 new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) { 175 nativeRevalidateNSWindowShadow(c.getNSWindowPtr()); 176 }}, 177 new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) { 178 if (value == null || !(value instanceof java.io.File)) { 179 nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), null); 180 return; 181 } 182 183 final String filename = ((java.io.File)value).getAbsolutePath(); 184 nativeSetNSWindowRepresentedFilename(c.getNSWindowPtr(), filename); 185 }} 186 }) { 187 public CPlatformWindow convertJComponentToTarget(final JRootPane p) { 188 Component root = SwingUtilities.getRoot(p); 189 if (root == null || (LWWindowPeer)root.getPeer() == null) return null; 190 return (CPlatformWindow)((LWWindowPeer)root.getPeer()).getPlatformWindow(); 191 } 192 }; 193 194 // Bounds of the native widget but in the Java coordinate system. 195 // In order to keep it up-to-date we will update them on 196 // 1) setting native bounds via nativeSetBounds() call 197 // 2) getting notification from the native level via deliverMoveResizeEvent() 198 private Rectangle nativeBounds = new Rectangle(0, 0, 0, 0); 199 private volatile boolean isFullScreenMode; 200 private boolean isFullScreenAnimationOn; 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 private CPlatformResponder responder; 210 private volatile boolean zoomed = false; // from native perspective 211 212 public CPlatformWindow() { 213 super(0, true); 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 initializeBase(_target, _peer, _owner, new CPlatformView()); 223 224 final int styleBits = getInitialStyleBits(); 225 226 // TODO: handle these misc properties 227 final long parentNSWindowPtr = (owner != null ? owner.getNSWindowPtr() : 0); 228 String warningString = target.getWarningString(); 229 230 responder = new CPlatformResponder(peer, false); 231 contentView.initialize(peer, responder); 232 233 final long nativeWindowPtr = nativeCreateNSWindow(contentView.getAWTView(), styleBits, 0, 0, 0, 0); 234 setPtr(nativeWindowPtr); 235 236 // TODO: implement on top of JObjC bridged class 237 // NSWindow window = JObjC.getInstance().AppKit().NSWindow().getInstance(nativeWindowPtr, JObjCRuntime.getInstance()); 238 239 if (target instanceof javax.swing.RootPaneContainer) { 240 final javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 241 if (rootpane != null) rootpane.addPropertyChangeListener("ancestor", new PropertyChangeListener() { 242 public void propertyChange(final PropertyChangeEvent evt) { 243 CLIENT_PROPERTY_APPLICATOR.attachAndApplyClientProperties(rootpane); 244 rootpane.removePropertyChangeListener("ancestor", this); 245 } 246 }); 247 } 248 249 validateSurface(); 250 } 251 252 protected void initializeBase(Window target, LWWindowPeer peer, PlatformWindow owner, CPlatformView view) { 253 this.peer = peer; 254 this.target = target; 255 if (owner instanceof CPlatformWindow) { 256 this.owner = (CPlatformWindow)owner; 257 } 258 this.contentView = view; 259 } 260 261 private int getInitialStyleBits() { 262 // defaults style bits 263 int styleBits = DECORATED | HAS_SHADOW | CLOSEABLE | MINIMIZABLE | ZOOMABLE | RESIZABLE; 264 265 if (isNativelyFocusableWindow()) { 266 styleBits = SET(styleBits, SHOULD_BECOME_KEY, true); 267 styleBits = SET(styleBits, SHOULD_BECOME_MAIN, true); 268 } 269 270 final boolean isFrame = (target instanceof Frame); 271 final boolean isDialog = (target instanceof Dialog); 272 final boolean isPopup = (target.getType() == Window.Type.POPUP); 273 if (isDialog) { 274 styleBits = SET(styleBits, MINIMIZABLE, false); 275 } 276 277 // Either java.awt.Frame or java.awt.Dialog can be undecorated, however java.awt.Window always is undecorated. 278 { 279 this.undecorated = isFrame ? ((Frame)target).isUndecorated() : (isDialog ? ((Dialog)target).isUndecorated() : true); 280 if (this.undecorated) styleBits = SET(styleBits, DECORATED, false); 281 } 282 283 // Either java.awt.Frame or java.awt.Dialog can be resizable, however java.awt.Window is never resizable 284 { 285 final boolean resizable = isFrame ? ((Frame)target).isResizable() : (isDialog ? ((Dialog)target).isResizable() : false); 286 styleBits = SET(styleBits, RESIZABLE, resizable); 287 if (!resizable) { 288 styleBits = SET(styleBits, ZOOMABLE, false); 289 } 290 } 291 292 if (target.isAlwaysOnTop()) { 293 styleBits = SET(styleBits, ALWAYS_ON_TOP, true); 294 } 295 296 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 297 styleBits = SET(styleBits, MODAL_EXCLUDED, true); 298 } 299 300 // If the target is a dialog, popup or tooltip we want it to ignore the brushed metal look. 301 if (isPopup) { 302 styleBits = SET(styleBits, TEXTURED, false); 303 // Popups in applets don't activate applet's process 304 styleBits = SET(styleBits, NONACTIVATING, true); 305 } 306 307 if (Window.Type.UTILITY.equals(target.getType())) { 308 styleBits = SET(styleBits, UTILITY, true); 309 } 310 311 if (target instanceof javax.swing.RootPaneContainer) { 312 javax.swing.JRootPane rootpane = ((javax.swing.RootPaneContainer)target).getRootPane(); 313 Object prop = null; 314 315 prop = rootpane.getClientProperty(WINDOW_BRUSH_METAL_LOOK); 316 if (prop != null) { 317 styleBits = SET(styleBits, TEXTURED, Boolean.parseBoolean(prop.toString())); 318 } 319 320 if (isDialog && ((Dialog)target).getModalityType() == ModalityType.DOCUMENT_MODAL) { 321 prop = rootpane.getClientProperty(WINDOW_DOC_MODAL_SHEET); 322 if (prop != null) { 323 styleBits = SET(styleBits, SHEET, Boolean.parseBoolean(prop.toString())); 324 } 325 } 326 327 prop = rootpane.getClientProperty(WINDOW_STYLE); 328 if (prop != null) { 329 if ("small".equals(prop)) { 330 styleBits = SET(styleBits, UTILITY, true); 331 if (target.isAlwaysOnTop() && rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE) == null) { 332 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, true); 333 } 334 } 335 if ("textured".equals(prop)) styleBits = SET(styleBits, TEXTURED, true); 336 if ("unified".equals(prop)) styleBits = SET(styleBits, UNIFIED, true); 337 if ("hud".equals(prop)) styleBits = SET(styleBits, HUD, true); 338 } 339 340 prop = rootpane.getClientProperty(WINDOW_HIDES_ON_DEACTIVATE); 341 if (prop != null) { 342 styleBits = SET(styleBits, HIDES_ON_DEACTIVATE, Boolean.parseBoolean(prop.toString())); 343 } 344 345 prop = rootpane.getClientProperty(WINDOW_CLOSEABLE); 346 if (prop != null) { 347 styleBits = SET(styleBits, CLOSEABLE, Boolean.parseBoolean(prop.toString())); 348 } 349 350 prop = rootpane.getClientProperty(WINDOW_MINIMIZABLE); 351 if (prop != null) { 352 styleBits = SET(styleBits, MINIMIZABLE, Boolean.parseBoolean(prop.toString())); 353 } 354 355 prop = rootpane.getClientProperty(WINDOW_ZOOMABLE); 356 if (prop != null) { 357 styleBits = SET(styleBits, ZOOMABLE, Boolean.parseBoolean(prop.toString())); 358 } 359 360 prop = rootpane.getClientProperty(WINDOW_FULLSCREENABLE); 361 if (prop != null) { 362 styleBits = SET(styleBits, FULLSCREENABLE, Boolean.parseBoolean(prop.toString())); 363 } 364 365 prop = rootpane.getClientProperty(WINDOW_SHADOW); 366 if (prop != null) { 367 styleBits = SET(styleBits, HAS_SHADOW, Boolean.parseBoolean(prop.toString())); 368 } 369 370 prop = rootpane.getClientProperty(WINDOW_DRAGGABLE_BACKGROUND); 371 if (prop != null) { 372 styleBits = SET(styleBits, DRAGGABLE_BACKGROUND, Boolean.parseBoolean(prop.toString())); 373 } 374 } 375 376 peer.setTextured(IS(TEXTURED, styleBits)); 377 378 return styleBits; 379 } 380 381 // this is the counter-point to -[CWindow _nativeSetStyleBit:] 382 private 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 void dispose() { 405 if (owner != null) { 406 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), getNSWindowPtr()); 407 } 408 contentView.dispose(); 409 nativeDispose(getNSWindowPtr()); 410 CPlatformWindow.super.dispose(); 411 } 412 413 @Override // PlatformWindow 414 public FontMetrics getFontMetrics(Font f) { 415 // TODO: not implemented 416 (new RuntimeException("unimplemented")).printStackTrace(); 417 return null; 418 } 419 420 @Override // PlatformWindow 421 public Insets getInsets() { 422 if (!isFullScreenMode) { 423 return nativeGetNSWindowInsets(getNSWindowPtr()); 424 } 425 return new Insets(0, 0, 0, 0); 426 } 427 428 @Override // PlatformWindow 429 public Point getLocationOnScreen() { 430 return new Point(nativeBounds.x, nativeBounds.y); 431 } 432 433 @Override 434 public GraphicsDevice getGraphicsDevice() { 435 return contentView.getGraphicsDevice(); 436 } 437 438 @Override // PlatformWindow 439 public SurfaceData getScreenSurface() { 440 // TODO: not implemented 441 return null; 442 } 443 444 @Override // PlatformWindow 445 public SurfaceData replaceSurfaceData() { 446 return contentView.replaceSurfaceData(); 447 } 448 449 @Override // PlatformWindow 450 public void setBounds(int x, int y, int w, int h) { 451 // assert CThreading.assertEventQueue(); 452 nativeSetNSWindowBounds(getNSWindowPtr(), x, y, w, h); 453 } 454 455 private boolean isMaximized() { 456 return undecorated ? this.normalBounds != null : zoomed; 457 } 458 459 private void maximize() { 460 if (isMaximized()) { 461 return; 462 } 463 if (!undecorated) { 464 zoomed = true; 465 CWrapper.NSWindow.zoom(getNSWindowPtr()); 466 } else { 467 deliverZoom(true); 468 469 this.normalBounds = peer.getBounds(); 470 long screen = CWrapper.NSWindow.screen(getNSWindowPtr()); 471 Rectangle toBounds = CWrapper.NSScreen.visibleFrame(screen).getBounds(); 472 // Flip the y coordinate 473 Rectangle frame = CWrapper.NSScreen.frame(screen).getBounds(); 474 toBounds.y = frame.height - toBounds.y - toBounds.height; 475 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 476 } 477 } 478 479 private void unmaximize() { 480 if (!isMaximized()) { 481 return; 482 } 483 if (!undecorated) { 484 zoomed = false; 485 CWrapper.NSWindow.zoom(getNSWindowPtr()); 486 } else { 487 deliverZoom(false); 488 489 Rectangle toBounds = this.normalBounds; 490 this.normalBounds = null; 491 setBounds(toBounds.x, toBounds.y, toBounds.width, toBounds.height); 492 } 493 } 494 495 private boolean isVisible() { 496 return this.visible; 497 } 498 499 @Override // PlatformWindow 500 public void setVisible(boolean visible) { 501 final long nsWindowPtr = getNSWindowPtr(); 502 503 // Process parent-child relationship when hiding 504 if (!visible) { 505 // Unparent my children 506 for (Window w : target.getOwnedWindows()) { 507 WindowPeer p = (WindowPeer)w.getPeer(); 508 if (p instanceof LWWindowPeer) { 509 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 510 if (pw != null && pw.isVisible()) { 511 CWrapper.NSWindow.removeChildWindow(nsWindowPtr, pw.getNSWindowPtr()); 512 } 513 } 514 } 515 516 // Unparent myself 517 if (owner != null && owner.isVisible()) { 518 CWrapper.NSWindow.removeChildWindow(owner.getNSWindowPtr(), nsWindowPtr); 519 } 520 } 521 522 // Configure stuff 523 updateIconImages(); 524 updateFocusabilityForAutoRequestFocus(false); 525 526 // Actually show or hide the window 527 LWWindowPeer blocker = peer.getBlocker(); 528 if (blocker == null || !visible) { 529 // If it ain't blocked, or is being hidden, go regular way 530 if (visible) { 531 CWrapper.NSWindow.makeFirstResponder(nsWindowPtr, contentView.getAWTView()); 532 533 boolean isPopup = (target.getType() == Window.Type.POPUP); 534 if (isPopup) { 535 // Popups in applets don't activate applet's process 536 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 537 } else { 538 CWrapper.NSWindow.orderFront(nsWindowPtr); 539 } 540 541 boolean isKeyWindow = CWrapper.NSWindow.isKeyWindow(nsWindowPtr); 542 if (!isKeyWindow) { 543 CWrapper.NSWindow.makeKeyWindow(nsWindowPtr); 544 } 545 } else { 546 CWrapper.NSWindow.orderOut(nsWindowPtr); 547 } 548 } else { 549 // otherwise, put it in a proper z-order 550 CWrapper.NSWindow.orderWindow(nsWindowPtr, CWrapper.NSWindow.NSWindowBelow, 551 ((CPlatformWindow)blocker.getPlatformWindow()).getNSWindowPtr()); 552 } 553 this.visible = visible; 554 555 // Manage the extended state when showing 556 if (visible) { 557 // Apply the extended state as expected in shared code 558 if (target instanceof Frame) { 559 switch (((Frame)target).getExtendedState()) { 560 case Frame.ICONIFIED: 561 CWrapper.NSWindow.miniaturize(nsWindowPtr); 562 break; 563 case Frame.MAXIMIZED_BOTH: 564 maximize(); 565 break; 566 default: // NORMAL 567 unmaximize(); // in case it was maximized, otherwise this is a no-op 568 break; 569 } 570 } 571 } 572 573 nativeSynthesizeMouseEnteredExitedEvents(); 574 575 // Configure stuff #2 576 updateFocusabilityForAutoRequestFocus(true); 577 578 // Manage parent-child relationship when showing 579 if (visible) { 580 // Add myself as a child 581 if (owner != null && owner.isVisible()) { 582 CWrapper.NSWindow.addChildWindow(owner.getNSWindowPtr(), nsWindowPtr, CWrapper.NSWindow.NSWindowAbove); 583 if (target.isAlwaysOnTop()) { 584 CWrapper.NSWindow.setLevel(nsWindowPtr, CWrapper.NSWindow.NSFloatingWindowLevel); 585 } 586 } 587 588 // Add my own children to myself 589 for (Window w : target.getOwnedWindows()) { 590 WindowPeer p = (WindowPeer)w.getPeer(); 591 if (p instanceof LWWindowPeer) { 592 CPlatformWindow pw = (CPlatformWindow)((LWWindowPeer)p).getPlatformWindow(); 593 if (pw != null && pw.isVisible()) { 594 CWrapper.NSWindow.addChildWindow(nsWindowPtr, pw.getNSWindowPtr(), CWrapper.NSWindow.NSWindowAbove); 595 if (w.isAlwaysOnTop()) { 596 CWrapper.NSWindow.setLevel(pw.getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 597 } 598 } 599 } 600 } 601 } 602 603 // Deal with the blocker of the window being shown 604 if (blocker != null && visible) { 605 // Make sure the blocker is above its siblings 606 ((CPlatformWindow)blocker.getPlatformWindow()).orderAboveSiblings(); 607 } 608 } 609 610 @Override // PlatformWindow 611 public void setTitle(String title) { 612 nativeSetNSWindowTitle(getNSWindowPtr(), title); 613 } 614 615 // Should be called on every window key property change. 616 @Override // PlatformWindow 617 public void updateIconImages() { 618 final long nsWindowPtr = getNSWindowPtr(); 619 final CImage cImage = getImageForTarget(); 620 nativeSetNSWindowMinimizedIcon(nsWindowPtr, cImage == null ? 0L : cImage.ptr); 621 } 622 623 public long getNSWindowPtr() { 624 final long nsWindowPtr = ptr; 625 if (nsWindowPtr == 0L) { 626 if(logger.isLoggable(PlatformLogger.FINE)) { 627 logger.fine("NSWindow already disposed?", new Exception("Pointer to native NSWindow is invalid.")); 628 } 629 } 630 return nsWindowPtr; 631 } 632 633 public SurfaceData getSurfaceData() { 634 return contentView.getSurfaceData(); 635 } 636 637 @Override // PlatformWindow 638 public void toBack() { 639 final long nsWindowPtr = getNSWindowPtr(); 640 nativePushNSWindowToBack(nsWindowPtr); 641 } 642 643 @Override // PlatformWindow 644 public void toFront() { 645 final long nsWindowPtr = getNSWindowPtr(); 646 updateFocusabilityForAutoRequestFocus(false); 647 nativePushNSWindowToFront(nsWindowPtr); 648 updateFocusabilityForAutoRequestFocus(true); 649 } 650 651 @Override 652 public void setResizable(final boolean resizable) { 653 setStyleBits(RESIZABLE, resizable); 654 } 655 656 @Override 657 public void setSizeConstraints(int minW, int minH, int maxW, int maxH) { 658 nativeSetNSWindowMinMax(getNSWindowPtr(), minW, minH, maxW, maxH); 659 } 660 661 @Override 662 public boolean rejectFocusRequest(CausedFocusEvent.Cause cause) { 663 // Cross-app activation requests are not allowed. 664 if (cause != CausedFocusEvent.Cause.MOUSE_EVENT && 665 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) 666 { 667 focusLogger.fine("the app is inactive, so the request is rejected"); 668 return true; 669 } 670 return false; 671 } 672 673 @Override 674 public boolean requestWindowFocus() { 675 676 long ptr = getNSWindowPtr(); 677 if (CWrapper.NSWindow.canBecomeMainWindow(ptr)) { 678 CWrapper.NSWindow.makeMainWindow(ptr); 679 } 680 CWrapper.NSWindow.makeKeyAndOrderFront(ptr); 681 return true; 682 } 683 684 @Override 685 public boolean isActive() { 686 long ptr = getNSWindowPtr(); 687 return CWrapper.NSWindow.isKeyWindow(ptr); 688 } 689 690 @Override 691 public void updateFocusableWindowState() { 692 final boolean isFocusable = isNativelyFocusableWindow(); 693 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 694 } 695 696 @Override 697 public Graphics transformGraphics(Graphics g) { 698 // is this where we can inject a transform for HiDPI? 699 return g; 700 } 701 702 @Override 703 public void setAlwaysOnTop(boolean isAlwaysOnTop) { 704 setStyleBits(ALWAYS_ON_TOP, isAlwaysOnTop); 705 } 706 707 public PlatformWindow getTopmostPlatformWindowUnderMouse(){ 708 return CPlatformWindow.nativeGetTopmostPlatformWindowUnderMouse(); 709 } 710 711 @Override 712 public void setOpacity(float opacity) { 713 CWrapper.NSWindow.setAlphaValue(getNSWindowPtr(), opacity); 714 } 715 716 @Override 717 public void setOpaque(boolean isOpaque) { 718 CWrapper.NSWindow.setOpaque(getNSWindowPtr(), isOpaque); 719 if (!isOpaque && !peer.isTextured()) { 720 long clearColor = CWrapper.NSColor.clearColor(); 721 CWrapper.NSWindow.setBackgroundColor(getNSWindowPtr(), clearColor); 722 } 723 724 //This is a temporary workaround. Looks like after 7124236 will be fixed 725 //the correct place for invalidateShadow() is CGLayer.drawInCGLContext. 726 SwingUtilities.invokeLater(new Runnable() { 727 @Override 728 public void run() { 729 invalidateShadow(); 730 } 731 }); 732 } 733 734 @Override 735 public void enterFullScreenMode() { 736 isFullScreenMode = true; 737 contentView.enterFullScreenMode(); 738 // the move/size notification from the underlying system comes 739 // but it contains a bounds smaller than the whole screen 740 // and therefore we need to create the synthetic notifications 741 Rectangle screenBounds; 742 final long screenPtr = CWrapper.NSWindow.screen(getNSWindowPtr()); 743 try { 744 screenBounds = CWrapper.NSScreen.frame(screenPtr).getBounds(); 745 } finally { 746 CWrapper.NSObject.release(screenPtr); 747 } 748 peer.notifyReshape(screenBounds.x, screenBounds.y, screenBounds.width, 749 screenBounds.height); 750 } 751 752 @Override 753 public void exitFullScreenMode() { 754 contentView.exitFullScreenMode(); 755 isFullScreenMode = false; 756 } 757 758 @Override 759 public void setWindowState(int windowState) { 760 if (!peer.isVisible()) { 761 // setVisible() applies the state 762 return; 763 } 764 765 int prevWindowState = peer.getState(); 766 if (prevWindowState == windowState) return; 767 768 final long nsWindowPtr = getNSWindowPtr(); 769 switch (windowState) { 770 case Frame.ICONIFIED: 771 if (prevWindowState == Frame.MAXIMIZED_BOTH) { 772 // let's return into the normal states first 773 // the zoom call toggles between the normal and the max states 774 unmaximize(); 775 } 776 CWrapper.NSWindow.miniaturize(nsWindowPtr); 777 break; 778 case Frame.MAXIMIZED_BOTH: 779 if (prevWindowState == Frame.ICONIFIED) { 780 // let's return into the normal states first 781 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 782 } 783 maximize(); 784 break; 785 case Frame.NORMAL: 786 if (prevWindowState == Frame.ICONIFIED) { 787 CWrapper.NSWindow.deminiaturize(nsWindowPtr); 788 } else if (prevWindowState == Frame.MAXIMIZED_BOTH) { 789 // the zoom call toggles between the normal and the max states 790 unmaximize(); 791 } 792 break; 793 default: 794 throw new RuntimeException("Unknown window state: " + windowState); 795 } 796 797 nativeSynthesizeMouseEnteredExitedEvents(); 798 799 // NOTE: the SWP.windowState field gets updated to the newWindowState 800 // value when the native notification comes to us 801 } 802 803 @Override 804 public void setModalBlocked(boolean blocked) { 805 if (target.getModalExclusionType() == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) { 806 return; 807 } 808 809 nativeSetEnabled(getNSWindowPtr(), !blocked); 810 } 811 812 public final void invalidateShadow(){ 813 nativeRevalidateNSWindowShadow(getNSWindowPtr()); 814 } 815 816 // ---------------------------------------------------------------------- 817 // UTILITY METHODS 818 // ---------------------------------------------------------------------- 819 820 /* 821 * Find image to install into Title or into Application icon. 822 * First try icons installed for toplevel. If there is no icon 823 * use default Duke image. 824 * This method shouldn't return null. 825 */ 826 private CImage getImageForTarget() { 827 List<Image> icons = target.getIconImages(); 828 if (icons == null || icons.size() == 0) { 829 return null; 830 } 831 return CImage.getCreator().createFromImages(icons); 832 } 833 834 /* 835 * Returns LWWindowPeer associated with this delegate. 836 */ 837 @Override 838 public LWWindowPeer getPeer() { 839 return peer; 840 } 841 842 @Override 843 public boolean isUnderMouse() { 844 return contentView.isUnderMouse(); 845 } 846 847 public CPlatformView getContentView() { 848 return contentView; 849 } 850 851 @Override 852 public long getLayerPtr() { 853 return contentView.getWindowLayerPtr(); 854 } 855 856 private void validateSurface() { 857 SurfaceData surfaceData = getSurfaceData(); 858 if (surfaceData instanceof CGLSurfaceData) { 859 ((CGLSurfaceData)surfaceData).validate(); 860 } 861 } 862 863 void flushBuffers() { 864 if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) { 865 try { 866 LWCToolkit.invokeAndWait(new Runnable() { 867 @Override 868 public void run() { 869 //Posting an empty to flush the EventQueue without blocking the main thread 870 } 871 }, target); 872 } catch (InterruptedException | InvocationTargetException e) { 873 e.printStackTrace(); 874 } 875 } 876 } 877 878 /** 879 * Helper method to get a pointer to the native view from the PlatformWindow. 880 */ 881 static long getNativeViewPtr(PlatformWindow platformWindow) { 882 long nativePeer = 0L; 883 if (platformWindow instanceof CPlatformWindow) { 884 nativePeer = ((CPlatformWindow) platformWindow).getContentView().getAWTView(); 885 } else if (platformWindow instanceof CViewPlatformEmbeddedFrame){ 886 nativePeer = ((CViewPlatformEmbeddedFrame) platformWindow).getNSViewPtr(); 887 } else { 888 throw new IllegalArgumentException("Unsupported platformWindow implementation"); 889 } 890 return nativePeer; 891 } 892 893 /************************************************************* 894 * Callbacks from the AWTWindow and AWTView objc classes. 895 *************************************************************/ 896 private void deliverWindowFocusEvent(boolean gained, CPlatformWindow opposite){ 897 // Fix for 7150349: ingore "gained" notifications when the app is inactive. 898 if (gained && !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive()) { 899 focusLogger.fine("the app is inactive, so the notification is ignored"); 900 return; 901 } 902 903 LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer(); 904 responder.handleWindowFocusEvent(gained, oppositePeer); 905 } 906 907 private void deliverMoveResizeEvent(int x, int y, int width, int height, 908 boolean byUser) { 909 // when the content view enters the full-screen mode, the native 910 // move/resize notifications contain a bounds smaller than 911 // the whole screen and therefore we ignore the native notifications 912 // and the content view itself creates correct synthetic notifications 913 if (isFullScreenMode) { 914 return; 915 } 916 917 final Rectangle oldB = nativeBounds; 918 nativeBounds = new Rectangle(x, y, width, height); 919 peer.notifyReshape(x, y, width, height); 920 if ((byUser && !oldB.getSize().equals(nativeBounds.getSize())) 921 || isFullScreenAnimationOn) { 922 flushBuffers(); 923 } 924 } 925 926 private void deliverWindowClosingEvent() { 927 if (peer.getBlocker() == null) { 928 peer.postEvent(new WindowEvent(target, WindowEvent.WINDOW_CLOSING)); 929 } 930 } 931 932 private void deliverIconify(final boolean iconify) { 933 peer.notifyIconify(iconify); 934 } 935 936 private void deliverZoom(final boolean isZoomed) { 937 peer.notifyZoom(isZoomed); 938 } 939 940 private void deliverNCMouseDown() { 941 peer.notifyNCMouseDown(); 942 } 943 944 /* 945 * Our focus model is synthetic and only non-simple window 946 * may become natively focusable window. 947 */ 948 private boolean isNativelyFocusableWindow() { 949 return !peer.isSimpleWindow() && target.getFocusableWindowState(); 950 } 951 952 /* 953 * An utility method for the support of the auto request focus. 954 * Updates the focusable state of the window under certain 955 * circumstances. 956 */ 957 private void updateFocusabilityForAutoRequestFocus(boolean isFocusable) { 958 if (target.isAutoRequestFocus() || !isNativelyFocusableWindow()) return; 959 setStyleBits(SHOULD_BECOME_KEY | SHOULD_BECOME_MAIN, isFocusable); // set both bits at once 960 } 961 962 private boolean checkBlocking() { 963 LWWindowPeer blocker = peer.getBlocker(); 964 if (blocker == null) { 965 return false; 966 } 967 968 if (blocker instanceof CPrinterDialogPeer) { 969 return true; 970 } 971 972 CPlatformWindow pWindow = (CPlatformWindow)blocker.getPlatformWindow(); 973 974 pWindow.orderAboveSiblings(); 975 976 final long nsWindowPtr = pWindow.getNSWindowPtr(); 977 CWrapper.NSWindow.orderFrontRegardless(nsWindowPtr); 978 CWrapper.NSWindow.makeKeyAndOrderFront(nsWindowPtr); 979 CWrapper.NSWindow.makeMainWindow(nsWindowPtr); 980 981 return true; 982 } 983 984 private void orderAboveSiblings() { 985 if (owner == null) { 986 return; 987 } 988 989 // NOTE: the logic will fail if we have a hierarchy like: 990 // visible root owner 991 // invisible owner 992 // visible dialog 993 // However, this is an unlikely scenario for real life apps 994 if (owner.isVisible()) { 995 // Recursively pop up the windows from the very bottom so that only 996 // the very top-most one becomes the main window 997 owner.orderAboveSiblings(); 998 999 // Order the window to front of the stack of child windows 1000 final long nsWindowSelfPtr = getNSWindowPtr(); 1001 final long nsWindowOwnerPtr = owner.getNSWindowPtr(); 1002 CWrapper.NSWindow.removeChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr); 1003 CWrapper.NSWindow.addChildWindow(nsWindowOwnerPtr, nsWindowSelfPtr, CWrapper.NSWindow.NSWindowAbove); 1004 } 1005 1006 if (target.isAlwaysOnTop()) { 1007 CWrapper.NSWindow.setLevel(getNSWindowPtr(), CWrapper.NSWindow.NSFloatingWindowLevel); 1008 } 1009 } 1010 1011 // ---------------------------------------------------------------------- 1012 // NATIVE CALLBACKS 1013 // ---------------------------------------------------------------------- 1014 1015 private void windowDidBecomeMain() { 1016 assert CThreading.assertAppKit(); 1017 1018 if (checkBlocking()) return; 1019 // If it's not blocked, make sure it's above its siblings 1020 orderAboveSiblings(); 1021 } 1022 1023 private void windowWillEnterFullScreen() { 1024 isFullScreenAnimationOn = true; 1025 } 1026 1027 private void windowDidEnterFullScreen() { 1028 isFullScreenAnimationOn = false; 1029 } 1030 1031 private void windowWillExitFullScreen() { 1032 isFullScreenAnimationOn = true; 1033 } 1034 1035 private void windowDidExitFullScreen() { 1036 isFullScreenAnimationOn = false; 1037 } 1038 }