| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_MODEL_H_ | 5 #ifndef UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_MODEL_H_ |
| 6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_MODEL_H_ | 6 #define UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_MODEL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 // The edit should not usually be merged with next edit. | 30 // The edit should not usually be merged with next edit. |
| 31 DO_NOT_MERGE, | 31 DO_NOT_MERGE, |
| 32 // The edit should be merged with next edit when possible. | 32 // The edit should be merged with next edit when possible. |
| 33 MERGEABLE, | 33 MERGEABLE, |
| 34 // The edit should be merged with the prior edit, even if marked DO_NOT_MERGE. | 34 // The edit should be merged with the prior edit, even if marked DO_NOT_MERGE. |
| 35 FORCE_MERGE, | 35 FORCE_MERGE, |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 } // namespace internal | 38 } // namespace internal |
| 39 | 39 |
| 40 namespace test { |
| 41 class BridgedNativeWidgetTest; |
| 42 } // namsespace test |
| 43 |
| 40 // A model that represents text content for a views::Textfield. | 44 // A model that represents text content for a views::Textfield. |
| 41 // It supports editing, selection and cursor manipulation. | 45 // It supports editing, selection and cursor manipulation. |
| 42 class VIEWS_EXPORT TextfieldModel { | 46 class VIEWS_EXPORT TextfieldModel { |
| 43 public: | 47 public: |
| 44 // Delegate interface implemented by the textfield view class to provide | 48 // Delegate interface implemented by the textfield view class to provide |
| 45 // additional functionalities required by the model. | 49 // additional functionalities required by the model. |
| 46 class VIEWS_EXPORT Delegate { | 50 class VIEWS_EXPORT Delegate { |
| 47 public: | 51 public: |
| 48 // Called when the current composition text is confirmed or cleared. | 52 // Called when the current composition text is confirmed or cleared. |
| 49 virtual void OnCompositionTextConfirmedOrCleared() = 0; | 53 virtual void OnCompositionTextConfirmedOrCleared() = 0; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 void ReplaceChar(base::char16 c) { | 92 void ReplaceChar(base::char16 c) { |
| 89 ReplaceTextInternal(base::string16(&c, 1), true); | 93 ReplaceTextInternal(base::string16(&c, 1), true); |
| 90 } | 94 } |
| 91 | 95 |
| 92 // Appends the text. | 96 // Appends the text. |
| 93 // The current composition text will be confirmed. | 97 // The current composition text will be confirmed. |
| 94 void Append(const base::string16& new_text); | 98 void Append(const base::string16& new_text); |
| 95 | 99 |
| 96 // Deletes the first character after the current cursor position (as if, the | 100 // Deletes the first character after the current cursor position (as if, the |
| 97 // the user has pressed delete key in the textfield). Returns true if | 101 // the user has pressed delete key in the textfield). Returns true if |
| 98 // the deletion is successful. | 102 // the deletion is successful. If |add_to_kill_buffer| is true, the deleted |
| 103 // text is copied to the kill buffer. |
| 99 // If there is composition text, it'll be deleted instead. | 104 // If there is composition text, it'll be deleted instead. |
| 100 bool Delete(); | 105 bool Delete(bool add_to_kill_buffer = false); |
| 101 | 106 |
| 102 // Deletes the first character before the current cursor position (as if, the | 107 // Deletes the first character before the current cursor position (as if, the |
| 103 // the user has pressed backspace key in the textfield). Returns true if | 108 // the user has pressed backspace key in the textfield). Returns true if |
| 104 // the removal is successful. | 109 // the removal is successful. If |add_to_kill_buffer| is true, the deleted |
| 110 // text is copied to the kill buffer. |
| 105 // If there is composition text, it'll be deleted instead. | 111 // If there is composition text, it'll be deleted instead. |
| 106 bool Backspace(); | 112 bool Backspace(bool add_to_kill_buffer = false); |
| 107 | 113 |
| 108 // Cursor related methods. | 114 // Cursor related methods. |
| 109 | 115 |
| 110 // Returns the current cursor position. | 116 // Returns the current cursor position. |
| 111 size_t GetCursorPosition() const; | 117 size_t GetCursorPosition() const; |
| 112 | 118 |
| 113 // Moves the cursor, see RenderText for additional details. | 119 // Moves the cursor, see RenderText for additional details. |
| 114 // The current composition text will be confirmed. | 120 // The current composition text will be confirmed. |
| 115 void MoveCursor(gfx::BreakType break_type, | 121 void MoveCursor(gfx::BreakType break_type, |
| 116 gfx::VisualCursorDirection direction, | 122 gfx::VisualCursorDirection direction, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 181 |
| 176 // Pastes text from the clipboard at current cursor position. Returns true | 182 // Pastes text from the clipboard at current cursor position. Returns true |
| 177 // if any text is pasted. | 183 // if any text is pasted. |
| 178 bool Paste(); | 184 bool Paste(); |
| 179 | 185 |
| 180 // Transposes the characters to either side of the insertion point and | 186 // Transposes the characters to either side of the insertion point and |
| 181 // advances the insertion point past both of them. Returns true if text is | 187 // advances the insertion point past both of them. Returns true if text is |
| 182 // changed. | 188 // changed. |
| 183 bool Transpose(); | 189 bool Transpose(); |
| 184 | 190 |
| 191 // Pastes text from the kill buffer at the current cursor position. Returns |
| 192 // true if the text has changed after yanking. |
| 193 bool Yank(); |
| 194 |
| 185 // Tells if any text is selected, even if the selection is in composition | 195 // Tells if any text is selected, even if the selection is in composition |
| 186 // text. | 196 // text. |
| 187 bool HasSelection() const; | 197 bool HasSelection() const; |
| 188 | 198 |
| 189 // Deletes the selected text. This method shouldn't be called with | 199 // Deletes the selected text. This method shouldn't be called with |
| 190 // composition text. | 200 // composition text. |
| 191 void DeleteSelection(); | 201 void DeleteSelection(); |
| 192 | 202 |
| 193 // Deletes the selected text (if any) and insert text at given position. | 203 // Deletes the selected text (if any) and insert text at given position. |
| 194 void DeleteSelectionAndInsertTextAt(const base::string16& new_text, | 204 void DeleteSelectionAndInsertTextAt(const base::string16& new_text, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 218 void GetCompositionTextRange(gfx::Range* range) const; | 228 void GetCompositionTextRange(gfx::Range* range) const; |
| 219 | 229 |
| 220 // Returns true if there is composition text. | 230 // Returns true if there is composition text. |
| 221 bool HasCompositionText() const; | 231 bool HasCompositionText() const; |
| 222 | 232 |
| 223 // Clears all edit history. | 233 // Clears all edit history. |
| 224 void ClearEditHistory(); | 234 void ClearEditHistory(); |
| 225 | 235 |
| 226 private: | 236 private: |
| 227 friend class internal::Edit; | 237 friend class internal::Edit; |
| 238 friend class test::BridgedNativeWidgetTest; |
| 239 friend class TextfieldModelTest; |
| 240 friend class TextfieldTest; |
| 228 | 241 |
| 229 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_BasicTest); | 242 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_BasicTest); |
| 230 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_CutCopyPasteTest); | 243 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_CutCopyPasteTest); |
| 231 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_ReplaceTest); | 244 FRIEND_TEST_ALL_PREFIXES(TextfieldModelTest, UndoRedo_ReplaceTest); |
| 232 | 245 |
| 233 // Insert the given |new_text|. |mergeable| indicates if this insert operation | 246 // Insert the given |new_text|. |mergeable| indicates if this insert operation |
| 234 // can be merged with previous edits in the history. | 247 // can be merged with previous edits in the history. |
| 235 void InsertTextInternal(const base::string16& new_text, bool mergeable); | 248 void InsertTextInternal(const base::string16& new_text, bool mergeable); |
| 236 | 249 |
| 237 // Replace the current text with the given |new_text|. |mergeable| indicates | 250 // Replace the current text with the given |new_text|. |mergeable| indicates |
| (...skipping 24 matching lines...) Expand all Loading... |
| 262 // Note that the index is after deletion. | 275 // Note that the index is after deletion. |
| 263 // 3) Move the cursor to |new_cursor_pos|. | 276 // 3) Move the cursor to |new_cursor_pos|. |
| 264 void ModifyText(size_t delete_from, | 277 void ModifyText(size_t delete_from, |
| 265 size_t delete_to, | 278 size_t delete_to, |
| 266 const base::string16& new_text, | 279 const base::string16& new_text, |
| 267 size_t new_text_insert_at, | 280 size_t new_text_insert_at, |
| 268 size_t new_cursor_pos); | 281 size_t new_cursor_pos); |
| 269 | 282 |
| 270 void ClearComposition(); | 283 void ClearComposition(); |
| 271 | 284 |
| 285 // Clears the kill buffer. Used to clear global state between tests. |
| 286 static void ClearKillBuffer(); |
| 287 |
| 272 // The TextfieldModel::Delegate instance should be provided by the owner. | 288 // The TextfieldModel::Delegate instance should be provided by the owner. |
| 273 Delegate* delegate_; | 289 Delegate* delegate_; |
| 274 | 290 |
| 275 // The stylized text, cursor, selection, and the visual layout model. | 291 // The stylized text, cursor, selection, and the visual layout model. |
| 276 std::unique_ptr<gfx::RenderText> render_text_; | 292 std::unique_ptr<gfx::RenderText> render_text_; |
| 277 | 293 |
| 278 // The composition range. | 294 // The composition range. |
| 279 gfx::Range composition_range_; | 295 gfx::Range composition_range_; |
| 280 | 296 |
| 281 typedef std::list<internal::Edit*> EditHistory; | 297 typedef std::list<internal::Edit*> EditHistory; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 294 // 2) new edit is added. (redo history is cleared) | 310 // 2) new edit is added. (redo history is cleared) |
| 295 // 3) redone all undone edits. | 311 // 3) redone all undone edits. |
| 296 EditHistory::iterator current_edit_; | 312 EditHistory::iterator current_edit_; |
| 297 | 313 |
| 298 DISALLOW_COPY_AND_ASSIGN(TextfieldModel); | 314 DISALLOW_COPY_AND_ASSIGN(TextfieldModel); |
| 299 }; | 315 }; |
| 300 | 316 |
| 301 } // namespace views | 317 } // namespace views |
| 302 | 318 |
| 303 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_MODEL_H_ | 319 #endif // UI_VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_MODEL_H_ |
| OLD | NEW |