package ij.gui;
import java.awt.*;
import ij.process.*;
import ij.*;
import ij.util.*;
import java.awt.event.*;
public class PlotCanvas extends ImageCanvas {
Plot plot;
int xScrolled, yScrolled; int oldWidth, oldHeight;
int rangeArrowIndexWhenPressed = -1;
public PlotCanvas(ImagePlus imp) {
super(imp);
oldWidth = imp.getWidth();
oldHeight = imp.getHeight();
}
public void setPlot(Plot plot) {
this.plot = plot;
}
public Plot getPlot() {
return plot;
}
public boolean isFrozen() {
return plot != null && plot.isFrozen();
}
public void zoom(String arg) {
int cursorX = -1, cursorY = -1;
if (cursorOverImage()) {
Point cursorLoc = getCursorLoc();
cursorX = screenX(cursorLoc.x);
cursorY = screenY(cursorLoc.y);
}
Rectangle roiRect = null;
Roi roi = imp.getRoi();
if (roi != null && roi.isArea())
roiRect = roi.getBounds();
if (arg.equals("in")) {
if (roiRect != null) {
plot.zoomToRect(roiRect);
imp.deleteRoi();
} else
zoomIn(cursorX, cursorY);
} else if (arg.equals("out")) {
zoomOut(cursorX, cursorY);
} else if (arg.equals("orig")) {
unzoom();
} else if (arg.equals("100%")) {
zoom100Percent();
} else if (arg.equals("to") && roiRect != null) {
plot.zoomToRect(roiRect);
imp.deleteRoi();
} else if (arg.equals("set"))
new PlotDialog(plot, PlotDialog.SET_RANGE).showDialog(null);
else if (arg.equals("max")) {
ImageWindow win = imp.getWindow();
win.setBounds(win.getMaximumBounds());
win.maximize();
} else if (arg.equals("scale"))
plot.setLimitsToFit(true);
}
public void zoomIn(int sx, int sy) {
zoom(sx, sy, Math.sqrt(2));
}
public void zoomOut(int sx, int sy) {
zoom(sx, sy, Math.sqrt(0.5));
}
void zoom(int sx, int sy, double zoomFactor) {
if (plot == null || plot.isFrozen()) {
if (zoomFactor > 1)
super.zoomIn(sx, sy);
else
super.zoomOut(sx, sy);
return;
}
boolean mouseMoved = sqr(sx-lastZoomSX) + sqr(sy-lastZoomSY) > MAX_MOUSEMOVE_ZOOM*MAX_MOUSEMOVE_ZOOM;
if (mouseMoved)
plot.zoom(sx, sy, zoomFactor); else
plot.zoom(Plot.ZOOM_AS_PREVIOUS, -Plot.ZOOM_AS_PREVIOUS, zoomFactor); lastZoomSX = sx;
lastZoomSY = sy;
}
public void unzoom() {
if (plot == null || plot.isFrozen()) {
super.unzoom();
return;
}
resetMagnification();
plot.setLimitsToDefaults(true);
}
public void zoom100Percent() {
if (plot == null || plot.isFrozen()) {
super.zoom100Percent();
return;
}
resetMagnification();
plot.setFrameSize(PlotWindow.plotWidth, PlotWindow.plotHeight);
}
public void fitToWindow() {
if (plot == null || plot.isFrozen()) {
super.fitToWindow();
return;
}
ImageWindow win = imp.getWindow();
if (win==null) return;
Rectangle bounds = win.getBounds();
Dimension extraSize = win.getExtraSize();
int width = bounds.width-extraSize.width; int height = bounds.height-extraSize.height; resizeCanvas(width, height);
getParent().doLayout();
}
void resizeCanvas(int width, int height) {
if (plot == null || plot.isFrozen()) {
super.resizeCanvas(width, height);
return;
}
resetMagnification();
if (width == oldWidth && height == oldHeight) return;
if (plot == null) return;
ImageWindow win = imp.getWindow();
if (win==null || !(win instanceof PlotWindow)) return;
if (!win.isVisible()) return;
if (!((PlotWindow)win).wasActivated) return; Dimension minSize = plot.getMinimumSize();
int plotWidth = width < minSize.width ? minSize.width : width;
int plotHeight = height < minSize.height ? minSize.height : height;
plot.setSize(plotWidth, plotHeight);
setSize(width, height);
oldWidth = width;
oldHeight = height;
}
public void setMagnification(double magnification) {
if (plot == null || plot.isFrozen())
super.setMagnification(magnification);
else
resetMagnification();
}
public void setSourceRect(Rectangle r) {
if (plot.isFrozen())
super.setSourceRect(r);
else
resetMagnification();
}
void resetMagnification() {
magnification = 1.0;
srcRect.x = 0;
srcRect.y = 0;
}
protected void setupScroll(int ox, int oy) {
if (plot.isFrozen()) {
super.setupScroll(ox, oy);
return;
}
xMouseStart = ox;
yMouseStart = oy;
xScrolled = 0;
yScrolled = 0;
}
protected void scroll(int sx, int sy) {
if (plot.isFrozen()) {
super.scroll(sx, sy);
return;
}
if (sx == 0 && sy == 0) return;
if (xScrolled == 0 && yScrolled == 0)
plot.saveMinMax();
int dx = sx - xMouseStart;
int dy = sy - yMouseStart;
plot.scroll(dx-xScrolled, dy-yScrolled);
xScrolled = dx;
yScrolled = dy;
Thread.yield();
}
public void mouseExited(MouseEvent e) {
ImageWindow win = imp.getWindow();
if (win instanceof PlotWindow)
((PlotWindow)win).mouseExited(e);
super.mouseExited(e);
}
public void mousePressed(MouseEvent e) {
rangeArrowIndexWhenPressed = getRangeArrowIndex(e);
if (rangeArrowIndexWhenPressed <0)
super.mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
if (rangeArrowIndexWhenPressed>=0 && rangeArrowIndexWhenPressed==getRangeArrowIndex(e))
plot.zoomOnRangeArrow(rangeArrowIndexWhenPressed);
else
super.mouseReleased(e);
}
int getRangeArrowIndex(MouseEvent e) {
ImageWindow win = imp.getWindow();
int rangeArrowIndex = -1;
if (win instanceof PlotWindow) {
int x = e.getX();
int y = e.getY();
rangeArrowIndex = ((PlotWindow)win).getRangeArrowIndex(x, y);
}
return rangeArrowIndex;
}
}