OLD | NEW |
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 #include "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 sel.set_caret_placement(SelectionModel::TRAILING); | 192 sel.set_caret_placement(SelectionModel::TRAILING); |
193 SetSelectionModel(sel); | 193 SetSelectionModel(sel); |
194 } | 194 } |
195 | 195 |
196 void RenderText::MoveCursorLeft(BreakType break_type, bool select) { | 196 void RenderText::MoveCursorLeft(BreakType break_type, bool select) { |
197 SelectionModel position(selection_model()); | 197 SelectionModel position(selection_model()); |
198 position.set_selection_start(GetCursorPosition()); | 198 position.set_selection_start(GetCursorPosition()); |
199 // Cancelling a selection moves to the edge of the selection. | 199 // Cancelling a selection moves to the edge of the selection. |
200 if (break_type != LINE_BREAK && !EmptySelection() && !select) { | 200 if (break_type != LINE_BREAK && !EmptySelection() && !select) { |
201 // Use the selection start if it is left of the selection end. | 201 // Use the selection start if it is left of the selection end. |
202 SelectionModel selection_start(GetSelectionStart(), GetSelectionStart(), | 202 SelectionModel selection_start = GetSelectionModelForSelectionStart(); |
203 SelectionModel::LEADING); | |
204 if (GetCursorBounds(selection_start, false).x() < | 203 if (GetCursorBounds(selection_start, false).x() < |
205 GetCursorBounds(position, false).x()) | 204 GetCursorBounds(position, false).x()) |
206 position = selection_start; | 205 position = selection_start; |
207 // For word breaks, use the nearest word boundary left of the selection. | 206 // For word breaks, use the nearest word boundary left of the selection. |
208 if (break_type == WORD_BREAK) | 207 if (break_type == WORD_BREAK) |
209 position = GetLeftSelectionModel(position, break_type); | 208 position = GetLeftSelectionModel(position, break_type); |
210 } else { | 209 } else { |
211 position = GetLeftSelectionModel(position, break_type); | 210 position = GetLeftSelectionModel(position, break_type); |
212 } | 211 } |
213 if (select) | 212 if (select) |
214 position.set_selection_start(GetSelectionStart()); | 213 position.set_selection_start(GetSelectionStart()); |
215 MoveCursorTo(position); | 214 MoveCursorTo(position); |
216 } | 215 } |
217 | 216 |
218 void RenderText::MoveCursorRight(BreakType break_type, bool select) { | 217 void RenderText::MoveCursorRight(BreakType break_type, bool select) { |
219 SelectionModel position(selection_model()); | 218 SelectionModel position(selection_model()); |
220 position.set_selection_start(GetCursorPosition()); | 219 position.set_selection_start(GetCursorPosition()); |
221 // Cancelling a selection moves to the edge of the selection. | 220 // Cancelling a selection moves to the edge of the selection. |
222 if (break_type != LINE_BREAK && !EmptySelection() && !select) { | 221 if (break_type != LINE_BREAK && !EmptySelection() && !select) { |
223 // Use the selection start if it is right of the selection end. | 222 // Use the selection start if it is right of the selection end. |
224 SelectionModel selection_start(GetSelectionStart(), GetSelectionStart(), | 223 SelectionModel selection_start = GetSelectionModelForSelectionStart(); |
225 SelectionModel::LEADING); | |
226 if (GetCursorBounds(selection_start, false).x() > | 224 if (GetCursorBounds(selection_start, false).x() > |
227 GetCursorBounds(position, false).x()) | 225 GetCursorBounds(position, false).x()) |
228 position = selection_start; | 226 position = selection_start; |
229 // For word breaks, use the nearest word boundary right of the selection. | 227 // For word breaks, use the nearest word boundary right of the selection. |
230 if (break_type == WORD_BREAK) | 228 if (break_type == WORD_BREAK) |
231 position = GetRightSelectionModel(position, break_type); | 229 position = GetRightSelectionModel(position, break_type); |
232 } else { | 230 } else { |
233 position = GetRightSelectionModel(position, break_type); | 231 position = GetRightSelectionModel(position, break_type); |
234 } | 232 } |
235 if (select) | 233 if (select) |
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 // Pan to show the cursor when it overflows to the right, | 599 // Pan to show the cursor when it overflows to the right, |
602 delta_offset = display_rect_.right() - cursor_bounds_.right(); | 600 delta_offset = display_rect_.right() - cursor_bounds_.right(); |
603 } else if (cursor_bounds_.x() < display_rect_.x()) { | 601 } else if (cursor_bounds_.x() < display_rect_.x()) { |
604 // Pan to show the cursor when it overflows to the left. | 602 // Pan to show the cursor when it overflows to the left. |
605 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 603 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
606 } | 604 } |
607 display_offset_.Offset(delta_offset, 0); | 605 display_offset_.Offset(delta_offset, 0); |
608 cursor_bounds_.Offset(delta_offset, 0); | 606 cursor_bounds_.Offset(delta_offset, 0); |
609 } | 607 } |
610 | 608 |
| 609 SelectionModel RenderText::GetSelectionModelForSelectionStart() { |
| 610 if (GetSelectionStart() < GetCursorPosition()) { |
| 611 return SelectionModel(GetSelectionStart(), |
| 612 GetSelectionStart(), |
| 613 SelectionModel::LEADING); |
| 614 } else { |
| 615 return SelectionModel(GetSelectionStart(), |
| 616 GetIndexOfPreviousGrapheme(GetSelectionStart()), |
| 617 SelectionModel::TRAILING); |
| 618 } |
| 619 } |
| 620 |
611 } // namespace gfx | 621 } // namespace gfx |
OLD | NEW |