//
//                 mxToolKit (c) 1999 by Mete Ciragan
//
// file:           mxLineEdit.cpp
// implementation: Win32 API
// last modified:  Mar 18 1999, Mete Ciragan
// copyright:      The programs and associated files contained in this
//                 distribution were developed by Mete Ciragan. The programs
//                 are not in the public domain, but they are freely
//                 distributable without licensing fees. These programs are
//                 provided without guarantee or warrantee expressed or
//                 implied.
//
#include <mx/mxLineEdit.h>
#include <windows.h>
#include <mx/mxEvent.h>
#include <mx/mxWindow.h>


class mxLineEdit_i
{
public:
	int dummy;
};



typedef LRESULT (CALLBACK * WndProc_t)(HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam);

static WndProc_t s_OldWndProc = 0;

static LRESULT CALLBACK EditWndProc (HWND hwnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
	// This lovely bit of hackery ensures we get return key events
	if ((uMessage == WM_CHAR) && (wParam == VK_RETURN))
	{
		// Post the message directly to all windows in the hierarchy until
		// someone responds
		mxLineEdit *lineEdit = (mxLineEdit *) GetWindowLong (hwnd, GWL_USERDATA);
		mxEvent event;
		event.event = mxEvent::KeyDown;
		event.key = (int) wParam;

		mxWindow* window = lineEdit->getParent();
		while (window)
		{
			if (window->handleEvent (&event))
				break;

			window = window->getParent ();
		}
		return 1;
	}
	else
	{
		return s_OldWndProc( hwnd, uMessage, wParam, lParam );
	}
}

mxLineEdit::mxLineEdit (mxWindow *parent, int x, int y, int w, int h, const char *label, int id, int style)
: mxWidget (parent, x, y, w, h, label)
{
	if (!parent)
		return;

	DWORD dwStyle = WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL | WS_TABSTOP | ES_WANTRETURN | ES_MULTILINE;
	HWND hwndParent = (HWND) ((mxWidget *) parent)->getHandle ();

	if (style == ReadOnly)
		dwStyle |= ES_READONLY;
	else if (style == Password)
		dwStyle |= ES_PASSWORD;

	if (!s_OldWndProc)
	{
		WNDCLASSEX editClass;
		GetClassInfoEx( (HINSTANCE) GetModuleHandle (NULL), "EDIT", &editClass );
		s_OldWndProc = editClass.lpfnWndProc;

		editClass.cbSize = sizeof(WNDCLASSEX);
		editClass.cbClsExtra = 0;
		editClass.lpfnWndProc = EditWndProc;
		editClass.lpszClassName = "mx_edit";
		ATOM hr = RegisterClassEx( &editClass ); 
	}

	void *handle = (void *) CreateWindowEx (WS_EX_CLIENTEDGE, "mx_edit", label, dwStyle, //WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL,
				x, y, w, h, hwndParent,
				(HMENU) id, (HINSTANCE) GetModuleHandle (NULL), NULL);
	
	SendMessage ((HWND) handle, WM_SETFONT, (WPARAM) (HFONT) GetStockObject (ANSI_VAR_FONT), MAKELPARAM (TRUE, 0));
	SendMessage ((HWND) getHandle (), EM_LIMITTEXT, (WPARAM) 256, 0L);
	SetWindowLong ((HWND) handle, GWL_USERDATA, (LONG) this);

	setHandle (handle);
	setType (MX_LINEEDIT);
	setParent (parent);
	setId (id);
}



mxLineEdit::~mxLineEdit ()
{
}


void
mxLineEdit::setMaxLength (int max)
{
	SendMessage ((HWND) getHandle (), EM_LIMITTEXT, (WPARAM) max, 0L);
}



int
mxLineEdit::getMaxLength () const
{
	return (int) SendMessage ((HWND) getHandle (), EM_GETLIMITTEXT, 0, 0L);
}
