OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 5 #ifndef UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 protected: | 58 protected: |
59 virtual ~Delegate(); | 59 virtual ~Delegate(); |
60 }; | 60 }; |
61 | 61 |
62 explicit TextfieldViewsModel(Delegate* delegate); | 62 explicit TextfieldViewsModel(Delegate* delegate); |
63 virtual ~TextfieldViewsModel(); | 63 virtual ~TextfieldViewsModel(); |
64 | 64 |
65 // Edit related methods. | 65 // Edit related methods. |
66 | 66 |
67 const base::string16& GetText() const; | 67 const base::string16& text() const { return render_text_->text(); } |
68 // Sets the text. Returns true if the text has been modified. The | 68 // Sets the text. Returns true if the text has been modified. The current |
69 // current composition text will be confirmed first. Setting | 69 // composition text will be confirmed first. Setting the same text will not |
70 // the same text will not add edit history because it's not user | 70 // add edit history because it's not user visible change nor user-initiated |
71 // visible change nor user-initiated change. This allow a client | 71 // change. This allow a client code to set the same text multiple times |
72 // code to set the same text multiple times without worrying about | 72 // without worrying about messing edit history. |
73 // messing edit history. | 73 bool SetText(const base::string16& new_text); |
74 bool SetText(const base::string16& text); | |
75 | 74 |
76 gfx::RenderText* render_text() { return render_text_.get(); } | 75 gfx::RenderText* render_text() { return render_text_.get(); } |
77 | 76 |
78 // Inserts given |text| at the current cursor position. | 77 // Inserts given |new_text| at the current cursor position. |
79 // The current composition text will be cleared. | 78 // The current composition text will be cleared. |
80 void InsertText(const base::string16& text) { | 79 void InsertText(const base::string16& new_text) { |
81 InsertTextInternal(text, false); | 80 InsertTextInternal(new_text, false); |
82 } | 81 } |
83 | 82 |
84 // Inserts a character at the current cursor position. | 83 // Inserts a character at the current cursor position. |
85 void InsertChar(base::char16 c) { | 84 void InsertChar(base::char16 c) { |
86 InsertTextInternal(base::string16(&c, 1), true); | 85 InsertTextInternal(base::string16(&c, 1), true); |
87 } | 86 } |
88 | 87 |
89 // Replaces characters at the current position with characters in given text. | 88 // Replaces characters at the current position with characters in given text. |
90 // The current composition text will be cleared. | 89 // The current composition text will be cleared. |
91 void ReplaceText(const base::string16& text) { | 90 void ReplaceText(const base::string16& new_text) { |
92 ReplaceTextInternal(text, false); | 91 ReplaceTextInternal(new_text, false); |
93 } | 92 } |
94 | 93 |
95 // Replaces the char at the current position with given character. | 94 // Replaces the char at the current position with given character. |
96 void ReplaceChar(base::char16 c) { | 95 void ReplaceChar(base::char16 c) { |
97 ReplaceTextInternal(base::string16(&c, 1), true); | 96 ReplaceTextInternal(base::string16(&c, 1), true); |
98 } | 97 } |
99 | 98 |
100 // Appends the text. | 99 // Appends the text. |
101 // The current composition text will be confirmed. | 100 // The current composition text will be confirmed. |
102 void Append(const base::string16& text); | 101 void Append(const base::string16& new_text); |
103 | 102 |
104 // Deletes the first character after the current cursor position (as if, the | 103 // Deletes the first character after the current cursor position (as if, the |
105 // the user has pressed delete key in the textfield). Returns true if | 104 // the user has pressed delete key in the textfield). Returns true if |
106 // the deletion is successful. | 105 // the deletion is successful. |
107 // If there is composition text, it'll be deleted instead. | 106 // If there is composition text, it'll be deleted instead. |
108 bool Delete(); | 107 bool Delete(); |
109 | 108 |
110 // Deletes the first character before the current cursor position (as if, the | 109 // Deletes the first character before the current cursor position (as if, the |
111 // the user has pressed backspace key in the textfield). Returns true if | 110 // the user has pressed backspace key in the textfield). Returns true if |
112 // the removal is successful. | 111 // the removal is successful. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 bool Paste(); | 187 bool Paste(); |
189 | 188 |
190 // Tells if any text is selected, even if the selection is in composition | 189 // Tells if any text is selected, even if the selection is in composition |
191 // text. | 190 // text. |
192 bool HasSelection() const; | 191 bool HasSelection() const; |
193 | 192 |
194 // Deletes the selected text. This method shouldn't be called with | 193 // Deletes the selected text. This method shouldn't be called with |
195 // composition text. | 194 // composition text. |
196 void DeleteSelection(); | 195 void DeleteSelection(); |
197 | 196 |
198 // Deletes the selected text (if any) and insert text at given | 197 // Deletes the selected text (if any) and insert text at given position. |
199 // position. | 198 void DeleteSelectionAndInsertTextAt(const base::string16& new_text, |
200 void DeleteSelectionAndInsertTextAt( | 199 size_t position); |
201 const base::string16& text, size_t position); | |
202 | 200 |
203 // Retrieves the text content in a given range. | 201 // Retrieves the text content in a given range. |
204 base::string16 GetTextFromRange(const gfx::Range& range) const; | 202 base::string16 GetTextFromRange(const gfx::Range& range) const; |
205 | 203 |
206 // Retrieves the range containing all text in the model. | 204 // Retrieves the range containing all text in the model. |
207 void GetTextRange(gfx::Range* range) const; | 205 void GetTextRange(gfx::Range* range) const; |
208 | 206 |
209 // Sets composition text and attributes. If there is composition text already, | 207 // Sets composition text and attributes. If there is composition text already, |
210 // it'll be replaced by the new one. Otherwise, current selection will be | 208 // it'll be replaced by the new one. Otherwise, current selection will be |
211 // replaced. If there is no selection, the composition text will be inserted | 209 // replaced. If there is no selection, the composition text will be inserted |
212 // at the insertion point. | 210 // at the insertion point. |
213 // Any changes to the model except text insertion will confirm the current | 211 // Any changes to the model except text insertion will confirm the current |
214 // composition text. | 212 // composition text. |
215 void SetCompositionText(const ui::CompositionText& composition); | 213 void SetCompositionText(const ui::CompositionText& composition); |
216 | 214 |
217 // Converts current composition text into final content. | 215 // Converts current composition text into final content. |
218 void ConfirmCompositionText(); | 216 void ConfirmCompositionText(); |
219 | 217 |
220 // Removes current composition text. | 218 // Removes current composition text. |
221 void CancelCompositionText(); | 219 void CancelCompositionText(); |
222 | 220 |
223 // Retrieves the range of current composition text. | 221 // Retrieves the range of current composition text. |
224 void GetCompositionTextRange(gfx::Range* range) const; | 222 void GetCompositionTextRange(gfx::Range* range) const; |
225 | 223 |
226 // Returns true if there is composition text. | 224 // Returns true if there is composition text. |
227 bool HasCompositionText() const; | 225 bool HasCompositionText() const; |
228 | 226 |
| 227 // Clears all edit history. |
| 228 void ClearEditHistory(); |
| 229 |
229 private: | 230 private: |
230 friend class NativeTextfieldViews; | |
231 friend class NativeTextfieldViewsTest; | |
232 friend class TextfieldViewsModelTest; | 231 friend class TextfieldViewsModelTest; |
233 friend class UndoRedo_BasicTest; | 232 friend class UndoRedo_BasicTest; |
234 friend class UndoRedo_CutCopyPasteTest; | 233 friend class UndoRedo_CutCopyPasteTest; |
235 friend class UndoRedo_ReplaceTest; | 234 friend class UndoRedo_ReplaceTest; |
236 friend class internal::Edit; | 235 friend class internal::Edit; |
237 | 236 |
238 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_BasicTest); | 237 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_BasicTest); |
239 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest); | 238 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest); |
240 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_ReplaceTest); | 239 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_ReplaceTest); |
241 | 240 |
242 // Insert the given |text|. |mergeable| indicates if this insert | 241 // Insert the given |new_text|. |mergeable| indicates if this insert |
243 // operation can be merged to previous edit in the edit history. | 242 // operation can be merged to previous edit in the edit history. |
244 void InsertTextInternal(const base::string16& text, bool mergeable); | 243 void InsertTextInternal(const base::string16& new_text, bool mergeable); |
245 | 244 |
246 // Replace the current text with the given |text|. |mergeable| | 245 // Replace the current text with the given |new_text|. |mergeable| |
247 // indicates if this replace operation can be merged to previous | 246 // indicates if this replace operation can be merged to previous |
248 // edit in the edit history. | 247 // edit in the edit history. |
249 void ReplaceTextInternal(const base::string16& text, bool mergeable); | 248 void ReplaceTextInternal(const base::string16& new_text, bool mergeable); |
250 | |
251 // Clears all edit history. | |
252 void ClearEditHistory(); | |
253 | 249 |
254 // Clears redo history. | 250 // Clears redo history. |
255 void ClearRedoHistory(); | 251 void ClearRedoHistory(); |
256 | 252 |
257 // Executes and records edit operations. | 253 // Executes and records edit operations. |
258 void ExecuteAndRecordDelete(gfx::Range range, bool mergeable); | 254 void ExecuteAndRecordDelete(gfx::Range range, bool mergeable); |
259 void ExecuteAndRecordReplaceSelection(internal::MergeType merge_type, | 255 void ExecuteAndRecordReplaceSelection(internal::MergeType merge_type, |
260 const base::string16& text); | 256 const base::string16& new_text); |
261 void ExecuteAndRecordReplace(internal::MergeType merge_type, | 257 void ExecuteAndRecordReplace(internal::MergeType merge_type, |
262 size_t old_cursor_pos, | 258 size_t old_cursor_pos, |
263 size_t new_cursor_pos, | 259 size_t new_cursor_pos, |
264 const base::string16& text, | 260 const base::string16& new_text, |
265 size_t new_text_start); | 261 size_t new_text_start); |
266 void ExecuteAndRecordInsert(const base::string16& text, bool mergeable); | 262 void ExecuteAndRecordInsert(const base::string16& new_text, bool mergeable); |
267 | 263 |
268 // Adds or merge |edit| into edit history. Return true if the edit | 264 // Adds or merge |edit| into edit history. Return true if the edit |
269 // has been merged and must be deleted after redo. | 265 // has been merged and must be deleted after redo. |
270 bool AddOrMergeEditHistory(internal::Edit* edit); | 266 bool AddOrMergeEditHistory(internal::Edit* edit); |
271 | 267 |
272 // Modify the text buffer in following way: | 268 // Modify the text buffer in following way: |
273 // 1) Delete the string from |delete_from| to |delte_to|. | 269 // 1) Delete the string from |delete_from| to |delte_to|. |
274 // 2) Insert the |new_text| at the index |new_text_insert_at|. | 270 // 2) Insert the |new_text| at the index |new_text_insert_at|. |
275 // Note that the index is after deletion. | 271 // Note that the index is after deletion. |
276 // 3) Move the cursor to |new_cursor_pos|. | 272 // 3) Move the cursor to |new_cursor_pos|. |
(...skipping 27 matching lines...) Expand all Loading... |
304 // 2) new edit is added. (redo history is cleared) | 300 // 2) new edit is added. (redo history is cleared) |
305 // 3) redone all undone edits. | 301 // 3) redone all undone edits. |
306 EditHistory::iterator current_edit_; | 302 EditHistory::iterator current_edit_; |
307 | 303 |
308 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); | 304 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); |
309 }; | 305 }; |
310 | 306 |
311 } // namespace views | 307 } // namespace views |
312 | 308 |
313 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ | 309 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ |
OLD | NEW |