1 /*
   2  * Copyright (c) 2011, 2016, 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.mac;
  26 
  27 import com.sun.glass.events.WindowEvent;
  28 import com.sun.glass.events.mac.NpapiEvent;
  29 import com.sun.glass.ui.Cursor;
  30 import com.sun.glass.ui.Pixels;
  31 import com.sun.glass.ui.Screen;
  32 import com.sun.glass.ui.View;
  33 import com.sun.glass.ui.Window;
  34 import com.sun.glass.ui.Window.State;
  35 
  36 import java.util.Map;
  37 
  38 /**
  39  * MacOSX platform implementation class for Window.
  40  */
  41 final class MacWindow extends Window {
  42 
  43     private native static void _initIDs();
  44     static {
  45         _initIDs();
  46     }
  47 
  48     protected MacWindow(Window owner, Screen screen, int styleMask) {
  49         super(owner, screen, styleMask);
  50     }
  51     protected MacWindow(long parent) {
  52         super(parent);
  53     }
  54 
  55     @Override
  56     protected void setScreen(Screen screen) {
  57         // SceneState will be called to update with new scale values
  58         // before we return from super.setScreen()...
  59         super.setScreen(screen);
  60         notifyScaleChanged(1.0f, 1.0f,
  61                            screen.getRecommendedOutputScaleX(),
  62                            screen.getRecommendedOutputScaleY());
  63     }
  64 
  65     @Override native protected long _createWindow(long ownerPtr, long screenPtr, int mask);
  66     @Override native protected long _createChildWindow(long parent);
  67     @Override native protected boolean _close(long ptr);
  68     @Override native protected boolean _setView(long ptr, View view);
  69     @Override native protected boolean _setMenubar(long ptr, long menubarPtr);
  70     @Override native protected boolean _minimize(long ptr, boolean minimize);
  71     @Override native protected boolean _maximize(long ptr, boolean maximize, boolean wasMaximized);
  72     @Override protected void _setBounds(long ptr,
  73                                         int x, int y, boolean xSet, boolean ySet,
  74                                         int w, int h, int cw, int ch,
  75                                         float xGravity, float yGravity)
  76     {
  77         float sx = getPlatformScaleX();
  78         float sy = getPlatformScaleY();
  79         if (xSet)    x = Math.round( x / sx);
  80         if (ySet)    y = Math.round( y / sy);
  81         if ( w > 0)  w = Math.round( w / sx);
  82         if ( h > 0)  h = Math.round( h / sy);
  83         if (cw > 0) cw = Math.round(cw / sx);
  84         if (ch > 0) ch = Math.round(ch / sy);
  85         _setBounds2(ptr, x, y, xSet, ySet, w, h, cw, ch, xGravity, yGravity);
  86     }
  87     native protected void _setBounds2(long ptr, int x, int y, boolean xSet, boolean ySet, int w, int h, int cw, int ch, float xGravity, float yGravity);
  88     @Override native protected boolean _setVisible(long ptr, boolean visible);
  89     @Override native protected boolean _setResizable(long ptr, boolean resizable);
  90 
  91     native private boolean _requestFocus(long ptr);
  92     @Override protected boolean _requestFocus(long ptr, int event) {
  93         //TODO: provide reasonable impl for all possible events
  94         if (event != WindowEvent.FOCUS_LOST) {
  95             return _requestFocus(ptr);
  96         }
  97         return false;
  98     }
  99 
 100     @Override native protected void _setFocusable(long ptr, boolean isFocusable);
 101     @Override native protected boolean _setTitle(long ptr, String title);
 102     @Override native protected void _setLevel(long ptr, int level);
 103     @Override native protected void _setAlpha(long ptr, float alpha);
 104     @Override native protected boolean _setBackground(long ptr, float r, float g, float b);
 105     @Override native protected void _setEnabled(long ptr, boolean enabled);
 106     @Override native protected boolean _setMinimumSize(long ptr, int width, int height);
 107     @Override native protected boolean _setMaximumSize(long ptr, int width, int height);
 108     @Override native protected void _setIcon(long ptr, Pixels pixels);
 109     @Override native protected void _toFront(long ptr);
 110     @Override native protected void _toBack(long ptr);
 111     @Override native protected void _enterModal(long ptr);
 112     @Override native protected void _enterModalWithWindow(long dialog, long window);
 113     @Override native protected void _exitModal(long ptr);
 114 
 115     @Override native protected boolean _grabFocus(long ptr);
 116     @Override native protected void _ungrabFocus(long ptr);
 117 
 118     @Override native protected int _getEmbeddedX(long ptr);
 119     @Override native protected int _getEmbeddedY(long ptr);
 120 
 121     @Override
 122     protected void notifyResize(int type, int width, int height) {
 123         width  = Math.round( width * getPlatformScaleX());
 124         height = Math.round(height * getPlatformScaleY());
 125         super.notifyResize(type, width, height);
 126     }
 127 
 128     protected void notifyMove(final int x, final int y, boolean isMaximized) {
 129         if (isMaximized() != isMaximized && !isMinimized()) {
 130             setState(isMaximized ? State.MAXIMIZED : State.NORMAL);
 131             handleWindowEvent(System.nanoTime(),
 132                     isMaximized
 133                             ? WindowEvent.MAXIMIZE
 134                             : WindowEvent.RESTORE);
 135         }
 136         notifyMove(x, y);
 137     }
 138 
 139     @Override
 140     protected void _setCursor(long ptr, Cursor cursor) {
 141         ((MacCursor)cursor).set();
 142     }
 143 
 144     @Override
 145     public void dispatchNpapiEvent(Map eventInfo) {
 146         NpapiEvent.dispatchCocoaNpapiEvent(this, eventInfo);
 147     }
 148 
 149     @Override
 150     protected void _requestInput(long ptr, String text, int type, double width, double height,
 151                                     double Mxx, double Mxy, double Mxz, double Mxt,
 152                                     double Myx, double Myy, double Myz, double Myt,
 153                                     double Mzx, double Mzy, double Mzz, double Mzt) {
 154         throw new UnsupportedOperationException("Not supported yet.");
 155     }
 156 
 157     @Override
 158     protected void _releaseInput(long ptr) {
 159         throw new UnsupportedOperationException("Not supported yet.");
 160     }
 161 }
 162