File indexing completed on 2024-05-19 11:43:16

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 2001,2002,2003,2004,2005,2006 Ian Reinhart Geiser <geiseri@kde.org>
0003     Copyright (C) 2001,2002,2003,2004,2005,2006 Matt Broadstone <mbroadst@gmail.com>
0004     Copyright (C) 2001,2002,2003,2004,2005,2006 Richard J. Moore <rich@kde.org>
0005     Copyright (C) 2001,2002,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 #include "jseventmapper.h"
0023 
0024 #include "kjseglobal.h"
0025 
0026 namespace KJSEmbed
0027 {
0028 
0029 JSEventMapper *JSEventMapper::m_inst = nullptr;
0030 
0031 /** Used internally for the event handler table. */
0032 struct EventType {
0033     EventType(KJS::Identifier _id, QEvent::Type _type) :
0034         id(_id), type(_type)
0035     {
0036         ;
0037     }
0038 
0039     const KJS::Identifier id;
0040     const QEvent::Type type;
0041 };
0042 /*
0043 Qt4 events as of Qt 4.0
0044         None = 0,                               // invalid event
0045         Timer = 1,                              // timer event
0046         MouseButtonPress = 2,                   // mouse button pressed
0047         MouseButtonRelease = 3,                 // mouse button released
0048         MouseButtonDblClick = 4,                // mouse button double click
0049         MouseMove = 5,                          // mouse move
0050         KeyPress = 6,                           // key pressed
0051         KeyRelease = 7,                         // key released
0052         FocusIn = 8,                            // keyboard focus received
0053         FocusOut = 9,                           // keyboard focus lost
0054         Enter = 10,                             // mouse enters widget
0055         Leave = 11,                             // mouse leaves widget
0056         Paint = 12,                             // paint widget
0057         Move = 13,                              // move widget
0058         Resize = 14,                            // resize widget
0059         Create = 15,                            // after widget creation
0060         Destroy = 16,                           // during widget destruction
0061         Show = 17,                              // widget is shown
0062         Hide = 18,                              // widget is hidden
0063         Close = 19,                             // request to close widget
0064         Quit = 20,                              // request to quit application
0065         ParentChange = 21,                      // widget has been reparented
0066         ParentAboutToChange = 131,              // sent just before the parent change is done
0067         ThreadChange = 22,                      // object has changed threads
0068         WindowActivate = 24,                    // window was activated
0069         WindowDeactivate = 25,                  // window was deactivated
0070         ShowToParent = 26,                      // widget is shown to parent
0071         HideToParent = 27,                      // widget is hidden to parent
0072         Wheel = 31,                             // wheel event
0073         WindowTitleChange = 33,                 // window title changed
0074         WindowIconChange = 34,                  // icon changed
0075         ApplicationWindowIconChange = 35,       // application icon changed
0076         ApplicationFontChange = 36,             // application font changed
0077         ApplicationLayoutDirectionChange = 37,  // application layout direction changed
0078         ApplicationPaletteChange = 38,          // application palette changed
0079         PaletteChange = 39,                     // widget palette changed
0080         Clipboard = 40,                         // internal clipboard event
0081         Speech = 42,                            // reserved for speech input
0082         MetaCall =  43,                         // meta call event
0083         SockAct = 50,                           // socket activation
0084         WinEventAct = 123,                      // win event activation
0085         DeferredDelete = 52,                    // deferred delete event
0086         DragEnter = 60,                         // drag moves into widget
0087         DragMove = 61,                          // drag moves in widget
0088         DragLeave = 62,                         // drag leaves or is canceled
0089         Drop = 63,                              // actual drop
0090         DragResponse = 64,                      // drag accepted/rejected
0091         ChildAdded = 68,                        // new child widget
0092         ChildPolished = 69,                     // polished child widget
0093         ChildRemoved = 71,                      // deleted child widget
0094         ShowWindowRequest = 73,                 // widget's window should be mapped
0095         PolishRequest = 74,                     // widget should be polished
0096         Polish = 75,                            // widget is polished
0097         LayoutRequest = 76,                     // widget should be relayouted
0098         UpdateRequest = 77,                     // widget should be repainted
0099         EmbeddingControl = 79,                  // ActiveX embedding
0100         ActivateControl = 80,                   // ActiveX activation
0101         DeactivateControl = 81,                 // ActiveX deactivation
0102         ContextMenu = 82,                       // context popup menu
0103         InputMethod = 83,                       // input method
0104         AccessibilityPrepare = 86,              // accessibility information is requested
0105         TabletMove = 87,                        // Wacom tablet event
0106         LocaleChange = 88,                      // the system locale changed
0107         LanguageChange = 89,                    // the application language changed
0108         LayoutDirectionChange = 90,             // the layout direction changed
0109         Style = 91,                             // internal style event
0110         TabletPress = 92,                       // tablet press
0111         TabletRelease = 93,                     // tablet release
0112         OkRequest = 94,                         // CE (Ok) button pressed
0113         HelpRequest = 95,                       // CE (?)  button pressed
0114         IconDrag = 96,                          // proxy icon dragged
0115         FontChange = 97,                        // font has changed
0116         EnabledChange = 98,                     // enabled state has changed
0117         ActivationChange = 99,                  // window activation has changed
0118         StyleChange = 100,                      // style has changed
0119         IconTextChange = 101,                   // icon text has changed
0120         ModifiedChange = 102,                   // modified state has changed
0121         MouseTrackingChange = 109,              // mouse tracking state has changed
0122         WindowBlocked = 103,                    // window is about to be blocked modally
0123         WindowUnblocked = 104,                  // windows modal blocking has ended
0124         WindowStateChange = 105,
0125         ToolTip = 110,
0126         WhatsThis = 111,
0127         StatusTip = 112,
0128         ActionChanged = 113,
0129         ActionAdded = 114,
0130         ActionRemoved = 115,
0131         FileOpen = 116,                         // file open request
0132         Shortcut = 117,                         // shortcut triggered
0133         ShortcutOverride = 51,                  // shortcut override request
0134         WhatsThisClicked = 118,
0135         ToolBarChange = 120,                    // toolbar visibility toggled
0136         ApplicationActivated = 121,             // application has been changed to active
0137         ApplicationDeactivated = 122,           // application has been changed to inactive
0138         QueryWhatsThis = 123,                   // query what's this widget help
0139         EnterWhatsThisMode = 124,
0140         LeaveWhatsThisMode = 125,
0141         ZOrderChange = 126,                     // child widget has had its z-order changed
0142         HoverEnter = 127,                       // mouse cursor enters a hover widget
0143         HoverLeave = 128,                       // mouse cursor leaves a hover widget
0144         HoverMove = 129,                        // mouse cursor move inside a hover widget
0145         AccessibilityHelp = 119,                // accessibility help text request
0146         AccessibilityDescription = 130
0147 */
0148 static EventType events[] = {
0149     EventType(KJS::Identifier("onTimerEvent"), QEvent::Timer),
0150     EventType(KJS::Identifier("onMouseButtonPressEvent"), QEvent::MouseButtonPress),
0151     EventType(KJS::Identifier("onMouseButtonReleaseEvent"), QEvent::MouseButtonRelease),
0152     EventType(KJS::Identifier("onMouseButtonDblClickEvent"), QEvent::MouseButtonDblClick),
0153     EventType(KJS::Identifier("onMouseMoveEvent"), QEvent::MouseMove),
0154     EventType(KJS::Identifier("onKeyPressEvent"), QEvent::KeyPress),
0155     EventType(KJS::Identifier("onKeyReleaseEvent"), QEvent::KeyRelease),
0156     EventType(KJS::Identifier("onFocusInEvent"), QEvent::FocusIn),
0157     EventType(KJS::Identifier("onFocusOutEvent"), QEvent::FocusOut),
0158     EventType(KJS::Identifier("onEnterEvent"), QEvent::Enter),
0159     EventType(KJS::Identifier("onLeaveEvent"), QEvent::Leave),
0160     EventType(KJS::Identifier("onPaintEvent"), QEvent::Paint),
0161     EventType(KJS::Identifier("onMoveEvent"), QEvent::Move),
0162     EventType(KJS::Identifier("onResizeEvent"), QEvent::Resize),
0163     EventType(KJS::Identifier("onCreateEvent"), QEvent::Create),
0164     EventType(KJS::Identifier("onDestroyEvent"), QEvent::Destroy),
0165     EventType(KJS::Identifier("onShowEvent"), QEvent::Show),
0166     EventType(KJS::Identifier("onHideEvent"), QEvent::Hide),
0167     EventType(KJS::Identifier("onCloseEvent"), QEvent::Close),
0168     EventType(KJS::Identifier("onQuitEvent"), QEvent::Quit),
0169     EventType(KJS::Identifier("onParentChangeEvent"), QEvent::ParentChange),
0170     EventType(KJS::Identifier("onParentAboutToChangeEvent"), QEvent::ParentAboutToChange),
0171     EventType(KJS::Identifier("onThreadChangeEvent"), QEvent::ThreadChange),
0172     EventType(KJS::Identifier("onWindowActivateEvent"), QEvent::WindowActivate),
0173     EventType(KJS::Identifier("onWindowDeactivateEvent"), QEvent::WindowDeactivate),
0174     EventType(KJS::Identifier("onShowToParentEvent"), QEvent::ShowToParent),
0175     EventType(KJS::Identifier("onHideToParentEvent"), QEvent::HideToParent),
0176     EventType(KJS::Identifier("onWheelEvent"), QEvent::Wheel),
0177     EventType(KJS::Identifier("onWindowTitleChangeEvent"), QEvent::WindowTitleChange),
0178     EventType(KJS::Identifier("onWindowIconChangeEvent"), QEvent::WindowIconChange),
0179     EventType(KJS::Identifier("onApplicationWindowIconChangeEvent"), QEvent::ApplicationWindowIconChange),
0180     EventType(KJS::Identifier("onApplicationFontChangeEvent"), QEvent::ApplicationFontChange),
0181     EventType(KJS::Identifier("onApplicationLayoutDirectionChangeEvent"), QEvent::ApplicationLayoutDirectionChange),
0182     EventType(KJS::Identifier("onApplicationPaletteChangeEvent"), QEvent::ApplicationPaletteChange),
0183     EventType(KJS::Identifier("onPaletteChangeEvent"), QEvent::PaletteChange),
0184     EventType(KJS::Identifier("onClipboardEvent"), QEvent::Clipboard),
0185     EventType(KJS::Identifier("onSpeechEvent"), QEvent::Speech),
0186     EventType(KJS::Identifier("onMetaCallEvent"), QEvent::MetaCall),
0187     EventType(KJS::Identifier("onSockActEvent"), QEvent::SockAct),
0188     EventType(KJS::Identifier("onWinEventActEvent"), QEvent::WinEventAct),
0189     EventType(KJS::Identifier("onDeferredDeleteEvent"), QEvent::DeferredDelete),
0190     EventType(KJS::Identifier("onDragEnterEvent"), QEvent::DragEnter),
0191     EventType(KJS::Identifier("onDragMoveEvent"), QEvent::DragMove),
0192     EventType(KJS::Identifier("onDragLeaveEvent"), QEvent::DragLeave),
0193     EventType(KJS::Identifier("onDropEvent"), QEvent::Drop),
0194     EventType(KJS::Identifier("onDragResponseEvent"), QEvent::DragResponse),
0195     EventType(KJS::Identifier("onChildAddedEvent"), QEvent::ChildAdded),
0196     EventType(KJS::Identifier("onChildPolishedEvent"), QEvent::ChildRemoved),
0197     EventType(KJS::Identifier("onShowWindowRequestEvent"), QEvent::ShowWindowRequest),
0198     EventType(KJS::Identifier("onPolishRequestEvent"), QEvent::PolishRequest),
0199     EventType(KJS::Identifier("onPolishEvent"), QEvent::Polish),
0200     EventType(KJS::Identifier("onLayoutRequestEvent"), QEvent::LayoutRequest),
0201     EventType(KJS::Identifier("onUpdateRequestEvent"), QEvent::UpdateRequest),
0202     EventType(KJS::Identifier("onEmbeddingControlEvent"), QEvent::EmbeddingControl),
0203     EventType(KJS::Identifier("onActivateControlEvent"), QEvent::ActivateControl),
0204     EventType(KJS::Identifier("onDeactivateControlEvent"), QEvent::DeactivateControl),
0205     EventType(KJS::Identifier("onContextMenuEvent"), QEvent::ContextMenu),
0206     EventType(KJS::Identifier("onInputMethodEvent"), QEvent::InputMethod),
0207     EventType(KJS::Identifier("onTabletMoveEvent"), QEvent::TabletMove),
0208     EventType(KJS::Identifier("onLocaleChangeEvent"), QEvent::LocaleChange),
0209     EventType(KJS::Identifier("onLanguageChangeEvent"), QEvent::LanguageChange),
0210     EventType(KJS::Identifier("onLayoutDirectionChangeEvent"), QEvent::LayoutDirectionChange),
0211     EventType(KJS::Identifier("onStyleEvent"), QEvent::Style),
0212     EventType(KJS::Identifier("onTabletPressEvent"), QEvent::TabletPress),
0213     EventType(KJS::Identifier("onTabletReleaseEvent"), QEvent::TabletRelease),
0214     EventType(KJS::Identifier("onOkRequestEvent"), QEvent::OkRequest),
0215     EventType(KJS::Identifier("onHelpRequestEvent"), QEvent::HelpRequest),
0216     EventType(KJS::Identifier("onIconDragEvent"), QEvent::IconDrag),
0217     EventType(KJS::Identifier("onFontChangeEvent"), QEvent::FontChange),
0218     EventType(KJS::Identifier("onEnabledChangeEvent"), QEvent::EnabledChange),
0219     EventType(KJS::Identifier("onActivationChangeEvent"), QEvent::ActivationChange),
0220     EventType(KJS::Identifier("onStyleChangeEvent"), QEvent::StyleChange),
0221     EventType(KJS::Identifier("onIconTextChangeEvent"), QEvent::IconTextChange),
0222     EventType(KJS::Identifier("onModifiedChangeEvent"), QEvent::ModifiedChange),
0223     EventType(KJS::Identifier("onMouseTrackingChangeEvent"), QEvent::MouseTrackingChange),
0224     EventType(KJS::Identifier("onWindowBlockedEvent"), QEvent::WindowBlocked),
0225     EventType(KJS::Identifier("onWindowUnblockedEvent"), QEvent::WindowUnblocked),
0226     EventType(KJS::Identifier("onWindowStateChangeEvent"), QEvent::WindowStateChange),
0227     EventType(KJS::Identifier("onToolTipEvent"), QEvent::ToolTip),
0228     EventType(KJS::Identifier("onWhatsThisEvent"), QEvent::WhatsThis),
0229     EventType(KJS::Identifier("onStatusTipEvent"), QEvent::StatusTip),
0230     EventType(KJS::Identifier("onActionChangedEvent"), QEvent::ActionChanged),
0231     EventType(KJS::Identifier("onActionAddedEvent"), QEvent::ActionAdded),
0232     EventType(KJS::Identifier("onActionRemovedEvent"), QEvent::ActionRemoved),
0233     EventType(KJS::Identifier("onFileOpenEvent"), QEvent::FileOpen),
0234     EventType(KJS::Identifier("onShortcutEvent"), QEvent::Shortcut),
0235     EventType(KJS::Identifier("onShortcutOverrideEvent"), QEvent::ShortcutOverride),
0236     EventType(KJS::Identifier("onWhatsThisClickedEvent"), QEvent::WhatsThisClicked),
0237     EventType(KJS::Identifier("onToolBarChangeEvent"), QEvent::ToolBarChange),
0238     EventType(KJS::Identifier("onApplicationActivatedEvent"), QEvent::ApplicationActivated),
0239     EventType(KJS::Identifier("onApplicationDeactivatedEvent"), QEvent::ApplicationDeactivated),
0240     EventType(KJS::Identifier("onQueryWhatsThisEvent"), QEvent::QueryWhatsThis),
0241     EventType(KJS::Identifier("onEnterWhatsThisModeEvent"), QEvent::EnterWhatsThisMode),
0242     EventType(KJS::Identifier("onLeaveWhatsThisModeEvent"), QEvent::LeaveWhatsThisMode),
0243     EventType(KJS::Identifier("onZOrderChangeEvent"), QEvent::ZOrderChange),
0244     EventType(KJS::Identifier("onHoverEnterEvent"), QEvent::HoverEnter),
0245     EventType(KJS::Identifier("onHoverLeaveEvent"), QEvent::HoverLeave),
0246     EventType(KJS::Identifier("onHoverMoveEvent"), QEvent::HoverMove),
0247     EventType(KJS::Identifier(), QEvent::None)
0248 };
0249 
0250 JSEventMapper::JSEventMapper()
0251 {
0252     int i = 0;
0253     do {
0254         addEvent(events[i].id, events[i].type);
0255         i++;
0256     } while (events[i].type != QEvent::None);
0257 }
0258 
0259 JSEventMapper::~JSEventMapper()
0260 {
0261 }
0262 
0263 void JSEventMapper::addEvent(const KJS::Identifier &name, QEvent::Type t)
0264 {
0265     m_handlerToEvent[ toQString(name) ] = t;
0266     m_eventToHandler[ t ] = name;
0267 }
0268 
0269 QEvent::Type JSEventMapper::findEventType(const KJS::Identifier &name) const
0270 {
0271     return  m_handlerToEvent[ toQString(name) ];
0272 }
0273 
0274 bool JSEventMapper::isEventHandler(const KJS::Identifier &name) const
0275 {
0276     return m_handlerToEvent.contains(toQString(name));
0277 }
0278 
0279 KJS::Identifier JSEventMapper::findEventHandler(QEvent::Type t) const
0280 {
0281     return m_eventToHandler[t];
0282 }
0283 
0284 JSEventMapper *JSEventMapper::mapper()
0285 {
0286     if (m_inst) {
0287         return m_inst;
0288     }
0289     m_inst = new JSEventMapper();
0290     return m_inst;
0291 }
0292 
0293 } // namespace KJSEmbed
0294 
0295 // Local Variables:
0296 // End:
0297