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 m_leftHandle->setIsMouseDowned(false);
152 m_rightHandle->setIsMouseDowned(false);
155 m_isTextSelectionMode = isTextSelectionMode;
158 void TextSelection::clear()
160 EditorState editorState = m_viewImpl->page()->editorState();
161 if (!editorState.selectionIsRange)
164 m_viewImpl->page()->selectionRangeClear();
167 void TextSelection::hide()
174 void TextSelection::updateHandlers()
176 WebCore::IntRect leftRect, rightRect;
177 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
180 m_lastLeftHandleRect = leftRect;
181 m_lastRightHandleRect = rightRect;
183 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
184 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
185 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
187 EditorState editorState = m_viewImpl->page()->editorState();
188 if (editorState.isContentEditable) {
189 m_leftHandle->hide();
190 m_rightHandle->hide();
192 WebCore::IntRect editorRect = editorState.editorRect;
193 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
194 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
195 int webViewX, webViewY, webViewWidth, webViewHeight;
197 evas_object_geometry_get(m_viewImpl->view(), &webViewX, &webViewY, &webViewWidth, &webViewHeight);
198 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
199 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
200 m_leftHandle->move(leftEvasPoint);
201 m_leftHandle->show();
204 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
205 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
206 m_rightHandle->move(rightEvasPoint);
207 m_rightHandle->show();
210 m_leftHandle->move(leftEvasPoint);
211 m_leftHandle->show();
213 m_rightHandle->move(rightEvasPoint);
214 m_rightHandle->show();
218 void TextSelection::hideHandlers()
220 m_leftHandle->hide();
221 m_rightHandle->hide();
224 void TextSelection::showMagnifier()
229 void TextSelection::hideMagnifier()
234 void TextSelection::updateMagnifier(const IntPoint& position)
236 m_magnifier->update(position);
237 m_magnifier->move(position);
240 void TextSelection::showContextMenu()
245 EditorState editorState = m_viewImpl->page()->editorState();
246 if (!editorState.selectionIsRange && !editorState.isContentEditable)
249 WebCore::IntPoint point;
250 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
251 bool isPresentInViewPort = false;
253 if (editorState.selectionIsRange) {
254 WebCore::IntRect leftRect, rightRect;
255 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
258 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
259 // Checking if this point is in viewport area. If the calcualated
260 // point/Left/Right point are in view port then draw else do not draw the
261 // context menu. Only draw the selection points.
262 FloatRect unscaledRect = FloatRect(m_viewImpl->pageClient->visibleContentRect());
263 unscaledRect.scale(1 / m_viewImpl->pageClient->scaleFactor());
264 IntRect viewportRect = enclosingIntRect(unscaledRect);
266 WebCore::IntPoint visiblePoint = leftRect.center();
267 if (viewportRect.contains(visiblePoint)) {
268 // First check That the modified points are present in view port
269 point = visiblePoint;
270 isPresentInViewPort = true;
271 } else if (viewportRect.contains(leftRect.location())) {
272 // else if the calculated point is not in the view port area the
273 // draw context menu at left point if visible
274 point = leftRect.location();
275 isPresentInViewPort = true;
276 } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
277 // else if the calculated point is not in the view port area the
278 // draw context menu at right point if visible
279 point = rightRect.maxXMinYCorner();
280 isPresentInViewPort = true;
283 point = leftRect.center();
285 } else if (editorState.isContentEditable) {
286 WebCore::IntRect caretRect;
287 m_viewImpl->page()->getCaretPosition(caretRect);
289 if (caretRect.isEmpty())
292 point = caretRect.center();
293 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
294 isPresentInViewPort = true;
297 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
298 if (!isPresentInViewPort)
302 // show context menu if its in viewport else do not show the contextmenu
304 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_viewImpl->view()));
305 if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
308 point = m_viewImpl->transformToScene().mapPoint(point);
309 Evas* evas = evas_object_evas_get(m_viewImpl->view());
312 Evas_Event_Mouse_Down mouseDown;
313 mouseDown.button = 3;
314 mouseDown.output.x = mouseDown.canvas.x = point.x();
315 mouseDown.output.y = mouseDown.canvas.y = point.y();
317 mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
318 mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
319 mouseDown.flags = EVAS_BUTTON_NONE;
320 mouseDown.timestamp = ecore_time_get() * 1000;
321 mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
323 smartData->api->mouse_down(smartData, &mouseDown);
326 Evas_Event_Mouse_Up mouseUp;
328 mouseUp.output.x = mouseUp.canvas.x = point.x();
329 mouseUp.output.y = mouseUp.canvas.y = point.y();
331 mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
332 mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
333 mouseUp.flags = EVAS_BUTTON_NONE;
334 mouseUp.timestamp = ecore_time_get() * 1000;
335 mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
337 smartData->api->mouse_up(smartData, &mouseUp);
340 void TextSelection::hideContextMenu()
345 m_viewImpl->page()->hideContextMenu();
348 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
350 m_viewImpl->page()->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
354 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
356 m_viewImpl->page()->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
361 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
363 WebCore::IntPoint basePosition;
364 EditorState editorState = m_viewImpl->page()->editorState();
366 if (editorState.selectionIsRange) {
367 WebCore::IntRect leftRect, rightRect;
368 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
373 if (handle->isLeft()) {
374 basePosition.setX(leftRect.x());
375 basePosition.setY(leftRect.y() + (leftRect.height()/2));
377 basePosition.setX(rightRect.x() + rightRect.width());
378 basePosition.setY(rightRect.y() + (rightRect.height()/2));
381 handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
386 updateMagnifier(handle->position());
390 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
392 if (handle->isLeft())
393 setLeftSelectionToEvasPoint(position);
395 setRightSelectionToEvasPoint(position);
397 updateMagnifier(handle->position());
400 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
404 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
405 m_viewImpl->page()->getTextStyleStateForSelection();
409 bool TextSelection::isMagnifierVisible()
411 return m_magnifier->isVisible();
414 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
416 if (isTextSelectionDowned() && !isScrolling) {
422 if (m_viewImpl->gestureClient->isGestureWorking())
432 if (isScrolling && isMagnifierVisible())
436 void TextSelection::startMoveAnimator()
438 if (!isEnabled() || !isTextSelectionDowned())
442 m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
445 void TextSelection::stopMoveAnimator()
447 if (m_moveAnimator) {
448 ecore_animator_del(m_moveAnimator);
453 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
455 static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
458 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
460 TextSelection* textSelection = static_cast<TextSelection*>(data);
462 Evas_Coord_Point point;
463 evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_viewImpl->view()), &point.x, &point.y);
464 textSelection->textSelectionMove(IntPoint(point.x, point.y));
466 return ECORE_CALLBACK_RENEW;
469 // 'return false' means text selection is not possible for point.
470 // 'return true' means text selection is possible.
471 bool TextSelection::textSelectionDown(const IntPoint& point, bool isStartedTextSelectionFromOutside)
473 // text selection should be ignored when longtap on handle from osp
474 if (!isEnabled() && isTextSelectionHandleDowned())
477 #if ENABLE(TIZEN_ISF_PORT)
478 m_viewImpl->inputMethodContext()->resetIMFContext();
480 setIsTextSelectionMode(false);
482 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
483 bool result = m_viewImpl->page()->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
487 if (isTextSelectionMode()) {
490 setIsTextSelectionMode(true);
492 setIsTextSelectionDowned(true);
494 updateMagnifier(point);
502 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
504 // text selection should be ignored when longtap on handle from osp
505 if (!isEnabled() && isTextSelectionHandleDowned())
508 if (!isTextSelectionMode()) {
513 WebCore::IntPoint viewPoint;
514 EditorState editorState = m_viewImpl->page()->editorState();
515 if (editorState.isContentEditable) {
516 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
517 IntPoint updatedPoint = point;
518 if ((point.y() < mapRect.y()) || (point.y() > ((mapRect.y()) + (mapRect.height()))))
519 updatedPoint.setY((mapRect.y()) + ((mapRect.height())/2) );
521 if (point.x() < mapRect.x()) {
522 updatedPoint.setX(mapRect.x());
523 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionBackward))
524 updateMagnifier(updatedPoint);
525 } else if (point.x() > ((mapRect.x()) + (mapRect.width()))) {
526 updatedPoint.setX((mapRect.x()) + (mapRect.width()));
527 if (m_viewImpl->page()->scrollContentByCharacter(point,WebCore::DirectionForward))
528 updateMagnifier(updatedPoint);
530 viewPoint = m_viewImpl->transformFromScene().mapPoint(updatedPoint);
531 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
532 updateMagnifier(updatedPoint);
535 viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
536 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
537 updateMagnifier(point);
542 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
544 // text selection should be ignored when longtap on handle from osp
545 if (!isEnabled() && isTextSelectionHandleDowned())
550 if (!isTextSelectionMode() || !isTextSelectionDowned())
553 setIsTextSelectionDowned(false);
556 EditorState editorState = m_viewImpl->page()->editorState();
557 if (editorState.selectionIsRange || editorState.isContentEditable) {
558 if (editorState.selectionIsRange)
562 } else if (!isStartedTextSelectionFromOutside)
563 setIsTextSelectionMode(false);
565 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
566 m_viewImpl->page()->getTextStyleStateForSelection();
570 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
571 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
573 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
574 if (leftHandleRect.contains(position))
577 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
578 if (rightHandleRect.contains(position))
579 return m_rightHandle;
584 void TextSelection::textSelectionHandleDown(const IntPoint& position)
586 m_selectedHandle = getSelectedHandle(position);
587 if (m_selectedHandle)
588 m_selectedHandle->mouseDown(position);
590 m_leftHandle->setIsMouseDowned(false);
591 m_rightHandle->setIsMouseDowned(false);
595 void TextSelection::textSelectionHandleMove(const IntPoint& position)
597 if (m_selectedHandle && isTextSelectionHandleDowned())
598 m_selectedHandle->mouseMove(position);
601 void TextSelection::textSelectionHandleUp()
603 if (m_selectedHandle && isTextSelectionHandleDowned()) {
604 m_selectedHandle->mouseUp();
605 m_selectedHandle = 0;
610 bool TextSelection::isEnabled()
612 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_viewImpl->view()));
615 bool TextSelection::isAutomaticClearEnabled()
617 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_viewImpl->view()));
620 void TextSelection::requestToShow()
623 ecore_timer_del(m_showTimer);
624 m_showTimer = ecore_timer_loop_add((double)200.0/1000.0, showTimerCallback, this);
627 Eina_Bool TextSelection::showTimerCallback(void* data)
629 TextSelection* textSelection = static_cast<TextSelection*>(data);
630 textSelection->showHandlesAndContextMenu();
632 return ECORE_CALLBACK_RENEW;
635 void TextSelection::showHandlesAndContextMenu()
637 WebCore::IntRect leftRect, rightRect;
638 if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
639 if ((leftRect == m_lastLeftHandleRect) && (rightRect == m_lastRightHandleRect)) {
641 ecore_timer_del(m_showTimer);
645 updateHandlesAndContextMenu(true);
648 m_lastLeftHandleRect = leftRect;
649 m_lastRightHandleRect = rightRect;
653 } // namespace WebKit
655 #endif // TIZEN_WEBKIT2_TEXT_SELECTION