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