blob: 6aadb2d3e9cd517cc49d2b3e3875bcb2954f9e1e [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;
26import javax.swing.JCheckBoxMenuItem;
27import javax.swing.JDialog;
DRCc5dc0382011-05-13 21:42:14 +000028import javax.swing.JFrame;
Brian Hinz92ddde22012-08-29 03:56:01 +000029import javax.swing.JFileChooser;
DRCc5dc0382011-05-13 21:42:14 +000030import javax.swing.JMenuItem;
Brian P. Hinzc4563872016-08-27 17:33:04 -040031import javax.swing.JOptionPane;
32import javax.swing.JPopupMenu;
DRCc5dc0382011-05-13 21:42:14 +000033
34import com.tigervnc.rfb.*;
35
Brian P. Hinzc4563872016-08-27 17:33:04 -040036import static com.tigervnc.vncviewer.Parameters.*;
37
DRCc5dc0382011-05-13 21:42:14 +000038public class F8Menu extends JPopupMenu implements ActionListener {
Brian P. Hinzc4563872016-08-27 17:33:04 -040039 public F8Menu(CConn cc) {
DRCc5dc0382011-05-13 21:42:14 +000040 super("VNC Menu");
41 setLightWeightPopupEnabled(false);
Brian P. Hinzc4563872016-08-27 17:33:04 -040042 String os = System.getProperty("os.name");
43 if (os.startsWith("Windows"))
44 com.sun.java.swing.plaf.windows.WindowsLookAndFeel.setMnemonicHidden(false);
45 this.cc = cc;
DRCc5dc0382011-05-13 21:42:14 +000046 restore = addMenuItem("Restore",KeyEvent.VK_R);
Brian P. Hinzc4563872016-08-27 17:33:04 -040047 restore.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000048 move = addMenuItem("Move");
49 move.setEnabled(false);
50 size = addMenuItem("Size");
51 size.setEnabled(false);
52 minimize = addMenuItem("Minimize", KeyEvent.VK_N);
Brian P. Hinzc4563872016-08-27 17:33:04 -040053 minimize.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000054 maximize = addMenuItem("Maximize", KeyEvent.VK_X);
Brian P. Hinzc4563872016-08-27 17:33:04 -040055 maximize.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000056 addSeparator();
57 exit = addMenuItem("Close Viewer", KeyEvent.VK_C);
58 addSeparator();
Brian P. Hinzc4563872016-08-27 17:33:04 -040059 fullScreenCheckbox = new JCheckBoxMenuItem("Full Screen");
60 fullScreenCheckbox.setMnemonic(KeyEvent.VK_F);
61 fullScreenCheckbox.setSelected(fullScreen.getValue());
62 fullScreenCheckbox.addActionListener(this);
63 fullScreenCheckbox.setEnabled(!embed.getValue());
64 add(fullScreenCheckbox);
DRCc5dc0382011-05-13 21:42:14 +000065 addSeparator();
66 clipboard = addMenuItem("Clipboard...");
67 addSeparator();
Brian P. Hinzc4563872016-08-27 17:33:04 -040068 int keyCode = MenuKey.getMenuKeyCode();
69 String keyText = KeyEvent.getKeyText(keyCode);
70 f8 = addMenuItem("Send "+keyText, keyCode);
DRCc5dc0382011-05-13 21:42:14 +000071 ctrlAltDel = addMenuItem("Send Ctrl-Alt-Del");
72 addSeparator();
73 refresh = addMenuItem("Refresh Screen", KeyEvent.VK_H);
74 addSeparator();
75 newConn = addMenuItem("New connection...", KeyEvent.VK_W);
Brian P. Hinzc4563872016-08-27 17:33:04 -040076 newConn.setEnabled(!embed.getValue());
DRCc5dc0382011-05-13 21:42:14 +000077 options = addMenuItem("Options...", KeyEvent.VK_O);
Brian Hinz92ddde22012-08-29 03:56:01 +000078 save = addMenuItem("Save connection info as...", KeyEvent.VK_S);
DRCc5dc0382011-05-13 21:42:14 +000079 info = addMenuItem("Connection info...", KeyEvent.VK_I);
Brian Hinz652953c2011-10-06 06:21:32 +000080 about = addMenuItem("About VncViewer...", KeyEvent.VK_A);
DRCc5dc0382011-05-13 21:42:14 +000081 addSeparator();
82 dismiss = addMenuItem("Dismiss menu");
83 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
84 }
85
86 JMenuItem addMenuItem(String str, int mnemonic) {
87 JMenuItem item = new JMenuItem(str, mnemonic);
88 item.addActionListener(this);
89 add(item);
90 return item;
91 }
92
93 JMenuItem addMenuItem(String str) {
94 JMenuItem item = new JMenuItem(str);
95 item.addActionListener(this);
96 add(item);
97 return item;
98 }
99
100 boolean actionMatch(ActionEvent ev, JMenuItem item) {
101 return ev.getActionCommand().equals(item.getActionCommand());
102 }
103
104 public void actionPerformed(ActionEvent ev) {
105 if (actionMatch(ev, exit)) {
Brian Hinz932fac52012-08-26 20:52:15 +0000106 cc.close();
Brian P. Hinzc4563872016-08-27 17:33:04 -0400107 } else if (actionMatch(ev, fullScreenCheckbox)) {
DRCc5dc0382011-05-13 21:42:14 +0000108 cc.toggleFullScreen();
109 } else if (actionMatch(ev, restore)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400110 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000111 cc.viewport.setExtendedState(JFrame.NORMAL);
112 } else if (actionMatch(ev, minimize)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400113 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000114 cc.viewport.setExtendedState(JFrame.ICONIFIED);
115 } else if (actionMatch(ev, maximize)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400116 if (fullScreen.getValue()) cc.toggleFullScreen();
DRCc5dc0382011-05-13 21:42:14 +0000117 cc.viewport.setExtendedState(JFrame.MAXIMIZED_BOTH);
118 } else if (actionMatch(ev, clipboard)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400119 //ClipboardDialog dlg = new ClipboardDialog(cc);
120 ClipboardDialog.showDialog(cc.viewport);
DRCc5dc0382011-05-13 21:42:14 +0000121 } else if (actionMatch(ev, f8)) {
Brian Hinz1e252c62013-02-09 02:42:14 +0000122 cc.writeKeyEvent(MenuKey.getMenuKeySym(), true);
123 cc.writeKeyEvent(MenuKey.getMenuKeySym(), false);
DRCc5dc0382011-05-13 21:42:14 +0000124 } else if (actionMatch(ev, ctrlAltDel)) {
125 cc.writeKeyEvent(Keysyms.Control_L, true);
126 cc.writeKeyEvent(Keysyms.Alt_L, true);
127 cc.writeKeyEvent(Keysyms.Delete, true);
128 cc.writeKeyEvent(Keysyms.Delete, false);
129 cc.writeKeyEvent(Keysyms.Alt_L, false);
130 cc.writeKeyEvent(Keysyms.Control_L, false);
131 } else if (actionMatch(ev, refresh)) {
132 cc.refresh();
133 } else if (actionMatch(ev, newConn)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400134 VncViewer.newViewer();
DRCc5dc0382011-05-13 21:42:14 +0000135 } else if (actionMatch(ev, options)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400136 OptionsDialog.showDialog(cc.viewport);
Brian Hinz92ddde22012-08-29 03:56:01 +0000137 } else if (actionMatch(ev, save)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400138 String title = "Save the TigerVNC configuration to file";
139 File dflt = new File(FileUtils.getVncHomeDir().concat("default.tigervnc"));
140 if (!dflt.exists() || !dflt.isFile())
141 dflt = new File(FileUtils.getVncHomeDir());
142 File f = Dialog.showChooser(title, dflt, this);
143 while (f != null && f.exists() && f.isFile()) {
144 String msg = f.getAbsolutePath();
145 msg = msg.concat(" already exists. Do you want to overwrite?");
146 Object[] options = {"Overwrite", "No \u21B5"};
147 JOptionPane op =
148 new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE,
149 JOptionPane.OK_CANCEL_OPTION, null, options, options[1]);
150 JDialog dlg = op.createDialog(this, "TigerVNC Viewer");
151 dlg.setIconImage(VncViewer.frameIcon);
152 dlg.setAlwaysOnTop(true);
153 dlg.setVisible(true);
154 if (op.getValue() == options[0])
155 break;
156 else
157 f = Dialog.showChooser(title, f, this);
158 }
159 if (f != null && (!f.exists() || f.canWrite()))
160 saveViewerParameters(f.getAbsolutePath(), vncServerName.getValue());
DRCc5dc0382011-05-13 21:42:14 +0000161 } else if (actionMatch(ev, info)) {
162 cc.showInfo();
163 } else if (actionMatch(ev, about)) {
Brian P. Hinzc4563872016-08-27 17:33:04 -0400164 VncViewer.showAbout(cc.desktop);
DRCc5dc0382011-05-13 21:42:14 +0000165 } else if (actionMatch(ev, dismiss)) {
166 firePopupMenuCanceled();
167 }
168 }
169
170 CConn cc;
171 JMenuItem restore, move, size, minimize, maximize;
172 JMenuItem exit, clipboard, ctrlAltDel, refresh;
Brian Hinz92ddde22012-08-29 03:56:01 +0000173 JMenuItem newConn, options, save, info, about, dismiss;
DRCc5dc0382011-05-13 21:42:14 +0000174 static JMenuItem f8;
Brian P. Hinzc4563872016-08-27 17:33:04 -0400175 JCheckBoxMenuItem fullScreenCheckbox;
DRCc5dc0382011-05-13 21:42:14 +0000176 static LogWriter vlog = new LogWriter("F8Menu");
177}