public void fitToWindow() {
   ImageWindow win = imp.getWindow();
   if (win == null) return;
   Rectangle bounds = win.getBounds();
   Insets insets = win.getInsets();
   int sliderHeight = (win instanceof StackWindow) ? 20 : 0;
   double xmag = (double) (bounds.width - 10) / srcRect.width;
   double ymag = (double) (bounds.height - (10 + insets.top + sliderHeight)) / srcRect.height;
   setMagnification(Math.min(xmag, ymag));
   int width = (int) (imageWidth * magnification);
   int height = (int) (imageHeight * magnification);
   if (width == dstWidth && height == dstHeight) return;
   srcRect = new Rectangle(0, 0, imageWidth, imageHeight);
   setDrawingSize(width, height);
   getParent().doLayout();
 }
 protected Dimension canEnlarge(int newWidth, int newHeight) {
   // if ((flags&Event.CTRL_MASK)!=0 || IJ.controlKeyDown()) return null;
   ImageWindow win = imp.getWindow();
   if (win == null) return null;
   Rectangle r1 = win.getBounds();
   Insets insets = win.getInsets();
   Point loc = getLocation();
   if (loc.x > insets.left + 5 || loc.y > insets.top + 5) {
     r1.width = newWidth + insets.left + insets.right + 10;
     r1.height = newHeight + insets.top + insets.bottom + 10;
     if (win instanceof StackWindow) r1.height += 20;
   } else {
     r1.width = r1.width - dstWidth + newWidth + 10;
     r1.height = r1.height - dstHeight + newHeight + 10;
   }
   Rectangle max = win.getMaxWindow(r1.x, r1.y);
   boolean fitsHorizontally = r1.x + r1.width < max.x + max.width;
   boolean fitsVertically = r1.y + r1.height < max.y + max.height;
   if (fitsHorizontally && fitsVertically) return new Dimension(newWidth, newHeight);
   else if (fitsVertically && newHeight < dstWidth) return new Dimension(dstWidth, newHeight);
   else if (fitsHorizontally && newWidth < dstHeight) return new Dimension(newWidth, dstHeight);
   else return null;
 }