IME show properly after disappear the clipboard.
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / efl / InputMethodContextEfl.cpp
index d7f993d..d810116 100755 (executable)
@@ -34,6 +34,11 @@ InputMethodContextEfl::InputMethodContextEfl(EwkViewImpl* viewImpl, PassOwnPtr<E
     : m_viewImpl(viewImpl)
     , m_context(context)
     , m_focused(false)
+#if ENABLE(TIZEN_ISF_PORT)
+    , m_useInputMethod(false)
+    , m_state(ECORE_IMF_INPUT_PANEL_STATE_HIDE)
+    , m_inputPickerType(-1)
+#endif
 {
     ASSERT(context);
 #if ENABLE(TIZEN_ISF_PORT)
@@ -52,31 +57,33 @@ InputMethodContextEfl::~InputMethodContextEfl()
 void InputMethodContextEfl::onIMFInputPanelStateChanged(void* data, Ecore_IMF_Context*, int state)
 {
     InputMethodContextEfl* inputMethodContext = static_cast<InputMethodContextEfl*>(data);
-    if (!inputMethodContext->m_viewImpl->page()->focusedFrame() || !inputMethodContext->m_focused)
-        return;
 
-    if (state == ECORE_IMF_INPUT_PANEL_STATE_HIDE)
+    inputMethodContext->setState(state);
+
+    if (state == ECORE_IMF_INPUT_PANEL_STATE_HIDE) {
+#if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
+        if (inputMethodContext->m_viewImpl->pageClient->isClipboardWindowOpened())
+            inputMethodContext->m_viewImpl->pageClient->closeClipboardWindow();
+#endif
         evas_object_smart_callback_call(inputMethodContext->m_viewImpl->view(), "editorclient,ime,closed", 0);
-    else if (state == ECORE_IMF_INPUT_PANEL_STATE_SHOW)
+    else if (state == ECORE_IMF_INPUT_PANEL_STATE_SHOW)
         evas_object_smart_callback_call(inputMethodContext->m_viewImpl->view(), "editorclient,ime,opened", 0);
 }
 
 void InputMethodContextEfl::onIMFInputPanelGeometryChanged(void* data, Ecore_IMF_Context*, int value)
 {
     InputMethodContextEfl* inputMethodContext = static_cast<InputMethodContextEfl*>(data);
-    if (!inputMethodContext->m_viewImpl->page()->focusedFrame() || !inputMethodContext->m_focused)
-        return;
 
     Eina_Rectangle rect;
     ecore_imf_context_input_panel_geometry_get(inputMethodContext->m_context.get(), &rect.x, &rect.y, &rect.w, &rect.h);
     evas_object_smart_callback_call(inputMethodContext->m_viewImpl->view(), "inputmethod,changed", &rect);
+
+    inputMethodContext->setIMERect(IntRect(rect.x, rect.y, rect.w, rect.h));
 }
 
 void InputMethodContextEfl::onIMFCandidatePanelStateChanged(void* data, Ecore_IMF_Context*, int state)
 {
     InputMethodContextEfl* inputMethodContext = static_cast<InputMethodContextEfl*>(data);
-    if (!inputMethodContext->m_viewImpl->page()->focusedFrame() || !inputMethodContext->m_focused)
-        return;
 
     if (state == ECORE_IMF_CANDIDATE_PANEL_SHOW)
         evas_object_smart_callback_call(inputMethodContext->m_viewImpl->view(), "editorclient,candidate,opened", 0);
@@ -87,8 +94,6 @@ void InputMethodContextEfl::onIMFCandidatePanelStateChanged(void* data, Ecore_IM
 void InputMethodContextEfl::onIMFCandidatePanelGeometryChanged(void* data, Ecore_IMF_Context*, int)
 {
     InputMethodContextEfl* inputMethodContext = static_cast<InputMethodContextEfl*>(data);
-    if (!inputMethodContext->m_viewImpl->page()->focusedFrame() || !inputMethodContext->m_focused)
-        return;
 
     Eina_Rectangle rect;
     ecore_imf_context_candidate_panel_geometry_get(inputMethodContext->m_context.get(), &rect.x, &rect.y, &rect.w, &rect.h);
@@ -316,100 +321,39 @@ void InputMethodContextEfl::handleKeyDownEvent(const Evas_Event_Key_Down* downEv
     *isFiltered = ecore_imf_context_filter_event(m_context.get(), ECORE_IMF_EVENT_KEY_DOWN, &inputMethodEvent);
 }
 
+#if ENABLE(TIZEN_ISF_PORT)
 void InputMethodContextEfl::updateTextInputState()
 {
-#if !ENABLE(TIZEN_ISF_PORT)
-    if (!m_context)
-        return;
-#endif
-
     const EditorState& editor = m_viewImpl->page()->editorState();
+    if (editor.shouldIgnoreCompositionSelectionChange)
+        return;
 
-    if (editor.isContentEditable) {
-#if ENABLE(TIZEN_ISF_PORT)
-        if (m_context)
-            ecore_imf_context_cursor_position_set(m_context.get(), editor.cursorPosition);
-#endif
-
-        if (m_focused)
-            return;
-
-#if ENABLE(TIZEN_ISF_PORT)
-        Ewk_Settings* settings = ewk_view_settings_get(m_viewImpl->view());
-        bool defaultKeypadEnabled = ewk_settings_default_keypad_enabled_get(settings);
-
+    if (editor.isContentEditable && m_useInputMethod) {
 #if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
-        if (m_viewImpl->pageClient->isClipboardWindowOpened()) {
-            LOG(ISF, "[FAIL] Clipboard\n");
-            return;
-        }
+        if (m_viewImpl->pageClient->isClipboardWindowOpened())
+            m_viewImpl->pageClient->closeClipboardWindow();
 #endif
+        showIMFContext(editor);
+    } else
+        hideIMFContext();
 
-#if ENABLE(TIZEN_INPUT_TAG_EXTENSION)
-        if (editor.inputMethodHints == "date") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_DATE, editor.surroundingText);
-            return;
-        } else if (editor.inputMethodHints == "datetime") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_DATETIME, editor.surroundingText);
-            return;
-        } else if (editor.inputMethodHints == "datetime-local") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_DATETIMELOCAL, editor.surroundingText);
-            return;
-        } else if (editor.inputMethodHints == "month") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_MONTH, editor.surroundingText);
-            return;
-        } else if (editor.inputMethodHints == "time") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_TIME, editor.surroundingText);
-            return;
-        } else if (editor.inputMethodHints == "week") {
-            ewkViewInputPickerRequest(m_viewImpl->view(), EWK_INPUT_TYPE_WEEK, editor.surroundingText);
-            return;
-        }
-
-#if ENABLE(TIZEN_DATALIST_ELEMENT)
-        Vector<String> optionList = m_viewImpl->page()->getFocusedInputElementDataList();
-        if (optionList.size() > 0) {
-            if (editor.inputMethodHints == "tel")
-                ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_TELEPHONE, optionList);
-            else if (editor.inputMethodHints == "number")
-                ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_NUMBER, optionList);
-            else if (editor.inputMethodHints == "email")
-                ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_EMAIL, optionList);
-            else if (editor.inputMethodHints == "url")
-                ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_URL, optionList);
-            else
-                ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_TEXT, optionList);
-
-            return;
-        }
-#endif
-#endif // ENABLE(TIZEN_INPUT_TAG_EXTENSION)
-
-        bool hasFocus = evas_object_focus_get(m_viewImpl->view());
-
-        if (!defaultKeypadEnabled) {
-            if (hasFocus) {
-                Eina_Rectangle dummyRectForCustomKeypadCallback;
-                memset(&dummyRectForCustomKeypadCallback, 0, sizeof(Eina_Rectangle));
-                evas_object_smart_callback_call(m_viewImpl->view(), "inputmethod,changed", &dummyRectForCustomKeypadCallback);
-            }
-            return;
-        }
+    if (m_context)
+        ecore_imf_context_cursor_position_set(m_context.get(), editor.cursorPosition);
+}
+#else
+void InputMethodContextEfl::updateTextInputState()
+{
+    if (!m_context)
+        return;
 
-        setType(editor.inputMethodHints);
+    const EditorState& editor = m_viewImpl->page()->editorState();
 
-        if (!hasFocus)
+    if (editor.isContentEditable) {
+        if (m_focused)
             return;
 
-        // input field zoom for external keyboard
-        ewk_view_focused_node_adjust(m_viewImpl->view(), EINA_TRUE);
-#endif
-
         ecore_imf_context_reset(m_context.get());
         ecore_imf_context_focus_in(m_context.get());
-#if ENABLE(TIZEN_ISF_PORT)
-        ecore_imf_context_input_panel_show(m_context.get());
-#endif
         m_focused = true;
     } else {
         if (!m_focused)
@@ -421,12 +365,9 @@ void InputMethodContextEfl::updateTextInputState()
         m_focused = false;
         ecore_imf_context_reset(m_context.get());
         ecore_imf_context_focus_out(m_context.get());
-#if ENABLE(TIZEN_ISF_PORT)
-        ecore_imf_context_input_panel_hide(m_context.get());
-        revertIMFContext();
-#endif
     }
 }
