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)
53 const Eina_List* defaultThemeList = elm_theme_list_get(0);
57 EINA_LIST_FOREACH(defaultThemeList, l, theme) {
58 char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
61 m_leftHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_left", true, this);
62 m_rightHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_right", false, this);
69 m_magnifier = new TextSelectionMagnifier(m_viewImpl);
71 evas_object_event_callback_add(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
74 TextSelection::~TextSelection()
85 ecore_animator_del(m_moveAnimator);
90 ecore_timer_del(m_showTimer);
93 evas_object_event_callback_del(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp);
96 void TextSelection::update()
98 EditorState editorState = m_viewImpl->page()->editorState();
99 if (editorState.updateEditorRectOnly)
102 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
103 if (!editorState.shouldIgnoreCompositionSelectionChange)
104 informTextStyleState();
107 if (isTextSelectionMode()) {
108 if (!editorState.selectionIsRange) {
109 if (editorState.isContentEditable && !evas_object_focus_get(m_viewImpl->view())) {
110 WebCore::IntRect caretRect;
111 m_viewImpl->page()->getCaretPosition(caretRect);
112 if (!caretRect.isEmpty())
115 WebCore::IntRect leftRect;
116 WebCore::IntRect rightRect;
117 if (isTextSelectionDowned() || m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
120 setIsTextSelectionMode(false);
123 if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
131 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
133 if (!isAutomaticClearEnabled())
136 if (!isTextSelectionMode) {
139 initHandlesMouseDownedStatus();
142 m_isTextSelectionMode = isTextSelectionMode;
145 void TextSelection::clear()
147 EditorState editorState = m_viewImpl->page()->editorState();
148 if (!editorState.selectionIsRange)
151 m_viewImpl->page()->selectionRangeClear();
154 void TextSelection::hide()
161 void TextSelection::updateHandlers()
163 WebCore::IntRect leftRect, rightRect;
164 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
167 m_lastLeftHandleRect = leftRect;
168 m_lastRightHandleRect = rightRect;
170 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
171 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
172 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
174 EditorState editorState = m_viewImpl->page()->editorState();
175 if (editorState.isContentEditable) {
176 m_leftHandle->hide();
177 m_rightHandle->hide();
179 WebCore::IntRect editorRect = editorState.editorRect;
180 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
181 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
182 int webViewX, webViewY, webViewWidth, webViewHeight;
184 evas_object_geometry_get(m_viewImpl->view(), &webViewX, &webViewY, &webViewWidth, &webViewHeight);
185 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
186 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
187 m_leftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
188 m_leftHandle->show();
191 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
192 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
193 m_rightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
194 m_rightHandle->show();
197 m_leftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
198 m_leftHandle->show();
200 m_rightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
201 m_rightHandle->show();
205 void TextSelection::hideHandlers()
207 m_leftHandle->hide();
208 m_rightHandle->hide();
211 void TextSelection::showMagnifier()
216 void TextSelection::hideMagnifier()
221 void TextSelection::updateMagnifier(const IntPoint& position)
223 m_magnifier->update(position);
224 m_magnifier->move(position);
227 void TextSelection::showContextMenu()
232 EditorState editorState = m_viewImpl->page()->editorState();
233 if (!editorState.selectionIsRange && !editorState.isContentEditable)
236 WebCore::IntPoint point;
237 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
238 bool isPresentInViewPort = false;
240 if (editorState.selectionIsRange) {
241 WebCore::IntRect leftRect, rightRect;
242 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
245 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
246 // Checking if this point is in viewport area. If the calcualated
247 // point/Left/Right point are in view port then draw else do not draw the
248 // context menu. Only draw the selection points.
249 FloatRect unscaledRect = FloatRect(m_viewImpl->pageClient->visibleContentRect());
250 unscaledRect.scale(1 / m_viewImpl->pageClient->scaleFactor());
251 IntRect viewportRect = enclosingIntRect(unscaledRect);
253 WebCore::IntPoint visiblePoint = leftRect.center();
254 if (viewportRect.contains(visiblePoint)) {
255 // First check That the modified points are present in view port
256 point = visiblePoint;
257 isPresentInViewPort = true;
258 } else if (viewportRect.contains(leftRect.location())) {
259 // else if the calculated point is not in the view port area the
260 // draw context menu at left point if visible
261 point = leftRect.location();
262 isPresentInViewPort = true;
263 } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
264 // else if the calculated point is not in the view port area the
265 // draw context menu at right point if visible
266 point = rightRect.maxXMinYCorner();
267 isPresentInViewPort = true;
270 if (isPresentInViewPort && editorState.isContentEditable) {
271 // In case of single line editor box.
272 if (leftRect == rightRect) {
273 // draw context menu at center point of visible selection range in the editor box
274 IntRect editorRect = editorState.editorRect;
275 leftRect.intersect(editorRect);
276 if (!leftRect.isEmpty())
277 point = leftRect.center();
279 // not draw context menu if there is no visible selection range in the editor box
280 isPresentInViewPort = false;
285 point = leftRect.center();
287 } else if (editorState.isContentEditable) {
288 WebCore::IntRect caretRect;
289 m_viewImpl->page()->getCaretPosition(caretRect);
291 if (caretRect.isEmpty())
294 point = caretRect.center();
295 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
296 isPresentInViewPort = true;
299 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
300 if (!isPresentInViewPort)
304 // show context menu if its in viewport else do not show the contextmenu
306 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_viewImpl->view()));
307 if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
310 point = m_viewImpl->transformToScene().mapPoint(point);
311 Evas* evas = evas_object_evas_get(m_viewImpl->view());
314 Evas_Event_Mouse_Down mouseDown;
315 mouseDown.button = 3;
316 mouseDown.output.x = mouseDown.canvas.x = point.x();
317 mouseDown.output.y = mouseDown.canvas.y = point.y();
319 mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
320 mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
321 mouseDown.flags = EVAS_BUTTON_NONE;
322 mouseDown.timestamp = ecore_time_get() * 1000;
323 mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
325 smartData->api->mouse_down(smartData, &mouseDown);
328 Evas_Event_Mouse_Up mouseUp;
330 mouseUp.output.x = mouseUp.canvas.x = point.x();
331 mouseUp.output.y = mouseUp.canvas.y = point.y();
333 mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
334 mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
335 mouseUp.flags = EVAS_BUTTON_NONE;
336 mouseUp.timestamp = ecore_time_get() * 1000;
337 mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
339 smartData->api->mouse_up(smartData, &mouseUp);
342 void TextSelection::hideContextMenu()
347 m_viewImpl->page()->hideContextMenu();
350 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
352 m_viewImpl->page()->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
356 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
358 m_viewImpl->page()->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
363 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
365 WebCore::IntPoint basePosition;
366 EditorState editorState = m_viewImpl->page()->editorState();
368 if (editorState.selectionIsRange) {
369 WebCore::IntRect leftRect, rightRect;
370 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
375 if (handle->isLeft()) {
376 basePosition.setX(leftRect.x());
377 basePosition.setY(leftRect.y() + (leftRect.height()/2));
379 basePosition.setX(rightRect.x() + rightRect.width());
380 basePosition.setY(rightRect.y() + (rightRect.height()/2));
383 handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
388 updateMagnifier(handle->position());
392 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
394 if (handle->isLeft())
395 setLeftSelectionToEvasPoint(position);
397 setRightSelectionToEvasPoint(position);
399 updateMagnifier(handle->position());
402 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
408 bool TextSelection::isMagnifierVisible()
410 return m_magnifier->isVisible();
413 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
415 if (isTextSelectionDowned() && !isScrolling) {
420 EditorState editorState = m_viewImpl->page()->editorState();
421 if (!editorState.selectionIsRange && editorState.isContentEditable)
422 setIsTextSelectionMode(false);
425 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
426 if (m_viewImpl->pageClient->isClipboardWindowOpened())
429 if (m_viewImpl->gestureClient->isGestureWorking())
439 if (isScrolling && isMagnifierVisible())
443 void TextSelection::startMoveAnimator()
445 if (!isEnabled() || !isTextSelectionDowned())
449 m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
452 void TextSelection::stopMoveAnimator()
454 if (m_moveAnimator) {
455 ecore_animator_del(m_moveAnimator);
460 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
462 static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
465 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
467 TextSelection* textSelection = static_cast<TextSelection*>(data);
469 Evas_Coord_Point point;
470 evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_viewImpl->view()), &point.x, &point.y);
471 textSelection->textSelectionMove(IntPoint(point.x, point.y));
473 return ECORE_CALLBACK_RENEW;
476 // 'return false' means text selection is not possible for point.
477 // 'return true' means text selection is possible.
478 bool TextSelection::textSelectionDown(const IntPoint& point, bool isStartedTextSelectionFromOutside)
480 // text selection should be ignored when longtap on handle from osp
481 if (!isEnabled() && isTextSelectionHandleDowned())
484 #if ENABLE(TIZEN_ISF_PORT)
485 m_viewImpl->inputMethodContext()->resetIMFContext();
487 setIsTextSelectionMode(false);
489 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
490 bool result = m_viewImpl->page()->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
494 if (isTextSelectionMode()) {
497 setIsTextSelectionMode(true);
499 setIsTextSelectionDowned(true);
501 updateMagnifier(point);
509 static int s_textSelectionMargin = 5;
511 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
513 // text selection should be ignored when longtap on handle from osp
514 if (!isEnabled() && isTextSelectionHandleDowned())
517 if (!isTextSelectionMode()) {
522 WebCore::IntPoint viewPoint;
523 EditorState editorState = m_viewImpl->page()->editorState();
524 if (editorState.isContentEditable) {
525 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
526 IntPoint updatedPoint = point;
527 bool scrolledY = false;
528 if (point.y() < mapRect.y()) {
529 updatedPoint.setY(mapRect.y() + s_textSelectionMargin);
530 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionBackward)) {
532 updateMagnifier(updatedPoint);
534 } else if (point.y() > mapRect.maxY()) {
535 updatedPoint.setY(mapRect.maxY() - s_textSelectionMargin);
536 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionForward)) {
538 updateMagnifier(updatedPoint);
542 bool scrolledX = false;
543 if (point.x() < mapRect.x()) {
544 updatedPoint.setX(mapRect.x() + s_textSelectionMargin);
545 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionBackward)) {
547 updateMagnifier(updatedPoint);
549 } else if (point.x() > mapRect.maxX()) {
550 updatedPoint.setX(mapRect.maxX() - s_textSelectionMargin);
551 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionForward)) {
553 updateMagnifier(updatedPoint);
557 if (!scrolledX && !scrolledY) {
558 viewPoint = m_viewImpl->transformFromScene().mapPoint(updatedPoint);
559 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
560 updateMagnifier(updatedPoint);
563 viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
564 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
565 updateMagnifier(point);
570 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
572 // text selection should be ignored when longtap on handle from osp
573 if (!isEnabled() && isTextSelectionHandleDowned())
578 if (!isTextSelectionMode() || !isTextSelectionDowned())
581 setIsTextSelectionDowned(false);
584 EditorState editorState = m_viewImpl->page()->editorState();
585 if (editorState.selectionIsRange || editorState.isContentEditable) {
586 if (editorState.selectionIsRange)
590 } else if (!isStartedTextSelectionFromOutside)
591 setIsTextSelectionMode(false);
594 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
595 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
597 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
598 if (!leftHandleRect.isEmpty() && leftHandleRect.contains(position))
601 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
602 if (!rightHandleRect.isEmpty() && rightHandleRect.contains(position))
603 return m_rightHandle;
608 void TextSelection::textSelectionHandleDown(const IntPoint& position)
610 m_selectedHandle = getSelectedHandle(position);
611 if (m_selectedHandle)
612 m_selectedHandle->mouseDown(position);
614 initHandlesMouseDownedStatus();
617 void TextSelection::textSelectionHandleMove(const IntPoint& position)
619 if (m_selectedHandle && isTextSelectionHandleDowned())
620 m_selectedHandle->mouseMove(position);
623 void TextSelection::textSelectionHandleUp()
625 if (m_selectedHandle && isTextSelectionHandleDowned()) {
626 m_selectedHandle->mouseUp();
627 m_selectedHandle = 0;
632 bool TextSelection::isEnabled()
634 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_viewImpl->view()));
637 bool TextSelection::isAutomaticClearEnabled()
639 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_viewImpl->view()));
642 void TextSelection::requestToShow()
645 ecore_timer_del(m_showTimer);
646 m_showTimer = ecore_timer_loop_add((double)200.0/1000.0, showTimerCallback, this);
649 Eina_Bool TextSelection::showTimerCallback(void* data)
651 TextSelection* textSelection = static_cast<TextSelection*>(data);
652 textSelection->showHandlesAndContextMenu();
654 return ECORE_CALLBACK_RENEW;
657 void TextSelection::showHandlesAndContextMenu()
659 WebCore::IntRect leftRect, rightRect;
660 if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
661 if ((leftRect == m_lastLeftHandleRect) && (rightRect == m_lastRightHandleRect)) {
663 ecore_timer_del(m_showTimer);
667 updateHandlesAndContextMenu(true);
670 m_lastLeftHandleRect = leftRect;
671 m_lastRightHandleRect = rightRect;
675 void TextSelection::initHandlesMouseDownedStatus()
677 m_leftHandle->setIsMouseDowned(false);
678 m_rightHandle->setIsMouseDowned(false);
681 void TextSelection::changeContextMenuPosition(WebCore::IntPoint& position)
683 if (m_leftHandle->isTop()) {
684 IntRect handleRect = m_leftHandle->getHandleRect();
685 position.setY(position.y() - handleRect.height());
689 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
690 void TextSelection::informTextStyleState()
692 WebCore::IntPoint startPoint, endPoint;
693 WebCore::IntRect leftRect, rightRect;
695 WebCore::IntRect caretRect;
696 m_viewImpl->page()->getCaretPosition(caretRect);
697 if (!caretRect.isEmpty()) {
698 startPoint.setX(caretRect.x());
699 startPoint.setY(caretRect.y() + caretRect.height());
701 endPoint.setX(caretRect.x() + caretRect.width());
702 endPoint.setY(caretRect.y() + caretRect.height());
704 else if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
705 startPoint.setX(leftRect.x());
706 startPoint.setY(leftRect.y() + leftRect.height());
708 endPoint.setX(rightRect.x() + rightRect.width());
709 endPoint.setY(rightRect.y() + rightRect.height());
712 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
713 WebCore::IntPoint startEvasPoint = toEvasTransform.mapPoint(startPoint);
714 WebCore::IntPoint endEvasPoint = toEvasTransform.mapPoint(endPoint);
716 ewkViewTextStyleState(m_viewImpl->view(), startEvasPoint, endEvasPoint);
719 } // namespace WebKit
721 #endif // TIZEN_WEBKIT2_TEXT_SELECTION