src/macosx/classes/sun/lwawt/macosx/CPlatformView.java

Print this page




   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 
  30 import sun.awt.CGraphicsConfig;

  31 import sun.lwawt.LWWindowPeer;
  32 import sun.lwawt.macosx.event.NSEvent;
  33 
  34 import sun.java2d.SurfaceData;
  35 import sun.java2d.opengl.CGLLayer;
  36 import sun.java2d.opengl.CGLSurfaceData;
  37 
  38 public class CPlatformView extends CFRetainedResource {
  39     private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);




  40 
  41     private LWWindowPeer peer;
  42     private SurfaceData surfaceData;
  43     private CGLLayer windowLayer;
  44     private CPlatformResponder responder;
  45 
  46     public CPlatformView() {
  47         super(0, true);
  48     }
  49 
  50     public void initialize(LWWindowPeer peer, CPlatformResponder responder) {
  51         this.peer = peer;
  52         this.responder = responder;
  53 
  54         if (!LWCToolkit.getSunAwtDisableCALayers()) {
  55             this.windowLayer = new CGLLayer(peer);
  56         }
  57         setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr()));
  58     }
  59 


 141     public SurfaceData getSurfaceData() {
 142         return surfaceData;
 143     }
 144 
 145     @Override
 146     public void dispose() {
 147         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 148             windowLayer.dispose();
 149         }
 150         super.dispose();
 151     }
 152 
 153     public long getWindowLayerPtr() {
 154         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 155             return windowLayer.getPointer();
 156         } else {
 157             return 0;
 158         }
 159     }
 160 


























 161     // ----------------------------------------------------------------------
 162     // NATIVE CALLBACKS
 163     // ----------------------------------------------------------------------
 164 










 165     private void deliverMouseEvent(NSEvent event) {
 166         int x = event.getX();
 167         int y = getBounds().height - event.getY();
 168 
 169         if (event.getType() == CocoaConstants.NSScrollWheel) {
 170             responder.handleScrollEvent(x, y, event.getModifierFlags(),
 171                                         event.getScrollDeltaX(), event.getScrollDeltaY());
 172         } else {
 173             responder.handleMouseEvent(event.getType(), event.getModifierFlags(), event.getButtonNumber(),
 174                                        event.getClickCount(), x, y, event.getAbsX(), event.getAbsY());
 175         }
 176     }
 177 
 178     private void deliverKeyEvent(NSEvent event) {
 179         responder.handleKeyEvent(event.getType(), event.getModifierFlags(),
 180                                  event.getCharactersIgnoringModifiers(), event.getKeyCode(), true);
 181     }
 182 
 183     /**
 184      * Called by the native delegate in layer backed view mode or in the simple


   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.geom.Rectangle2D;
  30 import java.awt.image.VolatileImage;
  31 
  32 import sun.awt.CGraphicsConfig;
  33 import sun.awt.CGraphicsEnvironment;
  34 import sun.lwawt.LWWindowPeer;
  35 import sun.lwawt.macosx.event.NSEvent;
  36 
  37 import sun.java2d.SurfaceData;
  38 import sun.java2d.opengl.CGLLayer;
  39 import sun.java2d.opengl.CGLSurfaceData;
  40 
  41 public class CPlatformView extends CFRetainedResource {
  42     private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
  43     private static native void nativeSetAutoResizable(long awtView, boolean toResize);
  44     private static native int nativeGetNSViewDisplayID(long awtView);
  45     private static native Rectangle2D nativeGetLocationOnScreen(long awtView);
  46     private static native boolean nativeIsViewUnderMouse(long ptr);
  47 
  48     private LWWindowPeer peer;
  49     private SurfaceData surfaceData;
  50     private CGLLayer windowLayer;
  51     private CPlatformResponder responder;
  52 
  53     public CPlatformView() {
  54         super(0, true);
  55     }
  56 
  57     public void initialize(LWWindowPeer peer, CPlatformResponder responder) {
  58         this.peer = peer;
  59         this.responder = responder;
  60 
  61         if (!LWCToolkit.getSunAwtDisableCALayers()) {
  62             this.windowLayer = new CGLLayer(peer);
  63         }
  64         setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr()));
  65     }
  66 


 148     public SurfaceData getSurfaceData() {
 149         return surfaceData;
 150     }
 151 
 152     @Override
 153     public void dispose() {
 154         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 155             windowLayer.dispose();
 156         }
 157         super.dispose();
 158     }
 159 
 160     public long getWindowLayerPtr() {
 161         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 162             return windowLayer.getPointer();
 163         } else {
 164             return 0;
 165         }
 166     }
 167     
 168     public void setAutoResizable(boolean toResize) {
 169         nativeSetAutoResizable(this.getAWTView(), toResize);
 170     }
 171 
 172     public boolean isUnderMouse() {
 173         return nativeIsViewUnderMouse(getAWTView());
 174     }
 175     
 176     public GraphicsDevice getGraphicsDevice() {
 177         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 178         CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
 179         int displayID = nativeGetNSViewDisplayID(getAWTView());
 180         GraphicsDevice gd = cge.getScreenDevice(displayID);
 181         if (gd == null) {
 182             // this could possibly happen during device removal
 183             // use the default screen device in this case
 184             gd = ge.getDefaultScreenDevice();
 185         }
 186         return gd;
 187     }
 188     
 189     public Point getLocationOnScreen() {
 190         Rectangle r = nativeGetLocationOnScreen(this.getAWTView()).getBounds();
 191         return new Point(r.x, r.y);
 192     }
 193 
 194     // ----------------------------------------------------------------------
 195     // NATIVE CALLBACKS
 196     // ----------------------------------------------------------------------
 197 
 198     /*
 199      * The callback is called only in the embedded case when the view is
 200      * automatically resized by the superview.
 201      * In normal mode this method is never called.
 202      */
 203     private void deliverResize(int x, int y, int w, int h) {  
 204         peer.notifyReshape(x, y, w, h);
 205     }
 206 
 207     
 208     private void deliverMouseEvent(NSEvent event) {
 209         int x = event.getX();
 210         int y = getBounds().height - event.getY();
 211 
 212         if (event.getType() == CocoaConstants.NSScrollWheel) {
 213             responder.handleScrollEvent(x, y, event.getModifierFlags(),
 214                                         event.getScrollDeltaX(), event.getScrollDeltaY());
 215         } else {
 216             responder.handleMouseEvent(event.getType(), event.getModifierFlags(), event.getButtonNumber(),
 217                                        event.getClickCount(), x, y, event.getAbsX(), event.getAbsY());
 218         }
 219     }
 220 
 221     private void deliverKeyEvent(NSEvent event) {
 222         responder.handleKeyEvent(event.getType(), event.getModifierFlags(),
 223                                  event.getCharactersIgnoringModifiers(), event.getKeyCode(), true);
 224     }
 225 
 226     /**
 227      * Called by the native delegate in layer backed view mode or in the simple