+#endif
 
 #if ENABLE(TIZEN_ISF_PORT)
 void InputMethodContextEfl::initializeIMFContext(Ecore_IMF_Context* context, Ecore_IMF_Input_Panel_Layout layout)
@@ -443,22 +384,30 @@ void InputMethodContextEfl::initializeIMFContext(Ecore_IMF_Context* context, Eco
     ecore_imf_context_input_panel_layout_set(m_context.get(), layout);
 }
 
-void InputMethodContextEfl::setType(const String& type)
+void InputMethodContextEfl::setUseInputMethod(bool use)
+{
+    m_useInputMethod = use;
+    updateTextInputState();
+}
+
+Ecore_IMF_Input_Panel_Layout InputMethodContextEfl::layoutType(const String& type)
 {
-    Ecore_IMF_Input_Panel_Layout layout;
     if (type == "number")
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBER;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_NUMBER;
     else if (type == "email")
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_EMAIL;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_EMAIL;
     else if (type == "url")
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_URL;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_URL;
     else if (type == "tel")
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_PHONENUMBER;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_PHONENUMBER;
     else if (type == "password")
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_PASSWORD;
     else
-        layout = ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL;
+        return ECORE_IMF_INPUT_PANEL_LAYOUT_NORMAL;
+}
 
+void InputMethodContextEfl::setIMFContext(Ecore_IMF_Input_Panel_Layout layout, const String& type)
+{
     if (m_contextList.contains(layout)) {
         revertIMFContext();
         m_context = m_contextList.take(layout);
@@ -493,6 +442,11 @@ Ecore_IMF_Autocapital_Type InputMethodContextEfl::autoCapitalType()
 
 void InputMethodContextEfl::onFocusIn()
 {
+    if (m_inputPickerType >= 0) {
+        showInputPicker(m_viewImpl->page()->editorState());
+        return;
+    }
+
     if (!m_context || !m_focused)
         return;
 
@@ -502,6 +456,13 @@ void InputMethodContextEfl::onFocusIn()
 
 void InputMethodContextEfl::onFocusOut()
 {
+#if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
+    if (m_state != ECORE_IMF_INPUT_PANEL_STATE_SHOW) {
+        if (m_viewImpl->pageClient->isClipboardWindowOpened())
+            m_viewImpl->pageClient->closeClipboardWindow();
+    }
+#endif
+
     if (!m_context || !m_focused)
         return;
 
@@ -515,7 +476,8 @@ void InputMethodContextEfl::revertIMFContext()
         return;
 
     PassOwnPtr<Ecore_IMF_Context> imfContext = m_context.release();
-    m_contextList.add(ecore_imf_context_input_panel_layout_get(imfContext.get()), imfContext);
+    int layout = ecore_imf_context_input_panel_layout_get(imfContext.get());
+    m_contextList.add(layout, imfContext);
 }
 
 void InputMethodContextEfl::resetIMFContext()
@@ -524,17 +486,111 @@ void InputMethodContextEfl::resetIMFContext()
         return;
 
     ecore_imf_context_reset(m_context.get());
+}
 
-    if (ecore_imf_context_input_panel_state_get(m_context.get()) == ECORE_IMF_INPUT_PANEL_STATE_HIDE)
-        revertIMFContext();
+void InputMethodContextEfl::showIMFContext(const EditorState& editor)
+{
+    Ecore_IMF_Input_Panel_Layout layout = layoutType(editor.inputMethodHints);
+
+    if (m_context && m_state != ECORE_IMF_INPUT_PANEL_STATE_HIDE && layout == ecore_imf_context_input_panel_layout_get(m_context.get()))
+        return;
+
+    Ewk_Settings* settings = ewk_view_settings_get(m_viewImpl->view());
+    bool defaultKeypadEnabled = ewk_settings_default_keypad_enabled_get(settings);
+
+#if ENABLE(TIZEN_INPUT_TAG_EXTENSION)
+    if (editor.inputMethodHints == "date")
+        m_inputPickerType = EWK_INPUT_TYPE_DATE;
+    else if (editor.inputMethodHints == "datetime")
+        m_inputPickerType = EWK_INPUT_TYPE_DATETIME;
+    else if (editor.inputMethodHints == "datetime-local")
+        m_inputPickerType = EWK_INPUT_TYPE_DATETIMELOCAL;
+    else if (editor.inputMethodHints == "month")
+        m_inputPickerType = EWK_INPUT_TYPE_MONTH;
+    else if (editor.inputMethodHints == "time")
+        m_inputPickerType = EWK_INPUT_TYPE_TIME;
+    else if (editor.inputMethodHints == "week")
+        m_inputPickerType = EWK_INPUT_TYPE_WEEK;
+    else
+        m_inputPickerType = -1;
+
+    if (m_inputPickerType >= 0) {
+        showInputPicker(editor);
+        return;
+    }
+
+#if ENABLE(TIZEN_DATALIST_ELEMENT)
+    Vector<String> optionList = m_viewImpl->page()->getFocusedInputElementDataList();
+    if (optionList.size() > 0) {
+        if (editor.selectionIsRange || !evas_object_focus_get(m_viewImpl->view()))
+            return;
+
+        if (editor.inputMethodHints == "tel")
+            ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_TELEPHONE, optionList);
+        else if (editor.inputMethodHints == "number")
+            ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_NUMBER, optionList);
+        else if (editor.inputMethodHints == "email")
+            ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_EMAIL, optionList);
+        else if (editor.inputMethodHints == "url")
+            ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_URL, optionList);
+        else
+            ewkViewDataListShowRequest(m_viewImpl->view(), EWK_INPUT_TYPE_TEXT, optionList);
+
+        return;
+    }
+#endif
+#endif // ENABLE(TIZEN_INPUT_TAG_EXTENSION)
+
+    bool hasFocus = evas_object_focus_get(m_viewImpl->view());
+
+    if (!defaultKeypadEnabled) {
+        if (hasFocus) {
+            Eina_Rectangle dummyRectForCustomKeypadCallback;
+            memset(&dummyRectForCustomKeypadCallback, 0, sizeof(Eina_Rectangle));
+            evas_object_smart_callback_call(m_viewImpl->view(), "inputmethod,changed", &dummyRectForCustomKeypadCallback);
+        }
+        return;
+    }
+
+    setIMFContext(layout, editor.inputMethodHints);
+
+    if (!hasFocus) {
+        m_focused = true;
+        return;
+    }
+
+    ecore_imf_context_reset(m_context.get());
+    ecore_imf_context_focus_in(m_context.get());
+    ecore_imf_context_input_panel_show(m_context.get());
+
+    // input field zoom for external keyboard
+    ewk_view_focused_node_adjust(m_viewImpl->view(), EINA_TRUE);
+
+    m_focused = true;
+    m_state = ECORE_IMF_INPUT_PANEL_STATE_WILL_SHOW;
 }
 
 void InputMethodContextEfl::hideIMFContext()
 {
-    if (!m_context)
+#if ENABLE(TIZEN_WEBKIT2_CONTEXT_MENU_CLIPBOARD)
+    if (m_state != ECORE_IMF_INPUT_PANEL_STATE_SHOW) {
+        if (m_viewImpl->pageClient->isClipboardWindowOpened())
+            m_viewImpl->pageClient->closeClipboardWindow();
+    }
+#endif
+
+    m_inputPickerType = -1;
+
+    if (!m_context || !m_focused)
         return;
 
-    if (ecore_imf_context_input_panel_state_get(m_context.get()) != ECORE_IMF_INPUT_PANEL_STATE_HIDE) {
+    if (m_viewImpl->page()->editorState().hasComposition)
+        m_viewImpl->page()->cancelComposition();
+
+    m_focused = false;
+
+    if (ecore_imf_context_input_panel_state_get(m_context.get()) != ECORE_IMF_INPUT_PANEL_STATE_HIDE
+        && evas_object_focus_get(m_viewImpl->view())) {
         ecore_imf_context_reset(m_context.get());
         ecore_imf_context_input_panel_hide(m_context.get());
         ecore_imf_context_focus_out(m_context.get());
@@ -547,6 +603,26 @@ void InputMethodContextEfl::destroyIMFContextList()
 {
     m_contextList.clear();
 }
+
+#if ENABLE(TIZEN_INPUT_TAG_EXTENSION)
+void InputMethodContextEfl::showInputPicker(const EditorState& editorState)
+{
+    if (editorState.selectionIsRange || !evas_object_focus_get(m_viewImpl->view()))
+        return;
+
+    ewkViewInputPickerRequest(m_viewImpl->view(), static_cast<Ewk_Input_Type>(m_inputPickerType), editorState.surroundingText);
+    m_inputPickerType = -1;
+}
+#endif
+
+bool InputMethodContextEfl::isIMEPostion(int x, int y)
+{
+    if (m_state == ECORE_IMF_INPUT_PANEL_STATE_SHOW)
+        return m_imeRect.contains(x, y);
+
+    return false;
+}
+
 #endif
 
 }