OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #include "views/controls/textfield/textfield_views_model.h" | 5 #include "views/controls/textfield/textfield_views_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 void TextfieldViewsModel::MoveCursorToPreviousWord(bool select) { | 125 void TextfieldViewsModel::MoveCursorToPreviousWord(bool select) { |
126 // Notes: We always iterate words from the begining. | 126 // Notes: We always iterate words from the begining. |
127 // This is probably fast enough for our usage, but we may | 127 // This is probably fast enough for our usage, but we may |
128 // want to modify WordIterator so that it can start from the | 128 // want to modify WordIterator so that it can start from the |
129 // middle of string and advance backwards. | 129 // middle of string and advance backwards. |
130 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); | 130 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); |
131 bool success = iter.Init(); | 131 bool success = iter.Init(); |
132 DCHECK(success); | 132 DCHECK(success); |
133 if (!success) | 133 if (!success) |
134 return; | 134 return; |
135 int prev = 0; | 135 int last = 0; |
136 while (iter.Advance()) { | 136 while (iter.Advance()) { |
137 if (iter.IsWord()) { | 137 if (iter.IsWord()) { |
138 size_t begin = iter.pos() - iter.GetString().length(); | 138 size_t begin = iter.pos() - iter.GetString().length(); |
139 if (begin == cursor_pos_) { | 139 if (begin == cursor_pos_) { |
140 // The cursor is at the beginning of a word. | 140 // The cursor is at the beginning of a word. |
141 // Move to previous word. | 141 // Move to previous word. |
142 cursor_pos_ = prev; | 142 break; |
143 } else if(iter.pos() >= cursor_pos_) { | 143 } else if(iter.pos() >= cursor_pos_) { |
144 // The cursor is in the middle or at the end of a word. | 144 // The cursor is in the middle or at the end of a word. |
145 // Move to the top of current word. | 145 // Move to the top of current word. |
146 cursor_pos_ = begin; | 146 last = begin; |
| 147 break; |
147 } else { | 148 } else { |
148 prev = iter.pos() - iter.GetString().length(); | 149 last = iter.pos() - iter.GetString().length(); |
149 continue; | |
150 } | 150 } |
151 if (!select) | |
152 ClearSelection(); | |
153 break; | |
154 } | 151 } |
155 } | 152 } |
| 153 |
| 154 cursor_pos_ = last; |
| 155 if (!select) |
| 156 ClearSelection(); |
156 } | 157 } |
157 | 158 |
158 void TextfieldViewsModel::MoveCursorToNextWord(bool select) { | 159 void TextfieldViewsModel::MoveCursorToNextWord(bool select) { |
159 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); | 160 base::BreakIterator iter(&text_, base::BreakIterator::BREAK_WORD); |
160 bool success = iter.Init(); | 161 bool success = iter.Init(); |
161 DCHECK(success); | 162 DCHECK(success); |
162 if (!success) | 163 if (!success) |
163 return; | 164 return; |
| 165 size_t pos = 0; |
164 while (iter.Advance()) { | 166 while (iter.Advance()) { |
165 if (iter.IsWord() && iter.pos() > cursor_pos_) { | 167 pos = iter.pos(); |
166 cursor_pos_ = iter.pos(); | 168 if (iter.IsWord() && pos > cursor_pos_) { |
167 if (!select) | |
168 ClearSelection(); | |
169 break; | 169 break; |
170 } | 170 } |
171 } | 171 } |
| 172 cursor_pos_ = pos; |
| 173 if (!select) |
| 174 ClearSelection(); |
172 } | 175 } |
173 | 176 |
174 void TextfieldViewsModel::MoveCursorToStart(bool select) { | 177 void TextfieldViewsModel::MoveCursorToStart(bool select) { |
175 cursor_pos_ = 0; | 178 cursor_pos_ = 0; |
176 if (!select) | 179 if (!select) |
177 ClearSelection(); | 180 ClearSelection(); |
178 } | 181 } |
179 | 182 |
180 void TextfieldViewsModel::MoveCursorToEnd(bool select) { | 183 void TextfieldViewsModel::MoveCursorToEnd(bool select) { |
181 cursor_pos_ = text_.length(); | 184 cursor_pos_ = text_.length(); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 240 |
238 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { | 241 string16 TextfieldViewsModel::GetVisibleText(size_t begin, size_t end) const { |
239 DCHECK(end >= begin); | 242 DCHECK(end >= begin); |
240 if (is_password_) { | 243 if (is_password_) { |
241 return string16(end - begin, '*'); | 244 return string16(end - begin, '*'); |
242 } | 245 } |
243 return text_.substr(begin, end - begin); | 246 return text_.substr(begin, end - begin); |
244 } | 247 } |
245 | 248 |
246 } // namespace views | 249 } // namespace views |
OLD | NEW |