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)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 109 | if (fullScreenCheckbox.isSelected()) |
| 110 | cc.desktop.fullscreen_on(); |
| 111 | else |
| 112 | cc.desktop.fullscreen_off(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 113 | } else if (actionMatch(ev, restore)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 114 | if (cc.desktop.fullscreen_active()) |
| 115 | cc.desktop.fullscreen_off(); |
| 116 | cc.desktop.setExtendedState(JFrame.NORMAL); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 117 | } else if (actionMatch(ev, minimize)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 118 | if (cc.desktop.fullscreen_active()) |
| 119 | cc.desktop.fullscreen_off(); |
| 120 | cc.desktop.setExtendedState(JFrame.ICONIFIED); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 121 | } else if (actionMatch(ev, maximize)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 122 | if (cc.desktop.fullscreen_active()) |
| 123 | cc.desktop.fullscreen_off(); |
| 124 | cc.desktop.setExtendedState(JFrame.MAXIMIZED_BOTH); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 125 | } else if (actionMatch(ev, clipboard)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 126 | ClipboardDialog.showDialog(cc.desktop); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 127 | } else if (actionMatch(ev, f8)) { |
Brian Hinz | 1e252c6 | 2013-02-09 02:42:14 +0000 | [diff] [blame] | 128 | cc.writeKeyEvent(MenuKey.getMenuKeySym(), true); |
| 129 | cc.writeKeyEvent(MenuKey.getMenuKeySym(), false); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 130 | } else if (actionMatch(ev, ctrlAltDel)) { |
| 131 | cc.writeKeyEvent(Keysyms.Control_L, true); |
| 132 | cc.writeKeyEvent(Keysyms.Alt_L, true); |
| 133 | cc.writeKeyEvent(Keysyms.Delete, true); |
| 134 | cc.writeKeyEvent(Keysyms.Delete, false); |
| 135 | cc.writeKeyEvent(Keysyms.Alt_L, false); |
| 136 | cc.writeKeyEvent(Keysyms.Control_L, false); |
| 137 | } else if (actionMatch(ev, refresh)) { |
| 138 | cc.refresh(); |
| 139 | } else if (actionMatch(ev, newConn)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 140 | VncViewer.newViewer(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 141 | } else if (actionMatch(ev, options)) { |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 142 | OptionsDialog.showDialog(cc.desktop); |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 143 | } else if (actionMatch(ev, save)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 144 | String title = "Save the TigerVNC configuration to file"; |
| 145 | File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc")); |
| 146 | if (!dflt.exists() || !dflt.isFile()) |
| 147 | dflt = new File(FileUtils.getVncHomeDir()); |
Brian P. Hinz | 2272734 | 2016-08-28 21:15:17 -0400 | [diff] [blame] | 148 | FileNameExtensionFilter filter = |
| 149 | new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc"); |
| 150 | File f = Dialog.showChooser(title, dflt, this, filter); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 151 | while (f != null && f.exists() && f.isFile()) { |
| 152 | String msg = f.getAbsolutePath(); |
| 153 | msg = msg.concat(" already exists. Do you want to overwrite?"); |
| 154 | Object[] options = {"Overwrite", "No \u21B5"}; |
| 155 | JOptionPane op = |
| 156 | new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE, |
| 157 | JOptionPane.OK_CANCEL_OPTION, null, options, options[1]); |
| 158 | JDialog dlg = op.createDialog(this, "TigerVNC Viewer"); |
| 159 | dlg.setIconImage(VncViewer.frameIcon); |
| 160 | dlg.setAlwaysOnTop(true); |
| 161 | dlg.setVisible(true); |
| 162 | if (op.getValue() == options[0]) |
| 163 | break; |
| 164 | else |
Brian P. Hinz | 2272734 | 2016-08-28 21:15:17 -0400 | [diff] [blame] | 165 | f = Dialog.showChooser(title, f, this, filter); |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 166 | } |
| 167 | if (f != null && (!f.exists() || f.canWrite())) |
| 168 | saveViewerParameters(f.getAbsolutePath(), vncServerName.getValue()); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 169 | } else if (actionMatch(ev, info)) { |
| 170 | cc.showInfo(); |
| 171 | } else if (actionMatch(ev, about)) { |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 172 | VncViewer.showAbout(cc.desktop); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 173 | } else if (actionMatch(ev, dismiss)) { |
| 174 | firePopupMenuCanceled(); |
| 175 | } |
| 176 | } |
| 177 | |
Brian P. Hinz | 985d0eb | 2016-12-10 19:28:42 -0500 | [diff] [blame^] | 178 | public void show(Component invoker, int x, int y) { |
| 179 | // lightweight components can't show in FullScreen Exclusive mode |
| 180 | /* |
| 181 | Window fsw = DesktopWindow.getFullScreenWindow(); |
| 182 | GraphicsDevice gd = null; |
| 183 | if (fsw != null) { |
| 184 | gd = fsw.getGraphicsConfiguration().getDevice(); |
| 185 | if (gd.isFullScreenSupported()) |
| 186 | DesktopWindow.setFullScreenWindow(null); |
| 187 | } |
| 188 | */ |
| 189 | super.show(invoker, x, y); |
| 190 | /* |
| 191 | if (fsw != null && gd.isFullScreenSupported()) |
| 192 | DesktopWindow.setFullScreenWindow(fsw); |
| 193 | */ |
| 194 | } |
| 195 | |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 196 | CConn cc; |
| 197 | JMenuItem restore, move, size, minimize, maximize; |
| 198 | JMenuItem exit, clipboard, ctrlAltDel, refresh; |
Brian Hinz | 92ddde2 | 2012-08-29 03:56:01 +0000 | [diff] [blame] | 199 | JMenuItem newConn, options, save, info, about, dismiss; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 200 | static JMenuItem f8; |
Brian P. Hinz | c456387 | 2016-08-27 17:33:04 -0400 | [diff] [blame] | 201 | JCheckBoxMenuItem fullScreenCheckbox; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 202 | static LogWriter vlog = new LogWriter("F8Menu"); |
| 203 | } |