OptionsFrame.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. //
  2. // Copyright (C) 2001 HorizonLive.com, Inc. All Rights Reserved.
  3. // Copyright (C) 2001 Constantin Kaplinsky. All Rights Reserved.
  4. // Copyright (C) 2000 Tridia Corporation. All Rights Reserved.
  5. // Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
  6. //
  7. // This is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // This software is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this software; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  20. // USA.
  21. //
  22. //
  23. // Options frame.
  24. //
  25. // This deals with all the options the user can play with.
  26. // It sets the encodings array and some booleans.
  27. //
  28. import java.awt.*;
  29. import java.awt.event.*;
  30. class OptionsFrame extends Frame
  31. implements WindowListener, ActionListener, ItemListener {
  32. static String[] names = {
  33. "Encoding",
  34. "Compression level",
  35. "JPEG image quality",
  36. "Cursor shape updates",
  37. "Use CopyRect",
  38. "Restricted colors",
  39. "Mouse buttons 2 and 3",
  40. "View only",
  41. "Share desktop",
  42. };
  43. static String[][] values = {
  44. { "Raw", "RRE", "CoRRE", "Hextile", "Zlib", "Tight" },
  45. { "Default", "1", "2", "3", "4", "5", "6", "7", "8", "9" },
  46. { "JPEG off", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" },
  47. { "Enable", "Ignore", "Disable" },
  48. { "Yes", "No" },
  49. { "Full", "256", "64", "8", "4 (Grey)", "2 (B&W)" },
  50. { "Normal", "Reversed" },
  51. { "Yes", "No" },
  52. { "Yes", "No" },
  53. };
  54. final int
  55. encodingIndex = 0,
  56. compressLevelIndex = 1,
  57. jpegQualityIndex = 2,
  58. cursorUpdatesIndex = 3,
  59. useCopyRectIndex = 4,
  60. eightBitColorsIndex = 5,
  61. mouseButtonIndex = 6,
  62. viewOnlyIndex = 7,
  63. shareDesktopIndex = 8;
  64. Label[] labels = new Label[names.length];
  65. Choice[] choices = new Choice[names.length];
  66. Button closeButton;
  67. VncViewer viewer;
  68. //
  69. // The actual data which other classes look at:
  70. //
  71. int[] encodings = new int[20];
  72. int nEncodings;
  73. int compressLevel;
  74. int jpegQuality;
  75. int eightBitColors; // sf@2005
  76. int oldEightBitColors;
  77. boolean requestCursorUpdates;
  78. boolean ignoreCursorUpdates;
  79. boolean reverseMouseButtons2And3;
  80. boolean shareDesktop;
  81. boolean viewOnly;
  82. //
  83. // Constructor. Set up the labels and choices from the names and values
  84. // arrays.
  85. //
  86. OptionsFrame(VncViewer v) {
  87. super("Ultr@VNC Options");
  88. viewer = v;
  89. GridBagLayout gridbag = new GridBagLayout();
  90. setLayout(gridbag);
  91. GridBagConstraints gbc = new GridBagConstraints();
  92. gbc.fill = GridBagConstraints.BOTH;
  93. for (int i = 0; i < names.length; i++) {
  94. labels[i] = new Label(names[i]);
  95. gbc.gridwidth = 1;
  96. gridbag.setConstraints(labels[i],gbc);
  97. add(labels[i]);
  98. choices[i] = new Choice();
  99. gbc.gridwidth = GridBagConstraints.REMAINDER;
  100. gridbag.setConstraints(choices[i],gbc);
  101. add(choices[i]);
  102. choices[i].addItemListener(this);
  103. for (int j = 0; j < values[i].length; j++) {
  104. choices[i].addItem(values[i][j]);
  105. }
  106. }
  107. closeButton = new Button("Close");
  108. gbc.gridwidth = GridBagConstraints.REMAINDER;
  109. gridbag.setConstraints(closeButton, gbc);
  110. add(closeButton);
  111. closeButton.addActionListener(this);
  112. pack();
  113. addWindowListener(this);
  114. // Set up defaults
  115. choices[encodingIndex].select("Tight");
  116. choices[compressLevelIndex].select("Default");
  117. choices[jpegQualityIndex].select("6");
  118. choices[cursorUpdatesIndex].select("Enable");
  119. choices[useCopyRectIndex].select("Yes");
  120. choices[eightBitColorsIndex].select("256");
  121. choices[mouseButtonIndex].select("Normal");
  122. choices[viewOnlyIndex].select("No");
  123. choices[shareDesktopIndex].select("Yes");
  124. // But let them be overridden by parameters
  125. for (int i = 0; i < names.length; i++) {
  126. String s = viewer.readParameter(names[i], false);
  127. if (s != null) {
  128. for (int j = 0; j < values[i].length; j++) {
  129. if (s.equalsIgnoreCase(values[i][j])) {
  130. choices[i].select(j);
  131. }
  132. }
  133. }
  134. }
  135. // Make the booleans and encodings array correspond to the state of the GUI
  136. setEncodings();
  137. setColorFormat();
  138. setOtherOptions();
  139. }
  140. //
  141. // Disable the shareDesktop option
  142. //
  143. void disableShareDesktop() {
  144. labels[shareDesktopIndex].setEnabled(false);
  145. choices[shareDesktopIndex].setEnabled(false);
  146. }
  147. //
  148. // setEncodings looks at the encoding, compression level, JPEG
  149. // quality level, cursor shape updates and copyRect choices and sets
  150. // the encodings array appropriately. It also calls the VncViewer's
  151. // setEncodings method to send a message to the RFB server if
  152. // necessary.
  153. //
  154. void setEncodings() {
  155. nEncodings = 0;
  156. if (choices[useCopyRectIndex].getSelectedItem().equals("Yes")) {
  157. encodings[nEncodings++] = RfbProto.EncodingCopyRect;
  158. }
  159. int preferredEncoding = RfbProto.EncodingRaw;
  160. boolean enableCompressLevel = false;
  161. if (choices[encodingIndex].getSelectedItem().equals("RRE")) {
  162. preferredEncoding = RfbProto.EncodingRRE;
  163. } else if (choices[encodingIndex].getSelectedItem().equals("CoRRE")) {
  164. preferredEncoding = RfbProto.EncodingCoRRE;
  165. } else if (choices[encodingIndex].getSelectedItem().equals("Hextile")) {
  166. preferredEncoding = RfbProto.EncodingHextile;
  167. } else if (choices[encodingIndex].getSelectedItem().equals("Zlib")) {
  168. preferredEncoding = RfbProto.EncodingZlib;
  169. enableCompressLevel = true;
  170. } else if (choices[encodingIndex].getSelectedItem().equals("Tight")) {
  171. preferredEncoding = RfbProto.EncodingTight;
  172. enableCompressLevel = true;
  173. }
  174. encodings[nEncodings++] = preferredEncoding;
  175. if (preferredEncoding != RfbProto.EncodingHextile) {
  176. encodings[nEncodings++] = RfbProto.EncodingHextile;
  177. }
  178. if (preferredEncoding != RfbProto.EncodingTight) {
  179. encodings[nEncodings++] = RfbProto.EncodingTight;
  180. }
  181. if (preferredEncoding != RfbProto.EncodingZlib) {
  182. encodings[nEncodings++] = RfbProto.EncodingZlib;
  183. }
  184. if (preferredEncoding != RfbProto.EncodingCoRRE) {
  185. encodings[nEncodings++] = RfbProto.EncodingCoRRE;
  186. }
  187. if (preferredEncoding != RfbProto.EncodingRRE) {
  188. encodings[nEncodings++] = RfbProto.EncodingRRE;
  189. }
  190. // Handle compression level setting.
  191. if (enableCompressLevel) {
  192. labels[compressLevelIndex].setEnabled(true);
  193. choices[compressLevelIndex].setEnabled(true);
  194. try {
  195. compressLevel =
  196. Integer.parseInt(choices[compressLevelIndex].getSelectedItem());
  197. }
  198. catch (NumberFormatException e) {
  199. compressLevel = -1;
  200. }
  201. if (compressLevel >= 1 && compressLevel <= 9) {
  202. encodings[nEncodings++] =
  203. RfbProto.EncodingCompressLevel0 + compressLevel;
  204. } else {
  205. compressLevel = -1;
  206. }
  207. } else {
  208. labels[compressLevelIndex].setEnabled(false);
  209. choices[compressLevelIndex].setEnabled(false);
  210. }
  211. // Handle JPEG quality setting.
  212. if (preferredEncoding == RfbProto.EncodingTight && (eightBitColors == 0)) {
  213. labels[jpegQualityIndex].setEnabled(true);
  214. choices[jpegQualityIndex].setEnabled(true);
  215. try {
  216. jpegQuality =
  217. Integer.parseInt(choices[jpegQualityIndex].getSelectedItem());
  218. }
  219. catch (NumberFormatException e) {
  220. jpegQuality = -1;
  221. }
  222. if (jpegQuality >= 0 && jpegQuality <= 9) {
  223. encodings[nEncodings++] =
  224. RfbProto.EncodingQualityLevel0 + jpegQuality;
  225. } else {
  226. jpegQuality = -1;
  227. }
  228. } else {
  229. labels[jpegQualityIndex].setEnabled(false);
  230. choices[jpegQualityIndex].setEnabled(false);
  231. }
  232. // Request cursor shape updates if necessary.
  233. requestCursorUpdates =
  234. !choices[cursorUpdatesIndex].getSelectedItem().equals("Disable");
  235. if (requestCursorUpdates) {
  236. encodings[nEncodings++] = RfbProto.EncodingXCursor;
  237. encodings[nEncodings++] = RfbProto.EncodingRichCursor;
  238. ignoreCursorUpdates =
  239. choices[cursorUpdatesIndex].getSelectedItem().equals("Ignore");
  240. // marscha - PointerPos
  241. if (!ignoreCursorUpdates) {
  242. encodings[nEncodings++] = RfbProto.EncodingPointerPos;
  243. }
  244. }
  245. encodings[nEncodings++] = RfbProto.EncodingLastRect;
  246. encodings[nEncodings++] = RfbProto.EncodingNewFBSize;
  247. viewer.setEncodings();
  248. }
  249. //
  250. // setColorFormat sets eightBitColors variable depending on the GUI
  251. // setting, causing switches between 8-bit and 24-bit colors mode if
  252. // necessary.
  253. //
  254. void setColorFormat() {
  255. // sf@2005 - Adding more color modes
  256. if (choices[eightBitColorsIndex].getSelectedItem().equals("Full"))
  257. eightBitColors = 0;
  258. else if (choices[eightBitColorsIndex].getSelectedItem().equals("256"))
  259. eightBitColors = 1;
  260. else if (choices[eightBitColorsIndex].getSelectedItem().equals("64"))
  261. eightBitColors = 2;
  262. else if (choices[eightBitColorsIndex].getSelectedItem().equals("8"))
  263. eightBitColors = 3;
  264. else if (choices[eightBitColorsIndex].getSelectedItem().equals("4 (Grey)"))
  265. eightBitColors = 4;
  266. else if (choices[eightBitColorsIndex].getSelectedItem().equals("2 (B&W)"))
  267. eightBitColors = 5;
  268. boolean enableJPEG = (eightBitColors == 0) &&
  269. choices[encodingIndex].getSelectedItem().equals("Tight");
  270. labels[jpegQualityIndex].setEnabled(enableJPEG);
  271. choices[jpegQualityIndex].setEnabled(enableJPEG);
  272. }
  273. //
  274. // setOtherOptions looks at the "other" choices (ones which don't set the
  275. // encoding or the color format) and sets the boolean flags appropriately.
  276. //
  277. void setOtherOptions() {
  278. reverseMouseButtons2And3
  279. = choices[mouseButtonIndex].getSelectedItem().equals("Reversed");
  280. viewOnly
  281. = choices[viewOnlyIndex].getSelectedItem().equals("Yes");
  282. if (viewer.vc != null)
  283. viewer.vc.enableInput(!viewOnly);
  284. shareDesktop
  285. = choices[shareDesktopIndex].getSelectedItem().equals("Yes");
  286. }
  287. //
  288. // Respond to actions on Choice controls
  289. //
  290. public void itemStateChanged(ItemEvent evt) {
  291. Object source = evt.getSource();
  292. if (source == choices[encodingIndex] ||
  293. source == choices[compressLevelIndex] ||
  294. source == choices[jpegQualityIndex] ||
  295. source == choices[cursorUpdatesIndex] ||
  296. source == choices[useCopyRectIndex]) {
  297. setEncodings();
  298. } else if (source == choices[eightBitColorsIndex]) {
  299. setColorFormat();
  300. } else if (source == choices[mouseButtonIndex] ||
  301. source == choices[shareDesktopIndex] ||
  302. source == choices[viewOnlyIndex]) {
  303. setOtherOptions();
  304. }
  305. }
  306. //
  307. // Respond to button press
  308. //
  309. public void actionPerformed(ActionEvent evt) {
  310. if (evt.getSource() == closeButton)
  311. setVisible(false);
  312. }
  313. //
  314. // Respond to window events
  315. //
  316. public void windowClosing(WindowEvent evt) {
  317. setVisible(false);
  318. }
  319. public void windowActivated(WindowEvent evt) {}
  320. public void windowDeactivated(WindowEvent evt) {}
  321. public void windowOpened(WindowEvent evt) {}
  322. public void windowClosed(WindowEvent evt) {}
  323. public void windowIconified(WindowEvent evt) {}
  324. public void windowDeiconified(WindowEvent evt) {}
  325. }