#include "XControl.h"
#include "XRect.h"
#include "XPushBtn.h"
#include "XDragHdl.h"
#include "XDragEvn.h"
#include "XMousHdl.h"
#include "XMousEvn.h"
#include "XFrame.h"
#include "XNoteBk.h"
#include "xcolor.h"
#include "xmsgbox.h"
#include "xexcept.h"
#include "XFrmWnd.h"
#include "XObjWnd.h"
#include "xrect.h"
#include "XColor.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


MRESULT HandleDefault(XWindow * w, ULONG msg, MPARAM mp1, MPARAM mp2, BOOL & handled);


MRESULT EXPENTRY ControlProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
	XControl *w = (XControl *) WinQueryWindowULong(hwnd, QWL_USER);

	if (msg == WM_QUERYFRAMECTLCOUNT || msg == WM_FORMATFRAME)
		return ((XFrame *) w)->HandleMessage(msg, mp1, mp2);

	if (w)
	{
		BOOL handled = FALSE;
		MRESULT mr = HandleDefault(w, msg, mp1, mp2, handled);

		if (handled)
			return mr;

		return w->oldfunc(hwnd, msg, mp1, mp2);
	}
	else
		return WinDefWindowProc(hwnd, msg, mp1, mp2);
}


/*@ 
@class XControl
@parent XWindow
@type overview
@symbol _
@remarks XControl is the basic class for user input/output windows. You cannot create
a XControl directly.
*/


XControl :: XControl(const HWND hwnd)
{
	winhandle = hwnd;
	if (hwnd)
	{
		WinSetWindowULong(winhandle, QWL_USER, (ULONG) this);
		oldfunc = WinSubclassWindow(winhandle, (PFNWP) ControlProc);
	}
}


XControl :: XControl(const XRect * rec, const ULONG style, const XWindow * ow, const char *string, const PSZ cl, const USHORT id, const char *font)
{
   PRES pres;
	HWND hwnd = 0;
	char *s = (char *) string;

	if (ow)
		hwnd = ow->GetHandle();
	ULONG st = style;
	void *p = NULL;

	if ((cl == WC_BUTTON && (style & BS_BITMAP || style & BS_ICON || style & BS_MINIICON)) || font)
	{
		pres.fcparam.id = PP_FOREGROUNDCOLORINDEX;
		pres.fcparam.cb = sizeof(pres.fcparam.ulColor);
		pres.fcparam.ulColor = 16;
		pres.fntparam.id = PP_FONTNAMESIZE;
		pres.fntparam.cb = 20;
		pres.cb = sizeof(pres.fcparam) + sizeof(pres.fntparam);
		p = (void *) &pres;

		if (!font)
		{
			strcpy(pres.fntparam.szFontNameSize, "7.Helv");
			char ti[50];

			if (style & BS_TEXT)
				sprintf(ti, "#%i\t%s", id, string);
			else
				sprintf(ti, "#%i", id);
			s = ti;
		}
		else
			strcpy(pres.fntparam.szFontNameSize, font);
	}
	if (cl == WC_BUTTON && st & BS_USERBUTTON && st & 0x8000)
		st ^= 0x8000;

	if (rec == NULL)
		rec = new XRect();
	winhandle = WinCreateWindow(hwnd, cl, (PSZ) s, st, rec->x, rec->y, rec->cx, rec->cy, hwnd, HWND_TOP, id, NULL, p);

	if (winhandle == 0)
		OOLThrow("error creating control", -10);

	WinSetWindowULong(winhandle, QWL_USER, (ULONG) this);
	oldfunc = WinSubclassWindow(winhandle, (PFNWP) ControlProc);
	if (ow && cl != WC_SCROLLBAR)
		WinPostMsg(hwnd, OOL_ADDCLIENT, (MPARAM) winhandle, 0);
}




XObjectWindow :: XObjectWindow(const XFrameWindow * id)
{
	owner = (XFrameWindow *) id;

	if (WinRegisterClass(WinQueryAnchorBlock(id->frame), (PSZ) "OOL_OBJECTWND", (PFNWP) WindowProc, 0, 4) == FALSE)
		OOLThrow("error registering objectwindow-class", -10);

	winhandle = WinCreateWindow(HWND_OBJECT, (PSZ) "OOL_OBJECTWND", (PSZ) "", 0, 0, 0, 0, 0, HWND_OBJECT, HWND_BOTTOM, 0, this, 0);
}



