blob: 44d40031a0df4958e5586968330f4607ddf403ea [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_
7#pragma once
8
[email protected]d9990212012-03-13 01:09:319#include <iosfwd>
[email protected]ed9f8fba2011-09-28 21:50:0910
[email protected]d9990212012-03-13 01:09:3111#include "ui/base/range/range.h"
[email protected]ed9f8fba2011-09-28 21:50:0912#include "ui/base/ui_export.h"
13
14namespace gfx {
15
[email protected]d9990212012-03-13 01:09:3116// VisualCursorDirection and LogicalCursorDirection represent directions of
17// motion of the cursor in BiDi text. The combinations that make sense are:
18//
19// base::i18n::TextDirection VisualCursorDirection LogicalCursorDirection
20// LEFT_TO_RIGHT CURSOR_LEFT CURSOR_BACKWARD
21// LEFT_TO_RIGHT CURSOR_RIGHT CURSOR_FORWARD
22// RIGHT_TO_LEFT CURSOR_RIGHT CURSOR_BACKWARD
23// RIGHT_TO_LEFT CURSOR_LEFT CURSOR_FORWARD
24enum VisualCursorDirection {
25 CURSOR_LEFT,
26 CURSOR_RIGHT
27};
28enum LogicalCursorDirection {
29 CURSOR_BACKWARD,
30 CURSOR_FORWARD
31};
32
[email protected]ed9f8fba2011-09-28 21:50:0933// TODO(xji): publish bidi-editing guide line and replace the place holder.
34// SelectionModel is used to represent the logical selection and visual
35// position of cursor.
36//
37// For bi-directional text, the mapping between visual position and logical
38// position is not one-to-one. For example, logical text "abcDEF" where capital
39// letters stand for Hebrew, the visual display is "abcFED". According to the
40// bidi editing guide (http://bidi-editing-guideline):
41// 1. If pointing to the right half of the cell of a LTR character, the current
42// position must be set after this character and the caret must be displayed
43// after this character.
44// 2. If pointing to the right half of the cell of a RTL character, the current
45// position must be set before this character and the caret must be displayed
46// before this character.
47//
48// Pointing to the right half of 'c' and pointing to the right half of 'D' both
49// set the logical cursor position to 3. But the cursor displayed visually at
50// different places:
51// Pointing to the right half of 'c' displays the cursor right of 'c' as
52// "abc|FED".
53// Pointing to the right half of 'D' displays the cursor right of 'D' as
54// "abcFED|".
55// So, besides the logical selection start point and end point, we need extra
[email protected]d9990212012-03-13 01:09:3156// information to specify to which character the visual cursor is bound. This
57// is given by a "caret affinity" which is either CURSOR_BACKWARD (indicating
58// the trailing half of the 'c' in this case) or CURSOR_FORWARD (indicating
59// the leading half of the 'D').
[email protected]ed9f8fba2011-09-28 21:50:0960class UI_EXPORT SelectionModel {
61 public:
[email protected]d9990212012-03-13 01:09:3162 // Create a default SelectionModel to be overwritten later.
[email protected]ed9f8fba2011-09-28 21:50:0963 SelectionModel();
[email protected]d9990212012-03-13 01:09:3164 // Create a SelectionModel representing a caret |position| without a
65 // selection. The |affinity| is meaningful only when the caret is positioned
66 // between bidi runs that are not visually contiguous: in that case, it
67 // indicates the run to which the caret is attached for display purposes.
68 SelectionModel(size_t position, LogicalCursorDirection affinity);
69 // Create a SelectionModel representing a selection (which may be empty).
70 // The caret position is the end of the range.
71 SelectionModel(ui::Range selection, LogicalCursorDirection affinity);
[email protected]ed9f8fba2011-09-28 21:50:0972
[email protected]d9990212012-03-13 01:09:3173 const ui::Range& selection() const { return selection_; }
74 size_t caret_pos() const { return selection_.end(); }
75 LogicalCursorDirection caret_affinity() const { return caret_affinity_; }
[email protected]ed9f8fba2011-09-28 21:50:0976
[email protected]d9990212012-03-13 01:09:3177 bool operator==(const SelectionModel& sel) const;
78 bool operator!=(const SelectionModel& sel) { return !(*this == sel); }
[email protected]ed9f8fba2011-09-28 21:50:0979
80 private:
[email protected]67e85512011-10-12 20:03:4581 friend class RenderText;
82
[email protected]d9990212012-03-13 01:09:3183 // TODO(benrg): Generally the selection start should not be changed without
84 // considering the effect on the caret affinity. This setter is exposed only
85 // to RenderText to discourage misuse, and should probably be removed.
86 void set_selection_start(size_t pos) { selection_.set_start(pos); }
[email protected]ed9f8fba2011-09-28 21:50:0987
[email protected]d9990212012-03-13 01:09:3188 // Logical selection. The logical caret position is the end of the selection.
89 ui::Range selection_;
[email protected]67e85512011-10-12 20:03:4590
[email protected]d9990212012-03-13 01:09:3191 // The logical direction from the caret position (selection_.end()) to the
92 // character it is attached to for display purposes. This matters only when
93 // the surrounding characters are not visually contiguous, which happens only
94 // in bidi text (and only at bidi run boundaries). The text is treated as
95 // though it was surrounded on both sides by runs in the dominant text
96 // direction. For example, supposing the dominant direction is LTR and the
97 // logical text is "abcDEF", where DEF is right-to-left text, the visual
98 // cursor will display as follows:
99 // caret position CURSOR_BACKWARD affinity CURSOR_FORWARD affinity
100 // 0 |abcFED |abcFED
101 // 1 a|bcFED a|bcFED
102 // 2 ab|cFED ab|cFED
103 // 3 abc|FED abcFED|
104 // 4 abcFE|D abcFE|D
105 // 5 abcF|ED abcF|ED
106 // 6 abc|FED abcFED|
107 LogicalCursorDirection caret_affinity_;
[email protected]ed9f8fba2011-09-28 21:50:09108};
109
[email protected]d9990212012-03-13 01:09:31110UI_EXPORT std::ostream& operator<<(std::ostream& out,
111 const SelectionModel& sel);
112
[email protected]ed9f8fba2011-09-28 21:50:09113} // namespace gfx
114
115#endif // UI_GFX_SELECTION_MODEL_H_