You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Viewport.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011-2015 Brian P. Hinz
  3. * Copyright (C) 2012-2013 D. R. Commander. All Rights Reserved.
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  18. * USA.
  19. */
  20. package com.tigervnc.vncviewer;
  21. import java.awt.Color;
  22. import java.awt.event.*;
  23. import java.awt.Dimension;
  24. import java.awt.Event;
  25. import java.awt.GraphicsConfiguration;
  26. import java.awt.GraphicsDevice;
  27. import java.awt.GraphicsEnvironment;
  28. import java.awt.Image;
  29. import java.awt.Insets;
  30. import java.awt.Window;
  31. import java.lang.reflect.*;
  32. import javax.swing.*;
  33. import com.tigervnc.rfb.*;
  34. import java.lang.Exception;
  35. import java.awt.Rectangle;
  36. public class Viewport extends JFrame
  37. {
  38. public Viewport(String name, CConn cc_) {
  39. cc = cc_;
  40. setTitle(name+" - TigerVNC");
  41. setFocusable(false);
  42. setFocusTraversalKeysEnabled(false);
  43. if (!VncViewer.os.startsWith("mac os x"))
  44. setIconImage(VncViewer.frameIcon);
  45. UIManager.getDefaults().put("ScrollPane.ancestorInputMap",
  46. new UIDefaults.LazyInputMap(new Object[]{}));
  47. sp = new JScrollPane();
  48. sp.getViewport().setBackground(Color.BLACK);
  49. sp.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
  50. getContentPane().add(sp);
  51. if (VncViewer.os.startsWith("mac os x")) {
  52. if (!VncViewer.noLionFS.getValue())
  53. enableLionFS();
  54. }
  55. addWindowFocusListener(new WindowAdapter() {
  56. public void windowGainedFocus(WindowEvent e) {
  57. if (isVisible())
  58. sp.getViewport().getView().requestFocusInWindow();
  59. }
  60. public void windowLostFocus(WindowEvent e) {
  61. cc.releaseDownKeys();
  62. }
  63. });
  64. addWindowListener(new WindowAdapter() {
  65. public void windowClosing(WindowEvent e) {
  66. if (VncViewer.nViewers == 1) {
  67. if (cc.closeListener != null) {
  68. cc.close();
  69. } else {
  70. cc.viewer.exit(1);
  71. }
  72. } else {
  73. cc.close();
  74. }
  75. }
  76. });
  77. addComponentListener(new ComponentAdapter() {
  78. public void componentResized(ComponentEvent e) {
  79. String scaleString = cc.viewer.scalingFactor.getValue();
  80. if (scaleString.equalsIgnoreCase("Auto") ||
  81. scaleString.equalsIgnoreCase("FixedRatio")) {
  82. if ((sp.getSize().width != cc.desktop.scaledWidth) ||
  83. (sp.getSize().height != cc.desktop.scaledHeight)) {
  84. cc.desktop.setScaledSize();
  85. sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  86. sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
  87. sp.validate();
  88. if (getExtendedState() != JFrame.MAXIMIZED_BOTH &&
  89. !cc.fullScreen) {
  90. sp.setSize(new Dimension(cc.desktop.scaledWidth,
  91. cc.desktop.scaledHeight));
  92. int w = cc.desktop.scaledWidth + getInsets().left + getInsets().right;
  93. int h = cc.desktop.scaledHeight + getInsets().top + getInsets().bottom;
  94. if (scaleString.equalsIgnoreCase("FixedRatio"))
  95. setSize(w, h);
  96. }
  97. }
  98. } else {
  99. sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  100. sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  101. sp.validate();
  102. }
  103. if (cc.desktop.cursor != null) {
  104. Cursor cursor = cc.desktop.cursor;
  105. cc.setCursor(cursor.width(),cursor.height(),cursor.hotspot,
  106. cursor.data, cursor.mask);
  107. }
  108. }
  109. });
  110. }
  111. boolean lionFSSupported() { return canDoLionFS; }
  112. void enableLionFS() {
  113. try {
  114. String version = System.getProperty("os.version");
  115. int firstDot = version.indexOf('.');
  116. int lastDot = version.lastIndexOf('.');
  117. if (lastDot > firstDot && lastDot >= 0) {
  118. version = version.substring(0, version.indexOf('.', firstDot + 1));
  119. }
  120. double v = Double.parseDouble(version);
  121. if (v < 10.7)
  122. throw new Exception("Operating system version is " + v);
  123. Class fsuClass = Class.forName("com.apple.eawt.FullScreenUtilities");
  124. Class argClasses[] = new Class[]{Window.class, Boolean.TYPE};
  125. Method setWindowCanFullScreen =
  126. fsuClass.getMethod("setWindowCanFullScreen", argClasses);
  127. setWindowCanFullScreen.invoke(fsuClass, this, true);
  128. canDoLionFS = true;
  129. } catch (Exception e) {
  130. vlog.debug("Could not enable OS X 10.7+ full-screen mode: " +
  131. e.getMessage());
  132. }
  133. }
  134. public void toggleLionFS() {
  135. try {
  136. Class appClass = Class.forName("com.apple.eawt.Application");
  137. Method getApplication = appClass.getMethod("getApplication",
  138. (Class[])null);
  139. Object app = getApplication.invoke(appClass);
  140. Method requestToggleFullScreen =
  141. appClass.getMethod("requestToggleFullScreen", Window.class);
  142. requestToggleFullScreen.invoke(app, this);
  143. } catch (Exception e) {
  144. vlog.debug("Could not toggle OS X 10.7+ full-screen mode: " +
  145. e.getMessage());
  146. }
  147. }
  148. public void setChild(DesktopWindow child) {
  149. sp.getViewport().setView(child);
  150. }
  151. public void setGeometry(int x, int y, int w, int h, boolean pack) {
  152. if (pack) {
  153. pack();
  154. } else {
  155. setSize(w, h);
  156. }
  157. if (!cc.fullScreen)
  158. setLocation(x, y);
  159. }
  160. public Dimension getScreenSize() {
  161. return getScreenBounds().getSize();
  162. }
  163. public Rectangle getScreenBounds() {
  164. GraphicsEnvironment ge =
  165. GraphicsEnvironment.getLocalGraphicsEnvironment();
  166. Rectangle r = new Rectangle();
  167. setMaximizedBounds(null);
  168. if (cc.viewer.fullScreenAllMonitors.getValue()) {
  169. for (GraphicsDevice gd : ge.getScreenDevices())
  170. for (GraphicsConfiguration gc : gd.getConfigurations())
  171. r = r.union(gc.getBounds());
  172. if (!cc.fullScreen)
  173. pack();
  174. Rectangle mb = new Rectangle(r);
  175. mb.grow(getInsets().left, getInsets().bottom);
  176. setMaximizedBounds(mb);
  177. } else {
  178. GraphicsDevice gd = ge.getDefaultScreenDevice();
  179. GraphicsConfiguration gc = gd.getDefaultConfiguration();
  180. r = gc.getBounds();
  181. }
  182. return r;
  183. }
  184. public static Window getFullScreenWindow() {
  185. GraphicsEnvironment ge =
  186. GraphicsEnvironment.getLocalGraphicsEnvironment();
  187. GraphicsDevice gd = ge.getDefaultScreenDevice();
  188. Window fullScreenWindow = gd.getFullScreenWindow();
  189. return fullScreenWindow;
  190. }
  191. public static void setFullScreenWindow(Window fullScreenWindow) {
  192. GraphicsEnvironment ge =
  193. GraphicsEnvironment.getLocalGraphicsEnvironment();
  194. GraphicsDevice gd = ge.getDefaultScreenDevice();
  195. if (gd.isFullScreenSupported())
  196. gd.setFullScreenWindow(fullScreenWindow);
  197. }
  198. CConn cc;
  199. JScrollPane sp;
  200. boolean canDoLionFS;
  201. static LogWriter vlog = new LogWriter("Viewport");
  202. }