package cnp.ew.richtext;

import java.awt.*;
import java.util.*;

import cnp.ew.util.*;
import cnp.ew.displayer.*;
import cnp.ew.scrolling.*;
import cnp.ew.image.*;
import cnp.ew.lightweight.*;

public class CpRuler extends CpAbstractLc implements CpObserver // Should be ObservableCanvas
{
    public static final int MARGIN_SET = 13000;
    public static final int COLUMN_SIZED = 13001;

    static final int LEFT = -1;
    static final int RIGHT = -2;
    static final int FIRST = -3;
    static final int BOTH = -4;
    static final int NONE = -5;

    static double PPI = 96.0;  // pixels per inch

    static int leftId;
    static int rightId;
    static int firstId;

    static {
         leftId = CpToolkit.registerImageName("wpleftmargin.gif");
         firstId = CpToolkit.registerImageName("wpfirstindent.gif");
         rightId = CpToolkit.registerImageName("wprightmargin.gif");
    }

    static Image leftImage, rightImage, firstImage;

    Image rulerImage;  // cache

    int left, right, first, pageLeft, pageRight;
    Vector tabs;

	int dragWhat;
	int dragX;
	int leftPos = 0;

	static Font theFont;

	CpRichTextPane pane;  // Questionable

	Cp3DBorderDisplayer displayer;

    public CpRuler(CpRichTextPane newPane)
    {
        pane = newPane;
        pane.addObserver(this);
        displayer = new Cp3DBorderDisplayer(false, false);
    }

    public void update(CpObservable o, int facet, Object arg)
    {
     //   System.out.println("In Scrolling controller update");

        if (facet == CpRichTextPane.CURSOR_MOVED && ((Boolean)arg).booleanValue()) {  // arg says whether the para or format has changed
            CpParagraph para = pane.getCurrentParagraph();
            if (left != para.getLeftMargin() || right != para.getRightMargin() || first != para.getFirstIndent()) {
                repaint();
            }
        }
    }

    public void scrollTo(int i)
    {
        if (leftPos != i) {
            leftPos = i;
            repaint();
        }
    }

  	public void paint(Graphics g, Rectangle clip)
	{
        // This doesn't seem right, to poll
        pageLeft = pane.getPageLeft();
        pageRight = pane.getPageRight();
        CpParagraph para = pane.getCurrentParagraph();
        left = para.getLeftMargin();
        right = para.getRightMargin();
        first = para.getFirstIndent();


        // is this the right way?
        if (rulerImage == null) {
            leftImage = CpToolkit.getImage(leftId);
            rightImage = CpToolkit.getImage(rightId);
            firstImage = CpToolkit.getImage(firstId);
            rulerImage = createImage(size().width, size().height);
            Graphics tempG = rulerImage.getGraphics();

    	    int curX = 0;

            tempG.setColor(Color.lightGray);
            tempG.fillRect(0, 0, size().width, size().height);
            tempG.setColor(Color.white);
            tempG.fillRect(pageLeft, 1, pageRight - pageLeft, size().height - 6);
            displayer.paintIn(pane.getScroller(), tempG, pageLeft - 2, 1, (pageRight - pageLeft) + 3, size().height - 6);
            tempG.setColor(Color.black);

            double inchesPageWidth = (pageRight - pageLeft) / PPI;
            for (double f = 0.125; f < inchesPageWidth - 0.125; f += 0.125) {
                int x = (int)(f * PPI);
                tempG.drawLine(x + pageLeft, 8, x + pageLeft, 9);
            }
            for (double f = 0.5; f < inchesPageWidth; f += 1.0) {
                int x = (int)(f * PPI);
                tempG.drawLine(x + pageLeft, 7, x + pageLeft, 11);
            }
            if (theFont == null) {
                theFont = new Font("Helvetica", Font.PLAIN, 10);
            }
            tempG.setFont(theFont);
            for (int i = 1; i < inchesPageWidth; i++) {
                int x = (int)(i * PPI);
                tempG.setColor(Color.white);
                tempG.fillRect(x + pageLeft - 1, 6, 3, 4); // Get rid of the 1/8 inch mark.  Yuck.
                tempG.setColor(Color.black);
                tempG.drawString("" + i, x + pageLeft - (tempG.getFontMetrics().stringWidth("" + i) / 2), 13);
            }
            tempG.dispose();
        }

        if (rulerImage != null) {
            g.drawImage(rulerImage, 0, 0, CpToolkit.defaultComponent());
        }

        g.drawImage(leftImage, pageLeft + left - 5, 11, CpToolkit.defaultComponent());
        g.drawImage(rightImage, pageRight - right - 5, 11, CpToolkit.defaultComponent());
        g.drawImage(firstImage, pageLeft + left + first - 5, 0, CpToolkit.defaultComponent());

	}


    public boolean mouseDown(Event e, int x, int y)
    {
        dragX = x;
        dragWhat = NONE;
	    if (x > left + pageLeft - 5 && x < left + pageLeft + 5 && y > 9) {
	        if (y > 17) {
	            dragWhat = BOTH;
	        } else {
    	        dragWhat = LEFT;
    	    }
	    }
	    if (x > first + left + pageLeft - 5 && x < first + left + pageLeft + 5 && y < 9) {
	        dragWhat = FIRST;
	    }
	    if (x > pageRight - right - 5 && x < pageRight - right + 5 && y > 9) {
	        dragWhat = RIGHT;
	    }


		return false;
    }

    public boolean mouseDrag(Event e, int x, int y)
    {
        if (x != dragX) {
            if (dragWhat == LEFT || dragWhat == BOTH) {
                int oldLeft = left;
                left = Math.min(pageRight - right - pageLeft - 20, Math.max(x - pageLeft, 0));
                if (dragWhat != BOTH) {
                    first += oldLeft - left;
                }
            }
            if (dragWhat == FIRST) {
                first =  x - pageLeft - left;
            }
            if (dragWhat == RIGHT) {
                right = Math.min(Math.max(pageRight - x, 0), pageRight - left - pageLeft - 20);
            }
            pane.setMargins(left, right, first);
            dragX = x;
            repaint();
        }
        return false;
    }
    public boolean mouseUp(Event e, int x, int y)
    {
//        repaint();
//        pane.setMargins(left, right, first);
        return false;
    }

    protected void drawSizingLine(int x)
    {
        // OUCH!  Grabbin the guts...
   /*     Graphics g = pane.getScroller().getGraphics();
        g.setColor(Color.white);
        g.setXORMode(Color.black);
       g.drawLine(x - 1, 0, x - 1, pane.getScroller().size().height);
        g.setPaintMode();
        */
    }
}


