1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.chassis;
\r
14 import java.awt.BorderLayout;
\r
15 import java.awt.DisplayMode;
\r
16 import java.awt.Frame;
\r
17 import java.awt.GraphicsDevice;
\r
18 import java.awt.GraphicsEnvironment;
\r
19 import java.awt.event.KeyAdapter;
\r
20 import java.awt.event.KeyEvent;
\r
22 import javax.swing.JFrame;
\r
24 import org.eclipse.jface.viewers.ArrayContentProvider;
\r
25 import org.eclipse.jface.viewers.LabelProvider;
\r
26 import org.eclipse.jface.window.Window;
\r
27 import org.eclipse.swt.graphics.Rectangle;
\r
28 import org.eclipse.swt.widgets.Display;
\r
29 import org.eclipse.swt.widgets.Monitor;
\r
30 import org.eclipse.swt.widgets.Shell;
\r
31 import org.eclipse.ui.dialogs.ListDialog;
\r
32 import org.simantics.g2d.canvas.ICanvasContext;
\r
33 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
\r
34 import org.simantics.g2d.participant.KeyToCommand;
\r
35 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
\r
36 import org.simantics.scenegraph.g2d.events.command.Command;
\r
37 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
\r
38 import org.simantics.scenegraph.g2d.events.command.CommandKeyBinding;
\r
39 import org.simantics.scenegraph.g2d.events.command.Commands;
\r
40 import org.simantics.utils.datastructures.cache.IProvider;
\r
42 public class FullscreenUtils {
\r
47 * Views a canvas in full screen mode
\r
48 * @param monitor display device
\r
49 * @param ctx canvas context
\r
52 public static Frame viewFullScreen(GraphicsDevice monitor, ICanvasContext ctx)
\r
54 DisplayMode dm = monitor.getDisplayMode();
\r
55 final JFrame frame = new JFrame("Fullscreen mode");
\r
56 frame.setSize(dm.getWidth(), dm.getHeight());
\r
57 frame.setUndecorated(true);
\r
59 // This is an empty content area in the frame
\r
60 AWTChassis chassis = new AWTChassis();
\r
61 // There is a background painter in canvas context (==it is opaque)
\r
62 chassis.setOpaque(true);
\r
64 chassis.addKeyListener(new KeyAdapter() {
\r
66 public void keyPressed(KeyEvent e) {
\r
67 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
\r
71 frame.getContentPane().add(chassis, BorderLayout.CENTER);
\r
75 java.awt.Rectangle awtBounds = monitor.getDefaultConfiguration().getBounds();
\r
76 frame.setBounds(awtBounds);
\r
77 frame.setAlwaysOnTop(true);
\r
78 frame.setVisible(true);
\r
80 chassis.setCanvasContext(ctx);
\r
85 * Adds Alt-Enter full screen handler to a canvas context
\r
88 public static void addFullScreenHandler(ICanvasContext ctx, final Shell shell, final IProvider<ICanvasContext> fullscreenProvider)
\r
91 KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
\r
93 ctx.add( commands = new KeyToCommand() );
\r
94 commands.addBinding( new CommandKeyBinding(Commands.FULL_SCREEN, "View in full screen mode", KeyEvent.VK_ENTER, KeyEvent.VK_CONTROL) );
\r
95 ctx.add( new AbstractCanvasParticipant() {
\r
96 @SuppressWarnings("unused")
\r
97 @EventHandler(priority = 0)
\r
98 public boolean handleEvent(CommandEvent e) {
\r
99 assertDependencies();
\r
100 Command c = e.command;
\r
101 // Arrow key panning
\r
102 if (c.equals(Commands.FULL_SCREEN))
\r
104 shell.getDisplay().asyncExec(new Runnable() {
\r
106 public void run() {
\r
107 GraphicsDevice gd = getOrQueryDisplay(shell);
\r
109 viewFullScreen(gd, fullscreenProvider.get());
\r
119 * Gets a single display or dialogs
\r
121 * @return null or a display
\r
123 public static GraphicsDevice getOrQueryDisplay(Shell shell)
\r
125 GraphicsDevice list[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
\r
126 if (list.length==0) return null;
\r
127 if (list.length==1) return list[0];
\r
128 return doDisplayDialog(shell);
\r
132 * Dialog for choosing display device
\r
135 public static GraphicsDevice doDisplayDialog(Shell shell)
\r
137 ListDialog ld = new ListDialog(shell);
\r
138 ld.setInput(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices());
\r
139 ld.setTitle("Select display device");
\r
140 ld.setInitialSelections(new Object[] {GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()});
\r
141 ld.setMessage("Select display device");
\r
142 ld.setContentProvider(new ArrayContentProvider());
\r
143 ld.setLabelProvider(new LabelProvider() {
\r
145 public String getText(Object element) {
\r
146 GraphicsDevice gd = (GraphicsDevice) element;
\r
147 return gd.getIDstring()+" ("+gd.getDisplayMode().getWidth()+"*"+gd.getDisplayMode().getHeight()+")";
\r
150 ld.setBlockOnOpen(true);
\r
151 if (ld.open() != Window.OK) return null;
\r
152 return (GraphicsDevice)ld.getResult()[0];
\r
156 * Adds esc to close full screen view
\r
159 @SuppressWarnings("unused")
\r
160 private static void addCloseHandler(ICanvasContext ctx, final Frame frame)
\r
163 KeyToCommand commands = ctx.getAtMostOneItemOfClass(KeyToCommand.class);
\r
164 if (commands==null)
\r
165 ctx.add( commands = new KeyToCommand() );
\r
166 commands.addBinding( new CommandKeyBinding(Commands.CLOSE, "Close Canvas", KeyEvent.VK_ESCAPE) );
\r
167 ctx.add( new AbstractCanvasParticipant() {
\r
168 @EventHandler(priority = 0)
\r
169 public boolean handleEvent(CommandEvent e) {
\r
170 assertDependencies();
\r
178 * Get the monitor the shell is mostly in
\r
182 public static Monitor getMonitor(Shell shell)
\r
184 Monitor result = null;
\r
185 long largestArea = 0;
\r
187 Rectangle shellRectangle = shell.getBounds();
\r
188 for (Monitor monitor : Display.getCurrent().getMonitors())
\r
190 Rectangle monitorBounds = monitor.getBounds();
\r
191 Rectangle intersection = monitorBounds.intersection(shellRectangle);
\r
192 long area = intersection.width * intersection.height;
\r
193 if (area>largestArea) {
\r
194 largestArea = area;
\r
202 * Gets corresponding awt monitor for a swt monitor
\r
203 * @param swtMonitor
\r
206 public static GraphicsDevice getMonitorCorrespondence(Monitor swtMonitor)
\r
208 Rectangle swtBounds = swtMonitor.getBounds();
\r
210 for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
\r
212 if (gd.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) continue;
\r
213 java.awt.Rectangle awtBounds = gd.getDefaultConfiguration().getBounds();
\r
214 if (awtBounds.x == swtBounds.x && awtBounds.y == swtBounds.y && awtBounds.width == swtBounds.width && awtBounds.height == swtBounds.height)
\r