Merge "[Cherry-pick] Refactor WrapShape classes to BasicShape" into tizen_2.1
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / TextSelection.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27
28 #if ENABLE(TIZEN_WEBKIT2_TEXT_SELECTION)
29 #include "TextSelection.h"
30
31 #include "EditorState.h"
32 #include "NativeWebMouseEvent.h"
33 #include "ewk_view.h"
34 #include <Elementary.h>
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 TextSelection::TextSelection(Evas_Object* viewWidget, WebPageProxy* page, PageClientImpl* pageClient)
41       : m_object(viewWidget)
42       , m_page(page)
43       , m_pageClient(pageClient)
44       , m_isTextSelectionDowned(false)
45       , m_isTextSelectionMode(false)
46       , m_moveAnimator(0)
47 {
48     ASSERT(viewWidget);
49
50     m_viewImpl = EwkViewImpl::fromEvasObject(m_object);
51
52     const Eina_List* defaultThemeList = elm_theme_list_get(0);
53
54     const Eina_List* l;
55     void* theme;
56     EINA_LIST_FOREACH(defaultThemeList, l, theme) {
57         char* themePath = elm_theme_list_item_path_get((const char*)theme, 0);
58
59         if (themePath) {
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);
62
63             free(themePath);
64             break;
65         }
66     }
67
68     m_magnifier = new TextSelectionMagnifier(m_object, page, pageClient);
69
70     evas_object_event_callback_add(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
71 }
72
73 TextSelection::~TextSelection()
74 {
75     if (m_leftHandle)
76         delete m_leftHandle;
77
78     if (m_rightHandle)
79         delete m_rightHandle;
80
81     delete m_magnifier;
82
83     evas_object_event_callback_del(m_object, EVAS_CALLBACK_MOUSE_UP, onMouseUp);
84 }
85
86 void TextSelection::update()
87 {
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())
95                     return;
96             } else {
97                 WebCore::IntRect leftRect;
98                 WebCore::IntRect rightRect;
99                 if (isTextSelectionDowned() || m_page->getSelectionHandlers(leftRect, rightRect))
100                     return;
101
102                 setIsTextSelectionMode(false);
103             }
104 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
105             m_page->getTextStyleStateForSelection();
106 #endif
107         } else {
108             if (!isTextSelectionDowned() && !isTextSelectionHandleDowned()) {
109                 updateHandlers();
110                 showContextMenu();
111 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
112                 m_page->getTextStyleStateForSelection();
113 #endif
114             }
115         }
116     }
117 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
118     else {
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();
125             }
126         }
127     }
128 #endif
129 }
130
131 void TextSelection::setIsTextSelectionMode(bool isTextSelectionMode)
132 {
133     if (!isAutomaticClearEnabled())
134         return;
135
136     if (!isTextSelectionMode) {
137         hide();
138         clear();
139     }
140
141     m_isTextSelectionMode = isTextSelectionMode;
142 }
143
144 void TextSelection::clear()
145 {
146     EditorState editorState = m_page->editorState();
147     if (!editorState.selectionIsRange)
148         return;
149
150     m_page->selectionRangeClear();
151 }
152
153 void TextSelection::hide()
154 {
155     hideHandlers();
156     hideMagnifier();
157     hideContextMenu();
158 }
159
160 void TextSelection::updateHandlers()
161 {
162     WebCore::IntRect leftRect, rightRect;
163     if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
164         clear();
165         return;
166     }
167
168     AffineTransform toEvasTransform = m_viewImpl->transformToScene();
169     WebCore::IntPoint leftEvasPoint = toEvasTransform.mapPoint(leftRect.minXMaxYCorner());
170     WebCore::IntPoint rightEvasPoint = toEvasTransform.mapPoint(rightRect.maxXMaxYCorner());
171
172     EditorState editorState = m_page->editorState();
173     if (editorState.isContentEditable) {
174         m_leftHandle->hide();
175         m_rightHandle->hide();
176
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;
181
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();
187         }
188
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();
193         }
194     } else {
195         m_leftHandle->move(leftEvasPoint);
196         m_leftHandle->show();
197
198         m_rightHandle->move(rightEvasPoint);
199         m_rightHandle->show();
200     }
201 }
202
203 void TextSelection::hideHandlers()
204 {
205     m_leftHandle->hide();
206     m_rightHandle->hide();
207 }
208
209 void TextSelection::showMagnifier()
210 {
211     m_magnifier->show();
212 }
213
214 void TextSelection::hideMagnifier()
215 {
216     m_magnifier->hide();
217 }
218
219 void TextSelection::updateMagnifier(const IntPoint& position)
220 {
221     m_magnifier->update(position);
222     m_magnifier->move(position);
223 }
224
225 void TextSelection::showContextMenu()
226 {
227     if (!isEnabled())
228         return;
229
230     EditorState editorState = m_page->editorState();
231     if (!editorState.selectionIsRange && !editorState.isContentEditable)
232         return;
233
234     WebCore::IntPoint point;
235 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
236     bool isPresentInViewPort = false;
237 #endif
238     if (editorState.selectionIsRange) {
239         WebCore::IntRect leftRect, rightRect;
240         if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
241             clear();
242             return;
243         }
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);
251
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;
267         }
268 #else
269         point = leftRect.center();
270 #endif
271     } else if (editorState.isContentEditable) {
272         WebCore::IntRect caretRect;
273         m_page->getCaretPosition(caretRect);
274
275         if (caretRect.isEmpty())
276             return;
277
278         point = caretRect.center();
279 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
280         isPresentInViewPort = true;
281 #endif
282     }
283 #if ENABLE(TIZEN_WEBKIT2_TILED_BACKING_STORE)
284     if (!isPresentInViewPort)
285         return;
286 #endif
287
288     // show context menu if its in viewport else do not show the contextmenu
289
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)
292         return;
293
294     point = m_viewImpl->transformToScene().mapPoint(point);
295     Evas* evas = evas_object_evas_get(m_object);
296
297     // send mouse down.
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();
302     mouseDown.data = 0;
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;
308     mouseDown.dev = 0;
309     smartData->api->mouse_down(smartData, &mouseDown);
310
311     // send mouse up.
312     Evas_Event_Mouse_Up mouseUp;
313     mouseUp.button = 3;
314     mouseUp.output.x = mouseUp.canvas.x = point.x();
315     mouseUp.output.y = mouseUp.canvas.y = point.y();
316     mouseUp.data = 0;
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;
322     mouseUp.dev = 0;
323     smartData->api->mouse_up(smartData, &mouseUp);
324 }
325
326 void TextSelection::hideContextMenu()
327 {
328     if (!isEnabled())
329         return;
330
331     m_page->hideContextMenu();
332 }
333
334 void TextSelection::setLeftSelectionToEvasPoint(const IntPoint& evasPoint)
335 {
336     m_page->setLeftSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
337     updateHandlers();
338 }
339
340 void TextSelection::setRightSelectionToEvasPoint(const IntPoint& evasPoint)
341 {
342     m_page->setRightSelection(m_viewImpl->transformFromScene().mapPoint(evasPoint));
343     updateHandlers();
344 }
345
346 // handle callbacks
347 void TextSelection::handleMouseDown(TextSelectionHandle* handle, const IntPoint& /*position*/)
348 {
349     WebCore::IntPoint basePosition;
350     EditorState editorState = m_page->editorState();
351
352     if (editorState.selectionIsRange) {
353         WebCore::IntRect leftRect, rightRect;
354         if (!m_page->getSelectionHandlers(leftRect, rightRect)) {
355             clear();
356             return;
357         }
358
359         if (handle->isLeft()) {
360             basePosition.setX(leftRect.x());
361             basePosition.setY(leftRect.y() + (leftRect.height()/2));
362         } else {
363             basePosition.setX(rightRect.x() + rightRect.width());
364             basePosition.setY(rightRect.y() + (rightRect.height()/2));
365         }
366
367         handle->setBasePositionForMove(m_viewImpl->transformToScene().mapPoint(basePosition));
368     } else
369         return;
370
371     hideContextMenu();
372     updateMagnifier(handle->position());
373     showMagnifier();
374 }
375
376 void TextSelection::handleMouseMove(TextSelectionHandle* handle, const IntPoint& position)
377 {
378     if (handle->isLeft())
379         setLeftSelectionToEvasPoint(position);
380     else
381         setRightSelectionToEvasPoint(position);
382
383     updateMagnifier(handle->position());
384 }
385
386 void TextSelection::handleMouseUp(TextSelectionHandle* /* handle */, const IntPoint& /* position */)
387 {
388     hideMagnifier();
389     showContextMenu();
390 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
391     m_page->getTextStyleStateForSelection();
392 #endif
393 }
394
395 bool TextSelection::isMagnifierVisible()
396 {
397     return m_magnifier->isVisible();
398 }
399
400 void TextSelection::updateHandlesAndContextMenu(bool isShow, bool isScrolling)
401 {
402     if (isTextSelectionDowned() && !isScrolling) {
403         showMagnifier();
404         return;
405     }
406
407     if (isShow) {
408         updateHandlers();
409         showContextMenu();
410     } else {
411         hideHandlers();
412         hideContextMenu();
413     }
414
415     if (isScrolling && isMagnifierVisible())
416         hideMagnifier();
417 }
418
419 void TextSelection::startMoveAnimator()
420 {
421     if (!isEnabled() || !isTextSelectionDowned())
422         return;
423
424     if (!m_moveAnimator)
425         m_moveAnimator = ecore_animator_add(moveAnimatorCallback, this);
426 }
427
428 void TextSelection::stopMoveAnimator()
429 {
430     if (m_moveAnimator) {
431         ecore_animator_del(m_moveAnimator);
432         m_moveAnimator = 0;
433     }
434 }
435
436 void TextSelection::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
437 {
438     static_cast<TextSelection*>(data)->textSelectionUp(IntPoint());
439 }
440
441 Eina_Bool TextSelection::moveAnimatorCallback(void* data)
442 {
443     TextSelection* textSelection = static_cast<TextSelection*>(data);
444
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));
448
449     return ECORE_CALLBACK_RENEW;
450 }
451
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)
455 {
456     setIsTextSelectionMode(false);
457
458     IntPoint contentsPoint = m_viewImpl->transformFromScene().mapPoint(point);
459     bool result = m_page->selectClosestWord(contentsPoint, isStartedTextSelectionFromOutside);
460     if (!result)
461         return false;
462
463     if (isTextSelectionMode()) {
464         hide();
465     } else {
466         setIsTextSelectionMode(true);
467     }
468     setIsTextSelectionDowned(true);
469
470     updateMagnifier(point);
471     showMagnifier();
472
473     startMoveAnimator();
474
475     return true;
476 }
477
478 void TextSelection::textSelectionMove(const IntPoint& point, bool isStartedTextSelectionFromOutside)
479 {
480     if (!isTextSelectionMode()) {
481         stopMoveAnimator();
482         return;
483     }
484
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);
492         }
493     } else {
494         m_page->selectClosestWord(viewPoint, isStartedTextSelectionFromOutside);
495         updateMagnifier(point);
496     }
497     showMagnifier();
498 }
499
500 void TextSelection::textSelectionUp(const IntPoint& point, bool isStartedTextSelectionFromOutside)
501 {
502     stopMoveAnimator();
503
504     if (!isTextSelectionMode() || !isTextSelectionDowned())
505         return;
506
507     setIsTextSelectionDowned(false);
508     hideMagnifier();
509
510     EditorState editorState = m_page->editorState();
511     if (editorState.selectionIsRange || editorState.isContentEditable) {
512         if (editorState.selectionIsRange)
513             updateHandlers();
514
515         showContextMenu();
516     } else if (!isStartedTextSelectionFromOutside)
517         setIsTextSelectionMode(false);
518
519 #if ENABLE(TIZEN_WEBKIT2_GET_TEXT_STYLE_FOR_SELECTION)
520     m_page->getTextStyleStateForSelection();
521 #endif
522 }
523
524 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
525 TextSelectionHandle* TextSelection::getSelectedHandle(const IntPoint& position)
526 {
527     WebCore::IntRect leftHandleRect = m_leftHandle->getHandleRect();
528     if (leftHandleRect.contains(position))
529         return m_leftHandle;
530
531     WebCore::IntRect rightHandleRect = m_rightHandle->getHandleRect();
532     if (rightHandleRect.contains(position))
533         return m_rightHandle;
534
535     return 0;
536 }
537
538 void TextSelection::textSelectionHandleDown(const IntPoint& position)
539 {
540     TextSelectionHandle* selectedHandle = getSelectedHandle(position);
541     if (selectedHandle) {
542         selectedHandle->mouseDown(position);
543         if (selectedHandle->isMouseDowned())
544             return;
545     }
546
547     EditorState editorState = m_page->editorState();
548     if (editorState.isContentEditable)
549         setIsTextSelectionMode(false);
550 }
551
552 void TextSelection::textSelectionHandleMove(const IntPoint& position)
553 {
554     if (isTextSelectionHandleDowned()) {
555         TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
556         selectedHandle->mouseMove(position);
557     }
558 }
559
560 void TextSelection::textSelectionHandleUp()
561 {
562     if (isTextSelectionHandleDowned()) {
563         TextSelectionHandle* selectedHandle = m_leftHandle->isMouseDowned() ? m_leftHandle : m_rightHandle;
564         selectedHandle->mouseUp();
565     }
566 }
567 #endif
568
569 bool TextSelection::isEnabled()
570 {
571     return ewk_settings_text_selection_enabled_get(ewk_view_settings_get(m_object));
572 }
573
574 bool TextSelection::isAutomaticClearEnabled()
575 {
576     return ewk_settings_clear_text_selection_automatically_get(ewk_view_settings_get(m_object));
577 }
578
579 } // namespace WebKit
580
581 #endif // TIZEN_WEBKIT2_TEXT_SELECTION