2 * Copyright (C) 2012 Samsung Electronics
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
29 #include "TextSelection.h"
31 #include "EditorState.h"
32 #include "EwkViewImpl.h"
33 #include "NativeWebMouseEvent.h"
35 #include <Elementary.h>
37 using namespace WebCore;
41 TextSelection::TextSelection(EwkViewImpl* viewImpl)
42 : m_viewImpl(viewImpl)
43 , m_isTextSelectionDowned(false)
44 , m_isTextSelectionMode(false)
47 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
50 , m_handleMovingDirection(HandleMovingDirectionNormal)
54 const Eina_List* defaultThemeList = elm_theme_list_get(0);
58 EINA_LIST_FOREACH(defaultThemeList, l, theme) {
59 char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
62 m_leftHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_left", true, this);
63 m_rightHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_right", false, this);
70 m_magnifier = new TextSelectionMagnifier(m_viewImpl);
72 evas_object_event_callback_add(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
75 TextSelection::~TextSelection()
86 ecore_animator_del(m_moveAnimator);
91 ecore_timer_del(m_showTimer);
94 evas_object_event_callback_del(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp);
97 void TextSelection::update()
99 EditorState editorState = m_viewImpl->page()->editorState();
100 if (editorState.updateEditorRectOnly)
103 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
104 if (!editorState.shouldIgnoreCompositionSelectionChange)
105 informTextStyleState();
108 if (isTextSelectionMode()) {
109 if (!editorState.selectionIsRange) {
110 if (editorState.isContentEditable && !evas_object_focus_get(m_viewImpl->view())) {
111 WebCore::IntRect caretRect;
112 m_viewImpl->page()->getCaretPosition(caretRect);
113 if (!caretRect.isEmpty())
116 WebCore::IntRect leftRect;
117 WebCore::IntRect rightRect;
118 if (isTextSelectionDowned() || m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
121 setIsTextSelectionMode(false);
124 if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
132 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
134 if (!isAutomaticClearEnabled())
137 if (!isTextSelectionMode) {
140 initHandlesMouseDownedStatus();
141 setIsTextSelectionDowned(false);
144 m_isTextSelectionMode = isTextSelectionMode;
147 void TextSelection::clear()
149 EditorState editorState = m_viewImpl->page()->editorState();
150 if (!editorState.selectionIsRange)
153 m_viewImpl->page()->selectionRangeClear();
156 void TextSelection::hide()
163 void TextSelection::updateHandlers()
165 WebCore::IntRect leftRect, rightRect;
166 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
169 m_lastLeftHandleRect = leftRect;
170 m_lastRightHandleRect = rightRect;
172 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
173 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
174 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
176 TextSelectionHandle* shownLeftHandle = m_leftHandle;
177 TextSelectionHandle* shownRightHandle = m_rightHandle;
179 if (m_handleMovingDirection == HandleMovingDirectionReverse) {
180 shownLeftHandle = m_rightHandle;
181 shownRightHandle = m_leftHandle;
184 EditorState editorState = m_viewImpl->page()->editorState();
185 if (editorState.isContentEditable) {
186 shownLeftHandle->hide();
187 shownRightHandle->hide();
189 WebCore::IntRect editorRect = editorState.editorRect;
190 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
191 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
192 int webViewX, webViewY, webViewWidth, webViewHeight;
194 evas_object_geometry_get(m_viewImpl->view(), &webViewX, &webViewY, &webViewWidth, &webViewHeight);
195 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
196 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
197 shownLeftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
198 shownLeftHandle->show();
201 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
202 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
203 shownRightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
204 shownRightHandle->show();
207 shownLeftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
208 shownLeftHandle->show();
210 shownRightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
211 shownRightHandle->show();
215 void TextSelection::hideHandlers()
217 m_leftHandle->hide();
218 m_rightHandle->hide();
221 void TextSelection::showMagnifier()
226 void TextSelection::hideMagnifier()
231 void TextSelection::updateMagnifier(const IntPoint& position)
233 m_magnifier->update(position);
234 m_magnifier->move(position);
237 void TextSelection::showContextMenu()
242 EditorState editorState = m_viewImpl->page()->editorState();
243 if (!editorState.selectionIsRange && !editorState.isContentEditable)
246 WebCore::IntPoint point;
247 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
248 bool isPresentInViewPort = false;
250 if (editorState.selectionIsRange) {
251 WebCore::IntRect leftRect, rightRect;
252 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
255 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
256 // Checking if this point is in viewport area. If the calcualated
257 // point/Left/Right point are in view port then draw else do not draw the
258 // context menu. Only draw the selection points.
259 FloatRect unscaledRect = FloatRect(m_viewImpl->pageClient->visibleContentRect());
260 unscaledRect.scale(1 / m_viewImpl->pageClient->scaleFactor());
261 IntRect viewportRect = enclosingIntRect(unscaledRect);
263 WebCore::IntPoint visiblePoint = leftRect.center();
264 if (viewportRect.contains(visiblePoint)) {
265 // First check That the modified points are present in view port
266 point = visiblePoint;
267 isPresentInViewPort = true;
268 } else if (viewportRect.contains(leftRect.location())) {
269 // else if the calculated point is not in the view port area the
270 // draw context menu at left point if visible
271 point = leftRect.location();
272 isPresentInViewPort = true;
273 } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
274 // else if the calculated point is not in the view port area the
275 // draw context menu at right point if visible
276 point = rightRect.maxXMinYCorner();
277 isPresentInViewPort = true;
280 if (isPresentInViewPort && editorState.isContentEditable) {
281 // In case of single line editor box.
282 if (leftRect == rightRect) {
283 // draw context menu at center point of visible selection range in the editor box
284 IntRect editorRect = editorState.editorRect;
285 leftRect.intersect(editorRect);
286 if (!leftRect.isEmpty())
287 point = leftRect.center();
289 // not draw context menu if there is no visible selection range in the editor box
290 isPresentInViewPort = false;
295 point = leftRect.center();
297 } else if (editorState.isContentEditable) {
298 WebCore::IntRect caretRect;
299 m_viewImpl->page()->getCaretPosition(caretRect);
301 if (caretRect.isEmpty())
304 point = caretRect.center();
305 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
306 isPresentInViewPort = true;
309 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
310 if (!isPresentInViewPort)
314 // show context menu if its in viewport else do not show the contextmenu
316 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_viewImpl->view()));
317 if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
320 point = m_viewImpl->transformToScene().mapPoint(point);
321 Evas* evas = evas_object_evas_get(m_viewImpl->view());
324 Evas_Event_Mouse_Down mouseDown;
325 mouseDown.button = 3;
326 mouseDown.output.x = mouseDown.canvas.x = point.x();
327 mouseDown.output.y = mouseDown.canvas.y = point.y();
329 mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
330 mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
331 mouseDown.flags = EVAS_BUTTON_NONE;
332 mouseDown.timestamp = ecore_time_get() * 1000;
333 mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
335 smartData->api->mouse_down(smartData, &mouseDown);
338 Evas_Event_Mouse_Up mouseUp;
340 mouseUp.output.x = mouseUp.canvas.x = point.x();
341 mouseUp.output.y = mouseUp.canvas.y = point.y();
343 mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
344 mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
345 mouseUp.flags = EVAS_BUTTON_NONE;
346 mouseUp.timestamp = ecore_time_get() * 1000;
347 mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
349 smartData->api->mouse_up(smartData, &mouseUp);
352 void TextSelection::hideContextMenu()
357 m_viewImpl->page()->hideContextMenu();
360 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
362 int result = m_viewImpl->page()->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint), m_handleMovingDirection);
364 m_handleMovingDirection = result;
368 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
370 int result = m_viewImpl->page()->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint), m_handleMovingDirection);
372 m_handleMovingDirection = result;
377 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
379 WebCore::IntPoint basePosition;
380 EditorState editorState = m_viewImpl->page()->editorState();
382 if (editorState.selectionIsRange) {
383 WebCore::IntRect leftRect, rightRect;
384 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
389 if (handle->isLeft()) {
390 basePosition.setX(leftRect.x());
391 basePosition.setY(leftRect.y() + (leftRect.height()/2));
393 basePosition.setX(rightRect.x() + rightRect.width());
394 basePosition.setY(rightRect.y() + (rightRect.height()/2));
397 handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
402 updateMagnifier(handle->position());
406 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
408 if (handle->isLeft())
409 setLeftSelectionToEvasPoint(position);
411 setRightSelectionToEvasPoint(position);
413 updateMagnifier(handle->position());
416 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
418 m_handleMovingDirection = HandleMovingDirectionNormal;
425 bool TextSelection::isMagnifierVisible()
427 return m_magnifier->isVisible();
430 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
432 if (isTextSelectionDowned() && !isScrolling) {
437 EditorState editorState = m_viewImpl->page()->editorState();
438 if (!editorState.selectionIsRange && editorState.isContentEditable)
439 setIsTextSelectionMode(false);
442 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
443 if (m_viewImpl->pageClient->isClipboardWindowOpened())
446 if (m_viewImpl->gestureClient->isGestureWorking())
456 if (isScrolling && isMagnifierVisible())
460 void TextSelection::startMoveAnimator()
462 if (!isEnabled() || !isTextSelectionDowned())
466 m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
469 void TextSelection::stopMoveAnimator()
471 if (m_moveAnimator) {
472 ecore_animator_del(m_moveAnimator);
477 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
479 static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
482 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
484 TextSelection* textSelection = static_cast<TextSelection*>(data);
486 Evas_Coord_Point point;
487 evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_viewImpl->view()), &point.x, &point.y);
488 textSelection->textSelectionMove(IntPoint(point.x, point.y));
490 return ECORE_CALLBACK_RENEW;
493 // 'return false' means text selection is not possible for point.
494 // 'return true' means text selection is possible.
495 bool TextSelection::textSelectionDown(const IntPoint& point)
497 // text selection should be ignored when longtap on handle from osp
498 if (!isEnabled() && isTextSelectionHandleDowned())
501 #if ENABLE(TIZEN_ISF_PORT)
502 m_viewImpl->inputMethodContext()->resetIMFContext();
504 setIsTextSelectionMode(false);
506 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
507 bool result = m_viewImpl->page()->selectClosestWord(contentsPoint);
511 if (isTextSelectionMode()) {
514 setIsTextSelectionMode(true);
516 setIsTextSelectionDowned(true);
518 updateMagnifier(point);
526 static int s_textSelectionMargin = 5;
528 void TextSelection::textSelectionMove(const IntPoint& point)
530 // text selection should be ignored when longtap on handle from osp
531 if (!isEnabled() && isTextSelectionHandleDowned())
534 if (!isTextSelectionMode()) {
539 WebCore::IntPoint viewPoint;
540 EditorState editorState = m_viewImpl->page()->editorState();
541 bool isInEditablePicker = false;
543 #if ENABLE(TIZEN_INPUT_TAG_EXTENSION)
544 if (editorState.isContentEditable) {
545 if (editorState.inputMethodHints == "date"
546 || editorState.inputMethodHints == "datetime"
547 || editorState.inputMethodHints == "datetime-local"
548 || editorState.inputMethodHints == "month"
549 || editorState.inputMethodHints == "time"
550 || editorState.inputMethodHints == "week")
551 isInEditablePicker = true;
555 if (editorState.isContentEditable && !isInEditablePicker) {
556 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
557 IntPoint updatedPoint = point;
558 bool scrolledY = false;
559 if (point.y() < mapRect.y()) {
560 updatedPoint.setY(mapRect.y() + s_textSelectionMargin);
561 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionBackward)) {
563 updateMagnifier(updatedPoint);
565 } else if (point.y() > mapRect.maxY()) {
566 updatedPoint.setY(mapRect.maxY() - s_textSelectionMargin);
567 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionForward)) {
569 updateMagnifier(updatedPoint);
573 bool scrolledX = false;
574 if (point.x() < mapRect.x()) {
575 updatedPoint.setX(mapRect.x() + s_textSelectionMargin);
576 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionBackward)) {
578 updateMagnifier(updatedPoint);
580 } else if (point.x() > mapRect.maxX()) {
581 updatedPoint.setX(mapRect.maxX() - s_textSelectionMargin);
582 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionForward)) {
584 updateMagnifier(updatedPoint);
588 if (!scrolledX && !scrolledY) {
589 viewPoint = m_viewImpl->transformFromScene().mapPoint(updatedPoint);
590 m_viewImpl->page()->selectClosestWord(viewPoint);
591 updateMagnifier(updatedPoint);
594 viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
595 m_viewImpl->page()->selectClosestWord(viewPoint);
596 updateMagnifier(point);
601 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
603 // text selection should be ignored when longtap on handle from osp
604 if (!isEnabled() && isTextSelectionHandleDowned())
609 if (!isTextSelectionMode() || !isTextSelectionDowned())
612 setIsTextSelectionDowned(false);
615 EditorState editorState = m_viewImpl->page()->editorState();
616 if (editorState.selectionIsRange || editorState.isContentEditable) {
617 if (editorState.selectionIsRange)
621 } else if (!isStartedTextSelectionFromOutside)
622 setIsTextSelectionMode(false);
625 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
626 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
628 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
629 if (!leftHandleRect.isEmpty() && leftHandleRect.contains(position))
632 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
633 if (!rightHandleRect.isEmpty() && rightHandleRect.contains(position))
634 return m_rightHandle;
639 void TextSelection::textSelectionHandleDown(const IntPoint& position)
641 m_selectedHandle = getSelectedHandle(position);
642 if (m_selectedHandle)
643 m_selectedHandle->mouseDown(position);
645 initHandlesMouseDownedStatus();
648 void TextSelection::textSelectionHandleMove(const IntPoint& position)
650 if (m_selectedHandle && isTextSelectionHandleDowned())
651 m_selectedHandle->mouseMove(position);
654 void TextSelection::textSelectionHandleUp()
656 if (m_selectedHandle && isTextSelectionHandleDowned()) {
657 m_selectedHandle->mouseUp();
658 m_selectedHandle = 0;
663 bool TextSelection::isEnabled()
665 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_viewImpl->view()));
668 bool TextSelection::isAutomaticClearEnabled()
670 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_viewImpl->view()));
673 void TextSelection::requestToShow()
676 ecore_timer_del(m_showTimer);
677 m_showTimer = ecore_timer_loop_add((double)200.0/1000.0, showTimerCallback, this);
680 Eina_Bool TextSelection::showTimerCallback(void* data)
682 TextSelection* textSelection = static_cast<TextSelection*>(data);
683 textSelection->showHandlesAndContextMenu();
685 return ECORE_CALLBACK_RENEW;
688 void TextSelection::showHandlesAndContextMenu()
690 WebCore::IntRect leftRect, rightRect;
691 if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
692 if ((leftRect == m_lastLeftHandleRect) && (rightRect == m_lastRightHandleRect)) {
694 ecore_timer_del(m_showTimer);
698 updateHandlesAndContextMenu(true);
701 m_lastLeftHandleRect = leftRect;
702 m_lastRightHandleRect = rightRect;
706 void TextSelection::initHandlesMouseDownedStatus()
708 m_leftHandle->setIsMouseDowned(false);
709 m_rightHandle->setIsMouseDowned(false);
712 void TextSelection::changeContextMenuPosition(WebCore::IntPoint& position)
714 if (m_leftHandle->isTop()) {
715 IntRect handleRect = m_leftHandle->getHandleRect();
716 position.setY(position.y() - handleRect.height());
720 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
721 void TextSelection::informTextStyleState()
723 WebCore::IntPoint startPoint, endPoint;
724 WebCore::IntRect leftRect, rightRect;
726 WebCore::IntRect caretRect;
727 m_viewImpl->page()->getCaretPosition(caretRect);
728 if (!caretRect.isEmpty()) {
729 startPoint.setX(caretRect.x());
730 startPoint.setY(caretRect.y() + caretRect.height());
732 endPoint.setX(caretRect.x() + caretRect.width());
733 endPoint.setY(caretRect.y() + caretRect.height());
735 else if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
736 startPoint.setX(leftRect.x());
737 startPoint.setY(leftRect.y() + leftRect.height());
739 endPoint.setX(rightRect.x() + rightRect.width());
740 endPoint.setY(rightRect.y() + rightRect.height());
743 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
744 WebCore::IntPoint startEvasPoint = toEvasTransform.mapPoint(startPoint);
745 WebCore::IntPoint endEvasPoint = toEvasTransform.mapPoint(endPoint);
747 ewkViewTextStyleState(m_viewImpl->view(), startEvasPoint, endEvasPoint);
750 } // namespace WebKit
752 #endif // TIZEN_WEBKIT2_TEXT_SELECTION