webengine/osswebengine/WebKit/s60/misc/WebTabbedNavigation.cpp
changeset 13 10e98eab6f85
parent 8 7c90e6132015
child 16 a359256acfc6
equal deleted inserted replaced
8:7c90e6132015 13:10e98eab6f85
    21 #include <e32base.h>
    21 #include <e32base.h>
    22 #include "WebTabbedNavigation.h"
    22 #include "WebTabbedNavigation.h"
    23 #include "WebUtil.h"
    23 #include "WebUtil.h"
    24 #include "Document.h"
    24 #include "Document.h"
    25 #include "Frame.h"
    25 #include "Frame.h"
       
    26 #include "HitTestRequest.h"
       
    27 #include "HitTestResult.h"
    26 #include "HTMLNames.h"
    28 #include "HTMLNames.h"
    27 #include "FrameTree.h"
    29 #include "FrameTree.h"
    28 #include "BrCtlDefs.h"
    30 #include "BrCtlDefs.h"
    29 #include "WebCursor.h"
    31 #include "WebCursor.h"
    30 #include "StaticObjectsContainer.h"
    32 #include "StaticObjectsContainer.h"
    36 #include "EventHandler.h"
    38 #include "EventHandler.h"
    37 #include "webkitlogger.h"
    39 #include "webkitlogger.h"
    38 #include "FocusController.h"
    40 #include "FocusController.h"
    39 #include "RenderListBox.h"
    41 #include "RenderListBox.h"
    40 #include "HTMLSelectElement.h"
    42 #include "HTMLSelectElement.h"
       
    43 #include "HTMLInputElement.h"
    41 
    44 
    42 using namespace WebCore;
    45 using namespace WebCore;
    43 using namespace HTMLNames;
    46 using namespace HTMLNames;
    44 
    47 
    45 const int KMaxJumpPercent = 90;
    48 const int KMaxJumpPercent = 90;
    73             m_firstNavigationOnPage = false;
    76             m_firstNavigationOnPage = false;
    74         }
    77         }
    75     }
    78     }
    76 }
    79 }
    77 
    80 
       
    81 void WebTabbedNavigation::updateCursorPosition(const TPoint& pos)
       
    82 {
       
    83     m_focusPosition = pos;
       
    84     WebFrame* frame = StaticObjectsContainer::instance()->webCursor()->getFrameAtPoint(pos);
       
    85     TPoint point(frame->frameView()->viewCoordsInFrameCoords(pos));
       
    86 
       
    87     Element* node = core(frame)->document()->elementFromPoint(point.iX, point.iY);
       
    88     if (node->isFocusable() && !node->hasTagName(iframeTag) && !node->hasTagName(frameTag))
       
    89         m_webView->page()->focusController()->setFocusedNode(node, core(frame));
       
    90     else
       
    91         m_webView->page()->focusController()->setFocusedNode(NULL, core(frame));
       
    92     m_selectedElementRect.SetRect(pos.iX, pos.iY, pos.iX + 1, pos.iY + 1);
       
    93 }
       
    94 
       
    95 void WebTabbedNavigation::focusedElementChanged(Element* element)
       
    96 {
       
    97     m_initializedForPage = true;
       
    98     m_firstNavigationOnPage = false;
       
    99     m_node = element;
       
   100     m_selectedElementRect = element->getRect().Rect();
       
   101     m_focusPosition = StaticObjectsContainer::instance()->webCursor()->position();
       
   102 }
    78 
   103 
    79 bool WebTabbedNavigation::navigate(int horizontalDir, int verticalDir)
   104 bool WebTabbedNavigation::navigate(int horizontalDir, int verticalDir)
    80 {
   105 {
    81     if (handleSelectElementScrolling(m_webView, verticalDir)) {
   106     if (handleSelectElementScrolling(m_webView, verticalDir)) {
    82         StaticObjectsContainer::instance()->webCursor()->cursorUpdate(true);
   107         StaticObjectsContainer::instance()->webCursor()->cursorUpdate(true);
   179                 kit(selectedNode->document()->frame())->frameView()->frameCoordsInViewCoords(selectedNode->getRect().Rect().iBr));
   204                 kit(selectedNode->document()->frame())->frameView()->frameCoordsInViewCoords(selectedNode->getRect().Rect().iBr));
   180             selectedPoint += (newRect.iTl - selectedRect.iTl);
   205             selectedPoint += (newRect.iTl - selectedRect.iTl);
   181             m_focusPosition = selectedPoint;
   206             m_focusPosition = selectedPoint;
   182             selectedRect = newRect;
   207             selectedRect = newRect;
   183             m_selectedElementRect = selectedRect;
   208             m_selectedElementRect = selectedRect;
       
   209 
       
   210             int x, y;
       
   211             selectedNode->renderer()->absolutePosition(x, y);
       
   212             Vector<IntRect> rects;
       
   213             selectedNode->renderer()->absoluteRects(rects, x, y);
       
   214             WebFrameView* fv = kit(selectedNode->document()->frame())->frameView();
       
   215             if (rects.size() > 0) {
       
   216                  selectedPoint = TPoint(rects[0].x(), rects[0].y());
       
   217                 selectedPoint = fv->frameCoordsInViewCoords(selectedPoint);
       
   218             }
   184             StaticObjectsContainer::instance()->webCursor()->updatePositionAndElemType(selectedPoint);
   219             StaticObjectsContainer::instance()->webCursor()->updatePositionAndElemType(selectedPoint);
   185             // special handling for Select-Multi
   220             // special handling for Select-Multi
   186             if (m_webView->focusedElementType() == TBrCtlDefs::EElementSelectMultiBox) {
   221             if (m_webView->focusedElementType() == TBrCtlDefs::EElementSelectMultiBox) {
   187                 Element* e = static_cast<Element*>(m_node);
   222                 Element* e = static_cast<Element*>(m_node);
   188                 if (e->isControl()) {
   223                 if (e->isControl()) {
   216     else {
   251     else {
   217         if (!m_firstNavigationOnPage) {
   252         if (!m_firstNavigationOnPage) {
   218             m_webView->mainFrame()->frameView()->scrollTo(contentPos + TPoint(horizontalDir * m_webView->Rect().Width() / KScrollWhenNotFound, verticalDir * m_webView->Rect().Height() / KScrollWhenNotFound));
   253             m_webView->mainFrame()->frameView()->scrollTo(contentPos + TPoint(horizontalDir * m_webView->Rect().Width() / KScrollWhenNotFound, verticalDir * m_webView->Rect().Height() / KScrollWhenNotFound));
   219             TPoint diff(m_webView->mainFrame()->frameView()->contentPos() - contentPos);
   254             TPoint diff(m_webView->mainFrame()->frameView()->contentPos() - contentPos);
   220             if (diff.iX || diff.iY) {
   255             if (diff.iX || diff.iY) {
       
   256                 Frame* focusedFrame = m_webView->page()->focusController()->focusedFrame();
       
   257                 if (focusedFrame == NULL) focusedFrame = m_webView->page()->mainFrame();
       
   258                 Node* focusNode = focusedFrame->document()->focusedNode();
       
   259                 if (focusNode) {
       
   260                     TRect selectedRect = focusNode->getRect().Rect();
       
   261                     selectedRect = TRect(kit(focusedFrame)->frameView()->frameCoordsInViewCoords(selectedRect.iTl), 
       
   262                         kit(focusedFrame)->frameView()->frameCoordsInViewCoords(selectedRect.iBr));
       
   263                     if (!selectedRect.Intersects(kit(focusedFrame)->frameView()->visibleRect()))
       
   264                         m_webView->page()->focusController()->setFocusedNode(NULL,0);
       
   265                 }
   221                 m_selectedElementRect.Move(diff);
   266                 m_selectedElementRect.Move(diff);
   222                 m_focusPosition = oldFocusPoint + diff;
   267                 m_focusPosition = oldFocusPoint + diff;
   223                 m_node = NULL;
   268                 m_node = NULL;
   224                 StaticObjectsContainer::instance()->webCursor()->updatePositionAndElemType(m_focusPosition - m_webView->mainFrame()->frameView()->contentPos());
   269                 StaticObjectsContainer::instance()->webCursor()->updatePositionAndElemType(m_focusPosition - m_webView->mainFrame()->frameView()->contentPos());
   225                 ret = true;
   270                 ret = true;
   287     return TPoint(x, y);
   332     return TPoint(x, y);
   288 }
   333 }
   289 
   334 
   290 int WebTabbedNavigation::distanceFunction(int horizontalDir, int verticalDir, TRect& rect, TPoint& point)
   335 int WebTabbedNavigation::distanceFunction(int horizontalDir, int verticalDir, TRect& rect, TPoint& point)
   291 {
   336 {
       
   337     // Based on http://www.w3.org/TR/WICD/#focus-navigation
   292     TReal x, y, euclidianDistance;
   338     TReal x, y, euclidianDistance;
   293     int sameAxisDist, otherAxisDist, overlap;
   339     int sameAxisDist, otherAxisDist, overlap;
   294 
   340 
   295     Math::Pow(x, (m_focusPosition.iX - point.iX), 2.0);
   341     Math::Pow(x, (m_focusPosition.iX - point.iX), 2.0);
   296     Math::Pow(y, (m_focusPosition.iY - point.iY), 2.0);
   342     Math::Pow(y, (m_focusPosition.iY - point.iY), 2.0);
   304         }
   350         }
   305         else {
   351         else {
   306             int top = max(m_selectedElementRect.iTl.iY, rect.iTl.iY);
   352             int top = max(m_selectedElementRect.iTl.iY, rect.iTl.iY);
   307             int bottom = std::min(m_selectedElementRect.iBr.iY, rect.iBr.iY);
   353             int bottom = std::min(m_selectedElementRect.iBr.iY, rect.iBr.iY);
   308             overlap = bottom - top;
   354             overlap = bottom - top;
       
   355             if (overlap == rect.Height()) {
       
   356                 euclidianDistance = (euclidianDistance * rect.Height()) / m_selectedElementRect.Height();
       
   357                 otherAxisDist = 0;
       
   358             }
   309         }
   359         }
   310     }
   360     }
   311     else { // vertical    
   361     else { // vertical    
   312         if (rect.iBr.iX < m_selectedElementRect.iTl.iX || rect.iTl.iX > m_selectedElementRect.iBr.iX) {
   362         if (rect.iBr.iX < m_selectedElementRect.iTl.iX || rect.iTl.iX > m_selectedElementRect.iBr.iX) {
   313             overlap = 0;
   363             overlap = 0;
   314         }
   364         }
   315         else {
   365         else {
   316             int top = max(m_selectedElementRect.iTl.iX, rect.iTl.iX);
   366             int top = max(m_selectedElementRect.iTl.iX, rect.iTl.iX);
   317             int bottom = std::min(m_selectedElementRect.iBr.iX, rect.iBr.iX);
   367             int bottom = std::min(m_selectedElementRect.iBr.iX, rect.iBr.iX);
   318             overlap = bottom - top;
   368             overlap = bottom - top;
       
   369             if (overlap == rect.Width()) {
       
   370                 euclidianDistance = (euclidianDistance * rect.Width()) / m_selectedElementRect.Width();
       
   371                 otherAxisDist = 0;
       
   372             }
   319         }
   373         }
   320     }
   374     }
   321     long ed, o;
   375     long ed, o;
   322     Math::Int(ed, euclidianDistance);
   376     Math::Int(ed, euclidianDistance);
   323     Math::Int(o, sqrt(overlap));
   377     Math::Int(o, sqrt(overlap));
   324     return ed + sameAxisDist + 2 * otherAxisDist + o;
   378     return ed + sameAxisDist + 2 * otherAxisDist - o;
   325 }
   379 }