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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 return changed; | 213 return changed; |
214 } | 214 } |
215 | 215 |
216 bool RenderText::MoveCursorTo(const Point& point, bool select) { | 216 bool RenderText::MoveCursorTo(const Point& point, bool select) { |
217 SelectionModel selection = FindCursorPosition(point); | 217 SelectionModel selection = FindCursorPosition(point); |
218 if (select) | 218 if (select) |
219 selection.set_selection_start(GetSelectionStart()); | 219 selection.set_selection_start(GetSelectionStart()); |
220 return MoveCursorTo(selection); | 220 return MoveCursorTo(selection); |
221 } | 221 } |
222 | 222 |
223 bool RenderText::SelectRange(const ui::Range& range) { | |
224 size_t text_length = text().length(); | |
225 size_t start = std::min(range.start(), text_length); | |
226 size_t end = std::min(range.end(), text_length); | |
227 | |
228 if (!IsCursorablePosition(start) || !IsCursorablePosition(end)) | |
229 return false; | |
230 | |
231 int pos; | |
232 SelectionModel::CaretPlacement placement; | |
233 if (start < end) { | |
234 pos = GetIndexOfPreviousGrapheme(end); | |
235 placement = SelectionModel::TRAILING; | |
msw
2011/10/04 07:25:41
Please DCHECK_LT(pos, end);
xji
2011/10/05 01:23:42
Done.
| |
236 } else if (end == text_length) { | |
237 SelectionModel boundary = GetTextDirection() == base::i18n::RIGHT_TO_LEFT ? | |
238 LeftEndSelectionModel() : RightEndSelectionModel(); | |
239 pos = boundary.caret_pos(); | |
240 placement = boundary.caret_placement(); | |
241 } else { | |
242 pos = end; | |
msw
2011/10/04 07:25:41
Initialize |pos| and |placement| with these 'defau
xji
2011/10/05 01:23:42
Done.
| |
243 placement = SelectionModel::LEADING; | |
244 } | |
245 SetSelectionModel(SelectionModel(start, end, pos, placement)); | |
246 return true; | |
247 } | |
248 | |
223 bool RenderText::IsPointInSelection(const Point& point) { | 249 bool RenderText::IsPointInSelection(const Point& point) { |
224 if (EmptySelection()) | 250 if (EmptySelection()) |
225 return false; | 251 return false; |
226 // TODO(xji): should this check whether the point is inside the visual | 252 // TODO(xji): should this check whether the point is inside the visual |
227 // selection bounds? In case of "abcFED", if "ED" is selected, |point| points | 253 // selection bounds? In case of "abcFED", if "ED" is selected, |point| points |
228 // to the right half of 'c', is the point in selection? | 254 // to the right half of 'c', is the point in selection? |
229 size_t pos = FindCursorPosition(point).selection_end(); | 255 size_t pos = FindCursorPosition(point).selection_end(); |
230 return (pos >= MinOfSelection() && pos < MaxOfSelection()); | 256 return (pos >= MinOfSelection() && pos < MaxOfSelection()); |
231 } | 257 } |
232 | 258 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 } | 469 } |
444 | 470 |
445 const Point& RenderText::GetUpdatedDisplayOffset() { | 471 const Point& RenderText::GetUpdatedDisplayOffset() { |
446 UpdateCachedBoundsAndOffset(); | 472 UpdateCachedBoundsAndOffset(); |
447 return display_offset_; | 473 return display_offset_; |
448 } | 474 } |
449 | 475 |
450 SelectionModel RenderText::GetLeftSelectionModel(const SelectionModel& current, | 476 SelectionModel RenderText::GetLeftSelectionModel(const SelectionModel& current, |
451 BreakType break_type) { | 477 BreakType break_type) { |
452 if (break_type == LINE_BREAK) | 478 if (break_type == LINE_BREAK) |
453 return SelectionModel(0, 0, SelectionModel::LEADING); | 479 return SelectionModel(0, 0, SelectionModel::LEADING); |
msw
2011/10/04 07:25:41
Can you please change this to "return LeftEndSelec
xji
2011/10/05 01:23:42
Done.
| |
454 size_t pos = std::max(static_cast<long>(current.selection_end() - 1), | 480 size_t pos = std::max(static_cast<long>(current.selection_end() - 1), |
455 static_cast<long>(0)); | 481 static_cast<long>(0)); |
456 if (break_type == CHARACTER_BREAK) | 482 if (break_type == CHARACTER_BREAK) |
457 return SelectionModel(pos, pos, SelectionModel::LEADING); | 483 return SelectionModel(pos, pos, SelectionModel::LEADING); |
458 | 484 |
459 // Notes: We always iterate words from the beginning. | 485 // Notes: We always iterate words from the beginning. |
460 // This is probably fast enough for our usage, but we may | 486 // This is probably fast enough for our usage, but we may |
461 // want to modify WordIterator so that it can start from the | 487 // want to modify WordIterator so that it can start from the |
462 // middle of string and advance backwards. | 488 // middle of string and advance backwards. |
463 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 489 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
(...skipping 20 matching lines...) Expand all Loading... | |
484 } | 510 } |
485 | 511 |
486 return SelectionModel(pos, pos, SelectionModel::LEADING); | 512 return SelectionModel(pos, pos, SelectionModel::LEADING); |
487 } | 513 } |
488 | 514 |
489 SelectionModel RenderText::GetRightSelectionModel(const SelectionModel& current, | 515 SelectionModel RenderText::GetRightSelectionModel(const SelectionModel& current, |
490 BreakType break_type) { | 516 BreakType break_type) { |
491 if (text_.empty()) | 517 if (text_.empty()) |
492 return SelectionModel(0, 0, SelectionModel::LEADING); | 518 return SelectionModel(0, 0, SelectionModel::LEADING); |
493 if (break_type == LINE_BREAK) | 519 if (break_type == LINE_BREAK) |
494 return SelectionModel(text().length(), | 520 return SelectionModel(text().length(), |
msw
2011/10/04 07:25:41
Can you please change this to "return RightEndSele
xji
2011/10/05 01:23:42
Done.
| |
495 GetIndexOfPreviousGrapheme(text().length()), SelectionModel::TRAILING); | 521 GetIndexOfPreviousGrapheme(text().length()), SelectionModel::TRAILING); |
496 size_t pos = std::min(current.selection_end() + 1, text().length()); | 522 size_t pos = std::min(current.selection_end() + 1, text().length()); |
497 if (break_type == CHARACTER_BREAK) | 523 if (break_type == CHARACTER_BREAK) |
498 return SelectionModel(pos, pos, SelectionModel::LEADING); | 524 return SelectionModel(pos, pos, SelectionModel::LEADING); |
499 | 525 |
500 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); | 526 base::i18n::BreakIterator iter(text(), base::i18n::BreakIterator::BREAK_WORD); |
501 bool success = iter.Init(); | 527 bool success = iter.Init(); |
502 DCHECK(success); | 528 DCHECK(success); |
503 if (!success) | 529 if (!success) |
504 return current; | 530 return current; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
630 // LTR character. | 656 // LTR character. |
631 // | 657 // |
632 // Pan to show the cursor when it overflows to the left. | 658 // Pan to show the cursor when it overflows to the left. |
633 delta_offset = display_rect_.x() - cursor_bounds_.x(); | 659 delta_offset = display_rect_.x() - cursor_bounds_.x(); |
634 } | 660 } |
635 display_offset_.Offset(delta_offset, 0); | 661 display_offset_.Offset(delta_offset, 0); |
636 cursor_bounds_.Offset(delta_offset, 0); | 662 cursor_bounds_.Offset(delta_offset, 0); |
637 } | 663 } |
638 | 664 |
639 } // namespace gfx | 665 } // namespace gfx |
OLD | NEW |