File indexing completed on 2024-07-14 14:35:11

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2003,2004,2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2003,2004,2005, 2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2003,2004,2005, 2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2003,2004,2005, 2006 Erik L. Bunce <kde@bunce.us>
0006 
0007     This library is free software; you can redistribute it and/or
0008     modify it under the terms of the GNU Library General Public
0009     License as published by the Free Software Foundation; either
0010     version 2 of the License, or (at your option) any later version.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Library General Public License for more details.
0016 
0017     You should have received a copy of the GNU Library General Public License
0018     along with this library; see the file COPYING.LIB.  If not, write to
0019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020     Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "jseventutils.h"
0024 #include "qobject_binding.h"
0025 
0026 namespace KJSEmbed
0027 {
0028 
0029 namespace JSEventUtils
0030 {
0031 KJS::JSObject *event(KJS::ExecState *exec, const QEvent *ev)
0032 {
0033     switch (ev->type()) {
0034     case QEvent::Resize:
0035         return convertEvent(exec, (QResizeEvent *)ev);
0036         break;
0037     case QEvent::Timer:
0038         return convertEvent(exec, (QTimerEvent *)ev);
0039         break;
0040     case QEvent::MouseButtonPress:
0041     case QEvent::MouseButtonRelease:
0042     case QEvent::MouseButtonDblClick:
0043     case QEvent::MouseMove:
0044     case QEvent::Enter:
0045     case QEvent::Leave:
0046         return convertEvent(exec, (QMouseEvent *)ev);
0047         break;
0048     case QEvent::KeyPress:
0049     case QEvent::KeyRelease:
0050         return convertEvent(exec, (QKeyEvent *)ev);
0051         break;
0052     case QEvent::FocusIn:
0053     case QEvent::FocusOut:
0054         return convertEvent(exec, (QFocusEvent *)ev);
0055         break;
0056     case QEvent::Paint:
0057         return convertEvent(exec, (QPaintEvent *)ev);
0058         break;
0059     case QEvent::Move:
0060         return convertEvent(exec, (QMoveEvent *)ev);
0061         break;
0062     case QEvent::Create:
0063     case QEvent::Destroy:
0064     case QEvent::Show:
0065     case QEvent::Hide:
0066     case QEvent::Close:
0067         return convertEvent(exec, (QCloseEvent *)ev);
0068         break;
0069     case QEvent::Quit:
0070     case QEvent::ParentChange:
0071     case QEvent::ParentAboutToChange:
0072     case QEvent::ThreadChange:
0073     case QEvent::WindowActivate:
0074     case QEvent::WindowDeactivate:
0075     case QEvent::ShowToParent:
0076     case QEvent::HideToParent:
0077     case QEvent::Wheel:
0078     case QEvent::WindowTitleChange:
0079     case QEvent::WindowIconChange:
0080     case QEvent::ApplicationWindowIconChange:
0081     case QEvent::ApplicationFontChange:
0082     case QEvent::ApplicationLayoutDirectionChange:
0083     case QEvent::ApplicationPaletteChange:
0084     case QEvent::PaletteChange:
0085     case QEvent::Clipboard:
0086     case QEvent::Speech:
0087     case QEvent::MetaCall:
0088     case QEvent::SockAct:
0089     //case QEvent::WinEventAct: conflicts with QEvent::QueryWhatsThis
0090     case QEvent::DeferredDelete:
0091     case QEvent::DragEnter:
0092     case QEvent::DragMove:
0093     case QEvent::DragLeave:
0094     case QEvent::Drop:
0095     case QEvent::DragResponse:
0096     case QEvent::ChildAdded:
0097     case QEvent::ChildPolished:
0098     case QEvent::ChildRemoved:
0099     case QEvent::ShowWindowRequest:
0100     case QEvent::PolishRequest:
0101     case QEvent::Polish:
0102     case QEvent::LayoutRequest:
0103     case QEvent::UpdateRequest:
0104     case QEvent::EmbeddingControl:
0105     case QEvent::ActivateControl:
0106     case QEvent::DeactivateControl:
0107     case QEvent::ContextMenu:
0108     case QEvent::InputMethod:
0109     case QEvent::TabletMove:
0110     case QEvent::LocaleChange:
0111     case QEvent::LanguageChange:
0112     case QEvent::LayoutDirectionChange:
0113     case QEvent::Style:
0114     case QEvent::TabletPress:
0115     case QEvent::TabletRelease:
0116     case QEvent::OkRequest:
0117     case QEvent::HelpRequest:
0118     case QEvent::IconDrag:
0119     case QEvent::FontChange:
0120     case QEvent::EnabledChange:
0121     case QEvent::ActivationChange:
0122     case QEvent::StyleChange:
0123     case QEvent::IconTextChange:
0124     case QEvent::ModifiedChange:
0125     case QEvent::MouseTrackingChange:
0126     case QEvent::WindowBlocked:
0127     case QEvent::WindowUnblocked:
0128     case QEvent::WindowStateChange:
0129     case QEvent::ToolTip:
0130     case QEvent::WhatsThis:
0131     case QEvent::StatusTip:
0132     case QEvent::ActionChanged:
0133     case QEvent::ActionAdded:
0134     case QEvent::ActionRemoved:
0135     case QEvent::FileOpen:
0136     case QEvent::Shortcut:
0137     case QEvent::ShortcutOverride:
0138     case QEvent::WhatsThisClicked:
0139     case QEvent::ToolBarChange:
0140     case QEvent::ApplicationActivated:
0141     case QEvent::ApplicationDeactivated:
0142     case QEvent::QueryWhatsThis:
0143     case QEvent::EnterWhatsThisMode:
0144     case QEvent::LeaveWhatsThisMode:
0145     case QEvent::ZOrderChange:
0146     case QEvent::HoverEnter:
0147     case QEvent::HoverLeave:
0148     case QEvent::HoverMove:
0149     default:
0150         return convertEvent(exec, ev);
0151         break;
0152     }
0153 }
0154 
0155 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QEvent *ev)
0156 {
0157     ObjectBinding *evnt = new ObjectBinding(exec, "QEvent", ev);
0158     evnt->put(exec, "type", KJS::jsNumber((int) ev->type()));
0159     evnt->put(exec, "spontaneous", KJS::jsBoolean(ev->spontaneous()));
0160     evnt->put(exec, "isAccepted", KJS::jsBoolean(ev->isAccepted()));
0161     return evnt;
0162 }
0163 
0164 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QInputEvent *ev)
0165 {
0166     KJS::JSObject *iev = convertEvent(exec, (QEvent *)ev);
0167     iev->put(exec, "modifiers", KJS::jsNumber(ev->modifiers()));
0168     return iev;
0169 }
0170 
0171 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QMouseEvent *ev)
0172 {
0173     KJS::JSObject *mev = convertEvent(exec, (QInputEvent *)ev);
0174 
0175     mev->put(exec, "pos", convertToValue(exec, ev->pos()));
0176     mev->put(exec, "x", KJS::jsNumber(ev->x()));
0177     mev->put(exec, "y", KJS::jsNumber(ev->y()));
0178 
0179     mev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
0180     mev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
0181     mev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));
0182 
0183     mev->put(exec, "button", KJS::jsNumber(ev->button()));
0184     mev->put(exec, "buttons", KJS::jsNumber(ev->buttons()));
0185 
0186     return mev;
0187 }
0188 
0189 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QPaintEvent *ev)
0190 {
0191     KJS::JSObject *pev = convertEvent(exec, (QEvent *)ev);
0192 
0193     pev->put(exec, "rect", convertToValue(exec, ev->rect()));
0194 //         pev->put( exec, "region", convertToValue( exec, ev->region() ) );
0195     return pev;
0196 }
0197 
0198 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QKeyEvent *ev)
0199 {
0200     KJS::JSObject *kev = convertEvent(exec, (QInputEvent *)ev);
0201 
0202     kev->put(exec, "key", KJS::jsNumber(ev->key()));
0203     kev->put(exec, "text", KJS::jsString(ev->text()));
0204     kev->put(exec, "isAutoRepeat", KJS::jsBoolean(ev->isAutoRepeat()));
0205     kev->put(exec, "count", KJS::jsNumber(ev->count()));
0206     return kev;
0207 }
0208 
0209 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QInputMethodEvent *ev)
0210 {
0211     KJS::JSObject *qev = convertEvent(exec, (QEvent *)ev);
0212 
0213     qev->put(exec, "commitString", KJS::jsString(ev->commitString()));
0214     qev->put(exec, "preeditString", KJS::jsString(ev->preeditString()));
0215     qev->put(exec, "replacementLength", KJS::jsNumber(ev->replacementLength()));
0216     qev->put(exec, "replacementStart", KJS::jsNumber(ev->replacementStart()));
0217     return qev;
0218 }
0219 
0220 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QResizeEvent *ev)
0221 {
0222     KJS::JSObject *rev = convertEvent(exec, (QEvent *)ev);
0223 
0224     rev->put(exec, "size", convertToValue(exec, ev->size()));
0225     rev->put(exec, "oldSize", convertToValue(exec, ev->oldSize()));
0226     return rev;
0227 }
0228 
0229 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QCloseEvent *ev)
0230 {
0231     KJS::JSObject *cev = convertEvent(exec, (QEvent *)ev);
0232 
0233     cev->put(exec, "isAccepted", KJS::jsBoolean(ev->isAccepted()));
0234     return cev;
0235 }
0236 
0237 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QMoveEvent *ev)
0238 {
0239     KJS::JSObject *mev = convertEvent(exec, (QEvent *)ev);
0240 
0241     mev->put(exec, "pos", convertToValue(exec, ev->pos()));
0242     mev->put(exec, "oldPos", convertToValue(exec, ev->oldPos()));
0243     return mev;
0244 }
0245 
0246 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QWheelEvent *ev)
0247 {
0248     KJS::JSObject *wev = convertEvent(exec, (QInputEvent *)ev);
0249 
0250     wev->put(exec, "delta", KJS::jsNumber(ev->delta()));
0251     wev->put(exec, "pos", convertToValue(exec, ev->pos()));
0252     wev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
0253     wev->put(exec, "x", KJS::jsNumber(ev->x()));
0254     wev->put(exec, "y", KJS::jsNumber(ev->y()));
0255     wev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
0256     wev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));
0257     wev->put(exec, "orientation", KJS::jsNumber(ev->orientation()));
0258     wev->put(exec, "buttons", KJS::jsNumber(ev->buttons()));
0259 
0260     return wev;
0261 }
0262 
0263 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QDropEvent *ev)
0264 {
0265     KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
0266     dev->put(exec, "pos", convertToValue(exec, ev->pos()));
0267     dev->put(exec, "source", new QObjectBinding(exec, (QObject *)ev->source()));
0268     dev->put(exec, "dropAction", KJS::jsNumber((int)(ev->dropAction())));
0269     dev->put(exec, "keyboardModifiers", KJS::jsNumber((int)(ev->keyboardModifiers())));
0270     dev->put(exec, "possibleActions", KJS::jsNumber((int)(ev->possibleActions())));
0271     dev->put(exec, "proposedAction", KJS::jsNumber((int)(ev->proposedAction())));
0272     return dev;
0273 }
0274 
0275 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QDragMoveEvent *ev)
0276 {
0277     KJS::JSObject *mev = convertEvent(exec, (QDropEvent *)ev);
0278     mev->put(exec, "answerRect", convertToValue(exec, ev->answerRect()));
0279     return mev;
0280 }
0281 
0282 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QTimerEvent *ev)
0283 {
0284     KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
0285     dev->put(exec, "timerId", KJS::jsNumber(ev->timerId()));
0286     return dev;
0287 }
0288 
0289 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QContextMenuEvent *ev)
0290 {
0291     KJS::JSObject *cxev = convertEvent(exec, (QInputEvent *)ev);
0292     cxev->put(exec, "x", KJS::jsNumber(ev->x()));
0293     cxev->put(exec, "y", KJS::jsNumber(ev->y()));
0294     cxev->put(exec, "globalX", KJS::jsNumber(ev->globalX()));
0295     cxev->put(exec, "globalY", KJS::jsNumber(ev->globalY()));
0296     cxev->put(exec, "pos", convertToValue(exec, ev->pos()));
0297     cxev->put(exec, "globalPos", convertToValue(exec, ev->globalPos()));
0298     cxev->put(exec, "reason", KJS::jsNumber((int)ev->reason()));
0299     return cxev;
0300 }
0301 
0302 KJS::JSObject *convertEvent(KJS::ExecState *exec, const QFocusEvent *ev)
0303 {
0304     KJS::JSObject *dev = convertEvent(exec, (QEvent *)ev);
0305     dev->put(exec, "gotFocus", KJS::jsBoolean(ev->gotFocus()));
0306     dev->put(exec, "lostFocus", KJS::jsBoolean(ev->lostFocus()));
0307     //Gold star trolltech!
0308     dev->put(exec, "reason", KJS::jsNumber((int) const_cast<QFocusEvent *>(ev)->reason()));
0309     return dev;
0310 }
0311 }// namespace JSEventUtils
0312 }// namespace KJSEmbed
0313