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 (isTextSelectionMode()) {
103 if (!editorState.selectionIsRange) {
104 if (editorState.isContentEditable && !evas_object_focus_get(m_viewImpl->view())) {
105 WebCore::IntRect caretRect;
106 m_viewImpl->page()->getCaretPosition(caretRect);
107 if (!caretRect.isEmpty())
110 WebCore::IntRect leftRect;
111 WebCore::IntRect rightRect;
112 if (isTextSelectionDowned() || m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
115 setIsTextSelectionMode(false);
117 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
118 m_viewImpl->page()->getTextStyleStateForSelection();
121 if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
124 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
125 m_viewImpl->page()->getTextStyleStateForSelection();
130 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
132 if (editorState.isContentEditable && !editorState.selectionIsRange) {
133 WebCore::IntRect caretRect;
134 m_viewImpl->page()->getCaretPosition(caretRect);
135 if (!caretRect.isEmpty()) {
136 m_viewImpl->page()->getTextStyleStateForSelection();
143 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
145 if (!isAutomaticClearEnabled())
148 if (!isTextSelectionMode) {
151 initHandlesMouseDownedStatus();
154 m_isTextSelectionMode = isTextSelectionMode;
157 void TextSelection::clear()
159 EditorState editorState = m_viewImpl->page()->editorState();
160 if (!editorState.selectionIsRange)
163 m_viewImpl->page()->selectionRangeClear();
166 void TextSelection::hide()
173 void TextSelection::updateHandlers()
175 WebCore::IntRect leftRect, rightRect;
176 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
179 m_lastLeftHandleRect = leftRect;
180 m_lastRightHandleRect = rightRect;
182 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
183 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
184 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
186 EditorState editorState = m_viewImpl->page()->editorState();
187 if (editorState.isContentEditable) {
188 m_leftHandle->hide();
189 m_rightHandle->hide();
191 WebCore::IntRect editorRect = editorState.editorRect;
192 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
193 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
194 int webViewX, webViewY, webViewWidth, webViewHeight;
196 evas_object_geometry_get(m_viewImpl->view(), &webViewX, &webViewY, &webViewWidth, &webViewHeight);
197 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
198 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
199 m_leftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
200 m_leftHandle->show();
203 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
204 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
205 m_rightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
206 m_rightHandle->show();
209 m_leftHandle->move(leftEvasPoint, m_viewImpl->transformToScene().mapRect(leftRect));
210 m_leftHandle->show();
212 m_rightHandle->move(rightEvasPoint, m_viewImpl->transformToScene().mapRect(rightRect));
213 m_rightHandle->show();
217 void TextSelection::hideHandlers()
219 m_leftHandle->hide();
220 m_rightHandle->hide();
223 void TextSelection::showMagnifier()
228 void TextSelection::hideMagnifier()
233 void TextSelection::updateMagnifier(const IntPoint& position)
235 m_magnifier->update(position);
236 m_magnifier->move(position);
239 void TextSelection::showContextMenu()
244 EditorState editorState = m_viewImpl->page()->editorState();
245 if (!editorState.selectionIsRange && !editorState.isContentEditable)
248 WebCore::IntPoint point;
249 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
250 bool isPresentInViewPort = false;
252 if (editorState.selectionIsRange) {
253 WebCore::IntRect leftRect, rightRect;
254 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
257 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
258 // Checking if this point is in viewport area. If the calcualated
259 // point/Left/Right point are in view port then draw else do not draw the
260 // context menu. Only draw the selection points.
261 FloatRect unscaledRect = FloatRect(m_viewImpl->pageClient->visibleContentRect());
262 unscaledRect.scale(1 / m_viewImpl->pageClient->scaleFactor());
263 IntRect viewportRect = enclosingIntRect(unscaledRect);
265 WebCore::IntPoint visiblePoint = leftRect.center();
266 if (viewportRect.contains(visiblePoint)) {
267 // First check That the modified points are present in view port
268 point = visiblePoint;
269 isPresentInViewPort = true;
270 } else if (viewportRect.contains(leftRect.location())) {
271 // else if the calculated point is not in the view port area the
272 // draw context menu at left point if visible
273 point = leftRect.location();
274 isPresentInViewPort = true;
275 } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
276 // else if the calculated point is not in the view port area the
277 // draw context menu at right point if visible
278 point = rightRect.maxXMinYCorner();
279 isPresentInViewPort = true;
282 if (isPresentInViewPort && editorState.isContentEditable) {
283 // In case of single line editor box.
284 if (leftRect == rightRect) {
285 // draw context menu at center point of visible selection range in the editor box
286 IntRect editorRect = editorState.editorRect;
287 leftRect.intersect(editorRect);
288 if (!leftRect.isEmpty())
289 point = leftRect.center();
291 // not draw context menu if there is no visible selection range in the editor box
292 isPresentInViewPort = false;
297 point = leftRect.center();
299 } else if (editorState.isContentEditable) {
300 WebCore::IntRect caretRect;
301 m_viewImpl->page()->getCaretPosition(caretRect);
303 if (caretRect.isEmpty())
306 point = caretRect.center();
307 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
308 isPresentInViewPort = true;
311 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
312 if (!isPresentInViewPort)
316 // show context menu if its in viewport else do not show the contextmenu
318 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_viewImpl->view()));
319 if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
322 point = m_viewImpl->transformToScene().mapPoint(point);
323 Evas* evas = evas_object_evas_get(m_viewImpl->view());
326 Evas_Event_Mouse_Down mouseDown;
327 mouseDown.button = 3;
328 mouseDown.output.x = mouseDown.canvas.x = point.x();
329 mouseDown.output.y = mouseDown.canvas.y = point.y();
331 mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
332 mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
333 mouseDown.flags = EVAS_BUTTON_NONE;
334 mouseDown.timestamp = ecore_time_get() * 1000;
335 mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
337 smartData->api->mouse_down(smartData, &mouseDown);
340 Evas_Event_Mouse_Up mouseUp;
342 mouseUp.output.x = mouseUp.canvas.x = point.x();
343 mouseUp.output.y = mouseUp.canvas.y = point.y();
345 mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
346 mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
347 mouseUp.flags = EVAS_BUTTON_NONE;
348 mouseUp.timestamp = ecore_time_get() * 1000;
349 mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
351 smartData->api->mouse_up(smartData, &mouseUp);
354 void TextSelection::hideContextMenu()
359 m_viewImpl->page()->hideContextMenu();
362 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
364 m_viewImpl->page()->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
368 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
370 m_viewImpl->page()->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
375 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
377 WebCore::IntPoint basePosition;
378 EditorState editorState = m_viewImpl->page()->editorState();
380 if (editorState.selectionIsRange) {
381 WebCore::IntRect leftRect, rightRect;
382 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
387 if (handle->isLeft()) {
388 basePosition.setX(leftRect.x());
389 basePosition.setY(leftRect.y() + (leftRect.height()/2));
391 basePosition.setX(rightRect.x() + rightRect.width());
392 basePosition.setY(rightRect.y() + (rightRect.height()/2));
395 handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
400 updateMagnifier(handle->position());
404 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
406 if (handle->isLeft())
407 setLeftSelectionToEvasPoint(position);
409 setRightSelectionToEvasPoint(position);
411 updateMagnifier(handle->position());
414 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
418 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
419 m_viewImpl->page()->getTextStyleStateForSelection();
423 bool TextSelection::isMagnifierVisible()
425 return m_magnifier->isVisible();
428 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
430 if (isTextSelectionDowned() && !isScrolling) {
435 EditorState editorState = m_viewImpl->page()->editorState();
436 if (!editorState.selectionIsRange && editorState.isContentEditable)
437 setIsTextSelectionMode(false);
440 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
441 if (m_viewImpl->pageClient->isClipboardWindowOpened())
444 if (m_viewImpl->gestureClient->isGestureWorking())
454 if (isScrolling && isMagnifierVisible())
458 void TextSelection::startMoveAnimator()
460 if (!isEnabled() || !isTextSelectionDowned())
464 m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
467 void TextSelection::stopMoveAnimator()
469 if (m_moveAnimator) {
470 ecore_animator_del(m_moveAnimator);
475 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
477 static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
480 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
482 TextSelection* textSelection = static_cast<TextSelection*>(data);
484 Evas_Coord_Point point;
485 evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_viewImpl->view()), &point.x, &point.y);
486 textSelection->textSelectionMove(IntPoint(point.x, point.y));
488 return ECORE_CALLBACK_RENEW;
491 // 'return false' means text selection is not possible for point.
492 // 'return true' means text selection is possible.
493 bool TextSelection::textSelectionDown(const IntPoint& point, bool isStartedTextSelectionFromOutside)
495 // text selection should be ignored when longtap on handle from osp
496 if (!isEnabled() && isTextSelectionHandleDowned())
499 #if ENABLE(TIZEN_ISF_PORT)
500 m_viewImpl->inputMethodContext()->resetIMFContext();
502 setIsTextSelectionMode(false);
504 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
505 bool result = m_viewImpl->page()->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
509 if (isTextSelectionMode()) {
512 setIsTextSelectionMode(true);
514 setIsTextSelectionDowned(true);
516 updateMagnifier(point);
524 static int s_textSelectionMargin = 5;
526 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
528 // text selection should be ignored when longtap on handle from osp
529 if (!isEnabled() && isTextSelectionHandleDowned())
532 if (!isTextSelectionMode()) {
537 WebCore::IntPoint viewPoint;
538 EditorState editorState = m_viewImpl->page()->editorState();
539 if (editorState.isContentEditable) {
540 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
541 IntPoint updatedPoint = point;
542 bool scrolledY = false;
543 if (point.y() < mapRect.y()) {
544 updatedPoint.setY(mapRect.y() + s_textSelectionMargin);
545 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionBackward)) {
547 updateMagnifier(updatedPoint);
549 } else if (point.y() > mapRect.maxY()) {
550 updatedPoint.setY(mapRect.maxY() - s_textSelectionMargin);
551 if (m_viewImpl->page()->scrollContentByLine(point,WebCore::DirectionForward)) {
553 updateMagnifier(updatedPoint);
557 bool scrolledX = false;
558 if (point.x() < mapRect.x()) {
559 updatedPoint.setX(mapRect.x() + s_textSelectionMargin);
560 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionBackward)) {
562 updateMagnifier(updatedPoint);
564 } else if (point.x() > mapRect.maxX()) {
565 updatedPoint.setX(mapRect.maxX() - s_textSelectionMargin);
566 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionForward)) {
568 updateMagnifier(updatedPoint);
572 if (!scrolledX && !scrolledY) {
573 viewPoint = m_viewImpl->transformFromScene().mapPoint(updatedPoint);
574 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
575 updateMagnifier(updatedPoint);
578 viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
579 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
580 updateMagnifier(point);
585 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
587 // text selection should be ignored when longtap on handle from osp
588 if (!isEnabled() && isTextSelectionHandleDowned())
593 if (!isTextSelectionMode() || !isTextSelectionDowned())
596 setIsTextSelectionDowned(false);
599 EditorState editorState = m_viewImpl->page()->editorState();
600 if (editorState.selectionIsRange || editorState.isContentEditable) {
601 if (editorState.selectionIsRange)
605 } else if (!isStartedTextSelectionFromOutside)
606 setIsTextSelectionMode(false);
608 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
609 m_viewImpl->page()->getTextStyleStateForSelection();
613 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
614 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
616 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
617 if (!leftHandleRect.isEmpty() && leftHandleRect.contains(position))
620 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
621 if (!rightHandleRect.isEmpty() && rightHandleRect.contains(position))
622 return m_rightHandle;
627 void TextSelection::textSelectionHandleDown(const IntPoint& position)
629 m_selectedHandle = getSelectedHandle(position);
630 if (m_selectedHandle)
631 m_selectedHandle->mouseDown(position);
633 initHandlesMouseDownedStatus();
636 void TextSelection::textSelectionHandleMove(const IntPoint& position)
638 if (m_selectedHandle && isTextSelectionHandleDowned())
639 m_selectedHandle->mouseMove(position);
642 void TextSelection::textSelectionHandleUp()
644 if (m_selectedHandle && isTextSelectionHandleDowned()) {
645 m_selectedHandle->mouseUp();
646 m_selectedHandle = 0;
651 bool TextSelection::isEnabled()
653 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_viewImpl->view()));
656 bool TextSelection::isAutomaticClearEnabled()
658 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_viewImpl->view()));
661 void TextSelection::requestToShow()
664 ecore_timer_del(m_showTimer);
665 m_showTimer = ecore_timer_loop_add((double)200.0/1000.0, showTimerCallback, this);
668 Eina_Bool TextSelection::showTimerCallback(void* data)
670 TextSelection* textSelection = static_cast<TextSelection*>(data);
671 textSelection->showHandlesAndContextMenu();
673 return ECORE_CALLBACK_RENEW;
676 void TextSelection::showHandlesAndContextMenu()
678 WebCore::IntRect leftRect, rightRect;
679 if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
680 if ((leftRect == m_lastLeftHandleRect) && (rightRect == m_lastRightHandleRect)) {
682 ecore_timer_del(m_showTimer);
686 updateHandlesAndContextMenu(true);
689 m_lastLeftHandleRect = leftRect;
690 m_lastRightHandleRect = rightRect;
694 void TextSelection::initHandlesMouseDownedStatus()
696 m_leftHandle->setIsMouseDowned(false);
697 m_rightHandle->setIsMouseDowned(false);
700 void TextSelection::changeContextMenuPosition(WebCore::IntPoint& position)
702 if (m_leftHandle->isTop()) {
703 IntRect handleRect = m_leftHandle->getHandleRect();
704 position.setY(position.y() - handleRect.height());
707 } // namespace WebKit
709 #endif // TIZEN_WEBKIT2_TEXT_SELECTION