| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.actions; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 6 | |
|---|
| 7 | import java.awt.Frame; |
|---|
| 8 | import java.awt.GraphicsDevice; |
|---|
| 9 | import java.awt.GraphicsEnvironment; |
|---|
| 10 | import java.awt.Rectangle; |
|---|
| 11 | import java.awt.Toolkit; |
|---|
| 12 | import java.awt.Window; |
|---|
| 13 | import java.awt.event.ActionEvent; |
|---|
| 14 | import java.awt.event.KeyEvent; |
|---|
| 15 | import java.util.ArrayList; |
|---|
| 16 | import java.util.List; |
|---|
| 17 | |
|---|
| 18 | import javax.swing.ButtonModel; |
|---|
| 19 | import javax.swing.JComponent; |
|---|
| 20 | import javax.swing.JFrame; |
|---|
| 21 | import javax.swing.KeyStroke; |
|---|
| 22 | |
|---|
| 23 | import org.openstreetmap.josm.Main; |
|---|
| 24 | import org.openstreetmap.josm.tools.PlatformHookWindows; |
|---|
| 25 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 26 | |
|---|
| 27 | public class FullscreenToggleAction extends JosmAction { |
|---|
| 28 | private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>(); |
|---|
| 29 | private GraphicsDevice gd; |
|---|
| 30 | private Rectangle prevBounds; |
|---|
| 31 | |
|---|
| 32 | public FullscreenToggleAction() { |
|---|
| 33 | super( |
|---|
| 34 | tr("Fullscreen view"), |
|---|
| 35 | null, /* no icon */ |
|---|
| 36 | tr("Toggle fullscreen view"), |
|---|
| 37 | Shortcut.registerShortcut("menu:view:fullscreen", tr("Toggle fullscreen view"),KeyEvent.VK_F11, Shortcut.DIRECT), |
|---|
| 38 | false /* register */ |
|---|
| 39 | ); |
|---|
| 40 | putValue("help", ht("/Action/FullscreenView")); |
|---|
| 41 | putValue("toolbar", "fullscreen"); |
|---|
| 42 | Main.toolbar.register(this); |
|---|
| 43 | gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); |
|---|
| 44 | putValue(SELECTED_KEY, Main.pref.getBoolean("draw.fullscreen", false)); |
|---|
| 45 | notifySelectedState(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public void addButtonModel(ButtonModel model) { |
|---|
| 49 | if (model != null && !buttonModels.contains(model)) { |
|---|
| 50 | buttonModels.add(model); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | public void removeButtonModel(ButtonModel model) { |
|---|
| 55 | if (model != null && buttonModels.contains(model)) { |
|---|
| 56 | buttonModels.remove(model); |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | protected void notifySelectedState() { |
|---|
| 61 | for (ButtonModel model: buttonModels) { |
|---|
| 62 | if (model.isSelected() != isSelected()) { |
|---|
| 63 | model.setSelected(isSelected()); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | protected void toggleSelectedState() { |
|---|
| 69 | putValue(SELECTED_KEY, !isSelected()); |
|---|
| 70 | Main.pref.put("draw.fullscreen", isSelected()); |
|---|
| 71 | notifySelectedState(); |
|---|
| 72 | setMode(); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | public void initial() { |
|---|
| 76 | if (isSelected()) { |
|---|
| 77 | setMode(); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | protected void setMode() { |
|---|
| 82 | JFrame frame = (JFrame) Main.parent; |
|---|
| 83 | |
|---|
| 84 | List<Window> visibleWindows = new ArrayList<Window>(); |
|---|
| 85 | visibleWindows.add(frame); |
|---|
| 86 | for (Window w : Frame.getWindows()) { |
|---|
| 87 | if (w.isVisible() && w != frame) { |
|---|
| 88 | visibleWindows.add(w); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | frame.dispose(); |
|---|
| 93 | frame.setUndecorated(isSelected()); |
|---|
| 94 | |
|---|
| 95 | if (isSelected()) { |
|---|
| 96 | prevBounds = frame.getBounds(); |
|---|
| 97 | frame.setBounds(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | // we cannot use hw-exclusive fullscreen mode in MS-Win, as long |
|---|
| 101 | // as josm throws out modal dialogs, see here: |
|---|
| 102 | // http://forums.sun.com/thread.jspa?threadID=5351882 FIXME this url does not work anymore |
|---|
| 103 | // |
|---|
| 104 | // the good thing is: fullscreen works without exclusive mode, |
|---|
| 105 | // since windows (or java?) draws the undecorated window full- |
|---|
| 106 | // screen by default (it's a simulated mode, but should be ok) |
|---|
| 107 | String exclusive = Main.pref.get("draw.fullscreen.exclusive-mode", "auto"); |
|---|
| 108 | if ("true".equals(exclusive) || ("auto".equals(exclusive) && !(Main.platform instanceof PlatformHookWindows))) { |
|---|
| 109 | gd.setFullScreenWindow(isSelected() ? frame : null); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | if (!isSelected() && prevBounds != null) { |
|---|
| 113 | frame.setBounds(prevBounds); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | for (Window wind : visibleWindows) { |
|---|
| 117 | wind.setVisible(true); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | // Free F10 key to allow it to be used by plugins, even after full screen (see #7502) |
|---|
| 121 | frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none"); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | public void actionPerformed(ActionEvent e) { |
|---|
| 125 | toggleSelectedState(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public final boolean isSelected() { |
|---|
| 129 | return (Boolean)getValue(SELECTED_KEY); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|