Return 0 value if textselection handlers are invisible.
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / TextSelectionHandle.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 "TextSelectionHandle.h"
30
31 #include <Edje.h>
32
33 using namespace WebCore;
34
35 namespace WebKit {
36
37 Ecore_Job* TextSelectionHandle::s_job = 0;
38
39 TextSelectionHandle::TextSelectionHandle(Evas_Object* object, const char* themePath, const char* part, bool isLeft, TextSelection* textSelection)
40     : m_isLeft(isLeft)
41     , m_widget(object)
42     , m_textSelection(textSelection)
43     , m_position(IntPoint(0, 0))
44     , m_isMouseDowned(false)
45     , m_isTop(false)
46 {
47     Evas* evas = evas_object_evas_get(object);
48     m_icon = edje_object_add(evas);
49
50     if (!m_icon)
51         return;
52
53     if (!edje_object_file_set(m_icon, themePath, part))
54         return;
55
56     edje_object_signal_emit(m_icon, "edje,focus,in", "edje");
57     edje_object_signal_emit(m_icon, "elm,state,bottom", "elm");
58     evas_object_smart_member_add(m_icon, object);
59
60     evas_object_propagate_events_set(m_icon, false);
61     evas_object_event_callback_add(m_icon, EVAS_CALLBACK_MOUSE_DOWN, onMouseDown, this);
62     evas_object_event_callback_add(m_icon, EVAS_CALLBACK_MOUSE_MOVE, onMouseMove, this);
63     evas_object_event_callback_add(m_icon, EVAS_CALLBACK_MOUSE_UP, onMouseUp, this);
64 }
65
66 TextSelectionHandle::~TextSelectionHandle()
67 {
68     evas_object_event_callback_del(m_icon, EVAS_CALLBACK_MOUSE_DOWN, onMouseDown);
69     evas_object_event_callback_del(m_icon, EVAS_CALLBACK_MOUSE_MOVE, onMouseMove);
70     evas_object_event_callback_del(m_icon, EVAS_CALLBACK_MOUSE_UP, onMouseUp);
71     evas_object_del(m_icon);
72     if (s_job) {
73         ecore_job_del(s_job);
74         s_job = 0;
75     }
76 }
77
78 void TextSelectionHandle::move(const IntPoint& point, const IntRect& selectionRect)
79 {
80     IntPoint movePoint = point;
81     int x, y, deviceWidth, deviceHeight;
82     evas_object_geometry_get(m_widget, &x, &y, &deviceWidth, &deviceHeight);
83     IntRect handleRect = getHandleRect();
84
85     if ((movePoint.y() + handleRect.height()) > (y + deviceHeight)) {
86         movePoint.setY(movePoint.y() - selectionRect.height());
87         edje_object_signal_emit(m_icon, "elm,state,top", "elm");
88         m_isTop = true;
89     } else {
90         edje_object_signal_emit(m_icon, "elm,state,bottom", "elm");
91         m_isTop = false;
92     }
93
94     evas_object_move(m_icon, movePoint.x(), movePoint.y());
95     m_position = movePoint;
96 }
97
98 void TextSelectionHandle::show()
99 {
100     evas_object_show(m_icon);
101 }
102
103 void TextSelectionHandle::hide()
104 {
105     evas_object_hide(m_icon);
106 }
107
108 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
109 const IntRect TextSelectionHandle::getHandleRect()
110 {
111     if (!evas_object_visible_get(m_icon))
112         return IntRect();
113
114     int x, y;
115     evas_object_geometry_get(m_icon, &x, &y, 0, 0);
116
117     int partX, partY, partWidth, partHeight;
118     edje_object_part_geometry_get(m_icon, "handle", &partX, &partY, &partWidth, &partHeight);
119
120     WebCore::IntRect handleRect(x + partX, y + partY, partWidth, partHeight);
121     return handleRect;
122 }
123 #endif
124
125 // callbacks
126 void TextSelectionHandle::mouseDown(const IntPoint& point)
127 {
128 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
129     edje_object_signal_emit(m_icon, "mouse,down,1", "handle");
130 #endif
131
132     setIsMouseDowned(true);
133     setFirstDownMousePosition(point);
134     setMousePosition(point);
135     m_textSelection->handleMouseDown(this, m_mousePosition);
136 }
137
138 void TextSelectionHandle::mouseMove(const IntPoint& point)
139 {
140     if (!m_textSelection->isMagnifierVisible())
141         return;
142
143     setMousePosition(point);
144
145     if (!s_job)
146        s_job = ecore_job_add(update, this);
147 }
148
149 void TextSelectionHandle::mouseUp()
150 {
151 #if ENABLE(TIZEN_WEBKIT2_FOR_MOVING_TEXT_SELECTION_HANDLE_FROM_OSP)
152     edje_object_signal_emit(m_icon, "mouse,up,1", "handle");
153 #endif
154
155     m_textSelection->handleMouseUp(this, m_mousePosition);
156 }
157
158 void TextSelectionHandle::onMouseDown(void* data, Evas*, Evas_Object*, void* eventInfo)
159 {
160     Evas_Event_Mouse_Down* event = static_cast<Evas_Event_Mouse_Down*>(eventInfo);
161     TextSelectionHandle* handle = static_cast<TextSelectionHandle*>(data);
162
163     handle->mouseDown(IntPoint(event->canvas.x, event->canvas.y));
164 }
165
166 void TextSelectionHandle::onMouseMove(void* data, Evas*, Evas_Object*, void* eventInfo)
167 {
168     Evas_Event_Mouse_Move* event = static_cast<Evas_Event_Mouse_Move*>(eventInfo);
169     TextSelectionHandle* handle = static_cast<TextSelectionHandle*>(data);
170
171     handle->mouseMove(IntPoint(event->cur.canvas.x, event->cur.canvas.y));
172 }
173
174 void TextSelectionHandle::onMouseUp(void* data, Evas*, Evas_Object*, void* eventInfo)
175 {
176     TextSelectionHandle* handle = static_cast<TextSelectionHandle*>(data);
177
178     handle->mouseUp();
179 }
180
181 // job
182 void TextSelectionHandle::update(void* data)
183 {
184     TextSelectionHandle* handle = static_cast<TextSelectionHandle*>(data);
185     int deltaX = handle->m_mousePosition.x() - handle->m_firstDownMousePosition.x();
186     int deltaY = handle->m_mousePosition.y() - handle->m_firstDownMousePosition.y();
187
188     if (deltaX || deltaY) {
189         WebCore::IntPoint movePosition;
190
191         movePosition.setX(handle->m_basePositionForMove.x() + deltaX);
192         movePosition.setY(handle->m_basePositionForMove.y() + deltaY);
193
194         handle->m_textSelection->handleMouseMove(handle, movePosition);
195     }
196     TextSelectionHandle::s_job = 0;
197 }
198
199 } // namespace WebKit
200
201 #endif // TIZEN_WEBKIT2_TEXT_SELECTION