package ij.plugin;
import ij.*;
import ij.process.*;
import ij.gui.*;
import ij.plugin.frame.ThresholdAdjuster;
import java.awt.*;

/** This class implements the Window menu's "Show All", "Main Window", "Cascade" and "Tile" commands. */
public class WindowOrganizer implements PlugIn {

	private static final int XSTART=4, YSTART=80, XOFFSET=8, YOFFSET=24,MAXSTEP=200,GAP=2;
	private int titlebarHeight = IJ.isMacintosh()?40:20;

	public void run(String arg) {
		if (arg.equals("imagej"))
			{showImageJ(); return;}
		int[] wList = WindowManager.getIDList();
		if (arg.equals("show"))
			{showAll(wList); return;}
		if (wList==null) {
			IJ.noImage();
			return;
		}
		if (arg.equals("tile"))
			tileWindows(wList);
		else
			cascadeWindows(wList);
	}
	
	void tileWindows(int[] wList) {
		Dimension screen = IJ.getScreenSize();
		int minWidth = Integer.MAX_VALUE;
		int minHeight = Integer.MAX_VALUE;
		boolean allSameSize = true;
		int width=0, height=0;
		double totalWidth = 0;
		double totalHeight = 0;
		for (int i=0; ihspace)
			tileWidth = hspace;
		int vspace = screen.height - YSTART;
		if (tileHeight>vspace)
			tileHeight = vspace;
		int hloc, vloc;
		boolean theyFit;
		do {
			hloc = XSTART;
			vloc = YSTART;
			theyFit = true;
			int i = 0;
			do {
				i++;
				if (hloc+tileWidth>screen.width) {
					hloc = XSTART;
					vloc = vloc + tileHeight;
					if (vloc+tileHeight> screen.height)
						theyFit = false;
				}
				hloc = hloc + tileWidth + GAP;
			} while (theyFit && (iscreen.width) {
				hloc = XSTART;
				vloc = vloc + tileHeight;
			}
			ImageWindow win = getWindow(wList[i]);
			if (win!=null) {
				win.setLocation(hloc, vloc);
				ImageCanvas canvas = win.getCanvas();
				while (win.getSize().width*0.85>=tileWidth && canvas.getMagnification()>0.03125)
					canvas.zoomOut(0, 0);
				win.toFront();
				ImagePlus imp = win.getImagePlus();
				if (imp!=null) imp.setIJMenuBar(i==nPics-1);
			}
			hloc += tileWidth + GAP;
		}
	}

	ImageWindow getWindow(int id) {
		ImageWindow win = null;
		ImagePlus imp = WindowManager.getImage(id);
		if (imp!=null)
			win = imp.getWindow();
		return win;
	}		
			
	void cascadeWindows(int[] wList) {
		Dimension screen = IJ.getScreenSize();
		int x = XSTART;
		int y = YSTART;
		int xstep = 0;
		int xstart = XSTART;
		for (int i=0; iMAXSTEP)
					xstep = MAXSTEP;
			}
			if (y+d.height*0.67>screen.height) {
				xstart += xstep;
				if (xstart+d.width*0.67>screen.width)
					xstart = XSTART+XOFFSET;
				x = xstart;
				y = YSTART;
			}
			win.setLocation(x, y);
			win.toFront();
				x += XOFFSET;
			y += YOFFSET;
			ImagePlus imp = win.getImagePlus();
			if (imp!=null) imp.setIJMenuBar(i==wList.length-1);
		}
	}
	
	void showImageJ() {
		ImageJ ij = IJ.getInstance();
		if (ij!=null)
			ij.toFront();
	}

	void showAll(int[] wList) {
		if (wList!=null) {
			for (int i=0; i 

Ads help maintain this website.