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 boolean selected;
|
---|
30 | private GraphicsDevice gd;
|
---|
31 | private Rectangle prevBounds;
|
---|
32 |
|
---|
33 | public FullscreenToggleAction() {
|
---|
34 | super(
|
---|
35 | tr("Fullscreen view"),
|
---|
36 | null, /* no icon */
|
---|
37 | tr("Toggle fullscreen view"),
|
---|
38 | Shortcut.registerShortcut("menu:view:fullscreen", tr("Toggle fullscreen view"),KeyEvent.VK_F11, Shortcut.DIRECT),
|
---|
39 | false /* register */
|
---|
40 | );
|
---|
41 | putValue("help", ht("/Action/FullscreenView"));
|
---|
42 | putValue("toolbar", "fullscreen");
|
---|
43 | Main.toolbar.register(this);
|
---|
44 | gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
|
---|
45 | selected = Main.pref.getBoolean("draw.fullscreen", false);
|
---|
46 | notifySelectedState();
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void addButtonModel(ButtonModel model) {
|
---|
50 | if (model != null && !buttonModels.contains(model)) {
|
---|
51 | buttonModels.add(model);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void removeButtonModel(ButtonModel model) {
|
---|
56 | if (model != null && buttonModels.contains(model)) {
|
---|
57 | buttonModels.remove(model);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected void notifySelectedState() {
|
---|
62 | for (ButtonModel model: buttonModels) {
|
---|
63 | if (model.isSelected() != selected) {
|
---|
64 | model.setSelected(selected);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void toggleSelectedState() {
|
---|
70 | selected = !selected;
|
---|
71 | Main.pref.put("draw.fullscreen", selected);
|
---|
72 | notifySelectedState();
|
---|
73 | setMode();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void initial() {
|
---|
77 | if(selected)
|
---|
78 | setMode();
|
---|
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(selected);
|
---|
94 |
|
---|
95 | if (selected) {
|
---|
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.
|
---|
102 | //
|
---|
103 | // the good thing is: fullscreen works without exclusive mode,
|
---|
104 | // since windows (or java?) draws the undecorated window full-
|
---|
105 | // screen by default (it's a simulated mode, but should be ok)
|
---|
106 | String exclusive = Main.pref.get("draw.fullscreen.exclusive-mode", "auto");
|
---|
107 | if ("true".equals(exclusive) || ("auto".equals(exclusive) && !(Main.platform instanceof PlatformHookWindows))) {
|
---|
108 | gd.setFullScreenWindow(selected ? frame : null);
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (!selected && prevBounds != null) {
|
---|
112 | frame.setBounds(prevBounds);
|
---|
113 | }
|
---|
114 |
|
---|
115 | for (Window wind : visibleWindows) {
|
---|
116 | wind.setVisible(true);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Free F10 key to allow it to be used by plugins, even after full screen (see #7502)
|
---|
120 | frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public void actionPerformed(ActionEvent e) {
|
---|
125 | toggleSelectedState();
|
---|
126 | }
|
---|
127 | }
|
---|