OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 5 #ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "third_party/skia/include/core/SkColor.h" | 12 #include "third_party/skia/include/core/SkColor.h" |
13 #include "ui/base/ime/composition_text.h" | |
13 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
14 | 15 |
15 namespace gfx { | 16 namespace gfx { |
16 class Font; | 17 class Font; |
17 } // namespace gfx | 18 } // namespace gfx |
18 | 19 |
19 namespace ui { | 20 namespace ui { |
20 class Range; | 21 class Range; |
21 } // namespace ui | 22 } // namespace ui |
22 | 23 |
23 namespace views { | 24 namespace views { |
24 | 25 |
25 // A model that represents a text content for TextfieldViews. | 26 // A model that represents a text content for TextfieldViews. |
26 // It supports editing, selection and cursor manipulation. | 27 // It supports editing, selection and cursor manipulation. |
27 class TextfieldViewsModel { | 28 class TextfieldViewsModel { |
28 public: | 29 public: |
29 TextfieldViewsModel(); | 30 |
31 // Delegate interface implemented by the textfield view class to provided | |
32 // additional functionalities required by the model. | |
33 class Delegate { | |
34 public: | |
35 // Called when the current composition text is confirmed or cleared. | |
36 virtual void OnCompositionTextConfirmedOrCleared() = 0; | |
37 | |
38 protected: | |
39 virtual ~Delegate(); | |
40 }; | |
41 | |
42 explicit TextfieldViewsModel(Delegate* delegate); | |
30 virtual ~TextfieldViewsModel(); | 43 virtual ~TextfieldViewsModel(); |
31 | 44 |
32 // Text fragment info. Used to draw selected text. | 45 // Text fragment info. Used to draw selected text. |
33 // We may replace this with TextAttribute class | 46 // We may replace this with TextAttribute class |
34 // in the future to support multi-color text | 47 // in the future to support multi-color text |
35 // for omnibox. | 48 // for omnibox. |
36 struct TextFragment { | 49 struct TextFragment { |
37 TextFragment(size_t b, size_t e, bool s) | 50 TextFragment(size_t s, size_t e, bool sel, bool u) |
38 : begin(b), end(e), selected(s) { | 51 : start(s), end(e), selected(sel), underline(u) { |
39 } | 52 } |
40 // The begin and end position of text fragment. | 53 // The start and end position of text fragment. |
41 size_t begin, end; | 54 size_t start, end; |
42 // True if the text is selected. | 55 // True if the text is selected. |
43 bool selected; | 56 bool selected; |
57 // True if the text has underline. | |
58 // TODO(suzhe): support underline color and thick style. | |
59 bool underline; | |
44 }; | 60 }; |
45 typedef std::vector<TextFragment> TextFragments; | 61 typedef std::vector<TextFragment> TextFragments; |
46 | 62 |
47 // Gets the text element info. | 63 // Gets the text element info. |
48 void GetFragments(TextFragments* elements) const; | 64 void GetFragments(TextFragments* elements) const; |
49 | 65 |
50 void set_is_password(bool is_password) { | 66 void set_is_password(bool is_password) { |
51 is_password_ = is_password; | 67 is_password_ = is_password; |
52 } | 68 } |
53 const string16& text() const { return text_; } | 69 const string16& text() const { return text_; } |
54 | 70 |
55 // Edit related methods. | 71 // Edit related methods. |
56 | 72 |
57 // Sest the text. Returns true if the text has been modified. | 73 // Sest the text. Returns true if the text has been modified. |
74 // The current composition text will be confirmed first. | |
58 bool SetText(const string16& text); | 75 bool SetText(const string16& text); |
59 | 76 |
77 // Inserts given |text| at the current cursor position. | |
78 // The current composition text will be cleared. | |
79 void InsertText(const string16& text); | |
80 | |
60 // Inserts a character at the current cursor position. | 81 // Inserts a character at the current cursor position. |
61 void Insert(char16 c); | 82 void InsertChar(char16 c) { |
83 InsertText(string16(&c, 1)); | |
84 } | |
85 | |
86 // Replaces characters at the current position with characters in given text. | |
87 // The current composition text will be cleared. | |
88 void ReplaceText(const string16& text); | |
62 | 89 |
63 // Replaces the char at the current position with given character. | 90 // Replaces the char at the current position with given character. |
64 void Replace(char16 c); | 91 void ReplaceChar(char16 c) { |
92 ReplaceText(string16(&c, 1)); | |
93 } | |
65 | 94 |
66 // Appends the text. | 95 // Appends the text. |
96 // The current composition text will be confirmed. | |
67 void Append(const string16& text); | 97 void Append(const string16& text); |
68 | 98 |
69 // Deletes the first character after the current cursor position (as if, the | 99 // Deletes the first character after the current cursor position (as if, the |
70 // the user has pressed delete key in the textfield). Returns true if | 100 // the user has pressed delete key in the textfield). Returns true if |
71 // the deletion is successful. | 101 // the deletion is successful. |
102 // If there is composition text, it'll be deleted instead. | |
72 bool Delete(); | 103 bool Delete(); |
73 | 104 |
74 // Deletes the first character before the current cursor position (as if, the | 105 // Deletes the first character before the current cursor position (as if, the |
75 // the user has pressed backspace key in the textfield). Returns true if | 106 // the user has pressed backspace key in the textfield). Returns true if |
76 // the removal is successful. | 107 // the removal is successful. |
108 // If there is composition text, it'll be deleted instead. | |
77 bool Backspace(); | 109 bool Backspace(); |
78 | 110 |
79 // Cursor related methods. | 111 // Cursor related methods. |
80 | 112 |
81 // Returns the current cursor position. | 113 // Returns the current cursor position. |
82 size_t cursor_pos() const { return cursor_pos_; } | 114 size_t cursor_pos() const { return cursor_pos_; } |
83 | 115 |
84 // Moves the cursor left by one position (as if, the user has pressed the left | 116 // Moves the cursor left by one position (as if, the user has pressed the left |
85 // arrow key). If |select| is true, it updates the selection accordingly. | 117 // arrow key). If |select| is true, it updates the selection accordingly. |
118 // The current composition text will be confirmed. | |
86 void MoveCursorLeft(bool select); | 119 void MoveCursorLeft(bool select); |
87 | 120 |
88 // Moves the cursor right by one position (as if, the user has pressed the | 121 // Moves the cursor right by one position (as if, the user has pressed the |
89 // right arrow key). If |select| is true, it updates the selection | 122 // right arrow key). If |select| is true, it updates the selection |
90 // accordingly. | 123 // accordingly. |
124 // The current composition text will be confirmed. | |
91 void MoveCursorRight(bool select); | 125 void MoveCursorRight(bool select); |
92 | 126 |
93 // Moves the cursor left by one word (word boundry is defined by space). | 127 // Moves the cursor left by one word (word boundry is defined by space). |
94 // If |select| is true, it updates the selection accordingly. | 128 // If |select| is true, it updates the selection accordingly. |
129 // The current composition text will be confirmed. | |
95 void MoveCursorToPreviousWord(bool select); | 130 void MoveCursorToPreviousWord(bool select); |
96 | 131 |
97 // Moves the cursor right by one word (word boundry is defined by space). | 132 // Moves the cursor right by one word (word boundry is defined by space). |
98 // If |select| is true, it updates the selection accordingly. | 133 // If |select| is true, it updates the selection accordingly. |
134 // The current composition text will be confirmed. | |
99 void MoveCursorToNextWord(bool select); | 135 void MoveCursorToNextWord(bool select); |
100 | 136 |
101 // Moves the cursor to start of the textfield contents. | 137 // Moves the cursor to start of the textfield contents. |
102 // If |select| is true, it updates the selection accordingly. | 138 // If |select| is true, it updates the selection accordingly. |
103 void MoveCursorToStart(bool select); | 139 // The current composition text will be confirmed. |
140 void MoveCursorToHome(bool select); | |
104 | 141 |
105 // Moves the cursor to end of the textfield contents. | 142 // Moves the cursor to end of the textfield contents. |
106 // If |select| is true, it updates the selection accordingly. | 143 // If |select| is true, it updates the selection accordingly. |
144 // The current composition text will be confirmed. | |
107 void MoveCursorToEnd(bool select); | 145 void MoveCursorToEnd(bool select); |
108 | 146 |
109 // Moves the cursor to the specified |position|. | 147 // Moves the cursor to the specified |position|. |
110 // If |select| is true, it updates the selection accordingly. | 148 // If |select| is true, it updates the selection accordingly. |
149 // The current composition text will be confirmed. | |
111 bool MoveCursorTo(size_t position, bool select); | 150 bool MoveCursorTo(size_t position, bool select); |
112 | 151 |
113 // Returns the bounds of character at the current cursor. | 152 // Returns the bounds of character at the current cursor. |
114 gfx::Rect GetCursorBounds(const gfx::Font& font) const; | 153 gfx::Rect GetCursorBounds(const gfx::Font& font) const; |
115 | 154 |
116 // Selection related method | 155 // Selection related method |
117 | 156 |
118 // Returns the selected text. | 157 // Returns the selected text. |
119 string16 GetSelectedText() const; | 158 string16 GetSelectedText() const; |
120 | 159 |
121 void GetSelectedRange(ui::Range* range) const; | 160 void GetSelectedRange(ui::Range* range) const; |
122 | 161 |
162 // The current composition text will be confirmed. | |
123 void SelectRange(const ui::Range& range); | 163 void SelectRange(const ui::Range& range); |
124 | 164 |
125 // Selects all text. | 165 // Selects all text. |
166 // The current composition text will be confirmed. | |
126 void SelectAll(); | 167 void SelectAll(); |
127 | 168 |
128 // Selects the word at which the cursor is currently positioned. | 169 // Selects the word at which the cursor is currently positioned. |
170 // The current composition text will be confirmed. | |
129 void SelectWord(); | 171 void SelectWord(); |
130 | 172 |
131 // Clears selection. | 173 // Clears selection. |
174 // The current composition text will be confirmed. | |
132 void ClearSelection(); | 175 void ClearSelection(); |
133 | 176 |
134 // Returns visible text. If the field is password, it returns the | 177 // Returns visible text. If the field is password, it returns the |
135 // sequence of "*". | 178 // sequence of "*". |
136 string16 GetVisibleText() const { | 179 string16 GetVisibleText() const { |
137 return GetVisibleText(0U, text_.length()); | 180 return GetVisibleText(0U, text_.length()); |
138 } | 181 } |
139 | 182 |
140 // Cuts the currently selected text and puts it to clipboard. Returns true | 183 // Cuts the currently selected text and puts it to clipboard. Returns true |
141 // if text has changed after cutting. | 184 // if text has changed after cutting. |
142 bool Cut(); | 185 bool Cut(); |
143 | 186 |
144 // Copies the currently selected text and puts it to clipboard. | 187 // Copies the currently selected text and puts it to clipboard. |
145 void Copy(); | 188 void Copy(); |
146 | 189 |
147 // Pastes text from the clipboard at current cursor position. Returns true | 190 // Pastes text from the clipboard at current cursor position. Returns true |
148 // if text has changed after pasting. | 191 // if text has changed after pasting. |
149 bool Paste(); | 192 bool Paste(); |
150 | 193 |
151 // Tells if any text is selected. | 194 // Tells if any text is selected, even if the selection is in composition |
195 // text. | |
152 bool HasSelection() const; | 196 bool HasSelection() const; |
153 | 197 |
198 // Deletes the selected text. This method shouldn't be called with | |
199 // composition text. | |
200 void DeleteSelection(); | |
201 | |
202 // Retrieves the text content in a given range. | |
203 string16 GetTextFromRange(const ui::Range& range) const; | |
204 | |
205 // Retrieves the range containing all text in the model. | |
206 void GetTextRange(ui::Range* range) const; | |
207 | |
208 // Sets composition text and attributes. If there is composition text already, | |
209 // it’ll be replaced by the new one. Otherwise, current selection will be | |
210 // replaced. If there is no selection, the composition text will be inserted | |
211 // at the insertion point. | |
212 // Any changes to the model except text insertion will confirm the current | |
213 // composition text. | |
214 void SetCompositionText(const ui::CompositionText& composition); | |
215 | |
216 // Converts current composition text into final content. | |
217 void ConfirmCompositionText(); | |
218 | |
219 // Removes current composition text. | |
220 void ClearCompositionText(); | |
221 | |
222 // Retrieves the range of current composition text. | |
223 void GetCompositionTextRange(ui::Range* range) const; | |
224 | |
225 // Returns true if there is composition text. | |
226 bool HasCompositionText() const; | |
227 | |
154 private: | 228 private: |
155 friend class NativeTextfieldViews; | 229 friend class NativeTextfieldViews; |
156 | 230 |
157 // Deletes the selected text. | |
158 void DeleteSelection(); | |
159 | |
160 // Returns the visible text given |start| and |end|. | 231 // Returns the visible text given |start| and |end|. |
161 string16 GetVisibleText(size_t start, size_t end) const; | 232 string16 GetVisibleText(size_t start, size_t end) const; |
162 | 233 |
163 // Utility for SelectWord(). Checks whether position pos is at word boundary. | 234 // Utility for SelectWord(). Checks whether position pos is at word boundary. |
164 bool IsPositionAtWordSelectionBoundary(size_t pos); | 235 bool IsPositionAtWordSelectionBoundary(size_t pos); |
165 | 236 |
166 // Returns the normalized cursor position that does not exceed the | 237 // Returns the normalized cursor position that does not exceed the |
167 // text length. | 238 // text length. |
168 size_t GetSafePosition(size_t position) const; | 239 size_t GetSafePosition(size_t position) const; |
169 | 240 |
241 Delegate* delegate_; | |
oshima
2011/03/24 22:58:47
comment
James Su
2011/03/25 05:57:21
Done.
| |
242 | |
170 // The text in utf16 format. | 243 // The text in utf16 format. |
171 string16 text_; | 244 string16 text_; |
172 | 245 |
173 // Current cursor position. | 246 // Current cursor position. |
174 size_t cursor_pos_; | 247 size_t cursor_pos_; |
175 | 248 |
176 // Selection range. | 249 // Selection range. |
177 size_t selection_begin_; | 250 size_t selection_start_; |
251 | |
252 // Composition text range. | |
253 size_t composition_start_; | |
254 size_t composition_end_; | |
255 | |
256 // Underline information of the composition text. | |
257 ui::CompositionUnderlines composition_underlines_; | |
178 | 258 |
179 // True if the text is the password. | 259 // True if the text is the password. |
180 bool is_password_; | 260 bool is_password_; |
181 | 261 |
182 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); | 262 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); |
183 }; | 263 }; |
184 | 264 |
185 } // namespace views | 265 } // namespace views |
186 | 266 |
187 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 267 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
OLD | NEW |