Chromium Code Reviews| 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 #ifndef UI_GFX_SELECTION_MODEL_H_ | 5 #ifndef UI_GFX_SELECTION_MODEL_H_ |
| 6 #define UI_GFX_SELECTION_MODEL_H_ | 6 #define UI_GFX_SELECTION_MODEL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <stdlib.h> | 9 #include <iosfwd> |
| 10 | 10 |
| 11 #include "ui/base/range/range.h" | |
| 11 #include "ui/base/ui_export.h" | 12 #include "ui/base/ui_export.h" |
| 12 | 13 |
| 13 namespace gfx { | 14 namespace gfx { |
| 14 | 15 |
| 15 // TODO(xji): publish bidi-editing guide line and replace the place holder. | 16 // VisualCursorDirection and LogicalCursorDirection represent directions of |
| 16 // SelectionModel is used to represent the logical selection and visual | 17 // motion of the cursor in BiDi text. The combinations that make sense are: |
| 17 // position of cursor. | |
| 18 // | 18 // |
| 19 // For bi-directional text, the mapping between visual position and logical | 19 // base::i18n::TextDirection VisualCursorDirection LogicalCursorDirection |
| 20 // position is not one-to-one. For example, logical text "abcDEF" where capital | 20 // LEFT_TO_RIGHT CURSOR_LEFT CURSOR_BACKWARD |
| 21 // letters stand for Hebrew, the visual display is "abcFED". According to the | 21 // LEFT_TO_RIGHT CURSOR_RIGHT CURSOR_FORWARD |
| 22 // bidi editing guide (http://bidi-editing-guideline): | 22 // RIGHT_TO_LEFT CURSOR_RIGHT CURSOR_BACKWARD |
| 23 // 1. If pointing to the right half of the cell of a LTR character, the current | 23 // RIGHT_TO_LEFT CURSOR_LEFT CURSOR_FORWARD |
| 24 // position must be set after this character and the caret must be displayed | 24 enum VisualCursorDirection { |
| 25 // after this character. | 25 CURSOR_LEFT, |
| 26 // 2. If pointing to the right half of the cell of a RTL character, the current | 26 CURSOR_RIGHT |
| 27 // position must be set before this character and the caret must be displayed | 27 }; |
| 28 // before this character. | 28 enum LogicalCursorDirection { |
| 29 // | 29 CURSOR_BACKWARD, |
| 30 // Pointing to the right half of 'c' and pointing to the right half of 'D' both | 30 CURSOR_FORWARD |
| 31 // set the logical cursor position to 3. But the cursor displayed visually at | 31 }; |
| 32 // different places: | 32 |
| 33 // Pointing to the right half of 'c' displays the cursor right of 'c' as | |
| 34 // "abc|FED". | |
| 35 // Pointing to the right half of 'D' displays the cursor right of 'D' as | |
| 36 // "abcFED|". | |
| 37 // So, besides the logical selection start point and end point, we need extra | |
| 38 // information to specify to which character and on which edge of the character | |
| 39 // the visual cursor is bound to. For example, the visual cursor is bound to | |
| 40 // the trailing side of the 2nd character 'c' when pointing to right half of | |
| 41 // 'c'. And it is bound to the leading edge of the 3rd character 'D' when | |
| 42 // pointing to right of 'D'. | |
| 43 class UI_EXPORT SelectionModel { | 33 class UI_EXPORT SelectionModel { |
|
xji
2012/02/24 20:16:52
Please add comments.
benrg
2012/03/07 01:04:44
I restored your original comment because actually
| |
| 44 public: | 34 public: |
| 45 enum CaretPlacement { | 35 // Create a default SelectionModel to be overwritten later. |
| 46 LEADING, | 36 SelectionModel(); |
| 47 TRAILING, | 37 // Create a SelectionModel representing a caret |position| without a |
| 48 }; | 38 // selection. The |affinity| is meaningful only when the caret is positioned |
| 39 // between bidi runs that are not visually contiguous: in that case, it | |
| 40 // indicates the run to which the caret is attached for display purposes. | |
| 41 SelectionModel(size_t position, LogicalCursorDirection affinity); | |
| 42 // Create a SelectionModel representing a selection (which may be empty). | |
| 43 // The caret position is the end of the range. | |
| 44 SelectionModel(ui::Range selection, LogicalCursorDirection affinity); | |
| 49 | 45 |
| 50 SelectionModel(); | 46 const ui::Range& selection() const { return selection_; } |
| 51 explicit SelectionModel(size_t pos); | 47 size_t caret_pos() const { return selection_.end(); } |
| 52 SelectionModel(size_t end, size_t pos, CaretPlacement status); | 48 LogicalCursorDirection caret_affinity() const { return caret_affinity_; } |
| 53 SelectionModel(size_t start, size_t end, size_t pos, CaretPlacement status); | |
| 54 | 49 |
| 55 virtual ~SelectionModel(); | 50 bool operator==(const SelectionModel& sel) const; |
| 56 | 51 bool operator!=(const SelectionModel& sel) { return !(*this == sel); } |
| 57 size_t selection_start() const { return selection_start_; } | |
| 58 size_t selection_end() const { return selection_end_; } | |
| 59 size_t caret_pos() const { return caret_pos_; } | |
| 60 CaretPlacement caret_placement() const { return caret_placement_; } | |
| 61 | |
| 62 bool Equals(const SelectionModel& sel) const; | |
| 63 | 52 |
| 64 private: | 53 private: |
| 65 friend class RenderText; | 54 friend class RenderText; |
| 66 | 55 |
| 67 void Init(size_t start, size_t end, size_t pos, CaretPlacement status); | 56 void set_selection_start(size_t pos) { selection_.set_start(pos); } |
|
msw
2012/02/28 22:51:32
Add a comment about why this is private, in case w
benrg
2012/03/07 01:04:44
I added a comment, but to be honest I'm not sure I
msw
2012/03/08 18:07:19
That's correct; users should flip the affinity whe
msw
2012/03/08 18:09:36
Gah, correction: when the "selection *end* is at a
xji
2012/03/08 20:30:02
What I mean is that if we explicitly know where th
| |
| 68 | 57 |
| 69 void set_selection_start(size_t pos) { selection_start_ = pos; } | 58 // Logical selection. The logical caret position is the end of the selection. |
| 70 void set_selection_end(size_t pos) { selection_end_ = pos; } | 59 ui::Range selection_; |
| 71 void set_caret_pos(size_t pos) { caret_pos_ = pos; } | |
| 72 void set_caret_placement(CaretPlacement placement) { | |
| 73 caret_placement_ = placement; | |
| 74 } | |
| 75 | 60 |
| 76 // Logical selection start. If there is non-empty selection, if | 61 // The logical direction from the caret position (selection_.end()) to the |
| 77 // selection_start_ is less than selection_end_, the selection starts visually | 62 // character it is attached to for display purposes. This matters only when |
| 78 // at the leading edge of the selection_start_. If selection_start_ is greater | 63 // the surrounding characters are not visually contiguous, which happens only |
| 79 // than selection_end_, the selection starts visually at the trailing edge of | 64 // in bidi text (and only at bidi run boundaries). The text is treated as |
| 80 // selection_start_'s previous grapheme. So, we do not need extra information | 65 // though it was surrounded on both sides by runs in the dominant text |
| 81 // for visual bounding. | 66 // direction. For example, supposing the dominant direction is LTR and the |
| 82 size_t selection_start_; | 67 // logical text is "abcDEF", where DEF is right-to-left text, the visual |
| 68 // cursor will display as follows: | |
| 69 // caret position CURSOR_BACKWARD affinity CURSOR_FORWARD affinity | |
| 70 // 0 |abcFED |abcFED | |
| 71 // 1 a|bcFED a|bcFED | |
| 72 // 2 ab|cFED ab|cFED | |
| 73 // 3 abc|FED abcFED| | |
| 74 // 4 abcFE|D abcFE|D | |
| 75 // 5 abcF|ED abcF|ED | |
| 76 // 6 abc|FED abcFED| | |
| 77 LogicalCursorDirection caret_affinity_; | |
| 78 }; | |
| 83 | 79 |
| 84 // The logical cursor position that next character will be inserted into. | 80 UI_EXPORT std::ostream& operator<<(std::ostream& out, |
| 85 // It is also the end of the selection. | 81 const SelectionModel& sel); |
| 86 size_t selection_end_; | |
| 87 | |
| 88 // The following two fields are used to guide cursor visual position. | |
| 89 // The index of the character that cursor is visually attached to. | |
| 90 size_t caret_pos_; | |
| 91 // The visual placement of the cursor, relative to its associated character. | |
| 92 CaretPlacement caret_placement_; | |
| 93 }; | |
| 94 | 82 |
| 95 } // namespace gfx | 83 } // namespace gfx |
| 96 | 84 |
| 97 #endif // UI_GFX_SELECTION_MODEL_H_ | 85 #endif // UI_GFX_SELECTION_MODEL_H_ |
| OLD | NEW |