/** * This method is needed in the case where the display has multiple monitors, but they do not form a uniform * rectangle. In this case, it is possible for Geometry.moveInside() to not detect that the window is partially or * completely clipped. We check to make sure at least the upper left portion of the rectangle is visible to give the * user the ability to reposition the dialog in this rare case. * * @param constrainee * @param display * @return */ private boolean isClippedByUnalignedMonitors( Rectangle constrainee, Display display ) { boolean isClipped; Monitor[] monitors = display.getMonitors(); if ( monitors.length > 0 ) { // Loop searches for a monitor proving false isClipped = true; for ( Monitor monitor : monitors ) { if ( monitor.getClientArea().contains( constrainee.x + 10, constrainee.y + 10 ) ) { isClipped = false; break; } } } else { isClipped = false; } return isClipped; }
public static void setSize( Shell shell, int minWidth, int minHeight, boolean packIt ) { PropsUI props = PropsUI.getInstance(); WindowProperty winprop = props.getScreen( shell.getText() ); if ( winprop != null ) { winprop.setShell( shell, minWidth, minHeight ); } else { if ( packIt ) { shell.pack(); } else { shell.layout(); } // OK, sometimes this produces dialogs that are waay too big. // Try to limit this a bit, m'kay? // Use the same algorithm by cheating :-) // winprop = new WindowProperty( shell ); winprop.setShell( shell, minWidth, minHeight ); // Now, as this is the first time it gets opened, try to put it in the middle of the screen... Rectangle shellBounds = shell.getBounds(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); int middleX = monitorClientArea.x + ( monitorClientArea.width - shellBounds.width ) / 2; int middleY = monitorClientArea.y + ( monitorClientArea.height - shellBounds.height ) / 2; shell.setLocation( middleX, middleY ); } }
public static void setSize( Shell shell, int prefWidth, int prefHeight ) { PropsUI props = PropsUI.getInstance(); WindowProperty winprop = props.getScreen( shell.getText() ); if ( winprop != null ) { winprop.setShell( shell, prefWidth, prefHeight ); } else { shell.layout(); winprop = new WindowProperty( shell.getText(), false, new Rectangle( 0, 0, prefWidth, prefHeight ) ); winprop.setShell( shell ); // Now, as this is the first time it gets opened, try to put it in the middle of the screen... Rectangle shellBounds = shell.getBounds(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); int middleX = monitorClientArea.x + ( monitorClientArea.width - shellBounds.width ) / 2; int middleY = monitorClientArea.y + ( monitorClientArea.height - shellBounds.height ) / 2; shell.setLocation( middleX, middleY ); } }
monitor = shell.getParent().getMonitor(); Rectangle monitorClientArea = monitor.getClientArea(); constrainRectangleToContainer( resizedRect, monitorClientArea );
monitor = shell.getParent().getMonitor(); Rectangle monitorClientArea = monitor.getClientArea();
/** * This method was copy/pasted from JFace. */ private Rectangle getConstrainedShellBounds(Display display, Rectangle preferredSize) { Rectangle result = new Rectangle(preferredSize.x, preferredSize.y, preferredSize.width, preferredSize.height); Point topLeft = new Point(preferredSize.x, preferredSize.y); Monitor mon = Util.getClosestMonitor(display, topLeft); Rectangle bounds = mon.getClientArea(); if (result.height > bounds.height) { result.height = bounds.height; } if (result.width > bounds.width) { result.width = bounds.width; } result.x = Math.max(bounds.x, Math.min(result.x, bounds.x + bounds.width - result.width)); result.y = Math.max(bounds.y, Math.min(result.y, bounds.y + bounds.height - result.height)); return result; }
/** * Sets the size of the shell to it's "packed" size, * unless that makes it larger than the monitor it is being displayed on, * in which case just set the shell size to be slightly smaller than the monitor. */ static void setShellSize(ControlExample instance, Shell shell) { Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT); Rectangle monitorArea = shell.getMonitor().getClientArea(); shell.setSize(Math.min(size.x, monitorArea.width), Math.min(size.y, monitorArea.height)); } }
@Override protected Point getInitialLocation(Point size) { if (fAnchor == null) { return super.getInitialLocation(size); } Point point = fAnchor; Rectangle monitor = getShell().getMonitor().getClientArea(); if (monitor.width < point.x + size.x) { point.x = Math.max(0, point.x - size.x); } if (monitor.height < point.y + size.y) { point.y = Math.max(0, point.y - size.y); } return point; }
@Override protected Point getInitialLocation(Point size) { if (fAnchor == null) { return super.getInitialLocation(size); } Point point = fAnchor; Rectangle monitor = getShell().getMonitor().getClientArea(); if (monitor.width < point.x + size.x) { point.x = Math.max(0, point.x - size.x); } if (monitor.height < point.y + size.y) { point.y = Math.max(0, point.y - size.y); } return point; }
private void sizeDialog(final Shell dialog) { dialog.pack(); Monitor monitor = dialog.getShell().getMonitor(); if (monitor != null) { if (dialog.getSize().x > monitor.getClientArea().width) { dialog.setSize(monitor.getClientArea().width, dialog.getSize().y); } if (dialog.getSize().y > monitor.getClientArea().height) { dialog.setSize(dialog.getSize().x, monitor.getClientArea().height); } } dialog.layout(); }
void center () { if (parent == null) return; Rectangle rect = getBoundsInPixels (); Rectangle parentRect = display.mapInPixels (parent, null, parent.getClientAreaInPixels()); int x = Math.max (parentRect.x, parentRect.x + (parentRect.width - rect.width) / 2); int y = Math.max (parentRect.y, parentRect.y + (parentRect.height - rect.height) / 2); Rectangle monitorRect = parent.getMonitor ().getClientArea(); if (x + rect.width > monitorRect.x + monitorRect.width) { x = Math.max (monitorRect.x, monitorRect.x + monitorRect.width - rect.width); } else { x = Math.max (x, monitorRect.x); } if (y + rect.height > monitorRect.y + monitorRect.height) { y = Math.max (monitorRect.y, monitorRect.y + monitorRect.height - rect.height); } else { y = Math.max (y, monitorRect.y); } setLocationInPixels (x, y); }
/** * * Returns true iff the given rectangle is located in the client area of any * monitor. * * @param display * the display * @param someRectangle * a rectangle in display coordinates (not null) * @return true iff the given point can be seen on any monitor */ public static boolean intersectsAnyMonitor(Display display, Rectangle someRectangle) { for (Monitor monitor : display.getMonitors()) { if (monitor.getClientArea().intersects(someRectangle)) { return true; } } return false; }
void center () { if (parent == null) return; Rectangle rect = getBounds (); Rectangle parentRect = display.map (parent, null, parent.getClientArea()); int x = Math.max (parentRect.x, parentRect.x + (parentRect.width - rect.width) / 2); int y = Math.max (parentRect.y, parentRect.y + (parentRect.height - rect.height) / 2); Rectangle monitorRect = parent.getMonitor ().getClientArea(); if (x + rect.width > monitorRect.x + monitorRect.width) { x = Math.max (monitorRect.x, monitorRect.x + monitorRect.width - rect.width); } else { x = Math.max (x, monitorRect.x); } if (y + rect.height > monitorRect.y + monitorRect.height) { y = Math.max (monitorRect.y, monitorRect.y + monitorRect.height - rect.height); } else { y = Math.max (y, monitorRect.y); } setLocation (x, y); }
void center () { if (parent == null) return; Rectangle rect = getBoundsInPixels (); Rectangle parentRect = display.mapInPixels (parent, null, parent.getClientAreaInPixels()); int x = Math.max (parentRect.x, parentRect.x + (parentRect.width - rect.width) / 2); int y = Math.max (parentRect.y, parentRect.y + (parentRect.height - rect.height) / 2); Rectangle monitorRect = DPIUtil.autoScaleUp(parent.getMonitor ().getClientArea()); if (x + rect.width > monitorRect.x + monitorRect.width) { x = Math.max (monitorRect.x, monitorRect.x + monitorRect.width - rect.width); } else { x = Math.max (x, monitorRect.x); } if (y + rect.height > monitorRect.y + monitorRect.height) { y = Math.max (monitorRect.y, monitorRect.y + monitorRect.height - rect.height); } else { y = Math.max (y, monitorRect.y); } setLocationInPixels (x, y); }
void center () { if (parent == null) return; Rectangle rect = getBoundsInPixels (); Rectangle parentRect = display.mapInPixels (parent, null, parent.getClientAreaInPixels()); int x = Math.max (parentRect.x, parentRect.x + (parentRect.width - rect.width) / 2); int y = Math.max (parentRect.y, parentRect.y + (parentRect.height - rect.height) / 2); Rectangle monitorRect = DPIUtil.autoScaleUp(parent.getMonitor ().getClientArea()); if (x + rect.width > monitorRect.x + monitorRect.width) { x = Math.max (monitorRect.x, monitorRect.x + monitorRect.width - rect.width); } else { x = Math.max (x, monitorRect.x); } if (y + rect.height > monitorRect.y + monitorRect.height) { y = Math.max (monitorRect.y, monitorRect.y + monitorRect.height - rect.height); } else { y = Math.max (y, monitorRect.y); } setLocationInPixels (x, y); }
/** * Crops the given bounds such that they lie completely on the closest monitor. * * @param bounds shell bounds to crop * @since 3.4 */ void cropToClosestMonitor(Rectangle bounds) { Rectangle monitorBounds= getClosestMonitor(fSubjectControl.getDisplay(), bounds).getClientArea(); bounds.intersect(monitorBounds); }
@Override protected Point getInitialLocation(Point initialSize) { Composite parent = getShell().getParent(); if (!centerOnMonitor || parent == null) { return super.getInitialLocation(initialSize); } Monitor monitor = parent.getMonitor(); Rectangle monitorBounds = monitor.getClientArea(); Point centerPoint = Geometry.centerPoint(monitorBounds); return new Point(centerPoint.x - (initialSize.x / 2), Math.max( monitorBounds.y, Math.min(centerPoint.y - (initialSize.y * 2 / 3), monitorBounds.y + monitorBounds.height - initialSize.y))); }
/** * Crops the given bounds such that they lie completely on the closest monitor. * * @param bounds shell bounds to crop * @since 3.4 */ void cropToClosestMonitor(Rectangle bounds) { Rectangle monitorBounds= Util.getClosestMonitor(fSubjectControl.getDisplay(), Geometry.centerPoint(bounds)).getClientArea(); bounds.intersect(monitorBounds); }
protected Point getStackedLocation(Shell shell, Shell parent) { Point p= parent.getLocation(); Point size= parent.getSize(); p.x += size.x / 4; p.y += size.y; p= parent.toDisplay(p); Point shellSize= shell.getSize(); Monitor monitor= getClosestMonitor(parent.getDisplay(), new Rectangle(p.x, p.y, 0, 0)); Rectangle displayBounds= monitor.getClientArea(); constrainLocation(p, shellSize, displayBounds); return p; }
protected Point getStackedLocation(Shell shell, Shell parent) { Point p= parent.getLocation(); Point size= parent.getSize(); p.x += size.x / 4; p.y += size.y; p= parent.toDisplay(p); Point shellSize= shell.getSize(); Monitor monitor= getClosestMonitor(parent.getDisplay(), new Rectangle(p.x, p.y, 0, 0)); Rectangle displayBounds= monitor.getClientArea(); constrainLocation(p, shellSize, displayBounds); return p; }