/*******************************************************************************
 * 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.SWTException;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.internal.browser.OS;
import org.eclipse.swt.internal.xhtml.Element;
import org.eclipse.swt.internal.xhtml.document;

/**
 * Instances of this class provide an etched border
 * with an optional title.
 * <p>
 * Shadow styles are hints and may not be honoured
 * by the platform.  To create a group with the
 * default shadow style for the platform, do not
 * specify a shadow style.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>SHADOW_ETCHED_IN, SHADOW_ETCHED_OUT, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
 * <dt><b>Events:</b></dt>
 * <dd>(none)</dd>
 * </dl>
 * <p>
 * Note: Only one of the above styles may be specified.
 * </p><p>
 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
 * </p>
 * 
 * @j2sPrefix
 * $WTC$$.registerCSS ("$wt.widgets.Group");
 */

public class Group extends Composite {
	static final int CLIENT_INSET = 3;
	/*
	static final int GroupProc;
	static final TCHAR GroupClass = new TCHAR (0, OS.IsWinCE ? "BUTTON" : "SWT_GROUP", true);
	static {
		/*
		* Feature in Windows.  The group box window class
		* uses the CS_HREDRAW and CS_VREDRAW style bits to
		* force a full redraw of the control and all children
		* when resized.  This causes flashing.  The fix is to
		* register a new window class without these bits and
		* implement special code that damages only the exposed
		* area.
		* 
		* Feature in WinCE.  On certain devices, defining
		* a new window class which looks like BUTTON causes
		* CreateWindowEx() to crash.  The workaround is to use
		* the class Button directly.
		*-/
		WNDCLASS lpWndClass = new WNDCLASS ();
		if (OS.IsWinCE) {
			OS.GetClassInfo (0, GroupClass, lpWndClass);
			GroupProc = lpWndClass.lpfnWndProc;
		} else {
			TCHAR WC_BUTTON = new TCHAR (0, "BUTTON", true);
			OS.GetClassInfo (0, WC_BUTTON, lpWndClass);
			GroupProc = lpWndClass.lpfnWndProc;
			int hInstance = OS.GetModuleHandle (null);
			if (!OS.GetClassInfo (hInstance, GroupClass, lpWndClass)) {
				int hHeap = OS.GetProcessHeap ();
				lpWndClass.hInstance = hInstance;
				lpWndClass.style &= ~(OS.CS_HREDRAW | OS.CS_VREDRAW);
				int byteCount = GroupClass.length () * TCHAR.sizeof;
				int lpszClassName = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
				OS.MoveMemory (lpszClassName, GroupClass, byteCount);
				lpWndClass.lpszClassName = lpszClassName;
				OS.RegisterClass (lpWndClass);
//				OS.HeapFree (hHeap, 0, lpszClassName);
			}
		}
	}
	*/
	String groupText;
	int textWidth;
	int textHeight;
	
