Fix clearing text issue of email composer
[framework/web/webkit-efl.git] / Source / WebKit2 / WebProcess / WebCoreSupport / efl / WebEditorClientEfl.cpp
1 /*
2  * Copyright (C) 2011 Samsung Electronics. All rights reserved.
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 #include "WebEditorClient.h"
28
29 #include "Frame.h"
30 #include "NativeWebKeyboardEvent.h"
31 #include "PlatformKeyboardEvent.h"
32 #include "WebPage.h"
33 #include "WebPageProxyMessages.h"
34 #include "WebProcess.h"
35 #if ENABLE(TIZEN_ISF_PORT)
36 #include "WindowsKeyboardCodes.h"
37 #endif
38 #include <WebCore/FocusController.h>
39 #include <WebCore/KeyboardEvent.h>
40 #include <WebCore/NotImplemented.h>
41 #include <WebCore/Page.h>
42
43 #if ENABLE(TIZEN_CLIPBOARD) || ENABLE(TIZEN_PASTEBOARD)
44 #include <Evas.h>
45 #include <WebCore/DataObjectTizen.h>
46 #endif
47
48 using namespace WebCore;
49
50 namespace WebKit {
51
52 #if ENABLE(TIZEN_ISF_PORT)
53 static bool handleKeyPressCommands(WebPage* page, KeyboardEvent* event)
54 {
55     const NativeWebKeyboardEvent* currentEvent = static_cast<const NativeWebKeyboardEvent*>(WebPage::currentEvent());
56     bool isFiltered = (currentEvent && currentEvent->isFiltered());
57
58     if (event->type() != eventNames().keypressEvent)
59         return isFiltered;
60
61     Vector<OwnPtr<KeyPressCommand> > commands;
62     page->swapKeyPressCommands(commands);
63
64     size_t size = commands.size();
65     if (!size)
66         return isFiltered;
67
68     for (size_t i = 0; i < size; ++i) {
69         switch (commands[i]->type) {
70         case KeyPressCommandSetComposition: {
71             const SetCompositionKeyPressCommand* command = reinterpret_cast<const SetCompositionKeyPressCommand*>(commands[i].get());
72             if (i + 1 < size) {
73                 int nextType = commands[i + 1]->type;
74                 if (nextType == KeyPressCommandSetComposition || nextType == KeyPressCommandConfirmComposition)
75                     break;
76             }
77             page->setComposition(command->compositionString, command->underlines, command->cursorPosition);
78             break;
79         }
80         case KeyPressCommandConfirmComposition: {
81             const ConfirmCompositionKeyPressCommand* command = reinterpret_cast<const ConfirmCompositionKeyPressCommand*>(commands[i].get());
82             page->confirmComposition(command->compositionString);
83             break;
84         }
85         case KeyPressCommandDeleteText: {
86             const DeleteTextKeyPressCommand* command = reinterpret_cast<const DeleteTextKeyPressCommand*>(commands[i].get());
87             page->deleteSurroundingText(command->offset, command->count);
88             break;
89         }
90         default:
91             break;
92         }
93     }
94
95     event->setDefaultHandled();
96
97     return isFiltered;
98 }
99 #endif
100
101 void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
102 {
103 #if ENABLE(TIZEN_ISF_PORT)
104     if (handleKeyPressCommands(m_page, event))
105         return;
106 #endif
107
108     if (m_page->handleEditingKeyboardEvent(event))
109         event->setDefaultHandled();
110 }
111
112 void WebEditorClient::handleInputMethodKeydown(KeyboardEvent* event)
113 {
114 #if ENABLE(TIZEN_ISF_PORT)
115     return;
116 #endif
117
118     Frame* frame = m_page->corePage()->focusController()->focusedOrMainFrame();
119     if (!frame || !frame->editor()->canEdit())
120         return;
121
122     // FIXME: sending sync message might make input lagging.
123     bool handled = false;
124     m_page->sendSync(Messages::WebPageProxy::HandleInputMethodKeydown(), Messages::WebPageProxy::HandleInputMethodKeydown::Reply(handled));
125
126     if (handled)
127         event->setDefaultHandled();
128 }
129
130 #if ENABLE(TIZEN_CLIPBOARD) || ENABLE(TIZEN_PASTEBOARD)
131 void WebEditorClient::setClipboardData(const String& data, const String& type)
132 {
133     m_page->send(Messages::WebPageProxy::SetClipboardData(data, type));
134 }
135
136 void WebEditorClient::setClipboardDataForPaste(const String& data, const String& type)
137 {
138     DataObjectTizen* dataObject = DataObjectTizen::sharedDataObject();
139
140     if (type == "Markup") {
141         dataObject->setMarkup(data);
142         char* plainText = evas_textblock_text_markup_to_utf8(0, data.utf8().data());
143         if (plainText) {
144             dataObject->setText(data);
145             free(plainText);
146         }
147     } else if (type == "PlainText") {
148         dataObject->setText(data);
149     } else if (type == "Image") {
150         dataObject->setImage(data);
151         dataObject->setText(data);
152     }
153 }
154
155 void WebEditorClient::clearClipboardData()
156 {
157     m_page->send(Messages::WebPageProxy::ClearClipboardData());
158 }
159 #endif
160
161 #if ENABLE(TIZEN_ISF_PORT)
162 void WebEditorClient::didCancelComposition(Node* valueChangedNode)
163 {
164     m_page->didCancelComposition(valueChangedNode);
165 }
166 #endif
167
168 }