1 /*
   2  * Copyright (c) 2010, 2015, 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 com.sun.glass.ui.mac;
  26 
  27 import com.sun.glass.ui.Pixels;
  28 import com.sun.glass.ui.View;
  29 import java.nio.Buffer;
  30 import java.nio.ByteBuffer;
  31 import java.nio.IntBuffer;
  32 import java.util.Map;
  33 import java.util.TreeSet;
  34 
  35 /**
  36  * MacOSX platform implementation class for View.
  37  */
  38 final class MacView extends View {
  39     
  40     private native static void _initIDs();
  41     static {
  42         _initIDs();
  43         multiClickTime = _getMultiClickTime_impl();
  44         multiClickMaxX = _getMultiClickMaxX_impl();
  45         multiClickMaxY = _getMultiClickMaxY_impl();
  46     }
  47 
  48     // Constants
  49     private static final long multiClickTime;
  50     private static final int multiClickMaxX, multiClickMaxY;
  51 
  52     private native static long _getMultiClickTime_impl();
  53     private native static int _getMultiClickMaxX_impl();
  54     private native static int _getMultiClickMaxY_impl();
  55 
  56     static long getMultiClickTime_impl() {
  57         return multiClickTime;
  58     }
  59 
  60     static int getMultiClickMaxX_impl() {
  61         return multiClickMaxX;
  62     }
  63 
  64     static int getMultiClickMaxY_impl() {
  65         return multiClickMaxY;
  66     }
  67  
  68     @Override native protected int _getNativeFrameBuffer(long ptr);
  69     @Override native protected long _create(Map caps);
  70     @Override native protected int _getX(long ptr);
  71     @Override native protected int _getY(long ptr);
  72     @Override native protected void _setParent(long ptr, long parentPtr);
  73     @Override native protected boolean _close(long ptr);
  74     @Override native protected void _scheduleRepaint(long ptr);
  75     @Override native protected void _begin(long ptr);
  76     @Override native protected void _end(long ptr);
  77     @Override native protected boolean _enterFullscreen(long ptr, boolean animate, boolean keepRatio, boolean hideCursor);
  78     @Override native protected void _exitFullscreen(long ptr, boolean animate);
  79     @Override native protected void _enableInputMethodEvents(long ptr, boolean enable);
  80     
  81     @Override protected void _uploadPixels(long ptr, Pixels pixels) {
  82         Buffer data = pixels.getPixels();
  83         if (data.isDirect() == true) {
  84             _uploadPixelsDirect(ptr, data, pixels.getWidth(), pixels.getHeight());
  85         } else if (data.hasArray() == true) {
  86             if (pixels.getBytesPerComponent() == 1) {
  87                 ByteBuffer bytes = (ByteBuffer)data;
  88                 _uploadPixelsByteArray(ptr, bytes.array(), bytes.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  89             } else {
  90                 IntBuffer ints = (IntBuffer)data;
  91                 _uploadPixelsIntArray(ptr, ints.array(), ints.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  92             }
  93         } else {
  94             // gznote: what are the circumstances under which this can happen?
  95             _uploadPixelsDirect(ptr, pixels.asByteBuffer(), pixels.getWidth(), pixels.getHeight());
  96         }
  97     }
  98     native void _uploadPixelsDirect(long viewPtr, Buffer pixels, int width, int height);
  99     native void _uploadPixelsByteArray(long viewPtr, byte[] pixels, int offset, int width, int height);
 100     native void _uploadPixelsIntArray(long viewPtr, int[] pixels, int offset, int width, int height);
 101     
 102     @Override protected long _getNativeView(long ptr) {
 103         return ptr;
 104     }
 105     
 106     native protected long _getNativeLayer(long ptr);
 107     public long getNativeLayer() {
 108         return _getNativeLayer(getNativeView());
 109     }
 110     
 111     native protected int _getNativeRemoteLayerId(long ptr, String serverName);
 112     @Override public int getNativeRemoteLayerId(String serverName) {
 113         // used when run inside plugin
 114         return _getNativeRemoteLayerId(getNativeLayer(), serverName);
 115     }
 116 
 117     native protected void _hostRemoteLayerId(long ptr, int nativeLayerId);
 118     public void hostRemoteLayerId(int nativeLayerId) {
 119         // used when run inside plugin
 120         _hostRemoteLayerId(getNativeLayer(), nativeLayerId);
 121     }
 122     
 123     protected void notifyInputMethodMac(String str, int attrib, int length,
 124                                             int cursor, int selStart, int selLength) {
 125         byte atts[] = new byte[1];
 126         atts[0] = (byte) attrib;
 127         int attBounds[] = new int[2];
 128         attBounds[0] = 0;
 129         attBounds[1] = length;
 130         if(attrib == 4) {
 131             // attrib == 4 means we are going to commit changes, so commitLength should be non-zero
 132             notifyInputMethod(str, null, attBounds, atts, length, cursor, 0);
 133         } else {
 134             // all other cases = just an update, update preview text but do not commit it
 135             if (selLength > 0
 136                     && str != null && str.length() > 0
 137                     && selStart >= 0
 138                     && selLength + selStart <= str.length()) {
 139 
 140                 TreeSet<Integer> b = new TreeSet<>();
 141                 b.add(0);
 142                 b.add(selStart);
 143                 b.add(selStart + selLength);
 144                 b.add(str.length());
 145 
 146                 int[] boundary = new int[b.size()];
 147                 int i = 0;
 148                 for (int e : b) {
 149                     boundary[i] = e;
 150                     i++;
 151                 }
 152 
 153                 byte[] values = new byte[boundary.length - 1];
 154 
 155                 for (i = 0; i < boundary.length - 1; i++) {
 156                     values[i] = (boundary[i] == selStart)
 157                                         ? IME_ATTR_TARGET_CONVERTED
 158                                         : IME_ATTR_CONVERTED;
 159                 }
 160 
 161                 notifyInputMethod(str, boundary, boundary, values, 0, cursor, 0);
 162             } else {
 163                 notifyInputMethod(str, null, attBounds, atts, 0, cursor, 0);
 164             }
 165         }
 166     }
 167 }
 168