< prev index next >

modules/graphics/src/main/java/com/sun/glass/ui/gtk/GtkView.java

Print this page




  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.Map;
  33 
  34 final class GtkView extends View {
  35     
  36     private boolean imEnabled = false;
  37     private boolean isInPreeditMode = false;
  38     private final StringBuilder preedit = new StringBuilder();

  39     private int lastCaret;
  40 
  41     private native void enableInputMethodEventsImpl(long ptr, boolean enable);
  42 
  43     @Override
  44     protected void _enableInputMethodEvents(long ptr, boolean enable) {
  45         enableInputMethodEventsImpl(ptr, enable);
  46         if (imEnabled) {
  47             preedit.setLength(0);
  48         }
  49         imEnabled = enable;
  50     }
  51 
  52     @Override
  53     protected native long _create(Map caps);
  54 
  55     @Override
  56     protected native long _getNativeView(long ptr);
  57 
  58     @Override


 100 
 101     @Override
 102     protected native boolean _enterFullscreen(long ptr, boolean animate, boolean keepRatio, boolean hideCursor);
 103 
 104     @Override
 105     protected native void _exitFullscreen(long ptr, boolean animate);
 106     
 107     @Override
 108     protected void _finishInputMethodComposition(long ptr) {
 109         if (imEnabled && isInPreeditMode) {
 110             // Discard any pre-edited text
 111             preedit.setLength(0);
 112             notifyInputMethod(preedit.toString(), null, null, null, 0, 0, 0);
 113         }
 114     }
 115 
 116     private void notifyPreeditMode(boolean enabled){
 117         isInPreeditMode = enabled;
 118     }
 119 
 120     protected void notifyInputMethodDraw(String text, int first, int length, int caret) {












 121         if (text != null) {
 122             preedit.replace(first, first + length, text);
 123         } else {

 124             preedit.setLength(0);
 125         }
 126         notifyInputMethod(preedit.toString(), null, null, null, 0, caret, 0);


















































 127         lastCaret = caret;
 128     }
 129     
 130     protected void notifyInputMethodCaret(int pos, int direction, int style) {
 131         switch (direction) {
 132             case 0: //XIMForwardChar
 133                 lastCaret += pos;
 134                 break;
 135             case 1: //XIMBackwardChar
 136                 lastCaret -= pos;
 137                 break;
 138             case 10: //XIMAbsolute
 139                 lastCaret = pos;
 140                 break;
 141             default:
 142                 //TODO: as we don't know the text structure, we cannot compute the position
 143                 // for other directions (like forward words, lines, etc...).
 144                 // Luckily, vast majority of IM uses XIMAbsolute (10)
 145         }
 146         notifyInputMethod(preedit.toString(), null, null, null, 0, lastCaret, 0);


  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


 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);
< prev index next >