Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(116)

Side by Side Diff: views/controls/textfield/textfield_views_model.h

Issue 7265011: RenderText API Outline. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Exclude Mac, fix font comparison. Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <list> 9 #include <list>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/gtest_prod_util.h" 12 #include "base/gtest_prod_util.h"
13 #include "base/string16.h" 13 #include "base/string16.h"
14 #include "third_party/skia/include/core/SkColor.h" 14 #include "third_party/skia/include/core/SkColor.h"
15 #include "ui/base/ime/composition_text.h" 15 #include "ui/base/ime/composition_text.h"
16 #include "ui/gfx/rect.h" 16 #include "ui/gfx/rect.h"
17 17
18 namespace gfx { 18 namespace gfx {
19 class Canvas;
19 class Font; 20 class Font;
21 class RenderText;
22 struct StyleRange;
20 } // namespace gfx 23 } // namespace gfx
21 24
22 namespace ui { 25 namespace ui {
23 class Range; 26 class Range;
24 } // namespace ui 27 } // namespace ui
25 28
26 namespace views { 29 namespace views {
27 30
28 class TextStyle;
29 typedef std::vector<TextStyle*> TextStyles;
30
31 namespace internal { 31 namespace internal {
32 // Internal Edit class that keeps track of edits for undo/redo. 32 // Internal Edit class that keeps track of edits for undo/redo.
33 class Edit; 33 class Edit;
34 34
35 struct TextStyleRange;
36
37 // C++ doesn't allow forward decl enum, so let's define here. 35 // C++ doesn't allow forward decl enum, so let's define here.
38 enum MergeType { 36 enum MergeType {
39 // The edit should not be merged with next edit. It still may 37 // The edit should not be merged with next edit. It still may
40 // be merged with an edit with MERGE_WITH_PREVIOUS. 38 // be merged with an edit with MERGE_WITH_PREVIOUS.
41 DO_NOT_MERGE, 39 DO_NOT_MERGE,
42 // The edit can be merged with next edit when possible. 40 // The edit can be merged with next edit when possible.
43 MERGEABLE, 41 MERGEABLE,
44 // Does the edit have to be merged with previous edit? 42 // Does the edit have to be merged with previous edit?
45 // This forces the merge even if the previous edit is marked 43 // This forces the merge even if the previous edit is marked
46 // as DO_NOT_MERGE. 44 // as DO_NOT_MERGE.
47 MERGE_WITH_PREVIOUS, 45 MERGE_WITH_PREVIOUS,
48 }; 46 };
49 47
50 } // namespace internal 48 } // namespace internal
51 49
52 typedef std::vector<internal::TextStyleRange*> TextStyleRanges;
53
54 // A model that represents a text content for TextfieldViews. 50 // A model that represents a text content for TextfieldViews.
55 // It supports editing, selection and cursor manipulation. 51 // It supports editing, selection and cursor manipulation.
56 class TextfieldViewsModel { 52 class TextfieldViewsModel {
57 public: 53 public:
58 54
59 // Delegate interface implemented by the textfield view class to provided 55 // Delegate interface implemented by the textfield view class to provided
60 // additional functionalities required by the model. 56 // additional functionalities required by the model.
61 class Delegate { 57 class Delegate {
62 public: 58 public:
63 // Called when the current composition text is confirmed or cleared. 59 // Called when the current composition text is confirmed or cleared.
64 virtual void OnCompositionTextConfirmedOrCleared() = 0; 60 virtual void OnCompositionTextConfirmedOrCleared() = 0;
65 61
66 protected: 62 protected:
67 virtual ~Delegate(); 63 virtual ~Delegate();
68 }; 64 };
69 65
70 explicit TextfieldViewsModel(Delegate* delegate); 66 explicit TextfieldViewsModel(Delegate* delegate);
71 virtual ~TextfieldViewsModel(); 67 virtual ~TextfieldViewsModel();
72 68
73 // Text fragment info. Used to draw selected text.
74 // We may replace this with TextAttribute class
75 // in the future to support multi-color text
76 // for omnibox.
77 struct TextFragment {
78 TextFragment(size_t start, size_t end, const views::TextStyle* s)
79 : range(start, end), style(s) {
80 }
81 // The start and end position of text fragment.
82 ui::Range range;
83 const TextStyle* style;
84 };
85 typedef std::vector<TextFragment> TextFragments;
86
87 // Gets the text element info.
88 void GetFragments(TextFragments* elements);
89
90 void set_is_password(bool is_password) { 69 void set_is_password(bool is_password) {
91 is_password_ = is_password; 70 is_password_ = is_password;
92 } 71 }
93 const string16& text() const { return text_; }
94 72
95 // Edit related methods. 73 // Edit related methods.
96 74
97 // Sest the text. Returns true if the text has been modified. The 75 const string16& GetText() const;
76 // Sets the text. Returns true if the text has been modified. The
98 // current composition text will be confirmed first. Setting 77 // current composition text will be confirmed first. Setting
99 // the same text will not add edit history because it's not user 78 // the same text will not add edit history because it's not user
100 // visible change nor user-initiated change. This allow a client 79 // visible change nor user-initiated change. This allow a client
101 // code to set the same text multiple times without worrying about 80 // code to set the same text multiple times without worrying about
102 // messing edit history. 81 // messing edit history.
103 bool SetText(const string16& text); 82 bool SetText(const string16& text);
104 83
84 gfx::RenderText* get_render_text() { return render_text_; }
85
105 // Inserts given |text| at the current cursor position. 86 // Inserts given |text| at the current cursor position.
106 // The current composition text will be cleared. 87 // The current composition text will be cleared.
107 void InsertText(const string16& text) { 88 void InsertText(const string16& text) {
108 InsertTextInternal(text, false); 89 InsertTextInternal(text, false);
109 } 90 }
110 91
111 // Inserts a character at the current cursor position. 92 // Inserts a character at the current cursor position.
112 void InsertChar(char16 c) { 93 void InsertChar(char16 c) {
113 InsertTextInternal(string16(&c, 1), true); 94 InsertTextInternal(string16(&c, 1), true);
114 } 95 }
(...skipping 21 matching lines...) Expand all
136 117
137 // Deletes the first character before the current cursor position (as if, the 118 // Deletes the first character before the current cursor position (as if, the
138 // the user has pressed backspace key in the textfield). Returns true if 119 // the user has pressed backspace key in the textfield). Returns true if
139 // the removal is successful. 120 // the removal is successful.
140 // If there is composition text, it'll be deleted instead. 121 // If there is composition text, it'll be deleted instead.
141 bool Backspace(); 122 bool Backspace();
142 123
143 // Cursor related methods. 124 // Cursor related methods.
144 125
145 // Returns the current cursor position. 126 // Returns the current cursor position.
146 size_t cursor_pos() const { return cursor_pos_; } 127 size_t GetCursorPosition() const;
147 128
148 // Moves the cursor left by one position (as if, the user has pressed the left 129 // Moves the cursor left by one position (as if, the user has pressed the left
149 // arrow key). If |select| is true, it updates the selection accordingly. 130 // arrow key). If |select| is true, it updates the selection accordingly.
150 // The current composition text will be confirmed. 131 // The current composition text will be confirmed.
151 void MoveCursorLeft(bool select); 132 void MoveCursorLeft(bool select);
152 133
153 // Moves the cursor right by one position (as if, the user has pressed the 134 // Moves the cursor right by one position (as if, the user has pressed the
154 // right arrow key). If |select| is true, it updates the selection 135 // right arrow key). If |select| is true, it updates the selection
155 // accordingly. 136 // accordingly.
156 // The current composition text will be confirmed. 137 // The current composition text will be confirmed.
157 void MoveCursorRight(bool select); 138 void MoveCursorRight(bool select);
158 139
159 // Moves the cursor left by one word (word boundry is defined by space). 140 // Moves the cursor left by one word (word boundry is defined by space).
160 // If |select| is true, it updates the selection accordingly. 141 // If |select| is true, it updates the selection accordingly.
161 // The current composition text will be confirmed. 142 // The current composition text will be confirmed.
162 void MoveCursorToPreviousWord(bool select); 143 void MoveCursorLeftByWord(bool select);
163 144
164 // Moves the cursor right by one word (word boundry is defined by space). 145 // Moves the cursor right by one word (word boundry is defined by space).
165 // If |select| is true, it updates the selection accordingly. 146 // If |select| is true, it updates the selection accordingly.
166 // The current composition text will be confirmed. 147 // The current composition text will be confirmed.
167 void MoveCursorToNextWord(bool select); 148 void MoveCursorRightByWord(bool select);
168 149
169 // Moves the cursor to start of the textfield contents. 150 // Moves the cursor to start of the textfield contents.
170 // If |select| is true, it updates the selection accordingly. 151 // If |select| is true, it updates the selection accordingly.
171 // The current composition text will be confirmed. 152 // The current composition text will be confirmed.
172 void MoveCursorToHome(bool select); 153 void MoveCursorToLeftEnd(bool select);
173 154
174 // Moves the cursor to end of the textfield contents. 155 // Moves the cursor to end of the textfield contents.
175 // If |select| is true, it updates the selection accordingly. 156 // If |select| is true, it updates the selection accordingly.
176 // The current composition text will be confirmed. 157 // The current composition text will be confirmed.
177 void MoveCursorToEnd(bool select); 158 void MoveCursorToRightEnd(bool select);
178 159
179 // Moves the cursor to the specified |position|. 160 // Moves the cursor to the specified |position|.
180 // If |select| is true, it updates the selection accordingly. 161 // If |select| is true, it updates the selection accordingly.
181 // The current composition text will be confirmed. 162 // The current composition text will be confirmed.
182 bool MoveCursorTo(size_t position, bool select); 163 bool MoveCursorTo(size_t position, bool select);
183 164
184 // Returns the bounds of selected text. 165 // Returns the bounds of selected text.
185 gfx::Rect GetSelectionBounds(const gfx::Font& font) const; 166 std::vector<gfx::Rect> GetSelectionBounds() const;
186 167
187 // Selection related method 168 // Selection related method
188 169
189 // Returns the selected text. 170 // Returns the selected text.
190 string16 GetSelectedText() const; 171 string16 GetSelectedText() const;
191 172
192 void GetSelectedRange(ui::Range* range) const; 173 void GetSelectedRange(ui::Range* range) const;
193 174
194 // The current composition text will be confirmed. The 175 // The current composition text will be confirmed. The
195 // selection starts with the range's start position, 176 // selection starts with the range's start position,
(...skipping 20 matching lines...) Expand all
216 bool CanRedo(); 197 bool CanRedo();
217 198
218 // Undo edit. Returns true if undo changed the text. 199 // Undo edit. Returns true if undo changed the text.
219 bool Undo(); 200 bool Undo();
220 201
221 // Redo edit. Returns true if redo changed the text. 202 // Redo edit. Returns true if redo changed the text.
222 bool Redo(); 203 bool Redo();
223 204
224 // Returns visible text. If the field is password, it returns the 205 // Returns visible text. If the field is password, it returns the
225 // sequence of "*". 206 // sequence of "*".
226 string16 GetVisibleText() const { 207 string16 GetVisibleText() const;
227 return GetVisibleText(0U, text_.length());
228 }
229 208
230 // Cuts the currently selected text and puts it to clipboard. Returns true 209 // Cuts the currently selected text and puts it to clipboard. Returns true
231 // if text has changed after cutting. 210 // if text has changed after cutting.
232 bool Cut(); 211 bool Cut();
233 212
234 // Copies the currently selected text and puts it to clipboard. 213 // Copies the currently selected text and puts it to clipboard.
235 void Copy(); 214 void Copy();
236 215
237 // Pastes text from the clipboard at current cursor position. Returns true 216 // Pastes text from the clipboard at current cursor position. Returns true
238 // if text has changed after pasting. 217 // if text has changed after pasting.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 249
271 // Removes current composition text. 250 // Removes current composition text.
272 void CancelCompositionText(); 251 void CancelCompositionText();
273 252
274 // Retrieves the range of current composition text. 253 // Retrieves the range of current composition text.
275 void GetCompositionTextRange(ui::Range* range) const; 254 void GetCompositionTextRange(ui::Range* range) const;
276 255
277 // Returns true if there is composition text. 256 // Returns true if there is composition text.
278 bool HasCompositionText() const; 257 bool HasCompositionText() const;
279 258
280 TextStyle* CreateTextStyle();
281
282 void ClearAllTextStyles();
283
284 private: 259 private:
285 friend class NativeTextfieldViews; 260 friend class NativeTextfieldViews;
286 friend class NativeTextfieldViewsTest; 261 friend class NativeTextfieldViewsTest;
287 friend class TextfieldViewsModelTest; 262 friend class TextfieldViewsModelTest;
288 friend class TextStyle;
289 friend class UndoRedo_BasicTest; 263 friend class UndoRedo_BasicTest;
290 friend class UndoRedo_CutCopyPasteTest; 264 friend class UndoRedo_CutCopyPasteTest;
291 friend class UndoRedo_ReplaceTest; 265 friend class UndoRedo_ReplaceTest;
292 friend class internal::Edit; 266 friend class internal::Edit;
293 267
294 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_BasicTest); 268 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_BasicTest);
295 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest); 269 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_CutCopyPasteTest);
296 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_ReplaceTest); 270 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, UndoRedo_ReplaceTest);
297 FRIEND_TEST_ALL_PREFIXES(TextfieldViewsModelTest, TextStyleTest);
298 271
299 // Returns the visible text given |start| and |end|. 272 // Returns the visible text given |start| and |end|.
300 string16 GetVisibleText(size_t start, size_t end) const; 273 string16 GetVisibleText(size_t start, size_t end) const;
301 274
302 // Utility for SelectWord(). Checks whether position pos is at word boundary.
303 bool IsPositionAtWordSelectionBoundary(size_t pos);
304
305 // Returns the normalized cursor position that does not exceed the
306 // text length.
307 size_t GetSafePosition(size_t position) const;
308
309 // Insert the given |text|. |mergeable| indicates if this insert 275 // Insert the given |text|. |mergeable| indicates if this insert
310 // operation can be merged to previous edit in the edit history. 276 // operation can be merged to previous edit in the edit history.
311 void InsertTextInternal(const string16& text, bool mergeable); 277 void InsertTextInternal(const string16& text, bool mergeable);
312 278
313 // Replace the current text with the given |text|. |mergeable| 279 // Replace the current text with the given |text|. |mergeable|
314 // indicates if this replace operation can be merged to previous 280 // indicates if this replace operation can be merged to previous
315 // edit in the edit history. 281 // edit in the edit history.
316 void ReplaceTextInternal(const string16& text, bool mergeable); 282 void ReplaceTextInternal(const string16& text, bool mergeable);
317 283
318 // Clears all edit history. 284 // Clears all edit history.
(...skipping 23 matching lines...) Expand all
342 // Note that the index is after deletion. 308 // Note that the index is after deletion.
343 // 3) Move the cursor to |new_cursor_pos|. 309 // 3) Move the cursor to |new_cursor_pos|.
344 void ModifyText(size_t delete_from, 310 void ModifyText(size_t delete_from,
345 size_t delete_to, 311 size_t delete_to,
346 const string16& new_text, 312 const string16& new_text,
347 size_t new_text_insert_at, 313 size_t new_text_insert_at,
348 size_t new_cursor_pos); 314 size_t new_cursor_pos);
349 315
350 void ClearComposition(); 316 void ClearComposition();
351 317
352 void ApplyTextStyle(const TextStyle* style, const ui::Range& range);
353
354 static TextStyle* CreateUnderlineStyle();
355
356 // Pointer to a TextfieldViewsModel::Delegate instance, should be provided by 318 // Pointer to a TextfieldViewsModel::Delegate instance, should be provided by
357 // the View object. 319 // the View object.
358 Delegate* delegate_; 320 Delegate* delegate_;
359 321
360 // The text in utf16 format. 322 // The stylized text, cursor, selection, and the visual layout model.
361 string16 text_; 323 gfx::RenderText* render_text_;
362
363 // Current cursor position.
364 size_t cursor_pos_;
365
366 // Selection range.
367 size_t selection_start_;
368
369 // Composition text range.
370 size_t composition_start_;
371 size_t composition_end_;
372 324
373 // True if the text is the password. 325 // True if the text is the password.
374 bool is_password_; 326 bool is_password_;
375 327
376 typedef std::list<internal::Edit*> EditHistory; 328 typedef std::list<internal::Edit*> EditHistory;
377 EditHistory edit_history_; 329 EditHistory edit_history_;
378 330
379 // An iterator that points to the current edit that can be undone. 331 // An iterator that points to the current edit that can be undone.
380 // This iterator moves from the |end()|, meaining no edit to undo, 332 // This iterator moves from the |end()|, meaining no edit to undo,
381 // to the last element (one before |end()|), meaning no edit to redo. 333 // to the last element (one before |end()|), meaning no edit to redo.
382 // There is no edit to undo (== end()) when: 334 // There is no edit to undo (== end()) when:
383 // 1) in initial state. (nothing to undo) 335 // 1) in initial state. (nothing to undo)
384 // 2) very 1st edit is undone. 336 // 2) very 1st edit is undone.
385 // 3) all edit history is removed. 337 // 3) all edit history is removed.
386 // There is no edit to redo (== last element or no element) when: 338 // There is no edit to redo (== last element or no element) when:
387 // 1) in initial state. (nothing to redo) 339 // 1) in initial state. (nothing to redo)
388 // 2) new edit is added. (redo history is cleared) 340 // 2) new edit is added. (redo history is cleared)
389 // 3) redone all undone edits. 341 // 3) redone all undone edits.
390 EditHistory::iterator current_edit_; 342 EditHistory::iterator current_edit_;
391 343
392 // This manages all styles objects.
393 TextStyles text_styles_;
394
395 // List of style ranges. Elements in the list never overlap each other.
396 // Elements are not sorted at the time of insertion, and gets sorted
397 // when it's painted (if necessary).
398 TextStyleRanges style_ranges_;
399 // True if the style_ranges_ needs to be sorted.
400 bool sort_style_ranges_;
401
402 // List of style ranges for composition text.
403 TextStyleRanges composition_style_ranges_;
404
405 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel); 344 DISALLOW_COPY_AND_ASSIGN(TextfieldViewsModel);
406 }; 345 };
407 346
408 } // namespace views 347 } // namespace views
409 348
410 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_ 349 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEWS_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698