blob: d7f9e482ceb6862742063ab248e59234e3bbc21e [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)) {
DRCc5dc0382011-05-13 21:42:14 +0000109 cc.toggleFullScreen();
110 } else if (actionMatch(ev, restore)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400111 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000112 cc.viewport.setExtendedState(JFrame.NORMAL);
113 } else if (actionMatch(ev, minimize)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400114 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000115 cc.viewport.setExtendedState(JFrame.ICONIFIED);
116 } else if (actionMatch(ev, maximize)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400117 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000118 cc.viewport.setExtendedState(JFrame.MAXIMIZED_BOTH);
119 } else if (actionMatch(ev, clipboard)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400120 //ClipboardDialog dlg = new ClipboardDialog(cc);
121 ClipboardDialog.showDialog(cc.viewport);
DRCc5dc0382011-05-13 21:42:14 +0000122 } else if (actionMatch(ev, f8)) {
Brian Hinz1e252c62013-02-09 02:42:14 +0000123 cc.writeKeyEvent(MenuKey.getMenuKeySym(), true);
124 cc.writeKeyEvent(MenuKey.getMenuKeySym(), false);
DRCc5dc0382011-05-13 21:42:14 +0000125 } 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. Hinzc4563872016-08-27 17:33:04 -0400135 VncViewer.newViewer();
DRCc5dc0382011-05-13 21:42:14 +0000136 } else if (actionMatch(ev, options)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400137 OptionsDialog.showDialog(cc.viewport);
Brian Hinz92ddde22012-08-29 03:56:01 +0000138 } else if (actionMatch(ev, save)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400139 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. Hinz22727342016-08-28 21:15:17 -0400143 FileNameExtensionFilter filter =
144 new FileNameExtensionFilter("TigerVNC configuration (*.tigervnc)", "tigervnc");
145 File f = Dialog.showChooser(title, dflt, this, filter);
Brian P. Hinzc4563872016-08-27 17:33:04 -0400146 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. Hinz22727342016-08-28 21:15:17 -0400160 f = Dialog.showChooser(title, f, this, filter);
Brian P. Hinzc4563872016-08-27 17:33:04 -0400161 }
162 if (f != null && (!f.exists() || f.canWrite()))
163 saveViewerParameters(f.getAbsolutePath(), vncServerName.getValue());
DRCc5dc0382011-05-13 21:42:14 +0000164 } else if (actionMatch(ev, info)) {
165 cc.showInfo();
166 } else if (actionMatch(ev, about)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400167 VncViewer.showAbout(cc.desktop);
DRCc5dc0382011-05-13 21:42:14 +0000168 } 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 Hinz92ddde22012-08-29 03:56:01 +0000176 JMenuItem newConn, options, save, info, about, dismiss;
DRCc5dc0382011-05-13 21:42:14 +0000177 static JMenuItem f8;
Brian P. Hinzc4563872016-08-27 17:33:04 -0400178 JCheckBoxMenuItem fullScreenCheckbox;
DRCc5dc0382011-05-13 21:42:14 +0000179 static LogWriter vlog = new LogWriter("F8Menu");
180}