/** * Returns a rectangle describing the receiver's size and location relative to its parent. * * @return the receiver's bounding rectangle * @exception SWTException * <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver * </ul> * * @since 1.3 */ public Rectangle getBounds() { checkWidget(); Rectangle result = new Rectangle(0, 0, 0, 0); int index = parent.indexOf(this); if (index != -1) { int selectionIndex = parent.getSelectionIndex(); boolean selected = index == selectionIndex; Rectangle padding = parent.getItemPadding(selected); String text = getText(); if (text != null) { Point extent = Graphics.stringExtent(parent.getFont(), text); result.width = extent.x; result.height = extent.y; } Image image = getImage(); if (image != null) { Rectangle imageSize = image.getBounds(); result.width += imageSize.width + IMAGE_TEXT_SPACING; result.height = Math.max(result.height, imageSize.height); } result.width += 2 * ITEM_BORDER + padding.width; result.height += ITEM_BORDER + padding.height; if (selected) { result.height += SELECTED_ITEM_BORDER; } if (selectionIndex != -1) { if (index + 1 == selectionIndex || index - 1 == selectionIndex) { result.width -= ITEM_BORDER; } } if (isBarTop()) { if (index != selectionIndex) { result.y += SELECTED_ITEM_BORDER; } } else { result.y = parent.getBounds().height - 2 * parent.getBorderWidth() - result.height; if (index != selectionIndex) { result.y -= SELECTED_ITEM_BORDER; } } if (index > 0) { TabItem leftItem = parent.getItem(index - 1); Rectangle leftItemBounds = leftItem.getBounds(); result.x = leftItemBounds.x + leftItemBounds.width + TABS_SPACING; if (index == selectionIndex || index - 1 == selectionIndex) { result.x -= TABS_SPACING; } } } return result; }
public void positionWindow(boolean resetToDefault) { String mapWindow; mapShellBounds = mapShell.getBounds(); mapWindow = prefs.get(RadarConsts.PREF_KEY_MAP_WINDOW, ""); if (mapWindow.equals("") || resetToDefault) { Rectangle dispBounds, tableBounds; dispBounds = ((Display.getCurrent()).getPrimaryMonitor()).getClientArea(); tableBounds = display.getShells()[1].getBounds(); mapShellBounds.width = dispBounds.width - tableBounds.width; mapShellBounds.height = dispBounds.height; mapShellBounds.x = dispBounds.x + tableBounds.width; mapShellBounds.y = dispBounds.y; } else { String[] tokens = mapWindow.split(","); mapShellBounds = new Rectangle( new Integer(tokens[0]), // X value new Integer(tokens[1]), // Y value new Integer(tokens[2]), // Width new Integer(tokens[3]) // Height ); } mapShell.setBounds(mapShellBounds); }
private Rectangle getDrawBounds(TableCellSWT cell) { Rectangle bounds = cell.getBounds(); bounds.height -= 12; bounds.y += 6; bounds.x += 4; bounds.width -= 4; return bounds; }
void moveRectangles(int xChange, int yChange) { if (bounds == null) return; if (xChange < 0 && ((style & SWT.LEFT) == 0)) xChange = 0; if (xChange > 0 && ((style & SWT.RIGHT) == 0)) xChange = 0; if (yChange < 0 && ((style & SWT.UP) == 0)) yChange = 0; if (yChange > 0 && ((style & SWT.DOWN) == 0)) yChange = 0; if (xChange == 0 && yChange == 0) return; bounds.x += xChange; bounds.y += yChange; for (int i = 0; i < rectangles.length; i++) { rectangles[i].x += xChange; rectangles[i].y += yChange; } }
void resizeControl() { if (control != null && !control.isDisposed()) { /* * Set the size and location of the control * separately to minimize flashing in the * case where the control does not resize * to the size that was requested. This * case can occur when the control is a * combo box. */ Rectangle itemRect = getBounds(); control.setSize(itemRect.width, itemRect.height); Rectangle rect = control.getBounds(); rect.x = itemRect.x + (itemRect.width - rect.width) / 2; rect.y = itemRect.y + (itemRect.height - rect.height) / 2; control.setLocation(rect.x, rect.y); } }
/** * Returns an axis-aligned bounding box of this polygon. * * <p>Note the returned Rectangle is cached in this polygon, and will be reused if this Polygon is * changed. * * @return this polygon's bounding box {@link Rectangle} */ public Rectangle getBoundingRectangle() { float[] vertices = getTransformedVertices(); float minX = vertices[0]; float minY = vertices[1]; float maxX = vertices[0]; float maxY = vertices[1]; final int numFloats = vertices.length; for (int i = 2; i < numFloats; i += 2) { minX = minX > vertices[i] ? vertices[i] : minX; minY = minY > vertices[i + 1] ? vertices[i + 1] : minY; maxX = maxX < vertices[i] ? vertices[i] : maxX; maxY = maxY < vertices[i + 1] ? vertices[i + 1] : maxY; } if (bounds == null) bounds = new Rectangle(); bounds.x = minX; bounds.y = minY; bounds.width = maxX - minX; bounds.height = maxY - minY; return bounds; }
/** * Adjusts the allocation given to the view to be a suitable allocation for a text field. If the * view has been allocated more than the preferred span vertically, the allocation is changed to * be centered vertically. Horizontally the view is adjusted according to the horizontal alignment * property set on the associated JTextField (if that is the type of the hosting component). * * @param a the allocation given to the view, which may need to be adjusted. * @return the allocation that the superclass should use. */ protected Shape adjustAllocation(Shape a) { if (a != null) { Rectangle bounds = a.getBounds(); int vspan = (int) getPreferredSpan(Y_AXIS); int hspan = (int) getPreferredSpan(X_AXIS); if (bounds.height != vspan) { int slop = bounds.height - vspan; bounds.y += slop / 2; bounds.height -= slop; } // horizontal adjustments Component c = getContainer(); if (c instanceof JTextField) { JTextField field = (JTextField) c; BoundedRangeModel vis = field.getHorizontalVisibility(); int max = Math.max(hspan, bounds.width); int value = vis.getValue(); int extent = Math.min(max, bounds.width - 1); if ((value + extent) > max) { value = max - extent; } vis.setRangeProperties(value, extent, vis.getMinimum(), max, false); if (hspan < bounds.width) { // horizontally align the interior int slop = bounds.width - 1 - hspan; int align = ((JTextField) c).getHorizontalAlignment(); if (Utilities.isLeftToRight(c)) { if (align == LEADING) { align = LEFT; } else if (align == TRAILING) { align = RIGHT; } } else { if (align == LEADING) { align = RIGHT; } else if (align == TRAILING) { align = LEFT; } } switch (align) { case SwingConstants.CENTER: bounds.x += slop / 2; bounds.width -= slop; break; case SwingConstants.RIGHT: bounds.x += slop; bounds.width -= slop; break; } } else { // adjust the allocation to match the bounded range. bounds.width = hspan; bounds.x -= vis.getValue(); } } return bounds; } return null; }
/* * Returns true if the pointer's orientation was initialized in some dimension, * and false otherwise. */ boolean resizeRectangles(int xChange, int yChange) { if (bounds == null) return false; boolean orientationInit = false; /* * If the cursor orientation has not been set in the orientation of * this change then try to set it here. */ if (xChange < 0 && ((style & SWT.LEFT) != 0) && ((cursorOrientation & SWT.RIGHT) == 0)) { if ((cursorOrientation & SWT.LEFT) == 0) { cursorOrientation |= SWT.LEFT; orientationInit = true; } } if (xChange > 0 && ((style & SWT.RIGHT) != 0) && ((cursorOrientation & SWT.LEFT) == 0)) { if ((cursorOrientation & SWT.RIGHT) == 0) { cursorOrientation |= SWT.RIGHT; orientationInit = true; } } if (yChange < 0 && ((style & SWT.UP) != 0) && ((cursorOrientation & SWT.DOWN) == 0)) { if ((cursorOrientation & SWT.UP) == 0) { cursorOrientation |= SWT.UP; orientationInit = true; } } if (yChange > 0 && ((style & SWT.DOWN) != 0) && ((cursorOrientation & SWT.UP) == 0)) { if ((cursorOrientation & SWT.DOWN) == 0) { cursorOrientation |= SWT.DOWN; orientationInit = true; } } /* * If the bounds will flip about the x or y axis then apply the adjustment * up to the axis (ie.- where bounds width/height becomes 0), change the * cursor's orientation accordingly, and flip each Rectangle's origin (only * necessary for > 1 Rectangles) */ if ((cursorOrientation & SWT.LEFT) != 0) { if (xChange > bounds.width) { if ((style & SWT.RIGHT) == 0) return orientationInit; cursorOrientation |= SWT.RIGHT; cursorOrientation &= ~SWT.LEFT; bounds.x += bounds.width; xChange -= bounds.width; bounds.width = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.x = 100 - proportion.x - proportion.width; } } } } else if ((cursorOrientation & SWT.RIGHT) != 0) { if (bounds.width < -xChange) { if ((style & SWT.LEFT) == 0) return orientationInit; cursorOrientation |= SWT.LEFT; cursorOrientation &= ~SWT.RIGHT; xChange += bounds.width; bounds.width = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.x = 100 - proportion.x - proportion.width; } } } } if ((cursorOrientation & SWT.UP) != 0) { if (yChange > bounds.height) { if ((style & SWT.DOWN) == 0) return orientationInit; cursorOrientation |= SWT.DOWN; cursorOrientation &= ~SWT.UP; bounds.y += bounds.height; yChange -= bounds.height; bounds.height = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.y = 100 - proportion.y - proportion.height; } } } } else if ((cursorOrientation & SWT.DOWN) != 0) { if (bounds.height < -yChange) { if ((style & SWT.UP) == 0) return orientationInit; cursorOrientation |= SWT.UP; cursorOrientation &= ~SWT.DOWN; yChange += bounds.height; bounds.height = 0; if (proportions.length > 1) { for (int i = 0; i < proportions.length; i++) { Rectangle proportion = proportions[i]; proportion.y = 100 - proportion.y - proportion.height; } } } } // apply the bounds adjustment if ((cursorOrientation & SWT.LEFT) != 0) { bounds.x += xChange; bounds.width -= xChange; } else if ((cursorOrientation & SWT.RIGHT) != 0) { bounds.width += xChange; } if ((cursorOrientation & SWT.UP) != 0) { bounds.y += yChange; bounds.height -= yChange; } else if ((cursorOrientation & SWT.DOWN) != 0) { bounds.height += yChange; } Rectangle[] newRects = new Rectangle[rectangles.length]; for (int i = 0; i < rectangles.length; i++) { Rectangle proportion = proportions[i]; newRects[i] = new Rectangle( proportion.x * bounds.width / 100 + bounds.x, proportion.y * bounds.height / 100 + bounds.y, proportion.width * bounds.width / 100, proportion.height * bounds.height / 100); } rectangles = newRects; return orientationInit; }
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ int startX, startY; @Override public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { Point p = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); startX = p.x - loc.x; startY = p.y - loc.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }