/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Zhou Renjian - initial Java2Script implementation
 *******************************************************************************/

package org.eclipse.swt.widgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.dnd.DragAndDrop;
import org.eclipse.swt.internal.dnd.DragEvent;
import org.eclipse.swt.internal.dnd.SashDND;
import org.eclipse.swt.internal.xhtml.Element;
import org.eclipse.swt.internal.xhtml.document;

/**
 * Instances of the receiver represent a selectable user interface object
 * that allows the user to drag a rubber banded outline of the sash within
 * the parent control.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>HORIZONTAL, VERTICAL, SMOOTH</dd>
 * <dt><b>Events:</b></dt>
 * <dd>Selection</dd>
 * </dl>
 * <p>
 * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
 * </p><p>
 * IMPORTANT: This class is intended to be subclassed <em>only</em>
 * within the SWT implementation.
 * </p>
 * 
 * @j2sPrefix
 * $WTC$$.registerCSS ("$wt.widgets.Sash");
 */
public class Sash extends Control {
	boolean dragging;
	int /*startX, startY, */lastX, lastY;
	final static int INCREMENT = 1;
	final static int PAGE_INCREMENT = 9;
	
/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>SWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>SWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 * </p>
 *
 * @param parent a composite control which will be the parent of the new instance (cannot be null)
 * @param style the style of control to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 *
 * @see SWT#HORIZONTAL
 * @see SWT#VERTICAL
 * @see Widget#checkSubclass
 * @see Widget#getStyle
 * 
 * @j2sIgnore
 */
public Sash (Composite parent, int style) {
	super (parent, checkStyle (style));
}

/**
 * Adds the listener to the collection of listeners who will
 * be notified when the control is selected, by sending
 * it one of the messages defined in the <code>SelectionListener</code>
 * interface.
 * <p>
 * When <code>widgetSelected</code> is called, the x, y, width, and height fields of the event object are valid.
 * If the reciever is being dragged, the event object detail field contains the value <code>SWT.DRAG</code>.
 * <code>widgetDefaultSelected</code> is not called.
 * </p>
 *
 * @param listener the listener which should be notified
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 *
 * @see SelectionListener
 * @see #removeSelectionListener
 * @see SelectionEvent
 */
public void addSelectionListener (SelectionListener listener) {
	checkWidget ();
	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
	TypedListener typedListener = new TypedListener (listener);
	addListener (SWT.Selection,typedListener);
	addListener (SWT.DefaultSelection,typedListener);
}

/*
int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
	if (handle == 0) return 0;
	return OS.DefWindowProc (hwnd, msg, wParam, lParam);
}
*/

static int checkStyle (int style) {
	return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
}

public Point computeSize (int wHint, int hHint, boolean changed) {
	checkWidget ();
	int border = getBorderWidth ();
	int width = border * 2, height = border * 2;
	if ((style & SWT.HORIZONTAL) != 0) {
		width += DEFAULT_WIDTH;  height += 3;
	} else {
		width += 3; height += DEFAULT_HEIGHT;
	}
	if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
	if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
	return new Point (width, height);
}

/*
void drawBand (int x, int y, int width, int height) {
	if ((style & SWT.SMOOTH) != 0) return;
	int hwndTrack = parent.handle;
	byte [] bits = {-86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0};
	int stippleBitmap = OS.CreateBitmap (8, 8, 1, 1, bits);
	int stippleBrush = OS.CreatePatternBrush (stippleBitmap);
	int hDC = OS.GetDCEx (hwndTrack, 0, OS.DCX_CACHE);
	int oldBrush = OS.SelectObject (hDC, stippleBrush);
	OS.PatBlt (hDC, x, y, width, height, OS.PATINVERT);
	OS.SelectObject (hDC, oldBrush);
	OS.ReleaseDC (hwndTrack, hDC);
	OS.DeleteObject (stippleBrush);
	OS.DeleteObject (stippleBitmap);
}
*/

void createHandle() {
	handle = document.createElement ("DIV");
	if ((style & SWT.HORIZONTAL) != 0) {
		handle.className = "sash-vertical-default";
	} else {
		handle.className = "sash-horizontal-default";
	}
	if ((style & SWT.BORDER) != 0) {
		handle.className += " sash-border";
	}
	if (parent != null) {
		Element parentHandle = parent.containerHandle();
		if (parentHandle!= null) {
			parentHandle.appendChild(handle);
		}
	}
	DragAndDrop dnd = new DragAndDrop();
	dnd.addDragListener(new SashDND() {
	
		public boolean dragEnded(DragEvent e) {
			super.dragEnded(e);
			/*
			 * TODO: Sash form on right to left should be fixed.
			 */
			Event event = new Event ();
			event.x = lastX; //e.currentX; //Integer.parseInt(thumb.style.left); //lastX;
			event.y = lastY; //e.currentY; //Integer.parseInt(thumb.style.top); //lastY;
			Point size = getSize();
			int delta = 0;
//			if ((style & SWT.BORDER) != 0) {
//				delta = 6;
//			}
			int width = size.x + delta;
			if (width < 2) {
				width = 2;
			}
			event.width = width;
			int height = size.y + delta;
			if (height < 2) {
				height = 2;
			}
			event.height = height;
			event.widget = Sash.this;
			event.item = Sash.this;
//			System.out.println("drop");
			sendEvent (SWT.Selection, event);
			if (event.doit) {
				if ((style & SWT.SMOOTH) != 0) {
					//setBounds (event.x, event.y, width, height);
					if (isHorizontal) {
						handle.style.left = lastX + "px";
					} else {
						handle.style.top = lastY + "px";
					}
					// widget could be disposed at this point
				}
			}
			return true;
		}

		public boolean dragging(DragEvent e) {
			super.dragging(e);
			Event event = new Event ();
			//Point location = getLocation();
			Point p = currentLocation(e);
			if (isHorizontal) {
				event.x = p.x; //SashDND.x;//location.x;
				event.y = Integer.parseInt(handle.style.top);//p.y; //SashDND.y;//location.y;
			} else {
				event.x = Integer.parseInt(handle.style.left);//p.x; //SashDND.x;//location.x;
				event.y = p.y; //SashDND.y;//location.y;
			}
			Point size = getSize();
			int delta = 0;
//			if ((style & SWT.BORDER) != 0) {
//				delta = 4;
//			}
			int width = size.x + delta;
			if (width < 2) {
				width = 2;
			}
			event.width = width;
			int height = size.y + delta;
			if (height < 2) {
				height = 2;
			}
			event.height = height;
			event.widget = Sash.this;
			event.item = Sash.this;
			if ((style & SWT.SMOOTH) == 0) {
				event.detail = SWT.DRAG;
			}
			sendEvent (SWT.Selection, event);
			if (event.doit) {
				lastX = event.x;
				lastY = event.y;
			}
			if ((style & SWT.SMOOTH) != 0) {
				//setBounds (lastX, lastY, width, height);
				if (isHorizontal) {
					handle.style.left = lastX + "px";
				} else {
					handle.style.top = lastY + "px";
				}
				// widget could be disposed at this point
			}
			return true;
		}
		
	});
	dnd.bind(handle);
}

/**
 * Removes the listener from the collection of listeners who will
 * be notified when the control is selected.
 *
 * @param listener the listener which should no longer be notified
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 *
 * @see SelectionListener
 * @see #addSelectionListener
 */
public void removeSelectionListener(SelectionListener listener) {
	checkWidget ();
	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
	if (eventTable == null) return;
	eventTable.unhook (SWT.Selection, listener);
	eventTable.unhook (SWT.DefaultSelection,listener);	
}

String windowClass () {
	return "DIV"; //display.windowClass;
}

/*
int windowProc () {
	return display.windowProc;
}

LRESULT WM_ERASEBKGND (int wParam, int lParam) {
	LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
	if (result != null) return result;
	drawBackground (wParam);
	return LRESULT.ONE;
}

LRESULT WM_KEYDOWN (int wParam, int lParam) {
	LRESULT result = super.WM_KEYDOWN (wParam, lParam);
	if (result != null) return result;
	switch (wParam) {
		case OS.VK_LEFT:
		case OS.VK_RIGHT:
		case OS.VK_UP:
		case OS.VK_DOWN:
			
			/* Calculate the new x or y position *-/
			if (OS.GetKeyState (OS.VK_LBUTTON) < 0) return result;
			int step = OS.GetKeyState (OS.VK_CONTROL) < 0 ? INCREMENT : PAGE_INCREMENT;
			POINT pt = new POINT ();
			if ((style & SWT.VERTICAL) != 0) {
				if (wParam == OS.VK_UP || wParam == OS.VK_DOWN) break;
				pt.x = wParam == OS.VK_LEFT ? -step : step;
			} else {
				if (wParam == OS.VK_LEFT || wParam == OS.VK_RIGHT) break;
				pt.y = wParam == OS.VK_UP ? -step : step;
			}
			int hwndTrack = parent.handle;
			OS.MapWindowPoints (handle, hwndTrack, pt, 1);
			RECT rect = new RECT (), clientRect = new RECT ();
			OS.GetWindowRect (handle, rect);
			int width = rect.right - rect.left;
			int height = rect.bottom - rect.top;
			OS.GetClientRect (hwndTrack, clientRect);
			int clientWidth = clientRect.right - clientRect.left;
			int clientHeight = clientRect.bottom - clientRect.top;
			int newX = lastX, newY = lastY;
			if ((style & SWT.VERTICAL) != 0) {
				newX = Math.min (Math.max (0, pt.x - startX), clientWidth - width);
			} else {
				newY = Math.min (Math.max (0, pt.y - startY), clientHeight - height);
			}
			if (newX == lastX && newY == lastY) return result;

			/* Update the pointer position *-/
			POINT cursorPt = new POINT ();
			cursorPt.x = pt.x;  cursorPt.y = pt.y;
			OS.ClientToScreen (hwndTrack, cursorPt);
			if ((style & SWT.VERTICAL) != 0) {
				cursorPt.y += height / 2;
			}
			else {
				cursorPt.x += width / 2;
			}
			OS.SetCursorPos (cursorPt.x, cursorPt.y);

			Event event = new Event ();
			event.x = newX;
			event.y = newY;
			event.width = width;
			event.height = height;
			sendEvent (SWT.Selection, event);
			if (isDisposed ()) return LRESULT.ZERO;
			if (event.doit) {
				if ((style & SWT.SMOOTH) != 0) {
					setBounds (event.x, event.y, width, height);
				}
			}
			return result;
	}
	return result;
}

LRESULT WM_GETDLGCODE (int wParam, int lParam) {
	return new LRESULT (OS.DLGC_STATIC);
}

LRESULT WM_LBUTTONDOWN (int wParam, int lParam) {
	LRESULT result = super.WM_LBUTTONDOWN (wParam, lParam);

	/* Compute the banding rectangle *-/
	int hwndTrack = parent.handle;
	POINT pt = new POINT ();
	pt.x = (short) (lParam & 0xFFFF);
	pt.y = (short) (lParam >> 16);
	RECT rect = new RECT ();
	OS.GetWindowRect (handle, rect);
	OS.MapWindowPoints (handle, 0, pt, 1);
	startX = pt.x - rect.left;
	startY = pt.y - rect.top;
	OS.MapWindowPoints (0, hwndTrack, rect, 2);
	lastX = rect.left;
	lastY = rect.top;
	int width = rect.right - rect.left;
	int height = rect.bottom - rect.top;

	/* The event must be sent because doit flag is used *-/
	Event event = new Event ();
	event.x = lastX;
	event.y = lastY;
	event.width = width;
	event.height = height;
	if ((style & SWT.SMOOTH) == 0) {
		event.detail = SWT.DRAG;
	}
	sendEvent (SWT.Selection, event);
	if (isDisposed ()) return LRESULT.ZERO;
	
	/* Draw the banding rectangle *-/
	if (event.doit) {
		dragging = true;
		lastX = event.x;
		lastY = event.y;
		menuShell ().bringToTop ();
		if (isDisposed ()) return LRESULT.ZERO;
		if (OS.IsWinCE) {
			OS.UpdateWindow (hwndTrack);
		} else {
			int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
			OS.RedrawWindow (hwndTrack, null, 0, flags);
		}
		drawBand (event.x, event.y, width, height);
		if ((style & SWT.SMOOTH) != 0) {
			setBounds (event.x, event.y, width, height);
			// widget could be disposed at this point
		}
	}
	return result;
}

LRESULT WM_LBUTTONUP (int wParam, int lParam) {
	LRESULT result = super.WM_LBUTTONUP (wParam, lParam);

	/* Compute the banding rectangle *-/
	if (!dragging) return result;
	dragging = false;
	RECT rect = new RECT ();
	OS.GetWindowRect (handle, rect);
	int width = rect.right - rect.left;
	int height = rect.bottom - rect.top;
	
	/* The event must be sent because doit flag is used *-/
	Event event = new Event ();
	event.x = lastX;
	event.y = lastY;
	event.width = width;
	event.height = height;
	drawBand (event.x, event.y, width, height);
	sendEvent (SWT.Selection, event);
	if (isDisposed ()) return result;
	if (event.doit) {
		if ((style & SWT.SMOOTH) != 0) {
			setBounds (event.x, event.y, width, height);
			// widget could be disposed at this point
		}
	}
	return result;
}

LRESULT WM_MOUSEMOVE (int wParam, int lParam) {
	LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
	if (result != null) return result;
	if (!dragging || (wParam & OS.MK_LBUTTON) == 0) return result;

	/* Compute the banding rectangle *-/
	POINT pt = new POINT ();
	pt.x = (short) (lParam & 0xFFFF);
	pt.y = (short) (lParam >> 16);
	int hwndTrack = parent.handle;
	OS.MapWindowPoints (handle, hwndTrack, pt, 1);
	RECT rect = new RECT (), clientRect = new RECT ();
	OS.GetWindowRect (handle, rect);
	int width = rect.right - rect.left;
	int height = rect.bottom - rect.top;
	OS.GetClientRect (hwndTrack, clientRect);
	int newX = lastX, newY = lastY;
	if ((style & SWT.VERTICAL) != 0) {
		int clientWidth = clientRect.right - clientRect.left;
		newX = Math.min (Math.max (0, pt.x - startX), clientWidth - width);
	} else {
		int clientHeight = clientRect.bottom - clientRect.top;
		newY = Math.min (Math.max (0, pt.y - startY), clientHeight - height);
	}
	if (newX == lastX && newY == lastY) return result;
	drawBand (lastX, lastY, width, height);

	/* The event must be sent because doit flag is used *-/
	Event event = new Event ();
	event.x = newX;
	event.y = newY;
	event.width = width;
	event.height = height;
	if ((style & SWT.SMOOTH) == 0) {
		event.detail = SWT.DRAG;
	}
	sendEvent (SWT.Selection, event);
	if (isDisposed ()) return LRESULT.ZERO;
	if (event.doit) {
		lastX = event.x;
		lastY = event.y;
	}
	if (OS.IsWinCE) {
		OS.UpdateWindow (hwndTrack);
	} else {
		int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
		OS.RedrawWindow (hwndTrack, null, 0, flags);
	}
	drawBand (lastX, lastY, width, height);
	if ((style & SWT.SMOOTH) != 0) {
		setBounds (lastX, lastY, width, height);
		// widget could be disposed at this point
	}
	return result;
}

LRESULT WM_SETCURSOR (int wParam, int lParam) {
	LRESULT result = super.WM_SETCURSOR (wParam, lParam);
	if (result != null) return result;
	int hitTest = lParam & 0xFFFF;
 	if (hitTest == OS.HTCLIENT) {
	 	int hCursor = 0;
	 	if ((style & SWT.HORIZONTAL) != 0) {
			hCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
	 	} else {
			hCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
	 	}
		OS.SetCursor (hCursor);
		return LRESULT.ONE;
	}
	return result;
}
*/

}
