OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef VIEWS_IME_IME_CONTEXT_H_ |
| 6 #define VIEWS_IME_IME_CONTEXT_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/string16.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 |
| 15 namespace gfx { |
| 16 class Rect; |
| 17 } |
| 18 |
| 19 namespace views { |
| 20 |
| 21 class IMEContext; |
| 22 class KeyEvent; |
| 23 class View; |
| 24 |
| 25 // Duplicate WebKit::WebCompositionUnderline |
| 26 struct CompositionUnderline { |
| 27 CompositionUnderline() |
| 28 : start_offset(0), |
| 29 end_offset(0), |
| 30 color(0), |
| 31 thick(false) {} |
| 32 |
| 33 CompositionUnderline(uint32 s, uint32 e, SkColor c, bool t) |
| 34 : start_offset(s), |
| 35 end_offset(e), |
| 36 color(c), |
| 37 thick(t) {} |
| 38 |
| 39 uint32 start_offset; |
| 40 uint32 end_offset; |
| 41 SkColor color; |
| 42 bool thick; |
| 43 }; |
| 44 |
| 45 // WebKit only supports underline attribue for composition, so we use |
| 46 // CompositionUnderline as CompositionAttribute right now. |
| 47 typedef struct CompositionUnderline CompositionAttribute; |
| 48 typedef std::vector<CompositionAttribute> CompositionAttributeList; |
| 49 |
| 50 // TODO(penghuang) more attributes (background, foreground color and etc) |
| 51 // class CompositionAttribute { |
| 52 // public: |
| 53 // enum CompositionAttributeType{ |
| 54 // CAT_UNDERLINE = 1, |
| 55 // CAT_FORGROUND = 2, |
| 56 // CAT_BACKGRUND = 3, |
| 57 // }; |
| 58 // |
| 59 // CompositionAttributeType GetType() const { return type_; } |
| 60 // unsigned GetStartOffset() const { return start_offset_; } |
| 61 // unsigned GetEndOffset() const { return end_offset_; } |
| 62 // unsigned GetValue() const { return value_; } |
| 63 // |
| 64 // private: |
| 65 // CompositionAttributeType type_; |
| 66 // unsigned start_offset_; |
| 67 // unsigned end_offset_; |
| 68 // unsigned value_; |
| 69 // }; |
| 70 |
| 71 // CommitTextListener is notified when a text is commited from the context. |
| 72 class CommitTextListener { |
| 73 public: |
| 74 virtual void OnCommitText(IMEContext* sender, |
| 75 const string16& text) = 0; |
| 76 |
| 77 protected: |
| 78 virtual ~CommitTextListener() {} |
| 79 }; |
| 80 |
| 81 // CompositionListener is notified when composition of the context is changed. |
| 82 class CompositionListener { |
| 83 public: |
| 84 virtual void OnStartComposition(IMEContext* sender) = 0; |
| 85 virtual void OnEndComposition(IMEContext* sender) = 0; |
| 86 virtual void OnSetComposition(IMEContext* sender, |
| 87 const string16& text, |
| 88 const CompositionAttributeList& attributes, |
| 89 uint32 cursor_pos) = 0; |
| 90 |
| 91 protected: |
| 92 virtual ~CompositionListener() {} |
| 93 }; |
| 94 |
| 95 // CompositionListener is notified when a key event is forwarded from |
| 96 // the context. |
| 97 class ForwardKeyEventListener { |
| 98 public: |
| 99 virtual void OnForwardKeyEvent(IMEContext* sender, |
| 100 const KeyEvent& event) = 0; |
| 101 |
| 102 protected: |
| 103 virtual ~ForwardKeyEventListener() {} |
| 104 }; |
| 105 |
| 106 // SurroundingListener is notified when an input method context needs to |
| 107 // manipulate the text surround the input cursor. If associated view supports |
| 108 // surrounding, it should set the listener to input method context. Some input |
| 109 // methods generate different result depends on the chars before or after the |
| 110 // input cursor. |
| 111 // |
| 112 // For example: |
| 113 // Some Korean input method: |
| 114 // Key events Unicode char |
| 115 // 1 'C' +U314a |
| 116 // 2 'K' +U314f |
| 117 // 3 'C' 'K' +Ucc28 |
| 118 // |
| 119 // In case 2 and 3, when users press 'K', the input method will check the char |
| 120 // before input cursor. If the char is +U314a, it will remove the char and |
| 121 // insert a new char +Ucc28. If no char before input cursor, it will insert char |
| 122 // +U314f. |
| 123 // |
| 124 // See also OnSetSurroundingActive() and OnDeleteSurrounding(). |
| 125 class SurroundingListener { |
| 126 public: |
| 127 // Activate or inactivate surrounding support. When surrounding is activated, |
| 128 // The assoicated view should call SetSurrounding() to notify any changes of |
| 129 // text surround the input cursor. |
| 130 // Return true if associated view can support surrounding. |
| 131 virtual bool OnSetSurroundingActive(IMEContext* sender, |
| 132 bool activate) = 0; |
| 133 |
| 134 // Delete a picse of text surround the input cursor. |
| 135 // Return true if associated view delete the surrounding text successfully. |
| 136 virtual bool OnDeleteSurrounding(IMEContext* sender, |
| 137 int offset, |
| 138 int chars) = 0; |
| 139 protected: |
| 140 virtual ~SurroundingListener() {} |
| 141 }; |
| 142 |
| 143 class IMEContext { |
| 144 public: |
| 145 virtual ~IMEContext() {} |
| 146 |
| 147 // Set associated view. |
| 148 void set_view(View* view) { view_ = view; } |
| 149 |
| 150 // Get associated view. |
| 151 const View* view() const { return view_; } |
| 152 |
| 153 // Set a listener to receive a callback when im context commits a text. |
| 154 void set_commit_text_listener(CommitTextListener* listener) { |
| 155 commit_text_listener_ = listener; |
| 156 } |
| 157 |
| 158 // Set a listener to receive a callback when im context changes composition. |
| 159 void set_composition_listener(CompositionListener* listener) { |
| 160 composition_listener_ = listener; |
| 161 } |
| 162 |
| 163 // Set a listener to receive a callback when im context forwards a key event. |
| 164 void set_forward_key_event_listener(ForwardKeyEventListener* listener) { |
| 165 forward_key_event_listener_ = listener; |
| 166 } |
| 167 |
| 168 // Set a listener to receive a callback when im context need operater |
| 169 // surrounding. |
| 170 void set_surrounding_listener(SurroundingListener* listener) { |
| 171 surrounding_listener_ = listener; |
| 172 } |
| 173 |
| 174 // Tell the context it got/lost focus. |
| 175 virtual void Focus() = 0; |
| 176 virtual void Blur() = 0; |
| 177 |
| 178 // Reset context, context will be reset to initial state. |
| 179 virtual void Reset() = 0; |
| 180 |
| 181 // Filter key event, returns true, if the key event is handled, |
| 182 // associated widget should ignore it. |
| 183 virtual bool FilterKeyEvent(const views::KeyEvent& event) = 0; |
| 184 |
| 185 // Set text input cursor location on screen. |
| 186 virtual void SetCursorLocation(const gfx::Rect& caret_rect) = 0; |
| 187 |
| 188 // Set surrounding context. |
| 189 virtual void SetSurrounding(const string16& text, |
| 190 int cursor_pos) = 0; |
| 191 |
| 192 // Create an im context. |
| 193 static IMEContext* Create(View* view); |
| 194 |
| 195 protected: |
| 196 explicit IMEContext(View* view); |
| 197 |
| 198 // Commit text. |
| 199 void CommitText(const string16& text); |
| 200 |
| 201 // Start compostion. |
| 202 void StartComposition(); |
| 203 |
| 204 // End composition. |
| 205 void EndComposition(); |
| 206 |
| 207 // Set composition. |
| 208 void SetComposition(const string16& text, |
| 209 const CompositionAttributeList& attributes, |
| 210 uint32 cursor_pos); |
| 211 |
| 212 // Forward key event. |
| 213 void ForwardKeyEvent(const KeyEvent& event); |
| 214 |
| 215 // Notify the associated view whether or not the input context needs |
| 216 // surrounding. When surrounding is activated, associated view should |
| 217 // call SetSurrounding() to update any changes of text arround the input |
| 218 // cursor. |
| 219 // Return true if associated view can support surrounding. |
| 220 bool SetSurroundingActive(bool activate); |
| 221 |
| 222 // Delete surrouding arround cursor. Return true, if it is handled. |
| 223 bool DeleteSurrounding(int offset, int nchars); |
| 224 |
| 225 private: |
| 226 // Client view. |
| 227 View* view_; |
| 228 |
| 229 // Listeners: |
| 230 CommitTextListener* commit_text_listener_; |
| 231 CompositionListener* composition_listener_; |
| 232 ForwardKeyEventListener* forward_key_event_listener_; |
| 233 SurroundingListener* surrounding_listener_; |
| 234 |
| 235 DISALLOW_COPY_AND_ASSIGN(IMEContext); |
| 236 }; |
| 237 |
| 238 } // namespace views |
| 239 |
| 240 #endif // VIEWS_IME_IM_CONTEXT_H_ |
OLD | NEW |