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.Cursor;
  28 import com.sun.glass.events.WindowEvent;
  29 import com.sun.glass.ui.Pixels;
  30 import com.sun.glass.ui.Screen;
  31 import com.sun.glass.ui.View;
  32 import com.sun.glass.ui.Window;
  33 
  34 class GtkWindow extends Window {
  35 
  36     public GtkWindow(Window owner, Screen screen, int styleMask) {
  37         super(owner, screen, styleMask);
  38     }
  39 
  40     protected GtkWindow(long parent) {
  41         super(parent);
  42     }
  43 
  44     @Override
  45     protected native long _createWindow(long ownerPtr, long screenPtr, int mask);
  46 
  47     @Override
  48     protected native long _createChildWindow(long parent);
  49 
  50     @Override
  51     protected native boolean _close(long ptr);
  52 
  53     @Override
  54     protected native boolean _setView(long ptr, View view);
  55 
  56     @Override
  57     protected boolean _setMenubar(long ptr, long menubarPtr) {
  58         //TODO is it needed?
  59         return true;
  60     }
  61 
  62     private native void minimizeImpl(long ptr, boolean minimize);
  63 
  64     private native void maximizeImpl(long ptr, boolean maximize, boolean wasMaximized);
  65 
  66     private native void setBoundsImpl(long ptr, int x, int y, boolean xSet, boolean ySet, int w, int h, int cw, int ch);
  67 
  68     private native void setVisibleImpl(long ptr, boolean visible);
  69 
  70     @Override
  71     protected native boolean _setResizable(long ptr, boolean resizable);
  72 
  73     @Override
  74     protected native boolean _requestFocus(long ptr, int event);
  75 
  76     @Override
  77     protected native void _setFocusable(long ptr, boolean isFocusable);
  78 
  79     @Override
  80     protected native boolean _grabFocus(long ptr);
  81 
  82     @Override
  83     protected native void _ungrabFocus(long ptr);
  84 
  85     @Override
  86     protected native boolean _setTitle(long ptr, String title);
  87 
  88     @Override
  89     protected native void _setLevel(long ptr, int level);
  90 
  91     @Override
  92     protected native void _setAlpha(long ptr, float alpha);
  93 
  94     @Override
  95     protected native boolean _setBackground(long ptr, float r, float g, float b);
  96 
  97     @Override
  98     protected native void _setEnabled(long ptr, boolean enabled);
  99 
 100     @Override
 101     protected native boolean _setMinimumSize(long ptr, int width, int height);
 102 
 103     @Override
 104     protected native boolean _setMaximumSize(long ptr, int width, int height);
 105 
 106     @Override
 107     protected native void _setIcon(long ptr, Pixels pixels);
 108 
 109     @Override
 110     protected native void _toFront(long ptr);
 111 
 112     @Override
 113     protected native void _toBack(long ptr);
 114 
 115     @Override
 116     protected native void _enterModal(long ptr);
 117 
 118     @Override
 119     protected native void _enterModalWithWindow(long dialog, long window);
 120 
 121     @Override
 122     protected native void _exitModal(long ptr);
 123 
 124     protected native long _getNativeWindowImpl(long ptr);
 125 
 126     private native boolean isVisible(long ptr);
 127 
 128     @Override
 129     protected boolean _setVisible(long ptr, boolean visible) {
 130         setVisibleImpl(ptr, visible);
 131         return isVisible(ptr);
 132     }
 133 
 134     @Override
 135     protected boolean _minimize(long ptr, boolean minimize) {
 136         minimizeImpl(ptr, minimize);
 137         notifyStateChanged(WindowEvent.MINIMIZE);
 138         return minimize;
 139     }
 140 
 141     @Override
 142     protected boolean _maximize(long ptr, boolean maximize,
 143                                 boolean wasMaximized) {
 144         maximizeImpl(ptr, maximize, wasMaximized);
 145         notifyStateChanged(WindowEvent.MAXIMIZE);
 146         return maximize;
 147     }
 148 
 149     private native void _showOrHideChildren(long ptr, boolean show);
 150 
 151     protected void notifyStateChanged(final int state) {
 152         if (state == WindowEvent.MINIMIZE) {
 153             _showOrHideChildren(getNativeHandle(), false);
 154         } else if (state == WindowEvent.RESTORE) {
 155             _showOrHideChildren(getNativeHandle(), true);
 156         }
 157         switch (state) {
 158             case WindowEvent.MINIMIZE:
 159             case WindowEvent.MAXIMIZE:
 160             case WindowEvent.RESTORE:
 161                 notifyResize(state, getWidth(), getHeight());
 162                 break;
 163             default:
 164                 System.err.println("Unknown window state: " + state);
 165                 break;
 166         }
 167     }
 168 
 169     @Override
 170     protected void _setCursor(long ptr, Cursor cursor) {
 171         if (cursor.getType() == Cursor.CURSOR_CUSTOM) {
 172             _setCustomCursor(ptr, cursor);
 173         } else {
 174             _setCursorType(ptr, cursor.getType());
 175         }
 176     }
 177 
 178     private native void _setCursorType(long ptr, int type);
 179     private native void _setCustomCursor(long ptr, Cursor cursor);
 180 
 181     @Override
 182     protected native int _getEmbeddedX(long ptr);
 183 
 184     @Override
 185     protected native int _getEmbeddedY(long ptr);
 186 
 187     /**
 188      * The lowest level (X11) window handle.
 189      * (Used in prism to create GLContext)
 190      * @return X11 Window handle is returned.
 191      */
 192     @Override
 193     public long getNativeWindow() {
 194         return _getNativeWindowImpl(super.getNativeWindow());
 195     }
 196 
 197     private native void _setGravity(long ptr, float xGravity, float yGravity);
 198 
 199     @Override
 200     protected void _setBounds(long ptr, int x, int y, boolean xSet, boolean ySet, int w, int h, int cw, int ch, float xGravity, float yGravity) {
 201         _setGravity(ptr, xGravity, yGravity);
 202         setBoundsImpl(ptr, x, y, xSet, ySet, w, h, cw, ch);
 203 
 204         if ((w <= 0) && (cw > 0) || (h <= 0) && (ch > 0)) {
 205             final int[] extarr = new int[4];
 206             getFrameExtents(ptr, extarr);
 207 
 208             // TODO: ((w <= 0) && (cw <= 0)) || ((h <= 0) && (ch <= 0))
 209             notifyResize(WindowEvent.RESIZE,
 210                          ((w <= 0) && (cw > 0)) ? cw + extarr[0] + extarr[1]
 211                                                 : w,
 212                          ((h <= 0) && (ch > 0)) ? ch + extarr[2] + extarr[3]
 213                                                 : h);
 214         }
 215     }
 216 
 217     private native void getFrameExtents(long ptr, int[] extarr);
 218 
 219     @Override
 220     protected void _requestInput(long ptr, String text, int type, double width, double height,
 221                                     double Mxx, double Mxy, double Mxz, double Mxt,
 222                                     double Myx, double Myy, double Myz, double Myt,
 223                                     double Mzx, double Mzy, double Mzz, double Mzt) {
 224         throw new UnsupportedOperationException("Not supported yet.");
 225     }
 226 
 227     @Override
 228     protected void _releaseInput(long ptr) {
 229         throw new UnsupportedOperationException("Not supported yet.");
 230     }
 231 
 232     @Override
 233     public long getRawHandle() {
 234         return getNativeWindow();
 235     }
 236 }