blob: 73bf113da5cdd782dc175c7738a2f66935f9c2c6 [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
avic89eb8d42015-12-23 08:08:188#include <stddef.h>
9
[email protected]9a55b8c92012-05-17 20:53:0810#include <string>
[email protected]ed9f8fba2011-09-28 21:50:0911
[email protected]d34e4072013-09-05 20:28:3012#include "ui/gfx/gfx_export.h"
[email protected]db4fc1e22013-09-06 20:01:5113#include "ui/gfx/range/range.h"
[email protected]ed9f8fba2011-09-28 21:50:0914
15namespace gfx {
16
[email protected]d9990212012-03-13 01:09:3117// VisualCursorDirection and LogicalCursorDirection represent directions of
18// motion of the cursor in BiDi text. The combinations that make sense are:
19//
20// base::i18n::TextDirection VisualCursorDirection LogicalCursorDirection
21// LEFT_TO_RIGHT CURSOR_LEFT CURSOR_BACKWARD
22// LEFT_TO_RIGHT CURSOR_RIGHT CURSOR_FORWARD
23// RIGHT_TO_LEFT CURSOR_RIGHT CURSOR_BACKWARD
24// RIGHT_TO_LEFT CURSOR_LEFT CURSOR_FORWARD
25enum VisualCursorDirection {
26 CURSOR_LEFT,
27 CURSOR_RIGHT
28};
29enum LogicalCursorDirection {
30 CURSOR_BACKWARD,
31 CURSOR_FORWARD
32};
33
[email protected]ed9f8fba2011-09-28 21:50:0934// TODO(xji): publish bidi-editing guide line and replace the place holder.
35// SelectionModel is used to represent the logical selection and visual
36// position of cursor.
37//
38// For bi-directional text, the mapping between visual position and logical
39// position is not one-to-one. For example, logical text "abcDEF" where capital
40// letters stand for Hebrew, the visual display is "abcFED". According to the
41// bidi editing guide (http://bidi-editing-guideline):
42// 1. If pointing to the right half of the cell of a LTR character, the current
43// position must be set after this character and the caret must be displayed
44// after this character.
45// 2. If pointing to the right half of the cell of a RTL character, the current
46// position must be set before this character and the caret must be displayed
47// before this character.
48//
49// Pointing to the right half of 'c' and pointing to the right half of 'D' both
50// set the logical cursor position to 3. But the cursor displayed visually at
51// different places:
52// Pointing to the right half of 'c' displays the cursor right of 'c' as
53// "abc|FED".
54// Pointing to the right half of 'D' displays the cursor right of 'D' as
55// "abcFED|".
56// So, besides the logical selection start point and end point, we need extra
[email protected]d9990212012-03-13 01:09:3157// information to specify to which character the visual cursor is bound. This
58// is given by a "caret affinity" which is either CURSOR_BACKWARD (indicating
59// the trailing half of the 'c' in this case) or CURSOR_FORWARD (indicating
60// the leading half of the 'D').
[email protected]4ffa7892013-09-27 16:56:0661class GFX_EXPORT SelectionModel {
[email protected]ed9f8fba2011-09-28 21:50:0962 public:
[email protected]d9990212012-03-13 01:09:3163 // Create a default SelectionModel to be overwritten later.
[email protected]ed9f8fba2011-09-28 21:50:0964 SelectionModel();
[email protected]d9990212012-03-13 01:09:3165 // Create a SelectionModel representing a caret |position| without a
66 // selection. The |affinity| is meaningful only when the caret is positioned
67 // between bidi runs that are not visually contiguous: in that case, it
68 // indicates the run to which the caret is attached for display purposes.
69 SelectionModel(size_t position, LogicalCursorDirection affinity);
70 // Create a SelectionModel representing a selection (which may be empty).
71 // The caret position is the end of the range.
[email protected]3dfe5c52013-09-18 22:01:2272 SelectionModel(const Range& selection, LogicalCursorDirection affinity);
[email protected]ed9f8fba2011-09-28 21:50:0973
[email protected]3dfe5c52013-09-18 22:01:2274 const Range& selection() const { return selection_; }
[email protected]d9990212012-03-13 01:09:3175 size_t caret_pos() const { return selection_.end(); }
76 LogicalCursorDirection caret_affinity() const { return caret_affinity_; }
[email protected]ed9f8fba2011-09-28 21:50:0977
[email protected]e7ae16b2014-05-07 15:08:4378 // WARNING: Generally the selection start should not be changed without
79 // considering the effect on the caret affinity.
jam1f0b47c2016-02-10 01:49:2580 void set_selection_start(uint32_t pos) { selection_.set_start(pos); }
[email protected]e7ae16b2014-05-07 15:08:4381
[email protected]d9990212012-03-13 01:09:3182 bool operator==(const SelectionModel& sel) const;
[email protected]ee8d8692013-07-25 20:22:2783 bool operator!=(const SelectionModel& sel) const { return !(*this == sel); }
[email protected]ed9f8fba2011-09-28 21:50:0984
[email protected]9a55b8c92012-05-17 20:53:0885 std::string ToString() const;
86
[email protected]ed9f8fba2011-09-28 21:50:0987 private:
[email protected]d9990212012-03-13 01:09:3188 // Logical selection. The logical caret position is the end of the selection.
[email protected]3dfe5c52013-09-18 22:01:2289 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]ed9f8fba2011-09-28 21:50:09110} // namespace gfx
111
112#endif // UI_GFX_SELECTION_MODEL_H_