1 /* 2 * Copyright (c) 1996, 2010, 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 package sun.awt.windows; 26 27 import java.awt.*; 28 import java.awt.peer.*; 29 import java.awt.image.VolatileImage; 30 import sun.awt.RepaintArea; 31 import sun.awt.CausedFocusEvent; 32 import sun.awt.image.SunVolatileImage; 33 import sun.awt.image.ToolkitImage; 34 import java.awt.image.BufferedImage; 35 import java.awt.image.ImageProducer; 36 import java.awt.image.ImageObserver; 37 import java.awt.image.ColorModel; 38 import java.awt.event.PaintEvent; 39 import java.awt.event.InvocationEvent; 40 import java.awt.event.KeyEvent; 41 import java.awt.event.FocusEvent; 42 import java.awt.event.MouseEvent; 43 import java.awt.event.MouseWheelEvent; 44 import java.awt.event.InputEvent; 45 import sun.awt.Win32GraphicsConfig; 46 import sun.awt.Win32GraphicsEnvironment; 47 import sun.java2d.InvalidPipeException; 48 import sun.java2d.SurfaceData; 49 import sun.java2d.ScreenUpdateManager; 50 import sun.java2d.d3d.D3DSurfaceData; 51 import sun.java2d.opengl.OGLSurfaceData; 52 import sun.java2d.pipe.Region; 53 import sun.awt.DisplayChangedListener; 54 import sun.awt.PaintEventDispatcher; 55 import sun.awt.SunToolkit; 56 import sun.awt.event.IgnorePaintEvent; 57 58 import java.awt.dnd.DropTarget; 59 import java.awt.dnd.peer.DropTargetPeer; 60 import sun.awt.AWTAccessor; 61 62 import sun.util.logging.PlatformLogger; 63 64 public abstract class WComponentPeer extends WObjectPeer 65 implements ComponentPeer, DropTargetPeer 66 { 67 /** 68 * Handle to native window 69 */ 70 protected volatile long hwnd; 71 72 private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.windows.WComponentPeer"); 73 private static final PlatformLogger shapeLog = PlatformLogger.getLogger("sun.awt.windows.shape.WComponentPeer"); 74 private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.windows.focus.WComponentPeer"); 75 76 // ComponentPeer implementation 77 SurfaceData surfaceData; 78 79 private RepaintArea paintArea; 80 81 protected Win32GraphicsConfig winGraphicsConfig; 82 83 boolean isLayouting = false; 84 boolean paintPending = false; 85 int oldWidth = -1; 86 int oldHeight = -1; 87 private int numBackBuffers = 0; 88 private VolatileImage backBuffer = null; 89 private BufferCapabilities backBufferCaps = null; 90 91 // foreground, background and color are cached to avoid calling back 92 // into the Component. 93 private Color foreground; 94 private Color background; 95 private Font font; 96 97 public native boolean isObscured(); 98 public boolean canDetermineObscurity() { return true; } 99 100 // DropTarget support 101 102 int nDropTargets; 103 long nativeDropTargetContext; // native pointer 104 105 public synchronized native void pShow(); 106 public synchronized native void hide(); 107 public synchronized native void enable(); 108 public synchronized native void disable(); 109 110 public long getHWnd() { 111 return hwnd; 112 } 113 114 /* New 1.1 API */ 115 public native Point getLocationOnScreen(); 116 117 /* New 1.1 API */ 118 public void setVisible(boolean b) { 119 if (b) { 120 show(); 121 } else { 122 hide(); 123 } 124 } 125 126 public void show() { 127 Dimension s = ((Component)target).getSize(); 128 oldHeight = s.height; 129 oldWidth = s.width; 130 pShow(); 131 } 132 133 /* New 1.1 API */ 134 public void setEnabled(boolean b) { 135 if (b) { 136 enable(); 137 } else { 138 disable(); 139 } 140 } 141 142 public int serialNum = 0; 143 144 private native void reshapeNoCheck(int x, int y, int width, int height); 145 146 /* New 1.1 API */ 147 public void setBounds(int x, int y, int width, int height, int op) { 148 // Should set paintPending before reahape to prevent 149 // thread race between paint events 150 // Native components do redraw after resize 151 paintPending = (width != oldWidth) || (height != oldHeight); 152 153 if ( (op & NO_EMBEDDED_CHECK) != 0 ) { 154 reshapeNoCheck(x, y, width, height); 155 } else { 156 reshape(x, y, width, height); 157 } 158 if ((width != oldWidth) || (height != oldHeight)) { 159 // Only recreate surfaceData if this setBounds is called 160 // for a resize; a simple move should not trigger a recreation 161 try { 162 replaceSurfaceData(); 163 } catch (InvalidPipeException e) { 164 // REMIND : what do we do if our surface creation failed? 165 } 166 oldWidth = width; 167 oldHeight = height; 168 } 169 170 serialNum++; 171 } 172 173 /* 174 * Called from native code (on Toolkit thread) in order to 175 * dynamically layout the Container during resizing 176 */ 177 void dynamicallyLayoutContainer() { 178 // If we got the WM_SIZING, this must be a Container, right? 179 // In fact, it must be the top-level Container. 180 if (log.isLoggable(PlatformLogger.FINE)) { 181 Container parent = WToolkit.getNativeContainer((Component)target); 182 if (parent != null) { 183 log.fine("Assertion (parent == null) failed"); 184 } 185 } 186 final Container cont = (Container)target; 187 188 WToolkit.executeOnEventHandlerThread(cont, new Runnable() { 189 public void run() { 190 // Discarding old paint events doesn't seem to be necessary. 191 cont.invalidate(); 192 cont.validate(); 193 194 if (surfaceData instanceof D3DSurfaceData.D3DWindowSurfaceData || 195 surfaceData instanceof OGLSurfaceData) 196 { 197 // When OGL or D3D is enabled, it is necessary to 198 // replace the SurfaceData for each dynamic layout 199 // request so that the viewport stays in sync 200 // with the window bounds. 201 try { 202 replaceSurfaceData(); 203 } catch (InvalidPipeException e) { 204 // REMIND: this is unlikely to occur for OGL, but 205 // what do we do if surface creation fails? 206 } 207 } 208 209 // Forcing a paint here doesn't seem to be necessary. 210 // paintDamagedAreaImmediately(); 211 } 212 }); 213 } 214 215 /* 216 * Paints any portion of the component that needs updating 217 * before the call returns (similar to the Win32 API UpdateWindow) 218 */ 219 void paintDamagedAreaImmediately() { 220 // force Windows to send any pending WM_PAINT events so 221 // the damage area is updated on the Java side 222 updateWindow(); 223 // make sure paint events are transferred to main event queue 224 // for coalescing 225 SunToolkit.flushPendingEvents(); 226 // paint the damaged area 227 paintArea.paint(target, shouldClearRectBeforePaint()); 228 } 229 230 native synchronized void updateWindow(); 231 232 public void paint(Graphics g) { 233 ((Component)target).paint(g); 234 } 235 236 public void repaint(long tm, int x, int y, int width, int height) { 237 } 238 239 private static final double BANDING_DIVISOR = 4.0; 240 private native int[] createPrintedPixels(int srcX, int srcY, 241 int srcW, int srcH, 242 int alpha); 243 public void print(Graphics g) { 244 245 Component comp = (Component)target; 246 247 // To conserve memory usage, we will band the image. 248 249 int totalW = comp.getWidth(); 250 int totalH = comp.getHeight(); 251 252 int hInc = (int)(totalH / BANDING_DIVISOR); 253 if (hInc == 0) { 254 hInc = totalH; 255 } 256 257 for (int startY = 0; startY < totalH; startY += hInc) { 258 int endY = startY + hInc - 1; 259 if (endY >= totalH) { 260 endY = totalH - 1; 261 } 262 int h = endY - startY + 1; 263 264 Color bgColor = comp.getBackground(); 265 int[] pix = createPrintedPixels(0, startY, totalW, h, 266 bgColor == null ? 255 : bgColor.getAlpha()); 267 if (pix != null) { 268 BufferedImage bim = new BufferedImage(totalW, h, 269 BufferedImage.TYPE_INT_ARGB); 270 bim.setRGB(0, 0, totalW, h, pix, 0, totalW); 271 g.drawImage(bim, 0, startY, null); 272 bim.flush(); 273 } 274 } 275 276 comp.print(g); 277 } 278 279 public void coalescePaintEvent(PaintEvent e) { 280 Rectangle r = e.getUpdateRect(); 281 if (!(e instanceof IgnorePaintEvent)) { 282 paintArea.add(r, e.getID()); 283 } 284 285 if (log.isLoggable(PlatformLogger.FINEST)) { 286 switch(e.getID()) { 287 case PaintEvent.UPDATE: 288 log.finest("coalescePaintEvent: UPDATE: add: x = " + 289 r.x + ", y = " + r.y + ", width = " + r.width + ", height = " + r.height); 290 return; 291 case PaintEvent.PAINT: 292 log.finest("coalescePaintEvent: PAINT: add: x = " + 293 r.x + ", y = " + r.y + ", width = " + r.width + ", height = " + r.height); 294 return; 295 } 296 } 297 } 298 299 public synchronized native void reshape(int x, int y, int width, int height); 300 301 // returns true if the event has been handled and shouldn't be propagated 302 // though handleEvent method chain - e.g. WTextFieldPeer returns true 303 // on handling '\n' to prevent it from being passed to native code 304 public boolean handleJavaKeyEvent(KeyEvent e) { return false; } 305 306 public void handleJavaMouseEvent(MouseEvent e) { 307 switch (e.getID()) { 308 case MouseEvent.MOUSE_PRESSED: 309 // Note that Swing requests focus in its own mouse event handler. 310 if (target == e.getSource() && 311 !((Component)target).isFocusOwner() && 312 WKeyboardFocusManagerPeer.shouldFocusOnClick((Component)target)) 313 { 314 WKeyboardFocusManagerPeer.requestFocusFor((Component)target, 315 CausedFocusEvent.Cause.MOUSE_EVENT); 316 } 317 break; 318 } 319 } 320 321 native void nativeHandleEvent(AWTEvent e); 322 323 @SuppressWarnings("fallthrough") 324 public void handleEvent(AWTEvent e) { 325 int id = e.getID(); 326 327 if ((e instanceof InputEvent) && !((InputEvent)e).isConsumed() && 328 ((Component)target).isEnabled()) 329 { 330 if (e instanceof MouseEvent && !(e instanceof MouseWheelEvent)) { 331 handleJavaMouseEvent((MouseEvent) e); 332 } else if (e instanceof KeyEvent) { 333 if (handleJavaKeyEvent((KeyEvent)e)) { 334 return; 335 } 336 } 337 } 338 339 switch(id) { 340 case PaintEvent.PAINT: 341 // Got native painting 342 paintPending = false; 343 // Fallthrough to next statement 344 case PaintEvent.UPDATE: 345 // Skip all painting while layouting and all UPDATEs 346 // while waiting for native paint 347 if (!isLayouting && ! paintPending) { 348 paintArea.paint(target,shouldClearRectBeforePaint()); 349 } 350 return; 351 case FocusEvent.FOCUS_LOST: 352 case FocusEvent.FOCUS_GAINED: 353 handleJavaFocusEvent((FocusEvent)e); 354 default: 355 break; 356 } 357 358 // Call the native code 359 nativeHandleEvent(e); 360 } 361 362 void handleJavaFocusEvent(FocusEvent fe) { 363 if (focusLog.isLoggable(PlatformLogger.FINER)) focusLog.finer(fe.toString()); 364 setFocus(fe.getID() == FocusEvent.FOCUS_GAINED); 365 } 366 367 native void setFocus(boolean doSetFocus); 368 369 public Dimension getMinimumSize() { 370 return ((Component)target).getSize(); 371 } 372 373 public Dimension getPreferredSize() { 374 return getMinimumSize(); 375 } 376 377 // Do nothing for heavyweight implementation 378 public void layout() {} 379 380 public Rectangle getBounds() { 381 return ((Component)target).getBounds(); 382 } 383 384 public boolean isFocusable() { 385 return false; 386 } 387 388 /* 389 * Return the GraphicsConfiguration associated with this peer, either 390 * the locally stored winGraphicsConfig, or that of the target Component. 391 */ 392 public GraphicsConfiguration getGraphicsConfiguration() { 393 if (winGraphicsConfig != null) { 394 return winGraphicsConfig; 395 } 396 else { 397 // we don't need a treelock here, since 398 // Component.getGraphicsConfiguration() gets it itself. 399 return ((Component)target).getGraphicsConfiguration(); 400 } 401 } 402 403 public SurfaceData getSurfaceData() { 404 return surfaceData; 405 } 406 407 /** 408 * Creates new surfaceData object and invalidates the previous 409 * surfaceData object. 410 * Replacing the surface data should never lock on any resources which are 411 * required by other threads which may have them and may require 412 * the tree-lock. 413 * This is a degenerate version of replaceSurfaceData(numBackBuffers), so 414 * just call that version with our current numBackBuffers. 415 */ 416 public void replaceSurfaceData() { 417 replaceSurfaceData(this.numBackBuffers, this.backBufferCaps); 418 } 419 420 public void createScreenSurface(boolean isResize) 421 { 422 Win32GraphicsConfig gc = (Win32GraphicsConfig)getGraphicsConfiguration(); 423 ScreenUpdateManager mgr = ScreenUpdateManager.getInstance(); 424 425 surfaceData = mgr.createScreenSurface(gc, this, numBackBuffers, isResize); 426 } 427 428 429 /** 430 * Multi-buffer version of replaceSurfaceData. This version is called 431 * by createBuffers(), which needs to acquire the same locks in the same 432 * order, but also needs to perform additional functions inside the 433 * locks. 434 */ 435 public void replaceSurfaceData(int newNumBackBuffers, 436 BufferCapabilities caps) 437 { 438 SurfaceData oldData = null; 439 VolatileImage oldBB = null; 440 synchronized(((Component)target).getTreeLock()) { 441 synchronized(this) { 442 if (pData == 0) { 443 return; 444 } 445 numBackBuffers = newNumBackBuffers; 446 ScreenUpdateManager mgr = ScreenUpdateManager.getInstance(); 447 oldData = surfaceData; 448 mgr.dropScreenSurface(oldData); 449 createScreenSurface(true); 450 if (oldData != null) { 451 oldData.invalidate(); 452 } 453 454 oldBB = backBuffer; 455 if (numBackBuffers > 0) { 456 // set the caps first, they're used when creating the bb 457 backBufferCaps = caps; 458 Win32GraphicsConfig gc = 459 (Win32GraphicsConfig)getGraphicsConfiguration(); 460 backBuffer = gc.createBackBuffer(this); 461 } else if (backBuffer != null) { 462 backBufferCaps = null; 463 backBuffer = null; 464 } 465 } 466 } 467 // it would be better to do this before we create new ones, 468 // but then we'd run into deadlock issues 469 if (oldData != null) { 470 oldData.flush(); 471 // null out the old data to make it collected faster 472 oldData = null; 473 } 474 if (oldBB != null) { 475 oldBB.flush(); 476 // null out the old data to make it collected faster 477 oldData = null; 478 } 479 } 480 481 public void replaceSurfaceDataLater() { 482 Runnable r = new Runnable() { 483 public void run() { 484 // Shouldn't do anything if object is disposed in meanwhile 485 // No need for sync as disposeAction in Window is performed 486 // on EDT 487 if (!isDisposed()) { 488 try { 489 replaceSurfaceData(); 490 } catch (InvalidPipeException e) { 491 // REMIND : what do we do if our surface creation failed? 492 } 493 } 494 } 495 }; 496 // Fix 6255371. 497 if (!PaintEventDispatcher.getPaintEventDispatcher().queueSurfaceDataReplacing((Component)target, r)) { 498 postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(), r)); 499 } 500 } 501 502 public boolean updateGraphicsData(GraphicsConfiguration gc) { 503 winGraphicsConfig = (Win32GraphicsConfig)gc; 504 try { 505 replaceSurfaceData(); 506 } catch (InvalidPipeException e) { 507 // REMIND : what do we do if our surface creation failed? 508 } 509 return false; 510 } 511 512 //This will return null for Components not yet added to a Container 513 public ColorModel getColorModel() { 514 GraphicsConfiguration gc = getGraphicsConfiguration(); 515 if (gc != null) { 516 return gc.getColorModel(); 517 } 518 else { 519 return null; 520 } 521 } 522 523 //This will return null for Components not yet added to a Container 524 public ColorModel getDeviceColorModel() { 525 Win32GraphicsConfig gc = 526 (Win32GraphicsConfig)getGraphicsConfiguration(); 527 if (gc != null) { 528 return gc.getDeviceColorModel(); 529 } 530 else { 531 return null; 532 } 533 } 534 535 //Returns null for Components not yet added to a Container 536 public ColorModel getColorModel(int transparency) { 537 // return WToolkit.config.getColorModel(transparency); 538 GraphicsConfiguration gc = getGraphicsConfiguration(); 539 if (gc != null) { 540 return gc.getColorModel(transparency); 541 } 542 else { 543 return null; 544 } 545 } 546 public java.awt.Toolkit getToolkit() { 547 return Toolkit.getDefaultToolkit(); 548 } 549 550 // fallback default font object 551 final static Font defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12); 552 553 @SuppressWarnings("deprecation") 554 public Graphics getGraphics() { 555 if (isDisposed()) { 556 return null; 557 } 558 559 Component target = (Component)getTarget(); 560 Window window = SunToolkit.getContainingWindow(target); 561 if (window != null) { 562 Graphics g = 563 ((WWindowPeer)window.getPeer()).getTranslucentGraphics(); 564 // getTranslucentGraphics() returns non-null value for non-opaque windows only 565 if (g != null) { 566 // Non-opaque windows do not support heavyweight children. 567 // Redirect all painting to the Window's Graphics instead. 568 // The caller is responsible for calling the 569 // WindowPeer.updateWindow() after painting has finished. 570 int x = 0, y = 0; 571 for (Component c = target; c != window; c = c.getParent()) { 572 x += c.getX(); 573 y += c.getY(); 574 } 575 576 g.translate(x, y); 577 g.clipRect(0, 0, target.getWidth(), target.getHeight()); 578 579 return g; 580 } 581 } 582 583 SurfaceData surfaceData = this.surfaceData; 584 if (surfaceData != null) { 585 /* Fix for bug 4746122. Color and Font shouldn't be null */ 586 Color bgColor = background; 587 if (bgColor == null) { 588 bgColor = SystemColor.window; 589 } 590 Color fgColor = foreground; 591 if (fgColor == null) { 592 fgColor = SystemColor.windowText; 593 } 594 Font font = this.font; 595 if (font == null) { 596 font = defaultFont; 597 } 598 ScreenUpdateManager mgr = 599 ScreenUpdateManager.getInstance(); 600 return mgr.createGraphics(surfaceData, this, fgColor, 601 bgColor, font); 602 } 603 return null; 604 } 605 public FontMetrics getFontMetrics(Font font) { 606 return WFontMetrics.getFontMetrics(font); 607 } 608 609 private synchronized native void _dispose(); 610 protected void disposeImpl() { 611 SurfaceData oldData = surfaceData; 612 surfaceData = null; 613 ScreenUpdateManager.getInstance().dropScreenSurface(oldData); 614 oldData.invalidate(); 615 // remove from updater before calling targetDisposedPeer 616 WToolkit.targetDisposedPeer(target, this); 617 _dispose(); 618 } 619 620 public void disposeLater() { 621 postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(), new Runnable() { 622 public void run() { 623 dispose(); 624 } 625 })); 626 } 627 628 public synchronized void setForeground(Color c) { 629 foreground = c; 630 _setForeground(c.getRGB()); 631 } 632 633 public synchronized void setBackground(Color c) { 634 background = c; 635 _setBackground(c.getRGB()); 636 } 637 638 /** 639 * This method is intentionally not synchronized as it is called while 640 * holding other locks. 641 * 642 * @see sun.java2d.d3d.D3DScreenUpdateManager#validate(D3DWindowSurfaceData) 643 */ 644 public Color getBackgroundNoSync() { 645 return background; 646 } 647 648 public native void _setForeground(int rgb); 649 public native void _setBackground(int rgb); 650 651 public synchronized void setFont(Font f) { 652 font = f; 653 _setFont(f); 654 } 655 public synchronized native void _setFont(Font f); 656 public final void updateCursorImmediately() { 657 WGlobalCursorManager.getCursorManager().updateCursorImmediately(); 658 } 659 660 // TODO: consider moving it to KeyboardFocusManagerPeerImpl 661 @SuppressWarnings("deprecation") 662 public boolean requestFocus(Component lightweightChild, boolean temporary, 663 boolean focusedWindowChangeAllowed, long time, 664 CausedFocusEvent.Cause cause) 665 { 666 if (WKeyboardFocusManagerPeer. 667 processSynchronousLightweightTransfer((Component)target, lightweightChild, temporary, 668 focusedWindowChangeAllowed, time)) 669 { 670 return true; 671 } 672 673 int result = WKeyboardFocusManagerPeer 674 .shouldNativelyFocusHeavyweight((Component)target, lightweightChild, 675 temporary, focusedWindowChangeAllowed, 676 time, cause); 677 678 switch (result) { 679 case WKeyboardFocusManagerPeer.SNFH_FAILURE: 680 return false; 681 case WKeyboardFocusManagerPeer.SNFH_SUCCESS_PROCEED: 682 if (focusLog.isLoggable(PlatformLogger.FINER)) { 683 focusLog.finer("Proceeding with request to " + lightweightChild + " in " + target); 684 } 685 Window parentWindow = SunToolkit.getContainingWindow((Component)target); 686 if (parentWindow == null) { 687 return rejectFocusRequestHelper("WARNING: Parent window is null"); 688 } 689 WWindowPeer wpeer = (WWindowPeer)parentWindow.getPeer(); 690 if (wpeer == null) { 691 return rejectFocusRequestHelper("WARNING: Parent window's peer is null"); 692 } 693 boolean res = wpeer.requestWindowFocus(cause); 694 695 if (focusLog.isLoggable(PlatformLogger.FINER)) focusLog.finer("Requested window focus: " + res); 696 // If parent window can be made focused and has been made focused(synchronously) 697 // then we can proceed with children, otherwise we retreat. 698 if (!(res && parentWindow.isFocused())) { 699 return rejectFocusRequestHelper("Waiting for asynchronous processing of the request"); 700 } 701 return WKeyboardFocusManagerPeer.deliverFocus(lightweightChild, 702 (Component)target, 703 temporary, 704 focusedWindowChangeAllowed, 705 time, cause); 706 707 case WKeyboardFocusManagerPeer.SNFH_SUCCESS_HANDLED: 708 // Either lightweight or excessive request - all events are generated. 709 return true; 710 } 711 return false; 712 } 713 714 private boolean rejectFocusRequestHelper(String logMsg) { 715 if (focusLog.isLoggable(PlatformLogger.FINER)) focusLog.finer(logMsg); 716 WKeyboardFocusManagerPeer.removeLastFocusRequest((Component)target); 717 return false; 718 } 719 720 public Image createImage(ImageProducer producer) { 721 return new ToolkitImage(producer); 722 } 723 724 public Image createImage(int width, int height) { 725 Win32GraphicsConfig gc = 726 (Win32GraphicsConfig)getGraphicsConfiguration(); 727 return gc.createAcceleratedImage((Component)target, width, height); 728 } 729 730 public VolatileImage createVolatileImage(int width, int height) { 731 return new SunVolatileImage((Component)target, width, height); 732 } 733 734 public boolean prepareImage(Image img, int w, int h, ImageObserver o) { 735 return getToolkit().prepareImage(img, w, h, o); 736 } 737 738 public int checkImage(Image img, int w, int h, ImageObserver o) { 739 return getToolkit().checkImage(img, w, h, o); 740 } 741 742 // Object overrides 743 744 public String toString() { 745 return getClass().getName() + "[" + target + "]"; 746 } 747 748 // Toolkit & peer internals 749 750 private int updateX1, updateY1, updateX2, updateY2; 751 752 WComponentPeer(Component target) { 753 this.target = target; 754 this.paintArea = new RepaintArea(); 755 Container parent = WToolkit.getNativeContainer(target); 756 WComponentPeer parentPeer = (WComponentPeer) WToolkit.targetToPeer(parent); 757 create(parentPeer); 758 // fix for 5088782: check if window object is created successfully 759 checkCreation(); 760 761 createScreenSurface(false); 762 initialize(); 763 start(); // Initialize enable/disable state, turn on callbacks 764 } 765 abstract void create(WComponentPeer parent); 766 767 protected void checkCreation() 768 { 769 if ((hwnd == 0) || (pData == 0)) 770 { 771 if (createError != null) 772 { 773 throw createError; 774 } 775 else 776 { 777 throw new InternalError("couldn't create component peer"); 778 } 779 } 780 } 781 782 synchronized native void start(); 783 784 void initialize() { 785 if (((Component)target).isVisible()) { 786 show(); // the wnd starts hidden 787 } 788 Color fg = ((Component)target).getForeground(); 789 if (fg != null) { 790 setForeground(fg); 791 } 792 // Set background color in C++, to avoid inheriting a parent's color. 793 Font f = ((Component)target).getFont(); 794 if (f != null) { 795 setFont(f); 796 } 797 if (! ((Component)target).isEnabled()) { 798 disable(); 799 } 800 Rectangle r = ((Component)target).getBounds(); 801 setBounds(r.x, r.y, r.width, r.height, SET_BOUNDS); 802 } 803 804 // Callbacks for window-system events to the frame 805 806 // Invoke a update() method call on the target 807 void handleRepaint(int x, int y, int w, int h) { 808 // Repaints are posted from updateClient now... 809 } 810 811 // Invoke a paint() method call on the target, after clearing the 812 // damaged area. 813 void handleExpose(int x, int y, int w, int h) { 814 // Bug ID 4081126 & 4129709 - can't do the clearRect() here, 815 // since it interferes with the java thread working in the 816 // same window on multi-processor NT machines. 817 818 postPaintIfNecessary(x, y, w, h); 819 } 820 821 /* Invoke a paint() method call on the target, without clearing the 822 * damaged area. This is normally called by a native control after 823 * it has painted itself. 824 * 825 * NOTE: This is called on the privileged toolkit thread. Do not 826 * call directly into user code using this thread! 827 */ 828 public void handlePaint(int x, int y, int w, int h) { 829 postPaintIfNecessary(x, y, w, h); 830 } 831 832 private void postPaintIfNecessary(int x, int y, int w, int h) { 833 if ( !AWTAccessor.getComponentAccessor().getIgnoreRepaint( (Component) target) ) { 834 PaintEvent event = PaintEventDispatcher.getPaintEventDispatcher(). 835 createPaintEvent((Component)target, x, y, w, h); 836 if (event != null) { 837 postEvent(event); 838 } 839 } 840 } 841 842 /* 843 * Post an event. Queue it for execution by the callback thread. 844 */ 845 void postEvent(AWTEvent event) { 846 preprocessPostEvent(event); 847 WToolkit.postEvent(WToolkit.targetToAppContext(target), event); 848 } 849 850 void preprocessPostEvent(AWTEvent event) {} 851 852 // Routines to support deferred window positioning. 853 public void beginLayout() { 854 // Skip all painting till endLayout 855 isLayouting = true; 856 } 857 858 public void endLayout() { 859 if(!paintArea.isEmpty() && !paintPending && 860 !((Component)target).getIgnoreRepaint()) { 861 // if not waiting for native painting repaint damaged area 862 postEvent(new PaintEvent((Component)target, PaintEvent.PAINT, 863 new Rectangle())); 864 } 865 isLayouting = false; 866 } 867 868 public native void beginValidate(); 869 public native void endValidate(); 870 871 /** 872 * DEPRECATED 873 */ 874 public Dimension minimumSize() { 875 return getMinimumSize(); 876 } 877 878 /** 879 * DEPRECATED 880 */ 881 public Dimension preferredSize() { 882 return getPreferredSize(); 883 } 884 885 /** 886 * register a DropTarget with this native peer 887 */ 888 889 public synchronized void addDropTarget(DropTarget dt) { 890 if (nDropTargets == 0) { 891 nativeDropTargetContext = addNativeDropTarget(); 892 } 893 nDropTargets++; 894 } 895 896 /** 897 * unregister a DropTarget with this native peer 898 */ 899 900 public synchronized void removeDropTarget(DropTarget dt) { 901 nDropTargets--; 902 if (nDropTargets == 0) { 903 removeNativeDropTarget(); 904 nativeDropTargetContext = 0; 905 } 906 } 907 908 /** 909 * add the native peer's AwtDropTarget COM object 910 * @return reference to AwtDropTarget object 911 */ 912 913 native long addNativeDropTarget(); 914 915 /** 916 * remove the native peer's AwtDropTarget COM object 917 */ 918 919 native void removeNativeDropTarget(); 920 native boolean nativeHandlesWheelScrolling(); 921 922 public boolean handlesWheelScrolling() { 923 // should this be cached? 924 return nativeHandlesWheelScrolling(); 925 } 926 927 // Returns true if we are inside begin/endLayout and 928 // are waiting for native painting 929 public boolean isPaintPending() { 930 return paintPending && isLayouting; 931 } 932 933 /** 934 * The following multibuffering-related methods delegate to our 935 * associated GraphicsConfig (Win or WGL) to handle the appropriate 936 * native windowing system specific actions. 937 */ 938 939 @Override 940 public void createBuffers(int numBuffers, BufferCapabilities caps) 941 throws AWTException 942 { 943 Win32GraphicsConfig gc = 944 (Win32GraphicsConfig)getGraphicsConfiguration(); 945 gc.assertOperationSupported((Component)target, numBuffers, caps); 946 947 // Re-create the primary surface with the new number of back buffers 948 try { 949 replaceSurfaceData(numBuffers - 1, caps); 950 } catch (InvalidPipeException e) { 951 throw new AWTException(e.getMessage()); 952 } 953 } 954 955 @Override 956 public void destroyBuffers() { 957 replaceSurfaceData(0, null); 958 } 959 960 @Override 961 public void flip(int x1, int y1, int x2, int y2, 962 BufferCapabilities.FlipContents flipAction) 963 { 964 VolatileImage backBuffer = this.backBuffer; 965 if (backBuffer == null) { 966 throw new IllegalStateException("Buffers have not been created"); 967 } 968 Win32GraphicsConfig gc = 969 (Win32GraphicsConfig)getGraphicsConfiguration(); 970 gc.flip(this, (Component)target, backBuffer, x1, y1, x2, y2, flipAction); 971 } 972 973 @Override 974 public synchronized Image getBackBuffer() { 975 Image backBuffer = this.backBuffer; 976 if (backBuffer == null) { 977 throw new IllegalStateException("Buffers have not been created"); 978 } 979 return backBuffer; 980 } 981 public BufferCapabilities getBackBufferCaps() { 982 return backBufferCaps; 983 } 984 public int getBackBuffersNum() { 985 return numBackBuffers; 986 } 987 988 /* override and return false on components that DO NOT require 989 a clearRect() before painting (i.e. native components) */ 990 public boolean shouldClearRectBeforePaint() { 991 return true; 992 } 993 994 native void pSetParent(ComponentPeer newNativeParent); 995 996 /** 997 * @see java.awt.peer.ComponentPeer#reparent 998 */ 999 public void reparent(ContainerPeer newNativeParent) { 1000 pSetParent(newNativeParent); 1001 } 1002 1003 /** 1004 * @see java.awt.peer.ComponentPeer#isReparentSupported 1005 */ 1006 public boolean isReparentSupported() { 1007 return true; 1008 } 1009 1010 public void setBoundsOperation(int operation) { 1011 } 1012 1013 private volatile boolean isAccelCapable = true; 1014 1015 /** 1016 * Returns whether this component is capable of being hw accelerated. 1017 * More specifically, whether rendering to this component or a 1018 * BufferStrategy's back-buffer for this component can be hw accelerated. 1019 * 1020 * Conditions which could prevent hw acceleration include the toplevel 1021 * window containing this component being 1022 * {@link GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT 1023 * PERPIXEL_TRANSLUCENT}. 1024 * 1025 * Another condition is if Xor paint mode was detected when rendering 1026 * to an on-screen accelerated surface associated with this peer. 1027 * in this case both on- and off-screen acceleration for this peer is 1028 * disabled. 1029 * 1030 * @return {@code true} if this component is capable of being hw 1031 * accelerated, {@code false} otherwise 1032 * @see GraphicsDevice.WindowTranslucency#PERPIXEL_TRANSLUCENT 1033 */ 1034 public boolean isAccelCapable() { 1035 if (!isAccelCapable || 1036 !isContainingTopLevelAccelCapable((Component)target)) 1037 { 1038 return false; 1039 } 1040 1041 boolean isTranslucent = 1042 SunToolkit.isContainingTopLevelTranslucent((Component)target); 1043 // D3D/OGL and translucent windows interacted poorly in Windows XP; 1044 // these problems are no longer present in Vista 1045 return !isTranslucent || Win32GraphicsEnvironment.isVistaOS(); 1046 } 1047 1048 /** 1049 * Disables acceleration for this peer. 1050 */ 1051 public void disableAcceleration() { 1052 isAccelCapable = false; 1053 } 1054 1055 1056 native void setRectangularShape(int lox, int loy, int hix, int hiy, 1057 Region region); 1058 1059 1060 // REMIND: Temp workaround for issues with using HW acceleration 1061 // in the browser on Vista when DWM is enabled. 1062 // @return true if the toplevel container is not an EmbeddedFrame or 1063 // if this EmbeddedFrame is acceleration capable, false otherwise 1064 @SuppressWarnings("deprecation") 1065 private static final boolean isContainingTopLevelAccelCapable(Component c) { 1066 while (c != null && !(c instanceof WEmbeddedFrame)) { 1067 c = c.getParent(); 1068 } 1069 if (c == null) { 1070 return true; 1071 } 1072 return ((WEmbeddedFramePeer)c.getPeer()).isAccelCapable(); 1073 } 1074 1075 /** 1076 * Applies the shape to the native component window. 1077 * @since 1.7 1078 */ 1079 @SuppressWarnings("deprecation") 1080 public void applyShape(Region shape) { 1081 if (shapeLog.isLoggable(PlatformLogger.FINER)) { 1082 shapeLog.finer( 1083 "*** INFO: Setting shape: PEER: " + this 1084 + "; TARGET: " + target 1085 + "; SHAPE: " + shape); 1086 } 1087 1088 if (shape != null) { 1089 setRectangularShape(shape.getLoX(), shape.getLoY(), shape.getHiX(), shape.getHiY(), 1090 (shape.isRectangular() ? null : shape)); 1091 } else { 1092 setRectangularShape(0, 0, 0, 0, null); 1093 } 1094 } 1095 1096 /** 1097 * Lowers this component at the bottom of the above component. If the above parameter 1098 * is null then the method places this component at the top of the Z-order. 1099 */ 1100 public void setZOrder(ComponentPeer above) { 1101 long aboveHWND = (above != null) ? ((WComponentPeer)above).getHWnd() : 0; 1102 1103 setZOrder(aboveHWND); 1104 } 1105 1106 private native void setZOrder(long above); 1107 }