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 "NativeWebMouseEvent.h"
34 #include <Elementary.h>
36 using namespace WebCore;
40 TextSelection::TextSelection(Evas_Object* viewWidget, WebPageProxy* page, PageClientImpl* pageClient)
41 : m_object(viewWidget)
43 , m_pageClient(pageClient)
44 , m_isTextSelectionDowned(false)
45 , m_isTextSelectionMode(false)
50 m_viewImpl = EwkViewImpl::fromEvasObject(m_object);
52 const Eina_List* defaultThemeList = elm_theme_list_get(0);
56 EINA_LIST_FOREACH(defaultThemeList, l, theme) {
57 char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
60 m_leftHandle = new TextSelectionHandle(m_object, themePath, "elm/entry/selection/block_handle_left", true, this);
61 m_rightHandle = new TextSelectionHandle(m_object, themePath, "elm/entry/selection/block_handle_right", false, this);
68 m_magnifier = new TextSelectionMagnifier(m_object, page, pageClient);
70 evas_object_event_callback_add(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
73 TextSelection::~TextSelection()
83 evas_object_event_callback_del(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp);
86 void TextSelection::update()
88 if (isTextSelectionMode() ) {
89 EditorState editorState = m_page->editorState();
90 if (!editorState.selectionIsRange) {
91 if (editorState.isContentEditable && !evas_object_focus_get(m_object)) {
92 WebCore::IntRect caretRect;
93 m_page->getCaretPosition(caretRect);
94 if (!caretRect.isEmpty())
97 WebCore::IntRect leftRect;
98 WebCore::IntRect rightRect;
99 if (isTextSelectionDowned() || m_page->getSelectionHandlers(leftRect, rightRect))
102 setIsTextSelectionMode(false);
104 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
105 m_page->getTextStyleStateForSelection();
108 if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
111 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
112 m_page->getTextStyleStateForSelection();
117 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
119 EditorState editorState = m_page->editorState();
120 if (editorState.isContentEditable && !editorState.selectionIsRange) {
121 WebCore::IntRect caretRect;
122 m_page->getCaretPosition(caretRect);
123 if (!caretRect.isEmpty()) {
124 m_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_page->editorState();
147 if (!editorState.selectionIsRange)
150 m_page->selectionRangeClear();
153 void TextSelection::hide()
160 void TextSelection::updateHandlers()
162 WebCore::IntRect leftRect, rightRect;
163 if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
168 AffineTransform toEvasTransform = m_viewImpl->transformToScene();
169 WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
170 WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
172 EditorState editorState = m_page->editorState();
173 if (editorState.isContentEditable) {
174 m_leftHandle->hide();
175 m_rightHandle->hide();
177 WebCore::IntRect editorRect = editorState.editorRect;
178 WebCore::IntPoint editorLeftEvasPoint = toEvasTransform.mapPoint(editorRect.location());
179 WebCore::IntPoint editorRightEvasPoint = toEvasTransform.mapPoint(editorRect.maxXMaxYCorner());
180 int webViewX, webViewY, webViewWidth, webViewHeight;
182 evas_object_geometry_get(m_object, &webViewX, &webViewY, &webViewWidth, &webViewHeight);
183 if ((editorLeftEvasPoint.x() <= leftEvasPoint.x() && editorLeftEvasPoint.y() <= leftEvasPoint.y())
184 && (webViewX <= leftEvasPoint.x() && webViewY <= leftEvasPoint.y())) {
185 m_leftHandle->move(leftEvasPoint);
186 m_leftHandle->show();
189 if ((editorRightEvasPoint.x() >= rightEvasPoint.x() && editorRightEvasPoint.y() >= rightEvasPoint.y())
190 && ((webViewX + webViewWidth) >= rightEvasPoint.x() && (webViewY <= rightEvasPoint.y() && (webViewY + webViewHeight) >= rightEvasPoint.y()))) {
191 m_rightHandle->move(rightEvasPoint);
192 m_rightHandle->show();
195 m_leftHandle->move(leftEvasPoint);
196 m_leftHandle->show();
198 m_rightHandle->move(rightEvasPoint);
199 m_rightHandle->show();
203 void TextSelection::hideHandlers()
205 m_leftHandle->hide();
206 m_rightHandle->hide();
209 void TextSelection::showMagnifier()
214 void TextSelection::hideMagnifier()
219 void TextSelection::updateMagnifier(const IntPoint& position)
221 m_magnifier->update(position);
222 m_magnifier->move(position);
225 void TextSelection::showContextMenu()
230 EditorState editorState = m_page->editorState();
231 if (!editorState.selectionIsRange && !editorState.isContentEditable)
234 WebCore::IntPoint point;
235 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
236 bool isPresentInViewPort = false;
238 if (editorState.selectionIsRange) {
239 WebCore::IntRect leftRect, rightRect;
240 if (!m_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_pageClient->visibleContentRect());
249 unscaledRect.scale(1 / m_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_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_object));
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_object);
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_page->hideContextMenu();
334 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
336 m_page->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
340 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
342 m_page->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
347 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
349 WebCore::IntPoint basePosition;
350 EditorState editorState = m_page->editorState();
352 if (editorState.selectionIsRange) {
353 WebCore::IntRect leftRect, rightRect;
354 if (!m_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_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_object), &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 setIsTextSelectionMode(false);
458 IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
459 bool result = m_page->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
463 if (isTextSelectionMode()) {
466 setIsTextSelectionMode(true);
468 setIsTextSelectionDowned(true);
470 updateMagnifier(point);
478 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
480 if (!isTextSelectionMode()) {
485 WebCore::IntPoint viewPoint = m_viewImpl->transformFromScene().mapPoint(point);
486 EditorState editorState = m_page->editorState();
487 if (editorState.isContentEditable) {
488 IntRect mapRect = m_viewImpl->transformToScene().mapRect(editorState.editorRect);
489 if(mapRect.contains(point)) {
490 m_page->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
491 updateMagnifier(point);
494 m_page->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
495 updateMagnifier(point);
500 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
504 if (!isTextSelectionMode() || !isTextSelectionDowned())
507 setIsTextSelectionDowned(false);
510 EditorState editorState = m_page->editorState();
511 if (editorState.selectionIsRange || editorState.isContentEditable) {
512 if (editorState.selectionIsRange)
516 } else if (!isStartedTextSelectionFromOutside)
517 setIsTextSelectionMode(false);
519 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
520 m_page->getTextStyleStateForSelection();
524 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
525 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
527 WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
528 if (leftHandleRect.contains(position))
531 WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
532 if (rightHandleRect.contains(position))
533 return m_rightHandle;
538 void TextSelection::textSelectionHandleDown(const IntPoint& position)
540 TextSelectionHandle* selectedHandle = getSelectedHandle(position);
541 if (selectedHandle) {
542 selectedHandle->mouseDown(position);
543 if (selectedHandle->isMouseDowned())
547 EditorState editorState = m_page->editorState();
548 if (editorState.isContentEditable)
549 setIsTextSelectionMode(false);
552 void TextSelection::textSelectionHandleMove(const IntPoint& position)
554 if (isTextSelectionHandleDowned()) {
555 TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
556 selectedHandle->mouseMove(position);
560 void TextSelection::textSelectionHandleUp()
562 if (isTextSelectionHandleDowned()) {
563 TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
564 selectedHandle->mouseUp();
569 bool TextSelection::isEnabled()
571 return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_object));
574 bool TextSelection::isAutomaticClearEnabled()
576 return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_object));
579 } // namespace WebKit
581 #endif // TIZEN_WEBKIT2_TEXT_SELECTION