2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/html/ime/InputMethodContext.h"
34 #include "core/dom/Document.h"
35 #include "core/dom/Text.h"
36 #include "core/editing/InputMethodController.h"
37 #include "core/events/Event.h"
38 #include "core/frame/LocalFrame.h"
42 PassOwnPtrWillBeRawPtr<InputMethodContext> InputMethodContext::create(HTMLElement* element)
44 return adoptPtrWillBeNoop(new InputMethodContext(element));
47 InputMethodContext::InputMethodContext(HTMLElement* element)
52 InputMethodContext::~InputMethodContext()
56 String InputMethodContext::locale() const
58 // FIXME: Implement this.
62 HTMLElement* InputMethodContext::target() const
67 unsigned InputMethodContext::compositionStartOffset()
70 return inputMethodController().compositionStart();
74 unsigned InputMethodContext::compositionEndOffset()
77 return inputMethodController().compositionEnd();
81 void InputMethodContext::confirmComposition()
84 inputMethodController().confirmCompositionAndResetState();
87 bool InputMethodContext::hasFocus() const
89 LocalFrame* frame = m_element->document().frame();
93 const Element* element = frame->document()->focusedElement();
94 return element && element->isHTMLElement() && m_element == toHTMLElement(element);
97 String InputMethodContext::compositionText() const
100 return emptyString();
102 Text* text = inputMethodController().compositionNode();
103 return text ? text->wholeText() : emptyString();
106 CompositionUnderline InputMethodContext::selectedSegment() const
108 CompositionUnderline underline;
112 const InputMethodController& controller = inputMethodController();
113 if (!controller.hasComposition())
116 Vector<CompositionUnderline> underlines = controller.customCompositionUnderlines();
117 for (size_t i = 0; i < underlines.size(); ++i) {
118 if (underlines[i].thick)
119 return underlines[i];
122 // When no underline information is available while composition exists,
123 // build a CompositionUnderline whose element is the whole composition.
124 underline.endOffset = controller.compositionEnd() - controller.compositionStart();
129 int InputMethodContext::selectionStart() const
131 return selectedSegment().startOffset;
134 int InputMethodContext::selectionEnd() const
136 return selectedSegment().endOffset;
139 const Vector<unsigned>& InputMethodContext::segments()
144 const InputMethodController& controller = inputMethodController();
145 if (!controller.hasComposition())
148 Vector<CompositionUnderline> underlines = controller.customCompositionUnderlines();
149 if (!underlines.size()) {
150 m_segments.append(0);
152 for (size_t i = 0; i < underlines.size(); ++i)
153 m_segments.append(underlines[i].startOffset);
159 InputMethodController& InputMethodContext::inputMethodController() const
161 return m_element->document().frame()->inputMethodController();
164 const AtomicString& InputMethodContext::interfaceName() const
166 return EventTargetNames::InputMethodContext;
169 ExecutionContext* InputMethodContext::executionContext() const
171 return &m_element->document();
174 void InputMethodContext::dispatchCandidateWindowShowEvent()
176 dispatchEvent(Event::create(EventTypeNames::candidatewindowshow));
179 void InputMethodContext::dispatchCandidateWindowUpdateEvent()
181 dispatchEvent(Event::create(EventTypeNames::candidatewindowupdate));
184 void InputMethodContext::dispatchCandidateWindowHideEvent()
186 dispatchEvent(Event::create(EventTypeNames::candidatewindowhide));
189 void InputMethodContext::trace(Visitor* visitor)
191 visitor->trace(m_element);
192 EventTargetWithInlineData::trace(visitor);