private CustomButton mZoomOutButton;
private CustomButton mZoomResetButton;
private CustomButton mZoomInButton;
-
+ private CustomButton mZoomFitButton;
private CustomButton mClippingButton;
public GraphicalEditorPart(LayoutEditor layoutEditor) {
gl.marginHeight = gl.marginWidth = 0;
// create the top part for the configuration control
+ IconFactory iconFactory = IconFactory.getInstance();
CustomButton[][] customButtons = new CustomButton[][] {
new CustomButton[] {
mZoomRealSizeButton = new CustomButton(
- "*",
- null, //image
- "Emulate real size",
+ null, // label
+ iconFactory.getIcon("zoomreal"), //$NON-NLS-1$
+ "Emulate Real Size",
true /*isToggle*/,
false /*defaultValue*/
) {
mZoomOutButton.setEnabled(!newState);
mZoomResetButton.setEnabled(!newState);
mZoomInButton.setEnabled(!newState);
+ mZoomFitButton.setEnabled(!newState);
} else {
mZoomRealSizeButton.setSelection(!newState);
}
}
},
- mZoomOutButton = new CustomButton(
- "-",
- null, //image
- "Canvas zoom out."
+ mZoomFitButton = new CustomButton(
+ null, // label
+ iconFactory.getIcon("zoomfit"), //$NON-NLS-1$
+ "Zoom to Fit (0)"
) {
@Override
public void onSelected(boolean newState) {
- rescale(-1);
+ rescaleToFit();
}
},
mZoomResetButton = new CustomButton(
- "100%",
- null, //image
- "Reset Canvas to 100%"
+ null, // label
+ iconFactory.getIcon("zoom100"), //$NON-NLS-1$
+ "Reset Zoom to 100% (1)"
) {
@Override
public void onSelected(boolean newState) {
resetScale();
}
+ }
+ },
+ // Group zoom in/out separately
+ new CustomButton[] {
+ mZoomOutButton = new CustomButton(
+ null, // label
+ iconFactory.getIcon("zoomminus"), //$NON-NLS-1$
+ "Zoom Out (-)"
+ ) {
+ @Override
+ public void onSelected(boolean newState) {
+ rescale(-1);
+ }
},
mZoomInButton = new CustomButton(
- "+",
- null, //image
- "Canvas zoom in."
+ null, // label
+ iconFactory.getIcon("zoomplus"), //$NON-NLS-1$
+ "Zoom In (+)"
) {
@Override
public void onSelected(boolean newState) {
new CustomButton[] {
new CustomButton(
null, //text
- IconFactory.getInstance().getIcon("explode"), //$NON-NLS-1$
- "Displays extra margins in the layout.",
+ iconFactory.getIcon("explode"), //$NON-NLS-1$
+ "Displays extra margins in the layout",
true /*toggle*/,
false /*defaultValue*/
) {
},
new CustomButton(
null, //text
- IconFactory.getInstance().getIcon("outline"), //$NON-NLS-1$
- "Shows the outline of all views in the layout.",
+ iconFactory.getIcon("outline"), //$NON-NLS-1$
+ "Shows the outline of all views in the layout",
true /*toggle*/,
false /*defaultValue*/
) {
},
mClippingButton = new CustomButton(
null, //text
- IconFactory.getInstance().getIcon("clipping"), //$NON-NLS-1$
+ iconFactory.getIcon("clipping"), //$NON-NLS-1$
"Toggles screen clipping on/off",
true /*toggle*/,
true /*defaultValue*/
}
/**
+ * Returns true if zooming in/out/to-fit/etc is allowed (which is not the case while
+ * emulating real size)
+ *
+ * @return true if zooming is allowed
+ */
+ boolean isZoomingAllowed() {
+ return mZoomInButton.isEnabled();
+ }
+
+ /**
* Listens to workbench selections that does NOT come from {@link LayoutEditor}
* (those are generated by ourselves).
* <p/>
* Rescales canvas.
* @param direction +1 for zoom in, -1 for zoom out
*/
- private void rescale(int direction) {
+ void rescale(int direction) {
double s = mCanvasViewer.getCanvas().getScale();
if (direction > 0) {
mCanvasViewer.getCanvas().setScale(1, true /*redraw*/);
}
+ /**
+ * Reset the canvas scale to best fit (so content is as large as possible without scrollbars)
+ */
+ private void rescaleToFit() {
+ mCanvasViewer.getCanvas().setFitScale();
+ }
+
private boolean rescaleToReal(boolean real) {
if (real) {
return computeAndSetRealScale(true /*redraw*/);
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
// handle backspace for other platforms as well.
if (e.keyCode == SWT.BS) {
mDeleteAction.run();
+ } else {
+ // Zooming actions
+ char c = e.character;
+ GraphicalEditorPart editor = mLayoutEditor.getGraphicalEditor();
+ if (c == '1' && editor.isZoomingAllowed()) {
+ setScale(1, true);
+ } else if (c == '0' && editor.isZoomingAllowed()) {
+ setFitScale();
+ } else if (c == '+' && editor.isZoomingAllowed()) {
+ editor.rescale(1);
+ } else if (c == '-' && editor.isZoomingAllowed()) {
+ editor.rescale(-1);
+ }
}
}
}
}
+ /** Scales the canvas to best fit */
+ void setFitScale() {
+ Rectangle canvasSize = getClientArea();
+ int canvasWidth = canvasSize.width - 2 * ICanvasTransform.IMAGE_MARGIN;
+ int canvasHeight = canvasSize.height - 2 * ICanvasTransform.IMAGE_MARGIN;
+
+ Image image = getImageOverlay().getImage();
+ if (image != null) {
+ ImageData imageData = image.getImageData();
+ int sceneWidth = imageData.width;
+ int sceneHeight = imageData.height;
+ double hScale = canvasWidth / (double) sceneWidth;
+ double vScale = canvasHeight / (double) sceneHeight;
+
+ double scale = Math.min(hScale, vScale);
+ setScale(scale, true);
+ }
+ }
+
/**
* Transforms a point, expressed in layout coordinates, into "client" coordinates
* relative to the control (and not relative to the display).