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)
50 const Eina_List* defaultThemeList = elm_theme_list_get(0);
54 EINA_LIST_FOREACH(defaultThemeList, l, theme) {
55 char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
58 m_leftHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_left", true, this);
59 m_rightHandle = new TextSelectionHandle(m_viewImpl->view(), themePath, "elm/entry/selection/block_handle_right", false, this);
66 m_magnifier = new TextSelectionMagnifier(m_viewImpl);
68 evas_object_event_callback_add(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
71 TextSelection::~TextSelection()
81 evas_object_event_callback_del(m_viewImpl->view(), EVAS_CALLBACK_MOUSE_UP, onMouseUp);
84 void TextSelection::update()
86 EditorState editorState = m_viewImpl->page()->editorState();
87 if (editorState.updateEditorRectOnly)
90 if (isTextSelectionMode()) {
91 if (!editorState.selectionIsRange) {
92 if (editorState.isContentEditable && !evas_object_focus_get(m_viewImpl->view())) {
93 WebCore::IntRect caretRect;
94 m_viewImpl->page()->getCaretPosition(caretRect);
95 if (!caretRect.isEmpty())
98 WebCore::IntRect leftRect;
99 WebCore::IntRect rightRect;
100 if (isTextSelectionDowned() || m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
103 setIsTextSelectionMode(false);
105 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
106 m_viewImpl->page()->getTextStyleStateForSelection();
109 if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
112 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
113 m_viewImpl->page()->getTextStyleStateForSelection();
118 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
120 if (editorState.isContentEditable && !editorState.selectionIsRange) {
121 WebCore::IntRect caretRect;
122 m_viewImpl->page()->getCaretPosition(caretRect);
123 if (!caretRect.isEmpty()) {
124 m_viewImpl->page()->getTextStyleStateForSelection();
131 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
133 if (!isAutomaticClearEnabled())
136 if (!isTextSelectionMode) {
141 m_isTextSelectionMode = isTextSelectionMode;
144 void TextSelection::clear()
146 EditorState editorState = m_viewImpl->page()->editorState();
147 if (!editorState.selectionIsRange)
150 m_viewImpl->page()->selectionRangeClear();
153 void TextSelection::hide()
160 void TextSelection::updateHandlers()
162 WebCore::IntRect leftRect, rightRect;
163 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
166 m_lastLeftHandleRect = leftRect;
167 m_lastRightHandleRect = rightRect;
169 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
170 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
171 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
173 EditorState editorState = m_viewImpl->page()->editorState();
174 if (editorState.isContentEditable) {
175 m_leftHandle->hide();
176 m_rightHandle->hide();
178 WebCore::IntRect editorRect = editorState.editorRect;
179 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
180 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
181 int webViewX, webViewY, webViewWidth, webViewHeight;
183 evas_object_geometry_get(m_viewImpl->view(), &webViewX, &webViewY, &webViewWidth, &webViewHeight);
184 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
185 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
186 m_leftHandle->move(leftEvasPoint);
187 m_leftHandle->show();
190 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
191 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
192 m_rightHandle->move(rightEvasPoint);
193 m_rightHandle->show();
196 m_leftHandle->move(leftEvasPoint);
197 m_leftHandle->show();
199 m_rightHandle->move(rightEvasPoint);
200 m_rightHandle->show();
204 void TextSelection::hideHandlers()
206 m_leftHandle->hide();
207 m_rightHandle->hide();
210 void TextSelection::showMagnifier()
215 void TextSelection::hideMagnifier()
220 void TextSelection::updateMagnifier(const IntPoint& position)
222 m_magnifier->update(position);
223 m_magnifier->move(position);
226 void TextSelection::showContextMenu()
231 EditorState editorState = m_viewImpl->page()->editorState();
232 if (!editorState.selectionIsRange && !editorState.isContentEditable)
235 WebCore::IntPoint point;
236 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
237 bool isPresentInViewPort = false;
239 if (editorState.selectionIsRange) {
240 WebCore::IntRect leftRect, rightRect;
241 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect))
244 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
245 // Checking if this point is in viewport area. If the calcualated
246 // point/Left/Right point are in view port then draw else do not draw the
247 // context menu. Only draw the selection points.
248 FloatRect unscaledRect = FloatRect(m_viewImpl->pageClient->visibleContentRect());
249 unscaledRect.scale(1 / m_viewImpl->pageClient->scaleFactor());
250 IntRect viewportRect = enclosingIntRect(unscaledRect);
252 WebCore::IntPoint visiblePoint = leftRect.center();
253 if (viewportRect.contains(visiblePoint)) {
254 // First check That the modified points are present in view port
255 point = visiblePoint;
256 isPresentInViewPort = true;
257 } else if (viewportRect.contains(leftRect.location())) {
258 // else if the calculated point is not in the view port area the
259 // draw context menu at left point if visible
260 point = leftRect.location();
261 isPresentInViewPort = true;
262 } else if (viewportRect.contains(rightRect.maxXMinYCorner())) {
263 // else if the calculated point is not in the view port area the
264 // draw context menu at right point if visible
265 point = rightRect.maxXMinYCorner();
266 isPresentInViewPort = true;
269 point = leftRect.center();
271 } else if (editorState.isContentEditable) {
272 WebCore::IntRect caretRect;
273 m_viewImpl->page()->getCaretPosition(caretRect);
275 if (caretRect.isEmpty())
278 point = caretRect.center();
279 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
280 isPresentInViewPort = true;
283 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
284 if (!isPresentInViewPort)
288 // show context menu if its in viewport else do not show the contextmenu
290 Ewk_View_Smart_Data* smartData = static_cast<Ewk_View_Smart_Data*>(evas_object_smart_data_get(m_viewImpl->view()));
291 if (!smartData || !smartData->api || !smartData->api->mouse_down || !smartData->api->mouse_up)
294 point = m_viewImpl->transformToScene().mapPoint(point);
295 Evas* evas = evas_object_evas_get(m_viewImpl->view());
298 Evas_Event_Mouse_Down mouseDown;
299 mouseDown.button = 3;
300 mouseDown.output.x = mouseDown.canvas.x = point.x();
301 mouseDown.output.y = mouseDown.canvas.y = point.y();
303 mouseDown.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
304 mouseDown.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
305 mouseDown.flags = EVAS_BUTTON_NONE;
306 mouseDown.timestamp = ecore_time_get() * 1000;
307 mouseDown.event_flags = EVAS_EVENT_FLAG_NONE;
309 smartData->api->mouse_down(smartData, &mouseDown);
312 Evas_Event_Mouse_Up mouseUp;
314 mouseUp.output.x = mouseUp.canvas.x = point.x();
315 mouseUp.output.y = mouseUp.canvas.y = point.y();
317 mouseUp.modifiers = const_cast<Evas_Modifier*>(evas_key_modifier_get(evas));
318 mouseUp.locks = const_cast<Evas_Lock*>(evas_key_lock_get(evas));
319 mouseUp.flags = EVAS_BUTTON_NONE;
320 mouseUp.timestamp = ecore_time_get() * 1000;
321 mouseUp.event_flags = EVAS_EVENT_FLAG_NONE;
323 smartData->api->mouse_up(smartData, &mouseUp);
326 void TextSelection::hideContextMenu()
331 m_viewImpl->page()->hideContextMenu();
334 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
336 m_viewImpl->page()->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
340 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
342 m_viewImpl->page()->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
347 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
349 WebCore::IntPoint basePosition;
350 EditorState editorState = m_viewImpl->page()->editorState();
352 if (editorState.selectionIsRange) {
353 WebCore::IntRect leftRect, rightRect;
354 if (!m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
359 if (handle->isLeft()) {
360 basePosition.setX(leftRect.x());
361 basePosition.setY(leftRect.y() + (leftRect.height()/2));
363 basePosition.setX(rightRect.x() + rightRect.width());
364 basePosition.setY(rightRect.y() + (rightRect.height()/2));
367 handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
372 updateMagnifier(handle->position());
376 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
378 if (handle->isLeft())
379 setLeftSelectionToEvasPoint(position);
381 setRightSelectionToEvasPoint(position);
383 updateMagnifier(handle->position());
386 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
390 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
391 m_viewImpl->page()->getTextStyleStateForSelection();
395 bool TextSelection::isMagnifierVisible()
397 return m_magnifier->isVisible();
400 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
402 if (isTextSelectionDowned() && !isScrolling) {
415 if (isScrolling && isMagnifierVisible())
419 void TextSelection::startMoveAnimator()
421 if (!isEnabled() || !isTextSelectionDowned())
425 m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
428 void TextSelection::stopMoveAnimator()
430 if (m_moveAnimator) {
431 ecore_animator_del(m_moveAnimator);
436 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
438 static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
441 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
443 TextSelection* textSelection = static_cast<TextSelection*>(data);
445 Evas_Coord_Point point;
446 evas_pointer_canvas_xy_get(evas_object_evas_get(textSelection->m_viewImpl->view()), &point.x, &point.y);
447 textSelection->textSelectionMove(IntPoint(point.x, point.y));
449 return ECORE_CALLBACK_RENEW;
452 // 'return false' means text selection is not possible for point.
453 // 'return true' means text selection is possible.
454 bool TextSelection::textSelectionDown(const IntPoint& point, bool isStartedTextSelectionFromOutside)
456 // text selection should be ignored when longtap on handle from osp
457 if (!isEnabled() && isTextSelectionHandleDowned())
460 setIsTextSelectionMode(false);
462 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
463 bool result = m_viewImpl->page()->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
467 if (isTextSelectionMode()) {
470 setIsTextSelectionMode(true);
472 setIsTextSelectionDowned(true);
474 updateMagnifier(point);
482 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
484 // text selection should be ignored when longtap on handle from osp
485 if (!isEnabled() && isTextSelectionHandleDowned())
488 if (!isTextSelectionMode()) {
493 WebCore::IntPoint viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
494 EditorState editorState = m_viewImpl->page()->editorState();
495 if (editorState.isContentEditable) {
496 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
497 if(mapRect.contains(point)) {
498 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
499 updateMagnifier(point);
502 m_viewImpl->page()->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
503 updateMagnifier(point);
508 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
510 // text selection should be ignored when longtap on handle from osp
511 if (!isEnabled() && isTextSelectionHandleDowned())
516 if (!isTextSelectionMode() || !isTextSelectionDowned())
519 setIsTextSelectionDowned(false);
522 EditorState editorState = m_viewImpl->page()->editorState();
523 if (editorState.selectionIsRange || editorState.isContentEditable) {
524 if (editorState.selectionIsRange)
528 } else if (!isStartedTextSelectionFromOutside)
529 setIsTextSelectionMode(false);
531 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
532 m_viewImpl->page()->getTextStyleStateForSelection();
536 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
537 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
539 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
540 if (leftHandleRect.contains(position))
543 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
544 if (rightHandleRect.contains(position))
545 return m_rightHandle;
550 void TextSelection::textSelectionHandleDown(const IntPoint& position)
552 TextSelectionHandle* selectedHandle = getSelectedHandle(position);
553 if (selectedHandle) {
554 selectedHandle->mouseDown(position);
555 if (selectedHandle->isMouseDowned())
560 void TextSelection::textSelectionHandleMove(const IntPoint& position)
562 if (isTextSelectionHandleDowned()) {
563 TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
564 selectedHandle->mouseMove(position);
568 void TextSelection::textSelectionHandleUp()
570 if (isTextSelectionHandleDowned()) {
571 TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
572 selectedHandle->mouseUp();
577 bool TextSelection::isEnabled()
579 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_viewImpl->view()));
582 bool TextSelection::isAutomaticClearEnabled()
584 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_viewImpl->view()));
587 void TextSelection::requestToShow()
590 ecore_timer_del(m_showTimer);
591 m_showTimer = ecore_timer_loop_add((double)200.0/1000.0, showTimerCallback, this);
594 Eina_Bool TextSelection::showTimerCallback(void* data)
596 TextSelection* textSelection = static_cast<TextSelection*>(data);
597 textSelection->showHandlesAndContextMenu();
599 return ECORE_CALLBACK_RENEW;
602 void TextSelection::showHandlesAndContextMenu()
604 WebCore::IntRect leftRect, rightRect;
605 if (m_viewImpl->page()->getSelectionHandlers(leftRect, rightRect)) {
606 if ((leftRect == m_lastLeftHandleRect) && (rightRect == m_lastRightHandleRect)) {
608 ecore_timer_del(m_showTimer);
612 updateHandlesAndContextMenu(true);
615 m_lastLeftHandleRect = leftRect;
616 m_lastRightHandleRect = rightRect;
620 } // namespace WebKit
622 #endif // TIZEN_WEBKIT2_TEXT_SELECTION