From ad815f4e2a4fafe6b293c5752d748e387d0f9bea Mon Sep 17 00:00:00 2001 From: "joki%netscape.com" Date: Fri, 17 Jul 1998 04:29:16 +0000 Subject: [PATCH] Adding new events code to JS event handler support --- dom/public/idl/events/Event.idl | 180 ++++ dom/public/idl/events/makefile.win | 51 ++ dom/public/idl/makefile.win | 4 +- dom/src/events/Makefile | 37 + dom/src/events/makefile.win | 48 ++ dom/src/events/nsJSEvent.cpp | 1160 ++++++++++++++++++++++++++ dom/src/events/nsJSEventListener.cpp | 121 +++ dom/src/events/nsJSEventListener.h | 45 + 8 files changed, 1644 insertions(+), 2 deletions(-) create mode 100644 dom/public/idl/events/Event.idl create mode 100644 dom/public/idl/events/makefile.win create mode 100644 dom/src/events/Makefile create mode 100644 dom/src/events/makefile.win create mode 100644 dom/src/events/nsJSEvent.cpp create mode 100644 dom/src/events/nsJSEventListener.cpp create mode 100644 dom/src/events/nsJSEventListener.h diff --git a/dom/public/idl/events/Event.idl b/dom/public/idl/events/Event.idl new file mode 100644 index 000000000000..cc81752ddc48 --- /dev/null +++ b/dom/public/idl/events/Event.idl @@ -0,0 +1,180 @@ + interface Event { + const int VK_CANCEL = 0x03; + const int VK_BACK = 0x08; + const int VK_TAB = 0x09; + const int VK_CLEAR = 0x0C; + const int VK_RETURN = 0x0D; + const int VK_SHIFT = 0x10; + const int VK_CONTROL = 0x11; + const int VK_ALT = 0x12; + const int VK_PAUSE = 0x13; + const int VK_CAPS_LOCK = 0x14; + const int VK_ESCAPE = 0x1B; + const int VK_SPACE = 0x20; + const int VK_PAGE_UP = 0x21; + const int VK_PAGE_DOWN = 0x22; + const int VK_END = 0x23; + const int VK_HOME = 0x24; + const int VK_LEFT = 0x25; + const int VK_UP = 0x26; + const int VK_RIGHT = 0x27; + const int VK_DOWN = 0x28; + const int VK_PRINTSCREEN = 0x2C; + const int VK_INSERT = 0x2D; + const int VK_DELETE = 0x2E; + + // VK_0 - VK_9 match their ascii values + const int VK_0 = 0x30; + const int VK_1 = 0x31; + const int VK_2 = 0x32; + const int VK_3 = 0x33; + const int VK_4 = 0x34; + const int VK_5 = 0x35; + const int VK_6 = 0x36; + const int VK_7 = 0x37; + const int VK_8 = 0x38; + const int VK_9 = 0x39; + + const int VK_SEMICOLON = 0x3B; + const int VK_EQUALS = 0x3D; + + // VK_A - VK_Z match their ascii values + const int VK_A = 0x41; + const int VK_B = 0x42; + const int VK_C = 0x43; + const int VK_D = 0x44; + const int VK_E = 0x45; + const int VK_F = 0x46; + const int VK_G = 0x47; + const int VK_H = 0x48; + const int VK_I = 0x49; + const int VK_J = 0x4A; + const int VK_K = 0x4B; + const int VK_L = 0x4C; + const int VK_M = 0x4D; + const int VK_N = 0x4E; + const int VK_O = 0x4F; + const int VK_P = 0x50; + const int VK_Q = 0x51; + const int VK_R = 0x52; + const int VK_S = 0x53; + const int VK_T = 0x54; + const int VK_U = 0x55; + const int VK_V = 0x56; + const int VK_W = 0x57; + const int VK_X = 0x58; + const int VK_Y = 0x59; + const int VK_Z = 0x5A; + + const int VK_NUMPAD0 = 0x60; + const int VK_NUMPAD1 = 0x61; + const int VK_NUMPAD2 = 0x62; + const int VK_NUMPAD3 = 0x63; + const int VK_NUMPAD4 = 0x64; + const int VK_NUMPAD5 = 0x65; + const int VK_NUMPAD6 = 0x66; + const int VK_NUMPAD7 = 0x67; + const int VK_NUMPAD8 = 0x68; + const int VK_NUMPAD9 = 0x69; + const int VK_MULTIPLY = 0x6A; + const int VK_ADD = 0x6B; + const int VK_SEPARATOR = 0x6C; + const int VK_SUBTRACT = 0x6D; + const int VK_DECIMAL = 0x6E; + const int VK_DIVIDE = 0x6F; + const int VK_F1 = 0x70; + const int VK_F2 = 0x71; + const int VK_F3 = 0x72; + const int VK_F4 = 0x73; + const int VK_F5 = 0x74; + const int VK_F6 = 0x75; + const int VK_F7 = 0x76; + const int VK_F8 = 0x77; + const int VK_F9 = 0x78; + const int VK_F10 = 0x79; + const int VK_F11 = 0x7A; + const int VK_F12 = 0x7B; + const int VK_F13 = 0x7C; + const int VK_F14 = 0x7D; + const int VK_F15 = 0x7E; + const int VK_F16 = 0x7F; + const int VK_F17 = 0x80; + const int VK_F18 = 0x81; + const int VK_F19 = 0x82; + const int VK_F20 = 0x83; + const int VK_F21 = 0x84; + const int VK_F22 = 0x85; + const int VK_F23 = 0x86; + const int VK_F24 = 0x87; + + const int VK_NUM_LOCK = 0x90; + const int VK_SCROLL_LOCK = 0x91; + + const int VK_COMMA = 0xBC; + const int VK_PERIOD = 0xBE; + const int VK_SLASH = 0xBF; + const int VK_BACK_QUOTE = 0xC0; + const int VK_OPEN_BRACKET = 0xDB; + const int VK_BACK_SLASH = 0xDC; + const int VK_CLOSE_BRACKET = 0xDD; + const int VK_QUOTE = 0xDE; + + attribute wstring type; + attribute Node target; + + attribute int screenX; + attribute int screenY; + + attribute int clientX; + attribute int clientY; + + attribute boolean altKey; + attribute boolean ctrlKey; + attribute boolean shiftKey; + attribute boolean metaKey; + + attribute unsigned long charCode; + attribute unsigned long keyCode; + attribute unsigned long button; + }; + + interface NSEvent { + const int EVENT_MOUSEDOWN = 0x00000001; + const int EVENT_MOUSEUP = 0x00000002; + const int EVENT_MOUSEOVER = 0x00000004; + const int EVENT_MOUSEOUT = 0x00000008; + const int EVENT_MOUSEMOVE = 0x00000010; + const int EVENT_MOUSEDRAG = 0x00000020; + const int EVENT_CLICK = 0x00000040; + const int EVENT_DBLCLICK = 0x00000080; + const int EVENT_KEYDOWN = 0x00000100; + const int EVENT_KEYUP = 0x00000200; + const int EVENT_KEYPRESS = 0x00000400; + const int EVENT_DRAGDROP = 0x00000800; + const int EVENT_FOCUS = 0x00001000; + const int EVENT_BLUR = 0x00002000; + const int EVENT_SELECT = 0x00004000; + const int EVENT_CHANGE = 0x00008000; + const int EVENT_RESET = 0x00010000; + const int EVENT_SUBMIT = 0x00020000; + const int EVENT_SCROLL = 0x00040000; + const int EVENT_LOAD = 0x00080000; + const int EVENT_UNLOAD = 0x00100000; + const int EVENT_XFER_DONE = 0x00200000; + const int EVENT_ABORT = 0x00400000; + const int EVENT_ERROR = 0x00800000; + const int EVENT_LOCATE = 0x01000000; + const int EVENT_MOVE = 0x02000000; + const int EVENT_RESIZE = 0x04000000; + const int EVENT_FORWARD = 0x08000000; + const int EVENT_HELP = 0x10000000; + const int EVENT_BACK = 0x20000000; + + const int EVENT_ALT_MASK = 0x00000001; + const int EVENT_CONTROL_MASK = 0x00000002; + const int EVENT_SHIFT_MASK = 0x00000004; + const int EVENT_META_MASK = 0x00000008; + + attribute int layerX; + attribute int layerY; + }; diff --git a/dom/public/idl/events/makefile.win b/dom/public/idl/events/makefile.win new file mode 100644 index 000000000000..706a3539e99d --- /dev/null +++ b/dom/public/idl/events/makefile.win @@ -0,0 +1,51 @@ +#!nmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=..\..\..\.. +IGNORE_MANIFEST=1 + +MODULE=raptor + +IDLSRCS = \ + Event.idl \ + +XPCOM_DESTDIR=$(DEPTH)\dom\public\coreEvents +JSSTUB_DESTDIR=$(DEPTH)\dom\src\events + +GENXDIR=genx +GENJSDIR=genjs + +!include <$(DEPTH)\config\rules.mak> + +$(GENXDIR): + -mkdir $(GENXDIR) + +$(GENJSDIR): + -mkdir $(GENJSDIR) + +IDLC=$(DIST)\bin\idlc.exe +GENIID=$(DIST)\bin\geniid.pl + +export:: $(GENXDIR) $(GENJSDIR) $(IDLSRCS) + @echo +++ make: generating xpcom headers + $(IDLC) -d $(GENXDIR) -x $(IDLSRCS) + @echo +++ make: generating JavaScript stubs + $(IDLC) -d $(GENJSDIR) -j $(IDLSRCS) + +install:: + for %g in ($(IDLSRCS:.idl=.h)) do $(MAKE_INSTALL:/=\) $(GENXDIR)\nsIDOM%g $(XPCOM_DESTDIR) + for %g in ($(IDLSRCS:.idl=.cpp)) do $(MAKE_INSTALL:/=\) $(GENJSDIR)\nsJS%g $(JSSTUB_DESTDIR) diff --git a/dom/public/idl/makefile.win b/dom/public/idl/makefile.win index 7e020837db17..80d6d992aa3f 100644 --- a/dom/public/idl/makefile.win +++ b/dom/public/idl/makefile.win @@ -18,7 +18,7 @@ DEPTH=..\..\.. IGNORE_MANIFEST=1 -DIRS=coreDom html +DIRS=coreDom html events MODULE=raptor IDLSRCS = \ @@ -27,7 +27,7 @@ IDLSRCS = \ GLOBAL_IDLSRC = Window.idl XPCOM_DESTDIR=$(DEPTH)\dom\public -JSSTUB_DESTDIR=$(DEPTH)\dom\src +JSSTUB_DESTDIR=$(DEPTH)\dom\src\base GENXDIR=genx GENJSDIR=genjs diff --git a/dom/src/events/Makefile b/dom/src/events/Makefile new file mode 100644 index 000000000000..6a107bad84c3 --- /dev/null +++ b/dom/src/events/Makefile @@ -0,0 +1,37 @@ +#!gmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=../../.. + +LIBRARY_NAME = jsdomevents_s + +DEFINES = -D_IMPL_NS_DOM + +CPPSRCS = \ + nsJSEventListener.cpp \ + nsJSEvent.cpp \ + $(NULL) + +MODULE = raptor + +REQUIRES = xpcom raptor dom js + +include $(DEPTH)/config/config.mk + +TARGETS = $(LIBRARY) + +include $(DEPTH)/config/rules.mk diff --git a/dom/src/events/makefile.win b/dom/src/events/makefile.win new file mode 100644 index 000000000000..53d42dc9da34 --- /dev/null +++ b/dom/src/events/makefile.win @@ -0,0 +1,48 @@ +#!nmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=..\..\.. +IGNORE_MANIFEST=1 + +LIBRARY_NAME=jsdomevents_s +MODULE=raptor +REQUIRES=xpcom raptor js + +DEFINES=-D_IMPL_NS_DOM -DWIN32_LEAN_AND_MEAN + +CPPSRCS = \ + nsJSEventListener.cpp \ + nsJSEvent.cpp \ + $(NULL) + +CPP_OBJS= \ + .\$(OBJDIR)\nsJSEventListener.obj .\$(OBJDIR)\nsJSEvent.obj + +LINCS=-I$(XPDIST)\public\xpcom -I$(XPDIST)\public\raptor -I$(XPDIST)\public\dom -I$(XPDIST)\public\js + +LCFLAGS = \ + $(LCFLAGS) \ + $(DEFINES) \ + $(NULL) + +include <$(DEPTH)\config\rules.mak> + +libs:: $(LIBRARY) + $(MAKE_INSTALL) $(LIBRARY) $(DIST)\lib + +clobber:: + rm -f $(DIST)\lib\$(LIBRARY_NAME).lib diff --git a/dom/src/events/nsJSEvent.cpp b/dom/src/events/nsJSEvent.cpp new file mode 100644 index 000000000000..b03d56bac7e8 --- /dev/null +++ b/dom/src/events/nsJSEvent.cpp @@ -0,0 +1,1160 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +/* AUTO-GENERATED. DO NOT EDIT!!! */ + +#include "jsapi.h" +#include "nscore.h" +#include "nsIScriptContext.h" +#include "nsIJSScriptObject.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" +#include "nsIPtr.h" +#include "nsString.h" +#include "nsIDOMNode.h" +#include "nsIDOMEvent.h" + + +static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); +static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); +static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID); +static NS_DEFINE_IID(kINodeIID, NS_IDOMNODE_IID); +static NS_DEFINE_IID(kIEventIID, NS_IDOMEVENT_IID); +static NS_DEFINE_IID(kINSEventIID, NS_IDOMNSEVENT_IID); + +NS_DEF_PTR(nsIDOMNode); +NS_DEF_PTR(nsIDOMEvent); +NS_DEF_PTR(nsIDOMNSEvent); + +// +// Event property ids +// +enum Event_slots { + EVENT_TYPE = -11, + EVENT_TARGET = -12, + EVENT_SCREENX = -13, + EVENT_SCREENY = -14, + EVENT_CLIENTX = -15, + EVENT_CLIENTY = -16, + EVENT_ALTKEY = -17, + EVENT_CTRLKEY = -18, + EVENT_SHIFTKEY = -19, + EVENT_METAKEY = -110, + EVENT_CHARCODE = -111, + EVENT_KEYCODE = -112, + EVENT_BUTTON = -113, + NSEVENT_LAYERX = -21, + NSEVENT_LAYERY = -22 +}; + +/***********************************************************************/ +// +// Event Properties Getter +// +PR_STATIC_CALLBACK(JSBool) +GetEventProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMEvent *a = (nsIDOMEvent*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case EVENT_TYPE: + { + nsAutoString prop; + if (NS_OK == a->GetType(prop)) { + JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length()); + // set the return value + *vp = STRING_TO_JSVAL(jsstring); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_TARGET: + { + nsIDOMNode* prop; + if (NS_OK == a->GetTarget(&prop)) { + // get the js object + if (prop != nsnull) { + nsIScriptObjectOwner *owner = nsnull; + if (NS_OK == prop->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + JSObject *object = nsnull; + nsIScriptContext *script_cx = (nsIScriptContext *)JS_GetContextPrivate(cx); + if (NS_OK == owner->GetScriptObject(script_cx, (void**)&object)) { + // set the return value + *vp = OBJECT_TO_JSVAL(object); + } + NS_RELEASE(owner); + } + NS_RELEASE(prop); + } + else { + *vp = JSVAL_NULL; + } + } + else { + return JS_FALSE; + } + break; + } + case EVENT_SCREENX: + { + PRInt32 prop; + if (NS_OK == a->GetScreenX(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_SCREENY: + { + PRInt32 prop; + if (NS_OK == a->GetScreenY(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_CLIENTX: + { + PRInt32 prop; + if (NS_OK == a->GetClientX(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_CLIENTY: + { + PRInt32 prop; + if (NS_OK == a->GetClientY(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_ALTKEY: + { + PRBool prop; + if (NS_OK == a->GetAltKey(&prop)) { + *vp = BOOLEAN_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_CTRLKEY: + { + PRBool prop; + if (NS_OK == a->GetCtrlKey(&prop)) { + *vp = BOOLEAN_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_SHIFTKEY: + { + PRBool prop; + if (NS_OK == a->GetShiftKey(&prop)) { + *vp = BOOLEAN_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_METAKEY: + { + PRBool prop; + if (NS_OK == a->GetMetaKey(&prop)) { + *vp = BOOLEAN_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_CHARCODE: + { + PRUint32 prop; + if (NS_OK == a->GetCharCode(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_KEYCODE: + { + PRUint32 prop; + if (NS_OK == a->GetKeyCode(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case EVENT_BUTTON: + { + PRUint32 prop; + if (NS_OK == a->GetButton(&prop)) { + *vp = INT_TO_JSVAL(prop); + } + else { + return JS_FALSE; + } + break; + } + case NSEVENT_LAYERX: + { + PRInt32 prop; + nsIDOMNSEvent* b; + if (NS_OK == a->QueryInterface(kINSEventIID, (void **)&b)) { + if(NS_OK == b->GetLayerX(&prop)) { + *vp = INT_TO_JSVAL(prop); + NS_RELEASE(b); + } + else { + NS_RELEASE(b); + return JS_FALSE; + } + } + else { + JS_ReportError(cx, "Object must be of type NSEvent"); + return JS_FALSE; + } + break; + } + case NSEVENT_LAYERY: + { + PRInt32 prop; + nsIDOMNSEvent* b; + if (NS_OK == a->QueryInterface(kINSEventIID, (void **)&b)) { + if(NS_OK == b->GetLayerY(&prop)) { + *vp = INT_TO_JSVAL(prop); + NS_RELEASE(b); + } + else { + NS_RELEASE(b); + return JS_FALSE; + } + } + else { + JS_ReportError(cx, "Object must be of type NSEvent"); + return JS_FALSE; + } + break; + } + default: + { + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + PRBool rval; + rval = object->GetProperty(cx, id, vp); + NS_RELEASE(object); + return rval; + } + } + } + } + else { + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + PRBool rval; + rval = object->GetProperty(cx, id, vp); + NS_RELEASE(object); + return rval; + } + } + + return PR_TRUE; +} + +/***********************************************************************/ +// +// Event Properties Setter +// +PR_STATIC_CALLBACK(JSBool) +SetEventProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMEvent *a = (nsIDOMEvent*)JS_GetPrivate(cx, obj); + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == a) { + return JS_TRUE; + } + + if (JSVAL_IS_INT(id)) { + switch(JSVAL_TO_INT(id)) { + case EVENT_TYPE: + { + nsAutoString prop; + JSString *jsstring; + if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) { + prop.SetString(JS_GetStringChars(jsstring)); + } + else { + prop.SetString((const char *)nsnull); + } + + a->SetType(prop); + + break; + } + case EVENT_TARGET: + { + nsIDOMNode* prop; + if (JSVAL_IS_NULL(*vp)) { + prop = nsnull; + } + else if (JSVAL_IS_OBJECT(*vp)) { + JSObject *jsobj = JSVAL_TO_OBJECT(*vp); + nsISupports *supports = (nsISupports *)JS_GetPrivate(cx, jsobj); + if (NS_OK != supports->QueryInterface(kINodeIID, (void **)&prop)) { + JS_ReportError(cx, "Parameter must be of type Node"); + return JS_FALSE; + } + } + else { + JS_ReportError(cx, "Parameter must be an object"); + return JS_FALSE; + } + + a->SetTarget(prop); + if (prop) NS_RELEASE(prop); + break; + } + case EVENT_SCREENX: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetScreenX(prop); + + break; + } + case EVENT_SCREENY: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetScreenY(prop); + + break; + } + case EVENT_CLIENTX: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetClientX(prop); + + break; + } + case EVENT_CLIENTY: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetClientY(prop); + + break; + } + case EVENT_ALTKEY: + { + PRBool prop; + JSBool temp; + if (JSVAL_IS_BOOLEAN(*vp) && JS_ValueToBoolean(cx, *vp, &temp)) { + prop = (PRBool)temp; + } + else { + JS_ReportError(cx, "Parameter must be a boolean"); + return JS_FALSE; + } + + a->SetAltKey(prop); + + break; + } + case EVENT_CTRLKEY: + { + PRBool prop; + JSBool temp; + if (JSVAL_IS_BOOLEAN(*vp) && JS_ValueToBoolean(cx, *vp, &temp)) { + prop = (PRBool)temp; + } + else { + JS_ReportError(cx, "Parameter must be a boolean"); + return JS_FALSE; + } + + a->SetCtrlKey(prop); + + break; + } + case EVENT_SHIFTKEY: + { + PRBool prop; + JSBool temp; + if (JSVAL_IS_BOOLEAN(*vp) && JS_ValueToBoolean(cx, *vp, &temp)) { + prop = (PRBool)temp; + } + else { + JS_ReportError(cx, "Parameter must be a boolean"); + return JS_FALSE; + } + + a->SetShiftKey(prop); + + break; + } + case EVENT_METAKEY: + { + PRBool prop; + JSBool temp; + if (JSVAL_IS_BOOLEAN(*vp) && JS_ValueToBoolean(cx, *vp, &temp)) { + prop = (PRBool)temp; + } + else { + JS_ReportError(cx, "Parameter must be a boolean"); + return JS_FALSE; + } + + a->SetMetaKey(prop); + + break; + } + case EVENT_CHARCODE: + { + PRUint32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRUint32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetCharCode(prop); + + break; + } + case EVENT_KEYCODE: + { + PRUint32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRUint32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetKeyCode(prop); + + break; + } + case EVENT_BUTTON: + { + PRUint32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRUint32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + a->SetButton(prop); + + break; + } + case NSEVENT_LAYERX: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + nsIDOMNSEvent *b; + if (NS_OK == a->QueryInterface(kINSEventIID, (void **)&b)) { + b->SetLayerX(prop); + NS_RELEASE(b); + } + else { + + JS_ReportError(cx, "Object must be of type NSEvent"); + return JS_FALSE; + } + + break; + } + case NSEVENT_LAYERY: + { + PRInt32 prop; + int32 temp; + if (JSVAL_IS_NUMBER(*vp) && JS_ValueToInt32(cx, *vp, &temp)) { + prop = (PRInt32)temp; + } + else { + JS_ReportError(cx, "Parameter must be a number"); + return JS_FALSE; + } + + nsIDOMNSEvent *b; + if (NS_OK == a->QueryInterface(kINSEventIID, (void **)&b)) { + b->SetLayerY(prop); + NS_RELEASE(b); + } + else { + + JS_ReportError(cx, "Object must be of type NSEvent"); + return JS_FALSE; + } + + break; + } + default: + { + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + PRBool rval; + rval = object->SetProperty(cx, id, vp); + NS_RELEASE(object); + return rval; + } + } + } + } + else { + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + PRBool rval; + rval = object->SetProperty(cx, id, vp); + NS_RELEASE(object); + return rval; + } + } + + return PR_TRUE; +} + + +// +// Event finalizer +// +PR_STATIC_CALLBACK(void) +FinalizeEvent(JSContext *cx, JSObject *obj) +{ + nsIDOMEvent *a = (nsIDOMEvent*)JS_GetPrivate(cx, obj); + + if (nsnull != a) { + // get the js object + nsIScriptObjectOwner *owner = nsnull; + if (NS_OK == a->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + owner->ResetScriptObject(); + NS_RELEASE(owner); + } + + NS_RELEASE(a); + } +} + + +// +// Event enumerate +// +PR_STATIC_CALLBACK(JSBool) +EnumerateEvent(JSContext *cx, JSObject *obj) +{ + nsIDOMEvent *a = (nsIDOMEvent*)JS_GetPrivate(cx, obj); + + if (nsnull != a) { + // get the js object + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + object->EnumerateProperty(cx); + NS_RELEASE(object); + } + } + return JS_TRUE; +} + + +// +// Event resolve +// +PR_STATIC_CALLBACK(JSBool) +ResolveEvent(JSContext *cx, JSObject *obj, jsval id) +{ + nsIDOMEvent *a = (nsIDOMEvent*)JS_GetPrivate(cx, obj); + + if (nsnull != a) { + // get the js object + nsIJSScriptObject *object; + if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) { + object->Resolve(cx, id); + NS_RELEASE(object); + } + } + return JS_TRUE; +} + + +/***********************************************************************/ +// +// class for Event +// +JSClass EventClass = { + "Event", + JSCLASS_HAS_PRIVATE, + JS_PropertyStub, + JS_PropertyStub, + GetEventProperty, + SetEventProperty, + EnumerateEvent, + ResolveEvent, + JS_ConvertStub, + FinalizeEvent +}; + + +// +// Event class properties +// +static JSPropertySpec EventProperties[] = +{ + {"type", EVENT_TYPE, JSPROP_ENUMERATE}, + {"target", EVENT_TARGET, JSPROP_ENUMERATE}, + {"screenX", EVENT_SCREENX, JSPROP_ENUMERATE}, + {"screenY", EVENT_SCREENY, JSPROP_ENUMERATE}, + {"clientX", EVENT_CLIENTX, JSPROP_ENUMERATE}, + {"clientY", EVENT_CLIENTY, JSPROP_ENUMERATE}, + {"altKey", EVENT_ALTKEY, JSPROP_ENUMERATE}, + {"ctrlKey", EVENT_CTRLKEY, JSPROP_ENUMERATE}, + {"shiftKey", EVENT_SHIFTKEY, JSPROP_ENUMERATE}, + {"metaKey", EVENT_METAKEY, JSPROP_ENUMERATE}, + {"charCode", EVENT_CHARCODE, JSPROP_ENUMERATE}, + {"keyCode", EVENT_KEYCODE, JSPROP_ENUMERATE}, + {"button", EVENT_BUTTON, JSPROP_ENUMERATE}, + {"layerX", NSEVENT_LAYERX, JSPROP_ENUMERATE}, + {"layerY", NSEVENT_LAYERY, JSPROP_ENUMERATE}, + {0} +}; + + +// +// Event class methods +// +static JSFunctionSpec EventMethods[] = +{ + {0} +}; + + +// +// Event constructor +// +PR_STATIC_CALLBACK(JSBool) +Event(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + return JS_TRUE; +} + + +// +// Event class initialization +// +nsresult NS_InitEventClass(nsIScriptContext *aContext, void **aPrototype) +{ + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + JSObject *proto = nsnull; + JSObject *constructor = nsnull; + JSObject *parent_proto = nsnull; + JSObject *global = JS_GetGlobalObject(jscontext); + jsval vp; + + if ((PR_TRUE != JS_LookupProperty(jscontext, global, "Event", &vp)) || + !JSVAL_IS_OBJECT(vp) || + ((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) || + (PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) || + !JSVAL_IS_OBJECT(vp)) { + + proto = JS_InitClass(jscontext, // context + global, // global object + parent_proto, // parent proto + &EventClass, // JSClass + Event, // JSNative ctor + 0, // ctor args + EventProperties, // proto props + EventMethods, // proto funcs + nsnull, // ctor props (static) + nsnull); // ctor funcs (static) + if (nsnull == proto) { + return NS_ERROR_FAILURE; + } + + if ((PR_TRUE == JS_LookupProperty(jscontext, global, "Event", &vp)) && + JSVAL_IS_OBJECT(vp) && + ((constructor = JSVAL_TO_OBJECT(vp)) != nsnull)) { + vp = INT_TO_JSVAL(nsIDOMEvent::VK_CANCEL); + JS_SetProperty(jscontext, constructor, "VK_CANCEL", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_BACK); + JS_SetProperty(jscontext, constructor, "VK_BACK", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_TAB); + JS_SetProperty(jscontext, constructor, "VK_TAB", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_CLEAR); + JS_SetProperty(jscontext, constructor, "VK_CLEAR", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_RETURN); + JS_SetProperty(jscontext, constructor, "VK_RETURN", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SHIFT); + JS_SetProperty(jscontext, constructor, "VK_SHIFT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_CONTROL); + JS_SetProperty(jscontext, constructor, "VK_CONTROL", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_ALT); + JS_SetProperty(jscontext, constructor, "VK_ALT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_PAUSE); + JS_SetProperty(jscontext, constructor, "VK_PAUSE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_CAPS_LOCK); + JS_SetProperty(jscontext, constructor, "VK_CAPS_LOCK", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_ESCAPE); + JS_SetProperty(jscontext, constructor, "VK_ESCAPE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SPACE); + JS_SetProperty(jscontext, constructor, "VK_SPACE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_PAGE_UP); + JS_SetProperty(jscontext, constructor, "VK_PAGE_UP", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_PAGE_DOWN); + JS_SetProperty(jscontext, constructor, "VK_PAGE_DOWN", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_END); + JS_SetProperty(jscontext, constructor, "VK_END", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_HOME); + JS_SetProperty(jscontext, constructor, "VK_HOME", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_LEFT); + JS_SetProperty(jscontext, constructor, "VK_LEFT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_UP); + JS_SetProperty(jscontext, constructor, "VK_UP", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_RIGHT); + JS_SetProperty(jscontext, constructor, "VK_RIGHT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_DOWN); + JS_SetProperty(jscontext, constructor, "VK_DOWN", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_PRINTSCREEN); + JS_SetProperty(jscontext, constructor, "VK_PRINTSCREEN", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_INSERT); + JS_SetProperty(jscontext, constructor, "VK_INSERT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_DELETE); + JS_SetProperty(jscontext, constructor, "VK_DELETE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_0); + JS_SetProperty(jscontext, constructor, "VK_0", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_1); + JS_SetProperty(jscontext, constructor, "VK_1", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_2); + JS_SetProperty(jscontext, constructor, "VK_2", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_3); + JS_SetProperty(jscontext, constructor, "VK_3", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_4); + JS_SetProperty(jscontext, constructor, "VK_4", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_5); + JS_SetProperty(jscontext, constructor, "VK_5", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_6); + JS_SetProperty(jscontext, constructor, "VK_6", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_7); + JS_SetProperty(jscontext, constructor, "VK_7", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_8); + JS_SetProperty(jscontext, constructor, "VK_8", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_9); + JS_SetProperty(jscontext, constructor, "VK_9", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SEMICOLON); + JS_SetProperty(jscontext, constructor, "VK_SEMICOLON", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_EQUALS); + JS_SetProperty(jscontext, constructor, "VK_EQUALS", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_A); + JS_SetProperty(jscontext, constructor, "VK_A", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_B); + JS_SetProperty(jscontext, constructor, "VK_B", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_C); + JS_SetProperty(jscontext, constructor, "VK_C", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_D); + JS_SetProperty(jscontext, constructor, "VK_D", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_E); + JS_SetProperty(jscontext, constructor, "VK_E", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F); + JS_SetProperty(jscontext, constructor, "VK_F", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_G); + JS_SetProperty(jscontext, constructor, "VK_G", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_H); + JS_SetProperty(jscontext, constructor, "VK_H", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_I); + JS_SetProperty(jscontext, constructor, "VK_I", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_J); + JS_SetProperty(jscontext, constructor, "VK_J", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_K); + JS_SetProperty(jscontext, constructor, "VK_K", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_L); + JS_SetProperty(jscontext, constructor, "VK_L", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_M); + JS_SetProperty(jscontext, constructor, "VK_M", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_N); + JS_SetProperty(jscontext, constructor, "VK_N", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_O); + JS_SetProperty(jscontext, constructor, "VK_O", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_P); + JS_SetProperty(jscontext, constructor, "VK_P", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_Q); + JS_SetProperty(jscontext, constructor, "VK_Q", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_R); + JS_SetProperty(jscontext, constructor, "VK_R", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_S); + JS_SetProperty(jscontext, constructor, "VK_S", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_T); + JS_SetProperty(jscontext, constructor, "VK_T", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_U); + JS_SetProperty(jscontext, constructor, "VK_U", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_V); + JS_SetProperty(jscontext, constructor, "VK_V", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_W); + JS_SetProperty(jscontext, constructor, "VK_W", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_X); + JS_SetProperty(jscontext, constructor, "VK_X", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_Y); + JS_SetProperty(jscontext, constructor, "VK_Y", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_Z); + JS_SetProperty(jscontext, constructor, "VK_Z", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD0); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD0", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD1); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD1", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD2); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD2", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD3); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD3", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD4); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD4", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD5); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD5", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD6); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD6", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD7); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD7", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD8); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD8", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUMPAD9); + JS_SetProperty(jscontext, constructor, "VK_NUMPAD9", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_MULTIPLY); + JS_SetProperty(jscontext, constructor, "VK_MULTIPLY", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_ADD); + JS_SetProperty(jscontext, constructor, "VK_ADD", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SEPARATOR); + JS_SetProperty(jscontext, constructor, "VK_SEPARATOR", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SUBTRACT); + JS_SetProperty(jscontext, constructor, "VK_SUBTRACT", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_DECIMAL); + JS_SetProperty(jscontext, constructor, "VK_DECIMAL", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_DIVIDE); + JS_SetProperty(jscontext, constructor, "VK_DIVIDE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F1); + JS_SetProperty(jscontext, constructor, "VK_F1", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F2); + JS_SetProperty(jscontext, constructor, "VK_F2", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F3); + JS_SetProperty(jscontext, constructor, "VK_F3", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F4); + JS_SetProperty(jscontext, constructor, "VK_F4", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F5); + JS_SetProperty(jscontext, constructor, "VK_F5", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F6); + JS_SetProperty(jscontext, constructor, "VK_F6", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F7); + JS_SetProperty(jscontext, constructor, "VK_F7", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F8); + JS_SetProperty(jscontext, constructor, "VK_F8", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F9); + JS_SetProperty(jscontext, constructor, "VK_F9", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F10); + JS_SetProperty(jscontext, constructor, "VK_F10", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F11); + JS_SetProperty(jscontext, constructor, "VK_F11", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F12); + JS_SetProperty(jscontext, constructor, "VK_F12", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F13); + JS_SetProperty(jscontext, constructor, "VK_F13", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F14); + JS_SetProperty(jscontext, constructor, "VK_F14", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F15); + JS_SetProperty(jscontext, constructor, "VK_F15", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F16); + JS_SetProperty(jscontext, constructor, "VK_F16", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F17); + JS_SetProperty(jscontext, constructor, "VK_F17", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F18); + JS_SetProperty(jscontext, constructor, "VK_F18", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F19); + JS_SetProperty(jscontext, constructor, "VK_F19", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F20); + JS_SetProperty(jscontext, constructor, "VK_F20", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F21); + JS_SetProperty(jscontext, constructor, "VK_F21", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F22); + JS_SetProperty(jscontext, constructor, "VK_F22", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F23); + JS_SetProperty(jscontext, constructor, "VK_F23", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_F24); + JS_SetProperty(jscontext, constructor, "VK_F24", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_NUM_LOCK); + JS_SetProperty(jscontext, constructor, "VK_NUM_LOCK", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SCROLL_LOCK); + JS_SetProperty(jscontext, constructor, "VK_SCROLL_LOCK", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_COMMA); + JS_SetProperty(jscontext, constructor, "VK_COMMA", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_PERIOD); + JS_SetProperty(jscontext, constructor, "VK_PERIOD", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_SLASH); + JS_SetProperty(jscontext, constructor, "VK_SLASH", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_BACK_QUOTE); + JS_SetProperty(jscontext, constructor, "VK_BACK_QUOTE", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_OPEN_BRACKET); + JS_SetProperty(jscontext, constructor, "VK_OPEN_BRACKET", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_BACK_SLASH); + JS_SetProperty(jscontext, constructor, "VK_BACK_SLASH", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_CLOSE_BRACKET); + JS_SetProperty(jscontext, constructor, "VK_CLOSE_BRACKET", &vp); + + vp = INT_TO_JSVAL(nsIDOMEvent::VK_QUOTE); + JS_SetProperty(jscontext, constructor, "VK_QUOTE", &vp); + + } + + } + else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) { + proto = JSVAL_TO_OBJECT(vp); + } + else { + return NS_ERROR_FAILURE; + } + + if (aPrototype) { + *aPrototype = proto; + } + return NS_OK; +} + + +// +// Method for creating a new Event JavaScript object +// +extern "C" NS_DOM nsresult NS_NewScriptEvent(nsIScriptContext *aContext, nsIDOMEvent *aSupports, nsISupports *aParent, void **aReturn) +{ + NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptEvent"); + JSObject *proto; + JSObject *parent; + nsIScriptObjectOwner *owner; + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + + if (nsnull == aParent) { + parent = nsnull; + } + else if (NS_OK == aParent->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) { + NS_RELEASE(owner); + return NS_ERROR_FAILURE; + } + NS_RELEASE(owner); + } + else { + return NS_ERROR_FAILURE; + } + + if (NS_OK != NS_InitEventClass(aContext, (void **)&proto)) { + return NS_ERROR_FAILURE; + } + + // create a js object for this class + *aReturn = JS_NewObject(jscontext, &EventClass, proto, parent); + if (nsnull != *aReturn) { + // connect the native object to the js object + JS_SetPrivate(jscontext, (JSObject *)*aReturn, aSupports); + NS_ADDREF(aSupports); + } + else { + return NS_ERROR_FAILURE; + } + + return NS_OK; +} diff --git a/dom/src/events/nsJSEventListener.cpp b/dom/src/events/nsJSEventListener.cpp new file mode 100644 index 000000000000..28e7592837af --- /dev/null +++ b/dom/src/events/nsJSEventListener.cpp @@ -0,0 +1,121 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ +#include "nsJSEventListener.h" +#include "nsString.h" + +/* + * nsJSEventListener implementation + */ +nsJSEventListener::nsJSEventListener(JSContext *aContext, JSObject *aObj) +{ + mContext = aContext; + mJSObj = aObj; +} + +nsJSEventListener::~nsJSEventListener() +{ +} + +nsresult nsJSEventListener::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + if (NULL == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + static NS_DEFINE_IID(kIScriptEventListenerIID, NS_ISCRIPTEVENTLISTENER_IID); + static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID); + static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + if (aIID.Equals(kIDOMEventListenerIID)) { + *aInstancePtr = (void*)(nsIDOMEventListener*)this; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIScriptEventListenerIID)) { + *aInstancePtr = (void*)(nsIScriptEventListener*)this; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kISupportsIID)) { + *aInstancePtr = (void*)(nsISupports*)(nsIDOMEventListener*)this; + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} + +NS_IMPL_ADDREF(nsJSEventListener) + +NS_IMPL_RELEASE(nsJSEventListener) + +nsresult nsJSEventListener::ProcessEvent(nsIDOMEvent* aEvent) +{ + jsval funval, result; + JSFunction *fun; + jsval argv[1]; + JSObject *mEventObj; + char* mEventChars; + nsString mEventString; + + if (NS_OK != aEvent->GetType(mEventString)) { + //JS can't handle this event yet or can't handle it at all + return NS_OK; + } + + mEventChars = mEventString.ToNewCString(); + + if (!JS_LookupProperty(mContext, mJSObj, mEventChars, &funval)) { + delete mEventChars; + return NS_ERROR_FAILURE; + } + + delete mEventChars; + + if (JS_TypeOfValue(mContext, funval) != JSTYPE_FUNCTION) { + return NS_OK; + } + + nsIScriptContext *mScriptCX = (nsIScriptContext *)JS_GetContextPrivate(mContext); + if (NS_OK != NS_NewScriptEvent(mScriptCX, aEvent, nsnull, (void**)&mEventObj)) { + return NS_ERROR_FAILURE; + } + + argv[0] = OBJECT_TO_JSVAL(mEventObj); + if (PR_TRUE == JS_CallFunctionValue(mContext, mJSObj, funval, 1, argv, &result)) { + return NS_OK; + } + + return NS_ERROR_FAILURE; +} + +/* + * Factory functions + */ + +extern "C" NS_DOM nsresult NS_NewScriptEventListener(nsIDOMEventListener ** aInstancePtrResult, nsIScriptContext *aContext, void *aObj) +{ + JSContext *mCX = (JSContext*)aContext->GetNativeContext(); + + nsJSEventListener* it = new nsJSEventListener(mCX, (JSObject*)aObj); + if (NULL == it) { + return NS_ERROR_OUT_OF_MEMORY; + } + + static NS_DEFINE_IID(kIDOMEventListenerIID, NS_IDOMEVENTLISTENER_IID); + + return it->QueryInterface(kIDOMEventListenerIID, (void **) aInstancePtrResult); +} + diff --git a/dom/src/events/nsJSEventListener.h b/dom/src/events/nsJSEventListener.h new file mode 100644 index 000000000000..b8ea0a8605fa --- /dev/null +++ b/dom/src/events/nsJSEventListener.h @@ -0,0 +1,45 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#ifndef nsJSEventListener_h__ +#define nsJSEventListener_h__ + +#include "nsIDOMEvent.h" +#include "nsIScriptEventListener.h" +#include "nsIDOMMouseListener.h" +#include "jsapi.h" + +//nsIDOMMouseListener interface +class nsJSEventListener : public nsIDOMEventListener, public nsIScriptEventListener { +public: + nsJSEventListener(JSContext *aContext, JSObject *aObj); + virtual ~nsJSEventListener(); + + NS_DECL_ISUPPORTS + + //nsIDOMEventListener interface + virtual nsresult ProcessEvent(nsIDOMEvent* aEvent); + + //nsIJSEventListener interface +protected: + JSContext *mContext; + JSObject *mJSObj; + +}; + +#endif //nsJSEventListener_h__ \ No newline at end of file