blob: 0c67305ad2b8c2b775ad811043d934d8a2aed440 [file] [log] [blame]
DRCc5dc0382011-05-13 21:42:14 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Brian Hinz430d8cf2014-01-02 01:23:56 +00002 * Copyright (C) 2011-2014 Brian P. Hinz
Brian Hinzc0a36092013-05-12 15:46:09 +00003 *
DRCc5dc0382011-05-13 21:42:14 +00004 * 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 Hinzc0a36092013-05-12 15:46:09 +00008 *
DRCc5dc0382011-05-13 21:42:14 +00009 * 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 Hinzc0a36092013-05-12 15:46:09 +000013 *
DRCc5dc0382011-05-13 21:42:14 +000014 * 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 Hinzb213da62012-04-11 22:00:55 +000016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
DRCc5dc0382011-05-13 21:42:14 +000017 * USA.
18 */
19
20package com.tigervnc.vncviewer;
21
Brian Hinz4a95c7f2012-09-01 19:24:26 +000022import java.awt.*;
DRCc5dc0382011-05-13 21:42:14 +000023import java.awt.Cursor;
24import java.awt.event.*;
Brian P. Hinzc4563872016-08-27 17:33:04 -040025import java.io.File;
Brian P. Hinz22727342016-08-28 21:15:17 -040026import javax.swing.filechooser.*;
Brian P. Hinzc4563872016-08-27 17:33:04 -040027import javax.swing.JCheckBoxMenuItem;
28import javax.swing.JDialog;
DRCc5dc0382011-05-13 21:42:14 +000029import javax.swing.JFrame;
Brian Hinz92ddde22012-08-29 03:56:01 +000030import javax.swing.JFileChooser;
DRCc5dc0382011-05-13 21:42:14 +000031import javax.swing.JMenuItem;
Brian P. Hinzc4563872016-08-27 17:33:04 -040032import javax.swing.JOptionPane;
33import javax.swing.JPopupMenu;
DRCc5dc0382011-05-13 21:42:14 +000034
35import com.tigervnc.rfb.*;
36
Brian P. Hinzc4563872016-08-27 17:33:04 -040037import static com.tigervnc.vncviewer.Parameters.*;
38
DRCc5dc0382011-05-13 21:42:14 +000039public class F8Menu extends JPopupMenu implements ActionListener {
Brian P. Hinzc4563872016-08-27 17:33:04 -040040 public F8Menu(CConn cc) {
DRCc5dc0382011-05-13 21:42:14 +000041 super("VNC Menu");
42 setLightWeightPopupEnabled(false);
Brian P. Hinzc4563872016-08-27 17:33:04 -040043 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;
DRCc5dc0382011-05-13 21:42:14 +000047 restore = addMenuItem("Restore",KeyEvent.VK_R);
Brian P. Hinzc4563872016-08-27 17:33:04 -040048 restore.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000049 move = addMenuItem("Move");
50 move.setEnabled(false);
51 size = addMenuItem("Size");
52 size.setEnabled(false);
53 minimize = addMenuItem("Minimize", KeyEvent.VK_N);
Brian P. Hinzc4563872016-08-27 17:33:04 -040054 minimize.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000055 maximize = addMenuItem("Maximize", KeyEvent.VK_X);
Brian P. Hinzc4563872016-08-27 17:33:04 -040056 maximize.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000057 addSeparator();
58 exit = addMenuItem("Close Viewer", KeyEvent.VK_C);
59 addSeparator();
Brian P. Hinzc4563872016-08-27 17:33:04 -040060 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);
DRCc5dc0382011-05-13 21:42:14 +000066 addSeparator();
67 clipboard = addMenuItem("Clipboard...");
68 addSeparator();
Brian P. Hinzc4563872016-08-27 17:33:04 -040069 int keyCode = MenuKey.getMenuKeyCode();
70 String keyText = KeyEvent.getKeyText(keyCode);
71 f8 = addMenuItem("Send "+keyText, keyCode);
DRCc5dc0382011-05-13 21:42:14 +000072 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. Hinzc4563872016-08-27 17:33:04 -040077 newConn.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000078 options = addMenuItem("Options...", KeyEvent.VK_O);
Brian Hinz92ddde22012-08-29 03:56:01 +000079 save = addMenuItem("Save connection info as...", KeyEvent.VK_S);
DRCc5dc0382011-05-13 21:42:14 +000080 info = addMenuItem("Connection info...", KeyEvent.VK_I);
Brian Hinz652953c2011-10-06 06:21:32 +000081 about = addMenuItem("About VncViewer...", KeyEvent.VK_A);
DRCc5dc0382011-05-13 21:42:14 +000082 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 Hinz932fac52012-08-26 20:52:15 +0000107 cc.close();
Brian P. Hinzc4563872016-08-27 17:33:04 -0400108 } else if (actionMatch(ev, fullScreenCheckbox)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500109 if (fullScreenCheckbox.isSelected())
110 cc.desktop.fullscreen_on();
111 else
112 cc.desktop.fullscreen_off();
DRCc5dc0382011-05-13 21:42:14 +0000113 } else if (actionMatch(ev, restore)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500114 if (cc.desktop.fullscreen_active())
115 cc.desktop.fullscreen_off();
116 cc.desktop.setExtendedState(JFrame.NORMAL);
DRCc5dc0382011-05-13 21:42:14 +0000117 } else if (actionMatch(ev, minimize)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500118 if (cc.desktop.fullscreen_active())
119 cc.desktop.fullscreen_off();
120 cc.desktop.setExtendedState(JFrame.ICONIFIED);
DRCc5dc0382011-05-13 21:42:14 +0000121 } else if (actionMatch(ev, maximize)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500122 if (cc.desktop.fullscreen_active())
123 cc.desktop.fullscreen_off();
124 cc.desktop.setExtendedState(JFrame.MAXIMIZED_BOTH);
DRCc5dc0382011-05-13 21:42:14 +0000125 } else if (actionMatch(ev, clipboard)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500126 ClipboardDialog.showDialog(cc.desktop);
DRCc5dc0382011-05-13 21:42:14 +0000127 } else if (actionMatch(ev, f8)) {
Brian Hinz1e252c62013-02-09 02:42:14 +0000128 cc.writeKeyEvent(MenuKey.getMenuKeySym(), true);
129 cc.writeKeyEvent(MenuKey.getMenuKeySym(), false);
DRCc5dc0382011-05-13 21:42:14 +0000130 } 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. Hinzc4563872016-08-27 17:33:04 -0400140 VncViewer.newViewer();
DRCc5dc0382011-05-13 21:42:14 +0000141 } else if (actionMatch(ev, options)) {
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500142 OptionsDialog.showDialog(cc.desktop);
Brian Hinz92ddde22012-08-29 03:56:01 +0000143 } else if (actionMatch(ev, save)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400144 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. Hinz22727342016-08-28 21:15:17 -0400148 FileNameExtensionFilter filter =
149 new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc");
150 File f = Dialog.showChooser(title, dflt, this, filter);
Brian P. Hinzc4563872016-08-27 17:33:04 -0400151 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. Hinz22727342016-08-28 21:15:17 -0400165 f = Dialog.showChooser(title, f, this, filter);
Brian P. Hinzc4563872016-08-27 17:33:04 -0400166 }
167 if (f != null && (!f.exists() || f.canWrite()))
168 saveViewerParameters(f.getAbsolutePath(), vncServerName.getValue());
DRCc5dc0382011-05-13 21:42:14 +0000169 } else if (actionMatch(ev, info)) {
170 cc.showInfo();
171 } else if (actionMatch(ev, about)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400172 VncViewer.showAbout(cc.desktop);
DRCc5dc0382011-05-13 21:42:14 +0000173 } else if (actionMatch(ev, dismiss)) {
174 firePopupMenuCanceled();
175 }
176 }
177
Brian P. Hinz985d0eb2016-12-10 19:28:42 -0500178 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
DRCc5dc0382011-05-13 21:42:14 +0000196 CConn cc;
197 JMenuItem restore, move, size, minimize, maximize;
198 JMenuItem exit, clipboard, ctrlAltDel, refresh;
Brian Hinz92ddde22012-08-29 03:56:01 +0000199 JMenuItem newConn, options, save, info, about, dismiss;
DRCc5dc0382011-05-13 21:42:14 +0000200 static JMenuItem f8;
Brian P. Hinzc4563872016-08-27 17:33:04 -0400201 JCheckBoxMenuItem fullScreenCheckbox;
DRCc5dc0382011-05-13 21:42:14 +0000202 static LogWriter vlog = new LogWriter("F8Menu");
203}