1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef EditingBehavior_h
22#define EditingBehavior_h
23
24#include "core/editing/EditingBehaviorTypes.h"
25
26namespace blink {
27class KeyboardEvent;
28
29class EditingBehavior {
30
31public:
32    explicit EditingBehavior(EditingBehaviorType type)
33        : m_type(type)
34    {
35    }
36
37    // Individual functions for each case where we have more than one style of editing behavior.
38    // Create a new function for any platform difference so we can control it here.
39
40    // When extending a selection beyond the top or bottom boundary of an editable area,
41    // maintain the horizontal position on Windows and Android but extend it to the boundary of
42    // the editable content on Mac and Linux.
43    bool shouldMoveCaretToHorizontalBoundaryWhenPastTopOrBottom() const
44    {
45        return m_type != EditingWindowsBehavior && m_type != EditingAndroidBehavior;
46    }
47
48    // On Windows, selections should always be considered as directional, regardless if it is
49    // mouse-based or keyboard-based.
50    bool shouldConsiderSelectionAsDirectional() const { return m_type != EditingMacBehavior; }
51
52    // On Mac, when revealing a selection (for example as a result of a Find operation on the Browser),
53    // content should be scrolled such that the selection gets certer aligned.
54    bool shouldCenterAlignWhenSelectionIsRevealed() const { return m_type == EditingMacBehavior; }
55
56    // On Mac, style is considered present when present at the beginning of selection. On other platforms,
57    // style has to be present throughout the selection.
58    bool shouldToggleStyleBasedOnStartOfSelection() const { return m_type == EditingMacBehavior; }
59
60    // Standard Mac behavior when extending to a boundary is grow the selection rather than leaving the base
61    // in place and moving the extent. Matches NSTextView.
62    bool shouldAlwaysGrowSelectionWhenExtendingToBoundary() const { return m_type == EditingMacBehavior; }
63
64    // On Mac, when processing a contextual click, the object being clicked upon should be selected.
65    bool shouldSelectOnContextualMenuClick() const { return m_type == EditingMacBehavior; }
66
67    // On Mac and Windows, pressing backspace (when it isn't handled otherwise) should navigate back.
68    bool shouldNavigateBackOnBackspace() const
69    {
70        return m_type != EditingUnixBehavior && m_type != EditingAndroidBehavior;
71    }
72
73    // On Mac, selecting backwards by word/line from the middle of a word/line, and then going
74    // forward leaves the caret back in the middle with no selection, instead of directly selecting
75    // to the other end of the line/word (Unix/Windows behavior).
76    bool shouldExtendSelectionByWordOrLineAcrossCaret() const { return m_type != EditingMacBehavior; }
77
78    // Based on native behavior, when using ctrl(alt)+arrow to move caret by word, ctrl(alt)+left arrow moves caret to
79    // immediately before the word in all platforms, for example, the word break positions are: "|abc |def |hij |opq".
80    // But ctrl+right arrow moves caret to "abc |def |hij |opq" on Windows and "abc| def| hij| opq|" on Mac and Linux.
81    bool shouldSkipSpaceWhenMovingRight() const { return m_type == EditingWindowsBehavior; }
82
83    // On Mac, undo of delete/forward-delete of text should select the deleted text. On other platforms deleted text
84    // should not be selected and the cursor should be placed where the deletion started.
85    bool shouldUndoOfDeleteSelectText() const { return m_type == EditingMacBehavior; }
86
87    // Support for global selections, used on platforms like the X Window
88    // System that treat selection as a type of clipboard.
89    bool supportsGlobalSelection() const
90    {
91        return m_type != EditingWindowsBehavior && m_type != EditingMacBehavior;
92    }
93
94    // Convert a KeyboardEvent to a command name like "Copy", "Undo" and so on.
95    // If nothing, return empty string.
96    const char* interpretKeyEvent(const KeyboardEvent&) const;
97
98    bool shouldInsertCharacter(const KeyboardEvent&) const;
99private:
100    EditingBehaviorType m_type;
101};
102
103} // namespace blink
104
105#endif // EditingBehavior_h
106