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