/*
 * Copyright 2016 DiffPlug
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.diffplug.common.swt;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

/** Positions within a rectangle (the corners, the center of the lines, and the center). */
public enum Corner {
	// @formatter:off
	TOP_LEFT(0, 0), TOP_RIGHT(1, 0), BOTTOM_LEFT(0, 1), BOTTOM_RIGHT(1, 1), // true corners
	TOP(0.5f, 0), LEFT(0, 0.5f), BOTTOM(0.5f, 1), RIGHT(1, 0.5f), CENTER(0.5f, 0.5f); // edges and center
	// @formatter:on

	private final float x, y;

	private Corner(float x, float y) {
		this.x = x;
		this.y = y;
	}

	/** Returns this corner's position within the given rectangle. */
	public Point getPosition(Rectangle rectangle) {
		return new Point(
				rectangle.x + (int) (x * rectangle.width),
				rectangle.y + (int) (y * rectangle.height));
	}

	/**
	 * If you move the topLeft of `rectangle` to the returned point,
	 * then this corner will be at `position`. 
	 */
	public Point topLeftRequiredFor(Rectangle rectangle, Point position) {
		Point current = getPosition(rectangle);
		return new Point(
				rectangle.x + position.x - current.x,
				rectangle.y + position.y - current.y);
	}
}

@@ -700,16 +700,175 @@
 ctangle;
+%0Aimport org.eclipse.swt.widgets.Control;%0Aimport org.eclipse.swt.widgets.Shell;%0Aimport org.eclipse.swt.widgets.ToolBar;%0Aimport org.eclipse.swt.widgets.ToolItem;
 %0A%0A/** Po
@@ -1532,24 +1532,586 @@
 ight));%0A%09%7D%0A%0A
+%09/** Returns this corner's position on the given control in display coordinates. */%0A%09public Point getPosition(Control control) %7B%0A%09%09if (control instanceof Shell) %7B%0A%09%09%09return getPosition(control.getBounds());%0A%09%09%7D else %7B%0A%09%09%09return control.getDisplay().map(control, null, getPosition(control.getBounds()));%0A%09%09%7D%0A%09%7D%0A%0A%09/** Returns this corner's position on the given ToolItem in display coordinates. */%0A%09public Point getPosition(ToolItem item) %7B%0A%09%09ToolBar toolbar = item.getParent();%0A%09%09return toolbar.getDisplay().map(toolbar, null, getPosition(item.getBounds()));%0A%09%7D%0A%0A
 %09/**%0A%09 * If 

Add Corner.getPosition(Control | ToolItem).