1 /*
   2  * Copyright (c) 2010, 2013, 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.gtk;
  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.ArrayList;
  33 import java.util.Map;
  34 
  35 final class GtkView extends View {
  36     
  37     private boolean imEnabled = false;
  38     private boolean isInPreeditMode = false;
  39     private final StringBuilder preedit = new StringBuilder();
  40     private ByteBuffer attributes;
  41     private int lastCaret;
  42 
  43     private native void enableInputMethodEventsImpl(long ptr, boolean enable);
  44 
  45     @Override
  46     protected void _enableInputMethodEvents(long ptr, boolean enable) {
  47         enableInputMethodEventsImpl(ptr, enable);
  48         if (imEnabled) {
  49             preedit.setLength(0);
  50         }
  51         imEnabled = enable;
  52     }
  53 
  54     @Override
  55     protected native long _create(Map caps);
  56 
  57     @Override
  58     protected native long _getNativeView(long ptr);
  59 
  60     @Override
  61     protected native int _getX(long ptr);
  62 
  63     @Override
  64     protected native int _getY(long ptr);
  65 
  66     @Override
  67     protected native void _setParent(long ptr, long parentPtr);
  68 
  69     @Override
  70     protected native boolean _close(long ptr);
  71 
  72     @Override
  73     protected native void _scheduleRepaint(long ptr);
  74 
  75     @Override
  76     protected void _begin(long ptr) {}
  77 
  78     @Override
  79     protected void _end(long ptr) {}
  80 
  81     @Override 
  82     protected void _uploadPixels(long ptr, Pixels pixels) {
  83         Buffer data = pixels.getPixels();
  84         if (data.isDirect() == true) {
  85             _uploadPixelsDirect(ptr, data, pixels.getWidth(), pixels.getHeight());
  86         } else if (data.hasArray() == true) {
  87             if (pixels.getBytesPerComponent() == 1) {
  88                 ByteBuffer bytes = (ByteBuffer)data;
  89                 _uploadPixelsByteArray(ptr, bytes.array(), bytes.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  90             } else {
  91                 IntBuffer ints = (IntBuffer)data;
  92                 _uploadPixelsIntArray(ptr, ints.array(), ints.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  93             }
  94         } else {
  95             // gznote: what are the circumstances under which this can happen?
  96             _uploadPixelsDirect(ptr, pixels.asByteBuffer(), pixels.getWidth(), pixels.getHeight());
  97         }
  98     }
  99     private native void _uploadPixelsDirect(long viewPtr, Buffer pixels, int width, int height);
 100     private native void _uploadPixelsByteArray(long viewPtr, byte[] pixels, int offset, int width, int height);
 101     private native void _uploadPixelsIntArray(long viewPtr, int[] pixels, int offset, int width, int height);
 102 
 103     @Override
 104     protected native boolean _enterFullscreen(long ptr, boolean animate, boolean keepRatio, boolean hideCursor);
 105 
 106     @Override
 107     protected native void _exitFullscreen(long ptr, boolean animate);
 108     
 109     @Override
 110     protected void _finishInputMethodComposition(long ptr) {
 111         if (imEnabled && isInPreeditMode) {
 112             // Discard any pre-edited text
 113             preedit.setLength(0);
 114             notifyInputMethod(preedit.toString(), null, null, null, 0, 0, 0);
 115         }
 116     }
 117 
 118     private void notifyPreeditMode(boolean enabled){
 119         isInPreeditMode = enabled;
 120     }
 121 
 122 
 123     protected void notifyInputMethodDraw(String text, int first, int length, int caret, byte[] attr) {
 124         int[] boundary = null;
 125         byte[] values = null;
 126 
 127         if (attributes == null ) {
 128             attributes = ByteBuffer.allocate(32);
 129         }
 130 
 131         if (length > 0) {
 132             preedit.replace(first, first + length, "");
 133         }
 134 
 135         if (text != null) {
 136             preedit.insert(first, text);
 137         } else {
 138             if (attr == null) {
 139                 preedit.setLength(0);
 140             }
 141         }
 142 
 143         if (attributes.capacity() < preedit.length()) {
 144             ByteBuffer tmp  = ByteBuffer.allocate((int) (preedit.length() * 1.5));
 145             tmp.put(attributes);
 146             attributes = tmp;
 147         }
 148 
 149         attributes.limit(preedit.length());
 150 
 151         if (attr != null && attributes.limit() >= (first + attr.length)) {
 152             attributes.position(first);
 153             attributes.put(attr);
 154         }
 155                     
 156         if (attributes.limit() > 0) {
 157             ArrayList<Integer> boundaryList = new ArrayList<>();
 158             ArrayList<Byte> valuesList = new ArrayList<>();
 159             attributes.rewind();
 160             byte lastAttribute = attributes.get();
 161 
 162             boundaryList.add(0);
 163             valuesList.add(lastAttribute);
 164 
 165             int i = 1;
 166             while (attributes.hasRemaining()) {
 167                 byte a = attributes.get();
 168                 if (lastAttribute != a) {
 169                     boundaryList.add(i);
 170                     valuesList.add(a);
 171                 }
 172                 lastAttribute = a;
 173                 i++;
 174             }
 175 
 176             boundaryList.add(attributes.limit());
 177 
 178             boundary = new int[boundaryList.size()];
 179             i = 0;
 180             for (Integer e : boundaryList) {
 181                 boundary[i++] = e;
 182             }
 183 
 184             values = new byte[valuesList.size()];
 185             i = 0;
 186             for (Byte e: valuesList) {
 187                 values[i++] = e;
 188             }
 189         }
 190 
 191         notifyInputMethod(preedit.toString(), boundary, boundary, values, 0, caret, 0);
 192         lastCaret = caret;
 193     }
 194     
 195     protected void notifyInputMethodCaret(int pos, int direction, int style) {
 196         switch (direction) {
 197             case 0: //XIMForwardChar
 198                 lastCaret += pos;
 199                 break;
 200             case 1: //XIMBackwardChar
 201                 lastCaret -= pos;
 202                 break;
 203             case 10: //XIMAbsolute
 204                 lastCaret = pos;
 205                 break;
 206             default:
 207                 //TODO: as we don't know the text structure, we cannot compute the position
 208                 // for other directions (like forward words, lines, etc...).
 209                 // Luckily, vast majority of IM uses XIMAbsolute (10)
 210         }
 211         notifyInputMethod(preedit.toString(), null, null, null, 0, lastCaret, 0);
 212     }
 213 }