webengine/osswebengine/WebKit/s60/webview/WebFepTextEditor.cpp
branchRCL_3
changeset 50 d96eed154187
parent 49 919f36ff910f
equal deleted inserted replaced
49:919f36ff910f 50:d96eed154187
    45 #include "HtmlInputElement.h"
    45 #include "HtmlInputElement.h"
    46 #include "HtmlTextAreaElement.h"
    46 #include "HtmlTextAreaElement.h"
    47 #include "PlatformKeyboardEvent.h"
    47 #include "PlatformKeyboardEvent.h"
    48 #include "CSSPropertyNames.h"
    48 #include "CSSPropertyNames.h"
    49 #include "CSSValueKeywords.h"
    49 #include "CSSValueKeywords.h"
    50 
    50 #include "RenderStyle.h"
       
    51 #include "htmlediting.h"
       
    52 #include "HtmlDivElement.h"
       
    53 #include "CSSStyleDeclaration.h"
       
    54 #include "CSSMutableStyleDeclaration.h"
       
    55 #include "SelectionController.h"
       
    56 #include "Selection.h"
    51 #include <coemain.h>
    57 #include <coemain.h>
    52 #include <eikpanic.h>
    58 #include <eikpanic.h>
    53 #include <aknedsts.h>
    59 #include <aknedsts.h>
    54 #include <aknutils.h>
    60 #include <aknutils.h>
    55 #include "Text.h"
    61 #include "Text.h"
   357 //
   363 //
   358 //
   364 //
   359 // -----------------------------------------------------------------------------
   365 // -----------------------------------------------------------------------------
   360 void CWebFepTextEditor::CancelFepInlineEdit()
   366 void CWebFepTextEditor::CancelFepInlineEdit()
   361 {
   367 {
   362     if (IsTextAreaFocused()||IsInputElementFocused()) {
   368     if (IsTextAreaFocused()||IsInputElementFocused() || IsDivElementFocused()) {
   363         if (m_inlineEditText && DocumentLengthForFep() < DocumentMaximumLengthForFep()) {
   369         if (m_inlineEditText && DocumentLengthForFep() < DocumentMaximumLengthForFep()) {
   364             HBufC* tempBuf = HBufC::NewLC(DocumentLengthForFep());
   370             HBufC* tempBuf = HBufC::NewLC(DocumentLengthForFep());
   365             TPtr ptr(tempBuf->Des());
   371             TPtr ptr(tempBuf->Des());
   366             GetEditorContentForFep(ptr, 0, DocumentLengthForFep());
   372             GetEditorContentForFep(ptr, 0, DocumentLengthForFep());
   367             TInt position = ptr.Locate(KLineEnterChar);
   373             TInt position = ptr.Locate(KLineEnterChar);
   396         }
   402         }
   397         else if (frame->document()->focusedNode()->hasTagName(HTMLNames::textareaTag)) {
   403         else if (frame->document()->focusedNode()->hasTagName(HTMLNames::textareaTag)) {
   398             HTMLTextAreaElement* ie = static_cast<HTMLTextAreaElement*>(frame->document()->focusedNode());
   404             HTMLTextAreaElement* ie = static_cast<HTMLTextAreaElement*>(frame->document()->focusedNode());
   399             length = ie->value().length();
   405             length = ie->value().length();
   400         }
   406         }
       
   407         else if (frame->document()->focusedNode()->hasTagName(HTMLNames::divTag)) {
       
   408             HTMLDivElement* ie = static_cast<HTMLDivElement*>(frame->document()->focusedNode());
       
   409             length = ie->innerText().length();
       
   410         }
       
   411         
   401     }
   412     }
   402 
   413 
   403     return length;
   414     return length;
   404 }
   415 }
   405 
   416 
   429         length += 1 ;
   440         length += 1 ;
   430     // TextArea node has no member function maxLength(), so return KMaxTInt
   441     // TextArea node has no member function maxLength(), so return KMaxTInt
   431     return length;
   442     return length;
   432 }
   443 }
   433 
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // indexForVisiblePosition
       
   447 //
       
   448 //
       
   449 // -----------------------------------------------------------------------------
       
   450 TInt CWebFepTextEditor::indexForVisiblePosition(const WebCore::VisiblePosition& pos,WebCore::Element* node) const
       
   451 {
       
   452     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();    
       
   453     Position indexPosition = pos.deepEquivalent();
       
   454     if (!indexPosition.node() || indexPosition.node()->rootEditableElement() != node)
       
   455         return 0;    
       
   456     ExceptionCode ec = 0;
       
   457     
       
   458     RefPtr<Range> range = frame->document()->createRange(); 
       
   459     range->setStart(node, 0, ec);
       
   460     ASSERT(!ec);
       
   461     range->setEnd(indexPosition.node(), indexPosition.offset(), ec);
       
   462     
       
   463     ASSERT(!ec);
       
   464     return TextIterator::rangeLength(range.get());
       
   465 }
       
   466 
       
   467 // -----------------------------------------------------------------------------
       
   468 // visiblePositionForIndex
       
   469 //
       
   470 //
       
   471 // -----------------------------------------------------------------------------
       
   472 VisiblePosition CWebFepTextEditor::visiblePositionForIndex(TInt index,WebCore::Element* node)
       
   473 {
       
   474     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();    
       
   475     if (index <= 0)
       
   476         return VisiblePosition(node, 0, DOWNSTREAM);
       
   477     ExceptionCode ec = 0;
       
   478     
       
   479     RefPtr<Range> range = frame->document()->createRange(); 
       
   480     range->selectNodeContents(node, ec);
       
   481     ASSERT(!ec);
       
   482     CharacterIterator it(range.get());
       
   483     it.advance(index - 1);
       
   484     Node* endContainer = it.range()->endContainer(ec);
       
   485     ASSERT(!ec);
       
   486     int endOffset = it.range()->endOffset(ec);
       
   487     ASSERT(!ec);
       
   488     return VisiblePosition(endContainer, endOffset, UPSTREAM);
       
   489 }
       
   490 
       
   491 // -----------------------------------------------------------------------------
       
   492 // setSelectionRange
       
   493 //
       
   494 //
       
   495 // -----------------------------------------------------------------------------
       
   496 void CWebFepTextEditor::setSelectionRange(TInt start, TInt end, WebCore::Element* node)
       
   497 {
       
   498     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();    
       
   499     end = Max(end, 0);
       
   500     start = Min(Max(start, 0), end);
       
   501 
       
   502     VisiblePosition startPosition = visiblePositionForIndex(start,node);
       
   503     VisiblePosition endPosition;
       
   504     if (start == end)
       
   505         endPosition = startPosition;
       
   506     else
       
   507         endPosition = visiblePositionForIndex(end,node);
       
   508 
       
   509     // startPosition and endPosition can be null position for example when
       
   510     // "-webkit-user-select: none" style attribute is specified.
       
   511     if (startPosition.isNotNull() && endPosition.isNotNull()) {
       
   512         ASSERT(startPosition.deepEquivalent().node()->shadowAncestorNode() == node() && endPosition.deepEquivalent().node()->shadowAncestorNode() == node());
       
   513     }
       
   514     
       
   515 
       
   516    if (frame)
       
   517        { 
       
   518         Selection newSelection = Selection(startPosition, endPosition);
       
   519         frame->selectionController()->setSelection(newSelection);
       
   520        } 
       
   521 }
   434 // -----------------------------------------------------------------------------
   522 // -----------------------------------------------------------------------------
   435 // SetCursorSelectionForFepL
   523 // SetCursorSelectionForFepL
   436 //
   524 //
   437 //
   525 //
   438 // -----------------------------------------------------------------------------
   526 // -----------------------------------------------------------------------------
   452 		else if (focusedNode->hasTagName(HTMLNames::inputTag)) {
   540 		else if (focusedNode->hasTagName(HTMLNames::inputTag)) {
   453 		    HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(focusedNode);
   541 		    HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(focusedNode);
   454 		    inputElement->setSelectionStart(lowPos);
   542 		    inputElement->setSelectionStart(lowPos);
   455 		    inputElement->setSelectionEnd(highPos);
   543 		    inputElement->setSelectionEnd(highPos);
   456 		}
   544 		}
       
   545         else if (focusedNode->hasTagName(HTMLNames::divTag)) {
       
   546             HTMLDivElement* inputElement = static_cast<HTMLDivElement*>(focusedNode);
       
   547             setSelectionRange(lowPos,highPos,inputElement);
       
   548         }
       
   549         
   457 		HandleUpdateCursor();
   550 		HandleUpdateCursor();
   458     }
   551     }
   459 }
   552 }
   460 
   553 
   461 // -----------------------------------------------------------------------------
   554 // -----------------------------------------------------------------------------
   480             HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(focusedNode);
   573             HTMLInputElement* inputElement = static_cast<HTMLInputElement*>(focusedNode);
   481             TInt anchorPos = inputElement->selectionStart();
   574             TInt anchorPos = inputElement->selectionStart();
   482             TInt cursorPos = inputElement->selectionEnd();
   575             TInt cursorPos = inputElement->selectionEnd();
   483             aCursorSelection.SetSelection(cursorPos, anchorPos);
   576             aCursorSelection.SetSelection(cursorPos, anchorPos);
   484         }
   577         }
       
   578         else if (focusedNode->hasTagName(HTMLNames::divTag)) {
       
   579             HTMLDivElement* inputElement = static_cast<HTMLDivElement*>(focusedNode);
       
   580             TInt anchorPos = indexForVisiblePosition(frame->selectionController()->start(),inputElement);
       
   581             TInt cursorPos = indexForVisiblePosition(frame->selectionController()->end(),inputElement);
       
   582             aCursorSelection.SetSelection(cursorPos, anchorPos);
       
   583         }
       
   584         
   485     }
   585     }
   486 }
   586 }
   487 
   587 
   488 // -----------------------------------------------------------------------------
   588 // -----------------------------------------------------------------------------
   489 // GetEditorContentForFep
   589 // GetEditorContentForFep
   515             // input editors (vkb, etc) ignore newline
   615             // input editors (vkb, etc) ignore newline
   516             String str(ie->value().substring(aDocumentPosition, aLengthToRetrieve));
   616             String str(ie->value().substring(aDocumentPosition, aLengthToRetrieve));
   517             str.replace(EKeyLineFeed, CEditableText::EParagraphDelimiter);
   617             str.replace(EKeyLineFeed, CEditableText::EParagraphDelimiter);
   518             aEditorContent = str;
   618             aEditorContent = str;
   519         }
   619         }
       
   620         else if (frame->document()->focusedNode()->hasTagName(HTMLNames::divTag)) {
       
   621             HTMLDivElement* ie = static_cast<HTMLDivElement*>(frame->document()->focusedNode());
       
   622             String str(ie->innerText().substring(aDocumentPosition, aLengthToRetrieve));
       
   623             str.replace(EKeyLineFeed, CEditableText::EParagraphDelimiter);
       
   624             aEditorContent = str;
       
   625         }
   520     }
   626     }
   521 }
   627 }
   522 
   628 
   523 // -----------------------------------------------------------------------------
   629 // -----------------------------------------------------------------------------
   524 // GetFormatForFep
   630 // GetFormatForFep
   542 Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();
   648 Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();
   543 if (frame &&
   649 if (frame &&
   544     frame->document() &&
   650     frame->document() &&
   545     frame->document()->focusedNode()){
   651     frame->document()->focusedNode()){
   546     if ( frame->document()->focusedNode()->hasTagName(HTMLNames::inputTag) ||
   652     if ( frame->document()->focusedNode()->hasTagName(HTMLNames::inputTag) ||
   547 		 frame->document()->focusedNode()->hasTagName(HTMLNames::textareaTag)){
   653 		 frame->document()->focusedNode()->hasTagName(HTMLNames::textareaTag) ||
       
   654 		 frame->document()->focusedNode()->hasTagName(HTMLNames::divTag)) {
   548 		HTMLGenericFormElement*  ie = static_cast<HTMLGenericFormElement*>(frame->document()->focusedNode());
   655 		HTMLGenericFormElement*  ie = static_cast<HTMLGenericFormElement*>(frame->document()->focusedNode());
   549 		SelectionController* sc = frame->selectionController();
   656 		SelectionController* sc = frame->selectionController();
   550 		int xPos(0);
   657 		int xPos(0);
   551 		int yPos(0);
   658 		int yPos(0);
   552 		if ( sc ){
   659 		if ( sc ){
  1514     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();
  1621     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();
  1515     return ( frame && frame->document()->focusedNode() &&
  1622     return ( frame && frame->document()->focusedNode() &&
  1516              frame->document()->focusedNode()->hasTagName(HTMLNames::inputTag));
  1623              frame->document()->focusedNode()->hasTagName(HTMLNames::inputTag));
  1517     }
  1624     }
  1518 
  1625 
       
  1626 TBool CWebFepTextEditor::IsDivElementFocused() const
       
  1627     {
       
  1628     Frame* frame = m_webView->page()->focusController()->focusedOrMainFrame();
       
  1629     return ( frame && frame->document()->focusedNode() &&
       
  1630              frame->document()->focusedNode()->hasTagName(HTMLNames::divTag));
       
  1631     }
       
  1632 
  1519 void CWebFepTextEditor::ReportEventL()
  1633 void CWebFepTextEditor::ReportEventL()
  1520     {
  1634     {
  1521     m_ExtendedInputCapabilities->ReportEventL(CAknExtendedInputCapabilities::
  1635     m_ExtendedInputCapabilities->ReportEventL(CAknExtendedInputCapabilities::
  1522                         MAknEventObserver::EPointerEventReceived, NULL );
  1636                         MAknEventObserver::EPointerEventReceived, NULL );
  1523     }
  1637     }