Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEW_MODEL_H_ | |
| 6 #define VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEW_MODEL_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/string16.h" | |
| 12 #include "gfx/rect.h" | |
| 13 #include "third_party/skia/include/core/SkColor.h" | |
| 14 | |
| 15 namespace gfx { | |
| 16 class Font; | |
| 17 } // namespace gfx | |
| 18 | |
| 19 namespace views { | |
| 20 | |
| 21 class TextfieldViewModel { | |
|
sky
2010/12/15 20:31:27
Description?
oshima
2010/12/16 01:15:19
Done.
| |
| 22 public: | |
| 23 TextfieldViewModel(); | |
| 24 virtual ~TextfieldViewModel(); | |
|
sky
2010/12/15 20:31:27
Why virtual?
oshima
2010/12/16 01:15:19
it's just my habit to always use virtual destructo
| |
| 25 | |
| 26 // Text fragment info. Used to draw selected text. | |
| 27 // We may replace this with TextAttribute class | |
| 28 // in the future to supportmulti-color text | |
|
sky
2010/12/15 20:31:27
support multi
oshima
2010/12/16 01:15:19
Done.
| |
| 29 // for omnibox. | |
| 30 struct TextFragment { | |
| 31 TextFragment(size_t b, size_t e, bool s) | |
| 32 : begin(b), end(e), selected(s) { | |
| 33 } | |
| 34 // The begin and end position of text fragment. | |
| 35 size_t begin, end; | |
| 36 // True if the text is selected. | |
| 37 bool selected; | |
| 38 }; | |
| 39 typedef std::vector<TextFragment> TextFragments; | |
| 40 | |
| 41 // Populate the text element info. | |
| 42 void PopulateFragments(TextFragments* elements) const; | |
|
sky
2010/12/15 20:31:27
How about GetFragments.
oshima
2010/12/16 01:15:19
Done.
| |
| 43 | |
| 44 void set_is_password(bool is_password) { | |
| 45 is_password_ = is_password; | |
| 46 } | |
| 47 const string16& text() const { return text_; } | |
| 48 | |
| 49 // Edit related methods. | |
| 50 | |
| 51 // Set the text. Returns true if the text has been modified. | |
| 52 bool SetText(const string16& text); | |
| 53 | |
| 54 // Inserts a character at the current cursor position. | |
| 55 void Insert(char16 c); | |
| 56 | |
| 57 // Replaces the char at the current position with given character. | |
| 58 void Replace(char16 c); | |
| 59 | |
| 60 // Appends the text. | |
| 61 void Append(const string16& text); | |
| 62 | |
| 63 // Deletes the first character after the current cursor position (as if, the | |
| 64 // the user has pressed delete key in the textfield). Returns true if | |
| 65 // the deletion is successful. | |
| 66 bool Delete(); | |
| 67 | |
| 68 // Deletes the first character before the current cursor position (as if, the | |
| 69 // the user has pressed backspace key in the textfield). Returns true if | |
| 70 // the removal is successful. | |
| 71 bool Backspace(); | |
| 72 | |
| 73 // Cursor related methods. | |
| 74 | |
| 75 // Returns the current cursor position. | |
| 76 int cursor_pos() const { return cursor_pos_; } | |
|
sky
2010/12/15 20:31:27
size_t
oshima
2010/12/16 01:15:19
good catch. Done
| |
| 77 | |
| 78 // Moves the cursor left by one position (as if, the user has pressed the left | |
| 79 // arrow key). If |select| is true, it updates the selection accordingly. | |
| 80 void MoveCursorLeft(bool select); | |
| 81 | |
| 82 // Moves the cursor right by one position (as if, the user has pressed the | |
| 83 // right arrow key). If |select| is true, it updates the selection | |
| 84 // accordingly. | |
| 85 void MoveCursorRight(bool select); | |
| 86 | |
| 87 // Moves the cursor left by one word (word boundry is defined by space). | |
| 88 // If |select| is true, it updates the selection accordingly. | |
| 89 void MoveCursorToPreviousWord(bool select); | |
| 90 | |
| 91 // Moves the cursor right by one word (word boundry is defined by space). | |
| 92 // If |select| is true, it updates the selection accordingly. | |
| 93 void MoveCursorToNextWord(bool select); | |
| 94 | |
| 95 // Moves the cursor to start of the textfield contents. | |
| 96 // If |select| is true, it updates the selection accordingly. | |
| 97 void MoveCursorToStart(bool select); | |
| 98 | |
| 99 // Moves the cursor to end of the textfield contents. | |
| 100 // If |select| is true, it updates the selection accordingly. | |
| 101 void MoveCursorToEnd(bool select); | |
| 102 | |
| 103 // Moves the cursor to the specified |position|. | |
| 104 // If |select| is true, it updates the selection accordingly. | |
| 105 bool MoveCursorTo(size_t position, bool select); | |
| 106 | |
| 107 // Returns the bounds of character at the current cursor. | |
| 108 gfx::Rect GetCursorBounds(const gfx::Font& font) const; | |
| 109 | |
| 110 // Selection related method | |
| 111 | |
| 112 // Returns the selected text. | |
| 113 string16 GetSelectedText() const; | |
|
sky
2010/12/15 20:31:27
Does this intentionally not return the password ch
oshima
2010/12/16 01:15:19
I believe this is now gtk version works and expect
| |
| 114 | |
| 115 // Selects all text. | |
| 116 void SelectAll(); | |
| 117 | |
| 118 // Clears selection. | |
| 119 void ClearSelection(); | |
| 120 | |
| 121 // Returns visible text. If the field is password, it returns the | |
| 122 // sequence of "*". | |
| 123 string16 GetVisibleText() const { | |
| 124 return GetVisibleText(0U, text_.length()); | |
| 125 } | |
| 126 | |
| 127 private: | |
| 128 friend class NativeTextfieldView; | |
| 129 | |
| 130 // Selects the text. |old| is the old cursor position and | |
| 131 // end is the new corsor position. | |
|
sky
2010/12/15 20:31:27
corsor->cursor
oshima
2010/12/16 01:15:19
Done.
| |
| 132 void Select(size_t old, size_t end); | |
| 133 | |
| 134 // Tells if any text is selected. | |
| 135 bool HasSelection() const; | |
| 136 | |
| 137 // Deletes the selected text. | |
| 138 void DeleteSelection(); | |
| 139 | |
| 140 // Returns the visible text given |start| and |end|. | |
| 141 string16 GetVisibleText(size_t start, size_t end) const; | |
| 142 | |
| 143 // The text in utf16 format. | |
| 144 string16 text_; | |
| 145 | |
| 146 // Current cursor position. | |
| 147 size_t cursor_pos_; | |
| 148 | |
| 149 // Selection range. | |
| 150 size_t begin_, end_; | |
|
sky
2010/12/15 20:31:27
Could you combine cursor_pos_ and begin_ end_ ? Is
oshima
2010/12/16 01:15:19
Only if something is selected. There are several o
sky
2010/12/16 17:29:43
I don't understand this con. You have to update so
| |
| 151 | |
| 152 // True if the text is the password. | |
| 153 bool is_password_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(TextfieldViewModel); | |
| 156 }; | |
| 157 | |
| 158 } // namespace views | |
| 159 | |
| 160 #endif // VIEWS_CONTROLS_TEXTFIELD_TEXTFIELD_VIEW_MODEL_H_ | |
| OLD | NEW |