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.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 59 protected native int _getX(long ptr); 60 61 @Override 62 protected native int _getY(long ptr); 63 64 @Override 65 protected native void _setParent(long ptr, long parentPtr); 66 67 @Override 68 protected native boolean _close(long ptr); 69 70 @Override 71 protected native void _scheduleRepaint(long ptr); 72 73 @Override 74 protected void _begin(long ptr) {} 75 76 @Override 77 protected void _end(long ptr) {} 78 79 @Override 80 protected void _uploadPixels(long ptr, Pixels pixels) { 81 Buffer data = pixels.getPixels(); 82 if (data.isDirect() == true) { 83 _uploadPixelsDirect(ptr, data, pixels.getWidth(), pixels.getHeight()); 84 } else if (data.hasArray() == true) { 85 if (pixels.getBytesPerComponent() == 1) { 86 ByteBuffer bytes = (ByteBuffer)data; 87 _uploadPixelsByteArray(ptr, bytes.array(), bytes.arrayOffset(), pixels.getWidth(), pixels.getHeight()); 88 } else { 89 IntBuffer ints = (IntBuffer)data; 90 _uploadPixelsIntArray(ptr, ints.array(), ints.arrayOffset(), pixels.getWidth(), pixels.getHeight()); 91 } 92 } else { 93 // gznote: what are the circumstances under which this can happen? 94 _uploadPixelsDirect(ptr, pixels.asByteBuffer(), pixels.getWidth(), pixels.getHeight()); 95 } 96 } 97 private native void _uploadPixelsDirect(long viewPtr, Buffer pixels, int width, int height); 98 private native void _uploadPixelsByteArray(long viewPtr, byte[] pixels, int offset, int width, int height); 99 private native void _uploadPixelsIntArray(long viewPtr, int[] pixels, int offset, int width, int height); 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); 147 } 148 }