webengine/osswebengine/WebCore/dom/MouseRelatedEvent.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /**
       
     2  * This file is part of the DOM implementation for KDE.
       
     3  *
       
     4  * Copyright (C) 2001 Peter Kelly (pmk@post.com)
       
     5  * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
       
     6  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
       
     7  * Copyright (C) 2003, 2005, 2006 Apple Computer, Inc.
       
     8  *
       
     9  * This library is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU Library General Public
       
    11  * License as published by the Free Software Foundation; either
       
    12  * version 2 of the License, or (at your option) any later version.
       
    13  *
       
    14  * This library is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * Library General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU Library General Public License
       
    20  * along with this library; see the file COPYING.LIB.  If not, write to
       
    21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    22  * Boston, MA 02110-1301, USA.
       
    23  */
       
    24 
       
    25 #include "config.h"
       
    26 #include "MouseRelatedEvent.h"
       
    27 
       
    28 #include "DOMWindow.h"
       
    29 #include "Document.h"
       
    30 #include "Frame.h"
       
    31 #include "FrameView.h"
       
    32 #include "Node.h"
       
    33 #include "RenderLayer.h"
       
    34 #include "RenderObject.h"
       
    35 
       
    36 namespace WebCore {
       
    37 
       
    38 MouseRelatedEvent::MouseRelatedEvent()
       
    39     : m_screenX(0)
       
    40     , m_screenY(0)
       
    41     , m_clientX(0)
       
    42     , m_clientY(0)
       
    43     , m_pageX(0)
       
    44     , m_pageY(0)
       
    45     , m_layerX(0)
       
    46     , m_layerY(0)
       
    47     , m_offsetX(0)
       
    48     , m_offsetY(0)
       
    49     , m_isSimulated(false)
       
    50 {
       
    51 }
       
    52 
       
    53 static int contentsX(AbstractView* abstractView)
       
    54 {
       
    55     if (!abstractView)
       
    56         return 0;
       
    57     Frame* frame = abstractView->frame();
       
    58     if (!frame)
       
    59         return 0;
       
    60     FrameView* frameView = frame->view();
       
    61     if (!frameView)
       
    62         return 0;
       
    63     return frameView->contentsX();
       
    64 }
       
    65 
       
    66 static int contentsY(AbstractView* abstractView)
       
    67 {
       
    68     if (!abstractView)
       
    69         return 0;
       
    70     Frame* frame = abstractView->frame();
       
    71     if (!frame)
       
    72         return 0;
       
    73     FrameView* frameView = frame->view();
       
    74     if (!frameView)
       
    75         return 0;
       
    76     return frameView->contentsY();
       
    77 }
       
    78 
       
    79 MouseRelatedEvent::MouseRelatedEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView* view,
       
    80                                      int detail, int screenX, int screenY, int pageX, int pageY,
       
    81                                      bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool isSimulated)
       
    82     : UIEventWithKeyState(eventType, canBubble, cancelable, view, detail, ctrlKey, altKey, shiftKey, metaKey)
       
    83     , m_screenX(screenX)
       
    84     , m_screenY(screenY)
       
    85     , m_clientX(pageX - contentsX(view))
       
    86     , m_clientY(pageY - contentsY(view))
       
    87     , m_pageX(pageX)
       
    88     , m_pageY(pageY)
       
    89     , m_isSimulated(isSimulated)
       
    90 {
       
    91     initCoordinates();
       
    92 }
       
    93 
       
    94 void MouseRelatedEvent::initCoordinates()
       
    95 {
       
    96     // Set up initial values for coordinates.
       
    97     // Correct values can't be computed until we have at target, so receivedTarget
       
    98     // does the "real" computation.
       
    99     m_layerX = m_pageX;
       
   100     m_layerY = m_pageY;
       
   101     m_offsetX = m_pageX;
       
   102     m_offsetY = m_pageY;
       
   103 }
       
   104 
       
   105 void MouseRelatedEvent::initCoordinates(int clientX, int clientY)
       
   106 {
       
   107     // Set up initial values for coordinates.
       
   108     // Correct values can't be computed until we have at target, so receivedTarget
       
   109     // does the "real" computation.
       
   110     m_clientX = clientX;
       
   111     m_clientY = clientY;
       
   112     m_pageX = clientX + contentsX(view());
       
   113     m_pageY = clientY + contentsY(view());
       
   114     m_layerX = m_pageX;
       
   115     m_layerY = m_pageY;
       
   116     m_offsetX = m_pageX;
       
   117     m_offsetY = m_pageY;
       
   118 }
       
   119 
       
   120 void MouseRelatedEvent::receivedTarget()
       
   121 {
       
   122     ASSERT(target());
       
   123     Node* targ = target()->toNode();
       
   124     if (!targ)
       
   125         return;
       
   126 
       
   127     // Compute coordinates that are based on the target.
       
   128     m_layerX = m_pageX;
       
   129     m_layerY = m_pageY;
       
   130     m_offsetX = m_pageX;
       
   131     m_offsetY = m_pageY;
       
   132 
       
   133     // Must have an updated render tree for this math to work correctly.
       
   134     targ->document()->updateRendering();
       
   135 
       
   136     // Adjust offsetX/Y to be relative to the target's position.
       
   137     if (!isSimulated()) {
       
   138         if (RenderObject* r = targ->renderer()) {
       
   139             int rx, ry;
       
   140             if (r->absolutePosition(rx, ry)) {
       
   141                 m_offsetX -= rx;
       
   142                 m_offsetY -= ry;
       
   143             }
       
   144         }
       
   145     }
       
   146 
       
   147     // Adjust layerX/Y to be relative to the layer.
       
   148     // FIXME: We're pretty sure this is the wrong defintion of "layer."
       
   149     // Our RenderLayer is a more modern concept, and layerX/Y is some
       
   150     // other notion about groups of elements (left over from the Netscape 4 days?);
       
   151     // we should test and fix this.
       
   152     Node* n = targ;
       
   153     while (n && !n->renderer())
       
   154         n = n->parent();
       
   155     if (n) {
       
   156         RenderLayer* layer = n->renderer()->enclosingLayer();
       
   157         layer->updateLayerPosition();
       
   158         for (; layer; layer = layer->parent()) {
       
   159             m_layerX -= layer->xPos();
       
   160             m_layerY -= layer->yPos();
       
   161         }
       
   162     }
       
   163 }
       
   164 
       
   165 int MouseRelatedEvent::pageX() const
       
   166 {
       
   167     return m_pageX;
       
   168 }
       
   169 
       
   170 int MouseRelatedEvent::pageY() const
       
   171 {
       
   172     return m_pageY;
       
   173 }
       
   174 
       
   175 int MouseRelatedEvent::x() const
       
   176 {
       
   177     // FIXME: This is not correct.
       
   178     // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>.
       
   179     return m_clientX;
       
   180 }
       
   181 
       
   182 int MouseRelatedEvent::y() const
       
   183 {
       
   184     // FIXME: This is not correct.
       
   185     // See Microsoft documentation and <http://www.quirksmode.org/dom/w3c_events.html>.
       
   186     return m_clientY;
       
   187 }
       
   188 
       
   189 } // namespace WebCore