DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Brian Hinz | 430d8cf | 2014-01-02 01:23:56 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011-2014 Brian P. Hinz |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame] | 3 | * |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame] | 8 | * |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Brian Hinz | c0a3609 | 2013-05-12 15:46:09 +0000 | [diff] [blame] | 13 | * |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
Brian Hinz | b213da6 | 2012-04-11 22:00:55 +0000 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | package com.tigervnc.vncviewer; |
| 21 | |
Brian Hinz | 4a95c7f | 2012-09-01 19:24:26 +0000 | [diff] [blame] | 22 | import java.awt.*; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 23 | import java.awt.Cursor; |
| 24 | import java.awt.event.*; |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 25 | import java.io.File; |
Brian P. Hinz | 2272734 | 2016-08-28 21:15:17 -0400 | [diff] [blame] | 26 | import javax.swing.filechooser.*; |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 27 | import javax.swing.JCheckBoxMenuItem; |
| 28 | import javax.swing.JDialog; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 29 | import javax.swing.JFrame; |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 30 | import javax.swing.JFileChooser; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 31 | import javax.swing.JMenuItem; |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 32 | import javax.swing.JOptionPane; |
| 33 | import javax.swing.JPopupMenu; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 34 | |
| 35 | import com.tigervnc.rfb.*; |
| 36 | |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 37 | import static com.tigervnc.vncviewer.Parameters.*; |
| 38 | |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 39 | public class F8Menu extends JPopupMenu implements ActionListener { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 40 | public F8Menu(CConn cc) { |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 41 | super("VNC Menu"); |
| 42 | setLightWeightPopupEnabled(false); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 43 | String os = System.getProperty("os.name"); |
| 44 | if (os.startsWith("Windows")) |
| 45 | com.sun.java.swing.plaf.windows.WindowsLookAndFeel.setMnemonicHidden(false); |
| 46 | this.cc = cc; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 47 | restore = addMenuItem("Restore",KeyEvent.VK_R); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 48 | restore.setEnabled(!embed.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 49 | move = addMenuItem("Move"); |
| 50 | move.setEnabled(false); |
| 51 | size = addMenuItem("Size"); |
| 52 | size.setEnabled(false); |
| 53 | minimize = addMenuItem("Minimize", KeyEvent.VK_N); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 54 | minimize.setEnabled(!embed.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 55 | maximize = addMenuItem("Maximize", KeyEvent.VK_X); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 56 | maximize.setEnabled(!embed.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 57 | addSeparator(); |
| 58 | exit = addMenuItem("Close Viewer", KeyEvent.VK_C); |
| 59 | addSeparator(); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 60 | fullScreenCheckbox = new JCheckBoxMenuItem("Full Screen"); |
| 61 | fullScreenCheckbox.setMnemonic(KeyEvent.VK_F); |
| 62 | fullScreenCheckbox.setSelected(fullScreen.getValue()); |
| 63 | fullScreenCheckbox.addActionListener(this); |
| 64 | fullScreenCheckbox.setEnabled(!embed.getValue()); |
| 65 | add(fullScreenCheckbox); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 66 | addSeparator(); |
| 67 | clipboard = addMenuItem("Clipboard..."); |
| 68 | addSeparator(); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 69 | int keyCode = MenuKey.getMenuKeyCode(); |
| 70 | String keyText = KeyEvent.getKeyText(keyCode); |
| 71 | f8 = addMenuItem("Send "+keyText, keyCode); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 72 | ctrlAltDel = addMenuItem("Send Ctrl-Alt-Del"); |
| 73 | addSeparator(); |
| 74 | refresh = addMenuItem("Refresh Screen", KeyEvent.VK_H); |
| 75 | addSeparator(); |
| 76 | newConn = addMenuItem("New connection...", KeyEvent.VK_W); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 77 | newConn.setEnabled(!embed.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 78 | options = addMenuItem("Options...", KeyEvent.VK_O); |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 79 | save = addMenuItem("Save connection info as...", KeyEvent.VK_S); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 80 | info = addMenuItem("Connection info...", KeyEvent.VK_I); |
Brian Hinz | 652953c | 2011-10-06 06:21:32 +0000 | [diff] [blame] | 81 | about = addMenuItem("About VncViewer...", KeyEvent.VK_A); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 82 | addSeparator(); |
| 83 | dismiss = addMenuItem("Dismiss menu"); |
| 84 | setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
| 85 | } |
| 86 | |
| 87 | JMenuItem addMenuItem(String str, int mnemonic) { |
| 88 | JMenuItem item = new JMenuItem(str, mnemonic); |
| 89 | item.addActionListener(this); |
| 90 | add(item); |
| 91 | return item; |
| 92 | } |
| 93 | |
| 94 | JMenuItem addMenuItem(String str) { |
| 95 | JMenuItem item = new JMenuItem(str); |
| 96 | item.addActionListener(this); |
| 97 | add(item); |
| 98 | return item; |
| 99 | } |
| 100 | |
| 101 | boolean actionMatch(ActionEvent ev, JMenuItem item) { |
| 102 | return ev.getActionCommand().equals(item.getActionCommand()); |
| 103 | } |
| 104 | |
| 105 | public void actionPerformed(ActionEvent ev) { |
| 106 | if (actionMatch(ev, exit)) { |
Brian Hinz | 932fac5 | 2012-08-26 20:52:15 +0000 | [diff] [blame] | 107 | cc.close(); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 108 | } else if (actionMatch(ev, fullScreenCheckbox)) { |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 109 | cc.toggleFullScreen(); |
| 110 | } else if (actionMatch(ev, restore)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 111 | if (fullScreen.getValue()) cc.toggleFullScreen(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 112 | cc.viewport.setExtendedState(JFrame.NORMAL); |
| 113 | } else if (actionMatch(ev, minimize)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 114 | if (fullScreen.getValue()) cc.toggleFullScreen(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 115 | cc.viewport.setExtendedState(JFrame.ICONIFIED); |
| 116 | } else if (actionMatch(ev, maximize)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 117 | if (fullScreen.getValue()) cc.toggleFullScreen(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 118 | cc.viewport.setExtendedState(JFrame.MAXIMIZED_BOTH); |
| 119 | } else if (actionMatch(ev, clipboard)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 120 | //ClipboardDialog dlg = new ClipboardDialog(cc); |
| 121 | ClipboardDialog.showDialog(cc.viewport); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 122 | } else if (actionMatch(ev, f8)) { |
Brian Hinz | 1e252c6 | 2013-02-09 02:42:14 +0000 | [diff] [blame] | 123 | cc.writeKeyEvent(MenuKey.getMenuKeySym(), true); |
| 124 | cc.writeKeyEvent(MenuKey.getMenuKeySym(), false); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 125 | } else if (actionMatch(ev, ctrlAltDel)) { |
| 126 | cc.writeKeyEvent(Keysyms.Control_L, true); |
| 127 | cc.writeKeyEvent(Keysyms.Alt_L, true); |
| 128 | cc.writeKeyEvent(Keysyms.Delete, true); |
| 129 | cc.writeKeyEvent(Keysyms.Delete, false); |
| 130 | cc.writeKeyEvent(Keysyms.Alt_L, false); |
| 131 | cc.writeKeyEvent(Keysyms.Control_L, false); |
| 132 | } else if (actionMatch(ev, refresh)) { |
| 133 | cc.refresh(); |
| 134 | } else if (actionMatch(ev, newConn)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 135 | VncViewer.newViewer(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 136 | } else if (actionMatch(ev, options)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 137 | OptionsDialog.showDialog(cc.viewport); |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 138 | } else if (actionMatch(ev, save)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 139 | String title = "Save the TigerVNC configuration to file"; |
| 140 | File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc")); |
| 141 | if (!dflt.exists() || !dflt.isFile()) |
| 142 | dflt = new File(FileUtils.getVncHomeDir()); |
Brian P. Hinz | 2272734 | 2016-08-28 21:15:17 -0400 | [diff] [blame] | 143 | FileNameExtensionFilter filter = |
| 144 | new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc"); |
| 145 | File f = Dialog.showChooser(title, dflt, this, filter); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 146 | while (f != null && f.exists() && f.isFile()) { |
| 147 | String msg = f.getAbsolutePath(); |
| 148 | msg = msg.concat(" already exists. Do you want to overwrite?"); |
| 149 | Object[] options = {"Overwrite", "No \u21B5"}; |
| 150 | JOptionPane op = |
| 151 | new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE, |
| 152 | JOptionPane.OK_CANCEL_OPTION, null, options, options[1]); |
| 153 | JDialog dlg = op.createDialog(this, "TigerVNC Viewer"); |
| 154 | dlg.setIconImage(VncViewer.frameIcon); |
| 155 | dlg.setAlwaysOnTop(true); |
| 156 | dlg.setVisible(true); |
| 157 | if (op.getValue() == options[0]) |
| 158 | break; |
| 159 | else |
Brian P. Hinz | 2272734 | 2016-08-28 21:15:17 -0400 | [diff] [blame] | 160 | f = Dialog.showChooser(title, f, this, filter); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 161 | } |
| 162 | if (f != null && (!f.exists() || f.canWrite())) |
| 163 | saveViewerParameters(f.getAbsolutePath(), vncServerName.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 164 | } else if (actionMatch(ev, info)) { |
| 165 | cc.showInfo(); |
| 166 | } else if (actionMatch(ev, about)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 167 | VncViewer.showAbout(cc.desktop); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 168 | } else if (actionMatch(ev, dismiss)) { |
| 169 | firePopupMenuCanceled(); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | CConn cc; |
| 174 | JMenuItem restore, move, size, minimize, maximize; |
| 175 | JMenuItem exit, clipboard, ctrlAltDel, refresh; |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 176 | JMenuItem newConn, options, save, info, about, dismiss; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 177 | static JMenuItem f8; |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 178 | JCheckBoxMenuItem fullScreenCheckbox; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 179 | static LogWriter vlog = new LogWriter("F8Menu"); |
| 180 | } |