	private Element borderFrame, titleLine, leftCorner, titleText, rightCorner, leftSide, rightSide, 
		bottomLeft, bottomRight, contentBox, content;
	
/**
 * 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#SHADOW_ETCHED_IN
 * @see SWT#SHADOW_ETCHED_OUT
 * @see SWT#SHADOW_IN
 * @see SWT#SHADOW_OUT
 * @see SWT#SHADOW_NONE
 * @see Widget#checkSubclass
 * @see Widget#getStyle
 * 
 * @j2sIgnore
 */
public Group (Composite parent, int style) {
	super (parent, checkStyle (style));
}

/*
int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
	if (handle == 0) return 0;
	/*
	* Feature in Windows.  When the user clicks on the group
	* box label, the group box takes focus.  This is unwanted.
	* The fix is to avoid calling the group box window proc.
	*-/
	switch (msg) {
		case OS.WM_LBUTTONDOWN:
		case OS.WM_LBUTTONDBLCLK: 
			return OS.DefWindowProc (hwnd, msg, wParam, lParam);
	}
	return OS.CallWindowProc (GroupProc, hwnd, msg, wParam, lParam);
}
*/

static int checkStyle (int style) {
	style |= SWT.NO_FOCUS;
	/*
	* Even though it is legal to create this widget
	* with scroll bars, they serve no useful purpose
	* because they do not automatically scroll the
	* widget's client area.  The fix is to clear
	* the SWT style.
	*/
	return style & ~(SWT.H_SCROLL | SWT.V_SCROLL);
}

protected void checkSubclass () {
	if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
}

public Point computeSize (int wHint, int hHint, boolean changed) {
	checkWidget ();
	Point size = super.computeSize (wHint, hHint, changed);
	int length = 0;
	if (groupText != null) {
		length = groupText.length();
	}
//	int length = OS.GetWindowTextLength (handle);
	if (length != 0) {
		/*
		* If the group has text, and the text is wider than the
		* client area, pad the width so the text is not clipped.
		*/
		/*
		TCHAR buffer1 = new TCHAR (getCodePage (), length + 1);
		OS.GetWindowText (handle, buffer1, length + 1);
		int newFont, oldFont = 0;
		int hDC = OS.GetDC (handle);
		newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
		if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
		RECT rect = new RECT ();
		int flags = OS.DT_CALCRECT | OS.DT_SINGLELINE;
		OS.DrawText (hDC, buffer1, length, rect, flags);
		if (newFont != 0) OS.SelectObject (hDC, oldFont);
		OS.ReleaseDC (handle, hDC);
		*/
		//size.x = Math.max (size.x, rect.right - rect.left + CLIENT_INSET * 6);
		size.x = Math.max (size.x, textWidth + CLIENT_INSET * 6);
	}
	return size;
}

public Rectangle computeTrim (int x, int y, int width, int height) {
	checkWidget ();
	Rectangle trim = super.computeTrim (x, y, width, height);
	/*
	int newFont, oldFont = 0;
	int hDC = OS.GetDC (handle);
	newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
	if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
	TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
	OS.GetTextMetrics (hDC, tm);
	if (newFont != 0) OS.SelectObject (hDC, oldFont);
	OS.ReleaseDC (handle, hDC);
	*/
	trim.x -= CLIENT_INSET;
//	trim.y -= tm.tmHeight;
	if (textHeight <= 0) {
		textHeight = OS.getStringStyledHeight(".", "group-default", handle.style.cssText);
	}
	trim.y -= textHeight;
	trim.width += CLIENT_INSET * 2;
//	trim.height += tm.tmHeight + CLIENT_INSET;
	trim.height += textHeight + CLIENT_INSET;
	return trim;
}

/* (non-Javadoc)
 * @see org.eclipse.swt.widgets.Composite#containerHandle()
 */
protected Element containerHandle() {
	return content;
}

Object createCSSElement(Object parent, String css) {
	Element el = document.createElement("DIV");
	el.className = css;
	((Element) parent).appendChild(el);
	return el;
}

protected void createHandle () {
	children = new Control[0];
	//super.createHandle ();
	state &= ~CANVAS;
	handle = document.createElement("DIV");

	if ((style & SWT.BORDER) != 0) {
		handle.className = "group-default group-border-default";
	} else {
		handle.className = "group-default";
	}

	if (parent != null) {
		Element parentHandle = parent.containerHandle();
		if (parentHandle!= null) {
			parentHandle.appendChild(handle);
		}
	}
	
	String className = null;
	if ((style & SWT.SHADOW_ETCHED_IN) != 0) {
		className = "group-shadow-etched-in";
	} else if ((style & SWT.SHADOW_ETCHED_OUT) != 0) {
		className = "group-shadow-etched-out";
	} else if ((style & SWT.SHADOW_IN) != 0) {
		className = "group-shadow-in";
	} else if ((style & SWT.SHADOW_OUT) != 0) {
		className = "group-shadow-out";
	} else if ((style & SWT.SHADOW_NONE) != 0) {
		className = "group-shadow-none";
	}
	if (className == null) {
		className = "group-border-frame group-no-title-text";
	} else {
		className = "group-border-frame group-no-title-text " + className;
	}
	borderFrame = (Element) createCSSElement(handle, className);
	
	titleLine = (Element) createCSSElement(borderFrame, "group-title-line");
	leftCorner = (Element) createCSSElement(borderFrame, "group-left-corner");
	rightCorner = (Element) createCSSElement(borderFrame, "group-right-corner");
	titleText = (Element) createCSSElement(borderFrame, "group-title-text");
	leftSide = (Element) createCSSElement(borderFrame, "group-side-line-left");
	rightSide = (Element) createCSSElement(borderFrame, "group-side-line-right");
	bottomLeft = (Element) createCSSElement(borderFrame, "group-bottom-line-left");
	bottomRight = (Element) createCSSElement(borderFrame, "group-bottom-line-right");
	
	contentBox = (Element) createCSSElement(handle, "group-content-box");
	content = (Element) createCSSElement(contentBox, "group-content");
	
}

public Rectangle getClientArea () {
	checkWidget ();
	forceResize ();
	/*
	RECT rect = new RECT ();
	OS.GetClientRect (handle, rect);
	int newFont, oldFont = 0;
	int hDC = OS.GetDC (handle);
	newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
	if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
	TEXTMETRIC tm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA ();
	OS.GetTextMetrics (hDC, tm);
	if (newFont != 0) OS.SelectObject (hDC, oldFont);
	OS.ReleaseDC (handle, hDC);
	int x = CLIENT_INSET, y = tm.tmHeight;
	int width = rect.right - CLIENT_INSET * 2;
	int height = rect.bottom - y - CLIENT_INSET;
	*/
	if (textHeight <= 0) {
		textHeight = OS.getStringStyledHeight(".", "group-default", handle.style.cssText);
	}
	int x = CLIENT_INSET, y = textHeight;
	int border = getBorderWidth();
	int width = this.width - border * 2 - CLIENT_INSET * 2;
	int height = this.height - border * 2 - y - CLIENT_INSET;
	//System.out.println("-->" + new Rectangle (x, y, width, height));
//	if (groupText == null || groupText.length() == 0) {
//		return new Rectangle (x, y / 2, width, height);
//	} else {
//		return new Rectangle (x, y, width, height);
//	}
	return new Rectangle (x, y, width, height);
	//return new Rectangle (left, top, width, height);
	//return super.getClientArea();
}

String getNameText () {
	return getText ();
}

/**
 * Returns the receiver's text, which is the string that the
 * is used as the <em>title</em>. If the text has not previously
 * been set, returns an empty string.
 *
 * @return the text
 *
 * @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>
 */
public String getText () {
	checkWidget ();
	/*
	int length = OS.GetWindowTextLength (handle);
	if (length == 0) return "";
	TCHAR buffer = new TCHAR (getCodePage (), length + 1);
	OS.GetWindowText (handle, buffer, length + 1);
	return buffer.toString (0, length);
	*/
	return groupText;
}

boolean mnemonicHit (char key) {
	return setFocus ();
}

boolean mnemonicMatch (char key) {
	char mnemonic = findMnemonic (getText ());
	if (mnemonic == '\0') return false;
	return Character.toUpperCase (key) == Character.toUpperCase (mnemonic);
}

/* (non-Javadoc)
 * @see org.eclipse.swt.widgets.Scrollable#releaseHandle()
 */
protected void releaseHandle() {
	if (titleLine != null) {
		OS.destroyHandle(titleLine);
		titleLine = null;
	}
	if (titleText != null) {
		OS.destroyHandle(titleText);
		titleText = null;
	}
	if (leftCorner != null) {
		OS.destroyHandle(leftCorner);
		leftCorner = null;
	}
	if (rightCorner != null) {
		OS.destroyHandle(rightCorner);
		rightCorner = null;
	}
	if (bottomLeft != null) {
		OS.destroyHandle(bottomLeft);
		bottomLeft = null;
	}
	if (bottomRight != null) {
		OS.destroyHandle(bottomRight);
		bottomRight = null;
	}
	if (leftSide != null) {
		OS.destroyHandle(leftSide);
		leftSide = null;
	}
	if (rightSide != null) {
		OS.destroyHandle(rightSide);
		rightSide = null;
	}
	if (borderFrame != null) {
		OS.destroyHandle(borderFrame);
		borderFrame = null;
	}
	if (content != null) {
		OS.destroyHandle(content);
		content = null;
	}
	if (contentBox != null) {
		OS.destroyHandle(contentBox);
		contentBox = null;
	}
	super.releaseHandle();
}

/* (non-Javadoc)
 * @see org.eclipse.swt.widgets.Control#setBounds(int, int, int, int)
 */
public void setBounds(int x, int y, int width, int height) {
	super.setBounds(x, y, width, height);
	if (textWidth == 0 && groupText != null && groupText.length() != 0) {
//		textWidth = UIStringUtil.calculateStyledStringLineWidth(groupText, "group-title-text");
		textWidth = OS.getStringStyledWidth(groupText, "group-default", handle.style.cssText);
	}
	if (textWidth != 0) {
		int w = this.width - textWidth - 12;
		if (w < 0) {
			w = 0; 
		}
		rightCorner.style.width = w + "px";
	}
}

/* (non-Javadoc)
 * @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
 */
public void setFont(Font font) {
	// TODO Auto-generated method stub
	super.setFont(font);
}

/**
 * Sets the receiver's text, which is the string that will
 * be displayed as the receiver's <em>title</em>, to the argument,
 * which may not be null. The string may include the mnemonic character.
 * </p>
 * Mnemonics are indicated by an '&amp' that causes the next
 * character to be the mnemonic.  When the user presses a
 * key sequence that matches the mnemonic, focus is assgned
 * to the first child of the group. On most platforms, the
 * mnemonic appears underlined but may be emphasised in a
 * platform specific manner.  The mnemonic indicator character
 *'&amp' can be escaped by doubling it in the string, causing
 * a single '&amp' to be displayed.
 * </p>
 * @param string the new text
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the text 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>
 */
public void setText (String string) {
	checkWidget ();
	if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
	/*
	TCHAR buffer = new TCHAR (getCodePage (), string, true);
	OS.SetWindowText (handle, buffer);
	*/
	OS.updateCSSClass(borderFrame, "group-no-title-text", string.length() == 0);
	if (string.length() != 0) {
		if (!string.equals(groupText)) {
			for (int i = titleText.childNodes.length - 1; i >= 0; i--) {
				titleText.removeChild(titleText.childNodes[i]);
			}
			titleText.appendChild(document.createTextNode(string));
			textWidth = OS.getContainerWidth(titleText);
			if (textWidth == 0) {
//				textWidth = UIStringUtil.calculatePlainStringLineWidth(string);
				textWidth = OS.getStringStyledWidth(string, "group-default", handle.style.cssText);
			}
			if (textWidth != 0) {
				int w = this.width - textWidth - 24;
				if (w > 0) {
					rightCorner.style.width = w + "px";
				}
			}
		}
	}
	groupText = string;
}
protected void _updateOrientation(){
	/*
	 * Force left to right
	 */
	if((style & SWT.RIGHT_TO_LEFT) != 0){
		handle.style.direction = "ltr";
	} else if (parent != null 
			&& (parent.style & SWT.RIGHT_TO_LEFT) != 0) {
		handle.style.direction = "ltr";
	}
}
/*
int widgetStyle () {
	/*
	* Bug in Windows.  When GetDCEx() is called with DCX_INTERSECTUPDATE,
	* the HDC that is returned does not include the current update region.
	* This was confirmed under DEBUG Windows when GetDCEx() complained about
	* invalid flags.  Therefore, it is not easily possible to get an HDC from
	* outside of WM_PAINT that includes the current damage and clips children.
	* Because the receiver has children and draws a frame and label, it is
	* necessary that the receiver always draw clipped, in the current damaged
	* area.  The fix is to force the receiver to be fully clipped by including
	* WS_CLIPCHILDREN and WS_CLIPSIBLINGS in the default style bits.
	*-/
	return super.widgetStyle () | OS.BS_GROUPBOX | OS.WS_CLIPCHILDREN | OS.WS_CLIPSIBLINGS;
}

String windowClass () {
	return "DIV";
}

int windowProc () {
	return GroupProc;
}

LRESULT WM_ERASEBKGND (int wParam, int lParam) {
	LRESULT result = super.WM_ERASEBKGND (wParam, lParam);
	if (result != null) return result;
	/*
	* Feaure in Windows.  Group boxes do not erase
	* the background before drawing.  The fix is to
	* fill the background.
	* 
	* NOTE:  This work around causes flashing on XP
	* and is not necessary when a parent has a theme.
	*-/
	if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
		if (findThemeControl () != null) return result;
	}
	drawBackground (wParam);
	return LRESULT.ONE;
}

LRESULT WM_NCHITTEST (int wParam, int lParam) {
	LRESULT result = super.WM_NCHITTEST (wParam, lParam);
	if (result != null) return result;
	/*
	* Feature in Windows.  The window proc for the group box
	* returns HTTRANSPARENT indicating that mouse messages
	* should not be delivered to the receiver and any children.
	* Normally, group boxes in Windows do not have children and
	* this is the correct behavior for this case.  Because we
	* allow children, answer HTCLIENT to allow mouse messages
	* to be delivered to the children.
	*-/
	int code = callWindowProc (handle, OS.WM_NCHITTEST, wParam, lParam);
	if (code == OS.HTTRANSPARENT) code = OS.HTCLIENT;
	return new LRESULT (code);
}

LRESULT WM_MOUSEMOVE (int wParam, int lParam) {
	LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
	if (result != null) return result;
	/*
	* Feature in Windows.  In version 6.00 of COMCTL32.DLL,
	* every time the mouse moves, the group title redraws.
	* This only happens when WM_NCHITTEST returns HTCLIENT.
	* The fix is to avoid calling the group window proc.
	*-/
	return LRESULT.ZERO;
}

LRESULT WM_PRINTCLIENT (int wParam, int lParam) {
	LRESULT result = super.WM_PRINTCLIENT (wParam, lParam);
	if (result != null) return result;
	/*
	* Feature in Windows.  In version 6.00 of COMCTL32.DLL,
	* when WM_PRINTCLIENT is sent from a child BS_GROUP
	* control to a parent BS_GROUP, the parent BS_GROUP
	* clears the font from the HDC.  Normally, group boxes
	* in Windows do not have children so this behavior is
	* undefined.  When the parent of a BS_GROUP is not a
	* BS_GROUP, there is no problem.  The fix is to save
	* and restore the current font.
	*-/
	if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
		int nSavedDC = OS.SaveDC (wParam);
		int code = callWindowProc (handle, OS.WM_PRINTCLIENT, wParam, lParam);
		OS.RestoreDC (wParam, nSavedDC);
		return new LRESULT (code);
	}
	return result;
}

LRESULT WM_SIZE (int wParam, int lParam) {
	LRESULT result = super.WM_SIZE (wParam, lParam);
	if (OS.IsWinCE) return result;
	OS.InvalidateRect (handle, null, true);
	return result;
}

LRESULT wmColorChild (int wParam, int lParam) {
	LRESULT result = super.wmColorChild (wParam, lParam);
	if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
		Control control = findThemeControl ();
		if (control != null) {
			OS.SetBkMode (wParam, OS.TRANSPARENT);
			RECT rect = new RECT ();
			OS.GetClientRect (control.handle, rect);
			OS.MapWindowPoints (control.handle, handle, rect, 2);
			control.drawThemeBackground (wParam, rect);
			return new LRESULT (OS.GetStockObject (OS.NULL_BRUSH));
		}
	}
	return result;
}
*/
}
