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 "core/editing/PlainTextRange.h"
     31 #include "wtf/Vector.h"
     32 
     33 namespace WebCore {
     34 
     35 class Editor;
     36 class EditorClient;
     37 class LocalFrame;
     38 class Range;
     39 class Text;
     40 
     41 class InputMethodController {
     42     WTF_MAKE_NONCOPYABLE(InputMethodController);
     43 public:
     44     enum ConfirmCompositionBehavior {
     45         DoNotKeepSelection,
     46         KeepSelection,
     47     };
     48 
     49     static PassOwnPtr<InputMethodController> create(LocalFrame&);
     50     ~InputMethodController();
     51 
     52     // international text input composition
     53     bool hasComposition() const;
     54     void setComposition(const String&, const Vector<CompositionUnderline>&, unsigned selectionStart, unsigned selectionEnd);
     55     void setCompositionFromExistingText(const Vector<CompositionUnderline>&, unsigned compositionStart, unsigned compositionEnd);
     56     // Inserts the text that is being composed as a regular text and returns true
     57     // if composition exists.
     58     bool confirmComposition();
     59     // Inserts the given text string in the place of the existing composition
     60     // and returns true.
     61     bool confirmComposition(const String& text);
     62     // Inserts the text that is being composed or specified non-empty text and
     63     // returns true.
     64     bool confirmCompositionOrInsertText(const String& text, ConfirmCompositionBehavior);
     65     void confirmCompositionAndResetState();
     66     // Deletes the existing composition text.
     67     void cancelComposition();
     68     void cancelCompositionIfSelectionIsInvalid();
     69     PassRefPtrWillBeRawPtr<Range> compositionRange() const;
     70 
     71     // getting international text input composition state (for use by InlineTextBox)
     72     Text* compositionNode() const { return m_compositionNode.get(); }
     73     unsigned compositionStart() const { return m_compositionStart; }
     74     unsigned compositionEnd() const { return m_compositionEnd; }
     75     bool compositionUsesCustomUnderlines() const { return !m_customCompositionUnderlines.isEmpty(); }
     76     const Vector<CompositionUnderline>& customCompositionUnderlines() const { return m_customCompositionUnderlines; }
     77 
     78     void clear();
     79 
     80     PlainTextRange getSelectionOffsets() const;
     81     // Returns true if setting selection to specified offsets, otherwise false.
     82     bool setEditableSelectionOffsets(const PlainTextRange&);
     83     void extendSelectionAndDelete(int before, int after);
     84 
     85 private:
     86     class SelectionOffsetsScope {
     87         WTF_MAKE_NONCOPYABLE(SelectionOffsetsScope);
     88     public:
     89         SelectionOffsetsScope(InputMethodController*);
     90         ~SelectionOffsetsScope();
     91     private:
     92         InputMethodController* m_inputMethodController;
     93         const PlainTextRange m_offsets;
     94     };
     95     friend class SelectionOffsetsScope;
     96 
     97     LocalFrame& m_frame;
     98     RefPtrWillBePersistent<Text> m_compositionNode;
     99     // We don't use PlainTextRange which is immutable, for composition range.
    100     unsigned m_compositionStart;
    101     unsigned m_compositionEnd;
    102     // startOffset and endOffset of CompositionUnderline are based on
    103     // m_compositionNode.
    104     Vector<CompositionUnderline> m_customCompositionUnderlines;
    105 
    106     explicit InputMethodController(LocalFrame&);
    107     Editor& editor() const;
    108     bool insertTextForConfirmedComposition(const String& text);
    109     void selectComposition() const;
    110     enum FinishCompositionMode { ConfirmComposition, CancelComposition };
    111     // Returns true if composition exists.
    112     bool finishComposition(const String&, FinishCompositionMode);
    113     bool setSelectionOffsets(const PlainTextRange&);
    114 };
    115 
    116 } // namespace WebCore
    117 
    118 #endif // InputMethodController_h
    119