Home | History | Annotate | Download | only in editing
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Inc. 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 COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef InputMethodController_h
     27 #define InputMethodController_h
     28 
     29 #include "core/editing/CompositionUnderline.h"
     30 #include "wtf/Vector.h"
     31 
     32 namespace WebCore {
     33 
     34 class Editor;
     35 class EditorClient;
     36 class Frame;
     37 class Range;
     38 class Text;
     39 
     40 // FIXME: We should move PlainTextOffsets to own file for using InputMethodController
     41 // and TextIterator and unify PlainTextRange defined in AccessibilityObject.h.
     42 class PlainTextOffsets {
     43 public:
     44     PlainTextOffsets();
     45     PlainTextOffsets(int start, int length);
     46     size_t end() const { return m_end; }
     47     size_t start() const { return m_start; }
     48     bool isNull() const { return m_start == notFound; }
     49 private:
     50     size_t m_start;
     51     size_t m_end;
     52 };
     53 
     54 class InputMethodController {
     55 public:
     56     static PassOwnPtr<InputMethodController> create(Frame*);
     57     ~InputMethodController();
     58 
     59     // international text input composition
     60     bool hasComposition() const { return m_compositionNode; }
     61     void setComposition(const String&, const Vector<CompositionUnderline>&, unsigned selectionStart, unsigned selectionEnd);
     62     void setCompositionFromExistingText(const Vector<CompositionUnderline>&, unsigned compositionStart, unsigned compositionEnd);
     63     // Inserts the text that is being composed as a regular text.
     64     // This method does nothing if composition node is not present.
     65     void confirmComposition();
     66     // Inserts the given text string in the place of the existing composition, or replaces the selection if composition is not present.
     67     void confirmComposition(const String& text);
     68     void confirmCompositionAndResetState();
     69     // Deletes the existing composition text.
     70     void cancelComposition();
     71     void cancelCompositionIfSelectionIsInvalid();
     72     PassRefPtr<Range> compositionRange() const;
     73 
     74     // getting international text input composition state (for use by InlineTextBox)
     75     Text* compositionNode() const { return m_compositionNode.get(); }
     76     unsigned compositionStart() const { return m_compositionStart; }
     77     unsigned compositionEnd() const { return m_compositionEnd; }
     78     bool compositionUsesCustomUnderlines() const { return !m_customCompositionUnderlines.isEmpty(); }
     79     const Vector<CompositionUnderline>& customCompositionUnderlines() const { return m_customCompositionUnderlines; }
     80 
     81     void clear();
     82 
     83 private:
     84     class SelectionOffsetsScope {
     85         WTF_MAKE_NONCOPYABLE(SelectionOffsetsScope);
     86     public:
     87         SelectionOffsetsScope(InputMethodController*);
     88         ~SelectionOffsetsScope();
     89     private:
     90         InputMethodController* m_inputMethodController;
     91         PlainTextOffsets m_offsets;
     92     };
     93     friend class SelectionOffsetsScope;
     94 
     95     Frame* m_frame;
     96     RefPtr<Text> m_compositionNode;
     97     // FIXME: We should use PlainTextOffsets m_compositionRange instead of
     98     // m_compositionStart/m_compositionEnd.
     99     unsigned m_compositionStart;
    100     unsigned m_compositionEnd;
    101     // startOffset and endOffset of CompositionUnderline are based on
    102     // m_compositionNode.
    103     Vector<CompositionUnderline> m_customCompositionUnderlines;
    104 
    105     explicit InputMethodController(Frame*);
    106     Editor& editor() const;
    107     EditorClient* editorClient() const;
    108     bool insertTextForConfirmedComposition(const String& text);
    109     void selectComposition() const;
    110     enum FinishCompositionMode { ConfirmComposition, CancelComposition };
    111     void finishComposition(const String&, FinishCompositionMode);
    112     PlainTextOffsets getSelectionOffsets() const;
    113     bool setSelectionOffsets(const PlainTextOffsets&);
    114 };
    115 
    116 } // namespace WebCore
    117 
    118 #endif // InputMethodController_h
    119