blob: b99b383714b68a5c56e39dcbe310f8480fb21314 [file] [log] [blame]
[email protected]d9990212012-03-13 01:09:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ed9f8fba2011-09-28 21:50:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_GFX_SELECTION_MODEL_H_
6#define UI_GFX_SELECTION_MODEL_H_
[email protected]ed9f8fba2011-09-28 21:50:097
[email protected]9a55b8c92012-05-17 20:53:088#include <string>
[email protected]ed9f8fba2011-09-28 21:50:099
[email protected]d9990212012-03-13 01:09:3110#include "ui/base/range/range.h"
[email protected]ed9f8fba2011-09-28 21:50:0911#include "ui/base/ui_export.h"
12
13namespace gfx {
14
[email protected]d9990212012-03-13 01:09:3115// VisualCursorDirection and LogicalCursorDirection represent directions of
16// motion of the cursor in BiDi text. The combinations that make sense are:
17//
18// base::i18n::TextDirection VisualCursorDirection LogicalCursorDirection
19// LEFT_TO_RIGHT CURSOR_LEFT CURSOR_BACKWARD
20// LEFT_TO_RIGHT CURSOR_RIGHT CURSOR_FORWARD
21// RIGHT_TO_LEFT CURSOR_RIGHT CURSOR_BACKWARD
22// RIGHT_TO_LEFT CURSOR_LEFT CURSOR_FORWARD
23enum VisualCursorDirection {
24 CURSOR_LEFT,
25 CURSOR_RIGHT
26};
27enum LogicalCursorDirection {
28 CURSOR_BACKWARD,
29 CURSOR_FORWARD
30};
31
[email protected]ed9f8fba2011-09-28 21:50:0932// TODO(xji): publish bidi-editing guide line and replace the place holder.
33// SelectionModel is used to represent the logical selection and visual
34// position of cursor.
35//
36// For bi-directional text, the mapping between visual position and logical
37// position is not one-to-one. For example, logical text "abcDEF" where capital
38// letters stand for Hebrew, the visual display is "abcFED". According to the
39// bidi editing guide (http://bidi-editing-guideline):
40// 1. If pointing to the right half of the cell of a LTR character, the current
41// position must be set after this character and the caret must be displayed
42// after this character.
43// 2. If pointing to the right half of the cell of a RTL character, the current
44// position must be set before this character and the caret must be displayed
45// before this character.
46//
47// Pointing to the right half of 'c' and pointing to the right half of 'D' both
48// set the logical cursor position to 3. But the cursor displayed visually at
49// different places:
50// Pointing to the right half of 'c' displays the cursor right of 'c' as
51// "abc|FED".
52// Pointing to the right half of 'D' displays the cursor right of 'D' as
53// "abcFED|".
54// So, besides the logical selection start point and end point, we need extra
[email protected]d9990212012-03-13 01:09:3155// information to specify to which character the visual cursor is bound. This
56// is given by a "caret affinity" which is either CURSOR_BACKWARD (indicating
57// the trailing half of the 'c' in this case) or CURSOR_FORWARD (indicating
58// the leading half of the 'D').
[email protected]ed9f8fba2011-09-28 21:50:0959class UI_EXPORT SelectionModel {
60 public:
[email protected]d9990212012-03-13 01:09:3161 // Create a default SelectionModel to be overwritten later.
[email protected]ed9f8fba2011-09-28 21:50:0962 SelectionModel();
[email protected]d9990212012-03-13 01:09:3163 // Create a SelectionModel representing a caret |position| without a
64 // selection. The |affinity| is meaningful only when the caret is positioned
65 // between bidi runs that are not visually contiguous: in that case, it
66 // indicates the run to which the caret is attached for display purposes.
67 SelectionModel(size_t position, LogicalCursorDirection affinity);
68 // Create a SelectionModel representing a selection (which may be empty).
69 // The caret position is the end of the range.
70 SelectionModel(ui::Range selection, LogicalCursorDirection affinity);
[email protected]ed9f8fba2011-09-28 21:50:0971
[email protected]d9990212012-03-13 01:09:3172 const ui::Range& selection() const { return selection_; }
73 size_t caret_pos() const { return selection_.end(); }
74 LogicalCursorDirection caret_affinity() const { return caret_affinity_; }
[email protected]ed9f8fba2011-09-28 21:50:0975
[email protected]d9990212012-03-13 01:09:3176 bool operator==(const SelectionModel& sel) const;
[email protected]ee8d8692013-07-25 20:22:2777 bool operator!=(const SelectionModel& sel) const { return !(*this == sel); }
[email protected]ed9f8fba2011-09-28 21:50:0978
[email protected]9a55b8c92012-05-17 20:53:0879 std::string ToString() const;
80
[email protected]ed9f8fba2011-09-28 21:50:0981 private:
[email protected]67e85512011-10-12 20:03:4582 friend class RenderText;
83
[email protected]d9990212012-03-13 01:09:3184 // TODO(benrg): Generally the selection start should not be changed without
85 // considering the effect on the caret affinity. This setter is exposed only
86 // to RenderText to discourage misuse, and should probably be removed.
87 void set_selection_start(size_t pos) { selection_.set_start(pos); }
[email protected]ed9f8fba2011-09-28 21:50:0988
[email protected]d9990212012-03-13 01:09:3189 // Logical selection. The logical caret position is the end of the selection.
90 ui::Range selection_;
[email protected]67e85512011-10-12 20:03:4591
[email protected]d9990212012-03-13 01:09:3192 // The logical direction from the caret position (selection_.end()) to the
93 // character it is attached to for display purposes. This matters only when
94 // the surrounding characters are not visually contiguous, which happens only
95 // in bidi text (and only at bidi run boundaries). The text is treated as
96 // though it was surrounded on both sides by runs in the dominant text
97 // direction. For example, supposing the dominant direction is LTR and the
98 // logical text is "abcDEF", where DEF is right-to-left text, the visual
99 // cursor will display as follows:
100 // caret position CURSOR_BACKWARD affinity CURSOR_FORWARD affinity
101 // 0 |abcFED |abcFED
102 // 1 a|bcFED a|bcFED
103 // 2 ab|cFED ab|cFED
104 // 3 abc|FED abcFED|
105 // 4 abcFE|D abcFE|D
106 // 5 abcF|ED abcF|ED
107 // 6 abc|FED abcFED|
108 LogicalCursorDirection caret_affinity_;
[email protected]ed9f8fba2011-09-28 21:50:09109};
110
[email protected]ed9f8fba2011-09-28 21:50:09111} // namespace gfx
112
113#endif // UI_GFX_SELECTION_MODEL_H_