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 StringBuilder preedit = new StringBuilder();
  38     private int lastCaret;
  39 
  40     private native void enableInputMethodEventsImpl(long ptr, boolean enable);
  41 
  42     @Override
  43     protected void _enableInputMethodEvents(long ptr, boolean enable) {
  44         enableInputMethodEventsImpl(ptr, enable);
  45         if (imEnabled) {
  46             preedit.setLength(0);
  47         }
  48         imEnabled = enable;
  49     }
  50 
  51     @Override
  52     protected native long _create(Map caps);
  53 
  54     @Override
  55     protected native long _getNativeView(long ptr);
  56 
  57     @Override
  58     protected native int _getX(long ptr);
  59 
  60     @Override
  61     protected native int _getY(long ptr);
  62 
  63     @Override
  64     protected native void _setParent(long ptr, long parentPtr);
  65 
  66     @Override
  67     protected native boolean _close(long ptr);
  68 
  69     @Override
  70     protected native void _scheduleRepaint(long ptr);
  71 
  72     @Override
  73     protected void _begin(long ptr) {}
  74 
  75     @Override
  76     protected void _end(long ptr) {}
  77 
  78     @Override 
  79     protected void _uploadPixels(long ptr, Pixels pixels) {
  80         Buffer data = pixels.getPixels();
  81         if (data.isDirect() == true) {
  82             _uploadPixelsDirect(ptr, data, pixels.getWidth(), pixels.getHeight());
  83         } else if (data.hasArray() == true) {
  84             if (pixels.getBytesPerComponent() == 1) {
  85                 ByteBuffer bytes = (ByteBuffer)data;
  86                 _uploadPixelsByteArray(ptr, bytes.array(), bytes.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  87             } else {
  88                 IntBuffer ints = (IntBuffer)data;
  89                 _uploadPixelsIntArray(ptr, ints.array(), ints.arrayOffset(), pixels.getWidth(), pixels.getHeight());
  90             }
  91         } else {
  92             // gznote: what are the circumstances under which this can happen?
  93             _uploadPixelsDirect(ptr, pixels.asByteBuffer(), pixels.getWidth(), pixels.getHeight());
  94         }
  95     }
  96     private native void _uploadPixelsDirect(long viewPtr, Buffer pixels, int width, int height);
  97     private native void _uploadPixelsByteArray(long viewPtr, byte[] pixels, int offset, int width, int height);
  98     private native void _uploadPixelsIntArray(long viewPtr, int[] pixels, int offset, int width, int height);
  99 
 100     @Override
 101     protected native boolean _enterFullscreen(long ptr, boolean animate, boolean keepRatio, boolean hideCursor);
 102 
 103     @Override
 104     protected native void _exitFullscreen(long ptr, boolean animate);
 105     
 106         
 107     protected void notifyInputMethodDraw(String text, int first, int length, int caret) {
 108         if (text != null) {
 109             preedit.replace(first, first + length, text);
 110             notifyInputMethod(preedit.toString(), null, null, null, 0, caret, 0);
 111             lastCaret = caret;
 112         } else {
 113             preedit.setLength(0);
 114         }
 115     }
 116     
 117     protected void notifyInputMethodCaret(int pos, int direction, int style) {
 118         switch (direction) {
 119             case 0: //XIMForwardChar
 120                 lastCaret += pos;
 121                 break;
 122             case 1: //XIMBackwardChar
 123                 lastCaret -= pos;
 124                 break;
 125             case 10: //XIMAbsolute
 126                 lastCaret = pos;
 127                 break;
 128             default:
 129                 //TODO: as we don't know the text structure, we cannot compute the position
 130                 // for other directions (like forward words, lines, etc...).
 131                 // Luckily, vast majority of IM uses XIMAbsolute (10)
 132         }
 133         notifyInputMethod(preedit.toString(), null, null, null, 0, lastCaret, 0);
 134     }
 135 }