Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / editing / Editor.h
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef Editor_h
27 #define Editor_h
28
29 #include "core/clipboard/ClipboardAccessPolicy.h"
30 #include "core/dom/DocumentMarker.h"
31 #include "core/editing/EditAction.h"
32 #include "core/editing/EditingBehavior.h"
33 #include "core/editing/FindOptions.h"
34 #include "core/editing/FrameSelection.h"
35 #include "core/editing/TextIterator.h"
36 #include "core/editing/VisibleSelection.h"
37 #include "core/editing/WritingDirection.h"
38 #include "core/frame/FrameDestructionObserver.h"
39 #include "platform/PasteMode.h"
40 #include "platform/heap/Handle.h"
41
42 namespace WebCore {
43
44 class Clipboard;
45 class CompositeEditCommand;
46 class EditCommand;
47 class EditCommandComposition;
48 class EditorClient;
49 class EditorInternalCommand;
50 class LocalFrame;
51 class HTMLElement;
52 class HitTestResult;
53 class KillRing;
54 class Pasteboard;
55 class SharedBuffer;
56 class SimpleFontData;
57 class SpellChecker;
58 class StylePropertySet;
59 class Text;
60 class TextEvent;
61 class UndoStack;
62
63 enum EditorCommandSource { CommandFromMenuOrKeyBinding, CommandFromDOM, CommandFromDOMWithUserInterface };
64 enum EditorParagraphSeparator { EditorParagraphSeparatorIsDiv, EditorParagraphSeparatorIsP };
65
66 class Editor {
67     WTF_MAKE_NONCOPYABLE(Editor);
68 public:
69     static PassOwnPtr<Editor> create(LocalFrame&);
70     ~Editor();
71
72     EditorClient& client() const;
73
74     LocalFrame& frame() const { return m_frame; }
75
76     CompositeEditCommand* lastEditCommand() { return m_lastEditCommand.get(); }
77
78     void handleKeyboardEvent(KeyboardEvent*);
79     bool handleTextEvent(TextEvent*);
80
81     bool canEdit() const;
82     bool canEditRichly() const;
83
84     bool canDHTMLCut();
85     bool canDHTMLCopy();
86     bool canDHTMLPaste();
87
88     bool canCut() const;
89     bool canCopy() const;
90     bool canPaste() const;
91     bool canDelete() const;
92     bool canSmartCopyOrDelete() const;
93
94     void cut();
95     void copy();
96     void paste();
97     void pasteAsPlainText();
98     void performDelete();
99
100     void copyImage(const HitTestResult&);
101
102     void indent();
103     void outdent();
104     void transpose();
105
106     bool shouldDeleteRange(Range*) const;
107
108     void respondToChangedContents(const VisibleSelection& endingSelection);
109
110     bool selectionStartHasStyle(CSSPropertyID, const String& value) const;
111     TriState selectionHasStyle(CSSPropertyID, const String& value) const;
112     String selectionStartCSSPropertyValue(CSSPropertyID);
113
114     TriState selectionUnorderedListState() const;
115     TriState selectionOrderedListState() const;
116
117     void removeFormattingAndStyle();
118
119     void clearLastEditCommand();
120
121     bool deleteWithDirection(SelectionDirection, TextGranularity, bool killRing, bool isTypingAction);
122     void deleteSelectionWithSmartDelete(bool smartDelete);
123
124     void applyStyle(StylePropertySet*, EditAction = EditActionUnspecified);
125     void applyParagraphStyle(StylePropertySet*, EditAction = EditActionUnspecified);
126     void applyStyleToSelection(StylePropertySet*, EditAction);
127     void applyParagraphStyleToSelection(StylePropertySet*, EditAction);
128
129     void appliedEditing(PassRefPtr<CompositeEditCommand>);
130     void unappliedEditing(PassRefPtr<EditCommandComposition>);
131     void reappliedEditing(PassRefPtr<EditCommandComposition>);
132
133     void setShouldStyleWithCSS(bool flag) { m_shouldStyleWithCSS = flag; }
134     bool shouldStyleWithCSS() const { return m_shouldStyleWithCSS; }
135
136     class Command {
137     public:
138         Command();
139         Command(const EditorInternalCommand*, EditorCommandSource, PassRefPtr<LocalFrame>);
140
141         bool execute(const String& parameter = String(), Event* triggeringEvent = 0) const;
142         bool execute(Event* triggeringEvent) const;
143
144         bool isSupported() const;
145         bool isEnabled(Event* triggeringEvent = 0) const;
146
147         TriState state(Event* triggeringEvent = 0) const;
148         String value(Event* triggeringEvent = 0) const;
149
150         bool isTextInsertion() const;
151
152         // Returns 0 if this Command is not supported.
153         int idForHistogram() const;
154     private:
155         const EditorInternalCommand* m_command;
156         EditorCommandSource m_source;
157         RefPtr<LocalFrame> m_frame;
158     };
159     Command command(const String& commandName); // Command source is CommandFromMenuOrKeyBinding.
160     Command command(const String& commandName, EditorCommandSource);
161
162     bool insertText(const String&, Event* triggeringEvent);
163     bool insertTextWithoutSendingTextEvent(const String&, bool selectInsertedText, TextEvent* triggeringEvent);
164     bool insertLineBreak();
165     bool insertParagraphSeparator();
166
167     bool isOverwriteModeEnabled() const { return m_overwriteModeEnabled; }
168     void toggleOverwriteModeEnabled();
169
170     bool canUndo();
171     void undo();
172     bool canRedo();
173     void redo();
174
175     void setBaseWritingDirection(WritingDirection);
176
177     // smartInsertDeleteEnabled and selectTrailingWhitespaceEnabled are
178     // mutually exclusive, meaning that enabling one will disable the other.
179     bool smartInsertDeleteEnabled() const;
180     bool isSelectTrailingWhitespaceEnabled() const;
181
182     bool preventRevealSelection() const { return m_preventRevealSelection; }
183
184     void setStartNewKillRingSequence(bool);
185
186     void clear();
187
188     VisibleSelection selectionForCommand(Event*);
189
190     KillRing& killRing() const { return *m_killRing; }
191
192     EditingBehavior behavior() const;
193
194     PassRefPtrWillBeRawPtr<Range> selectedRange();
195
196     void addToKillRing(Range*, bool prepend);
197
198     void pasteAsFragment(PassRefPtr<DocumentFragment>, bool smartReplace, bool matchStyle);
199     void pasteAsPlainText(const String&, bool smartReplace);
200
201     Node* findEventTargetFrom(const VisibleSelection&) const;
202
203     bool findString(const String&, FindOptions);
204     // FIXME: Switch callers over to the FindOptions version and retire this one.
205     bool findString(const String&, bool forward, bool caseFlag, bool wrapFlag, bool startInSelection);
206
207     PassRefPtrWillBeRawPtr<Range> findStringAndScrollToVisible(const String&, Range*, FindOptions);
208
209     const VisibleSelection& mark() const; // Mark, to be used as emacs uses it.
210     void setMark(const VisibleSelection&);
211
212     void computeAndSetTypingStyle(StylePropertySet* , EditAction = EditActionUnspecified);
213
214     IntRect firstRectForRange(Range*) const;
215
216     void respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions);
217
218     bool markedTextMatchesAreHighlighted() const;
219     void setMarkedTextMatchesAreHighlighted(bool);
220
221     void replaceSelectionWithFragment(PassRefPtr<DocumentFragment>, bool selectReplacement, bool smartReplace, bool matchStyle);
222     void replaceSelectionWithText(const String&, bool selectReplacement, bool smartReplace);
223
224     EditorParagraphSeparator defaultParagraphSeparator() const { return m_defaultParagraphSeparator; }
225     void setDefaultParagraphSeparator(EditorParagraphSeparator separator) { m_defaultParagraphSeparator = separator; }
226
227     class RevealSelectionScope {
228         WTF_MAKE_NONCOPYABLE(RevealSelectionScope);
229     public:
230         RevealSelectionScope(Editor*);
231         ~RevealSelectionScope();
232     private:
233         Editor* m_editor;
234     };
235     friend class RevealSelectionScope;
236
237 private:
238     LocalFrame& m_frame;
239     RefPtr<CompositeEditCommand> m_lastEditCommand;
240     int m_preventRevealSelection;
241     bool m_shouldStartNewKillRingSequence;
242     bool m_shouldStyleWithCSS;
243     const OwnPtr<KillRing> m_killRing;
244     VisibleSelection m_mark;
245     bool m_areMarkedTextMatchesHighlighted;
246     EditorParagraphSeparator m_defaultParagraphSeparator;
247     bool m_overwriteModeEnabled;
248
249     explicit Editor(LocalFrame&);
250
251     bool canDeleteRange(Range*) const;
252
253     UndoStack* undoStack() const;
254
255     bool tryDHTMLCopy();
256     bool tryDHTMLCut();
257     bool tryDHTMLPaste(PasteMode);
258
259     bool canSmartReplaceWithPasteboard(Pasteboard*);
260     void pasteAsPlainTextWithPasteboard(Pasteboard*);
261     void pasteWithPasteboard(Pasteboard*);
262     void writeSelectionToPasteboard(Pasteboard*, Range*, const String& plainText);
263     bool dispatchCPPEvent(const AtomicString&, ClipboardAccessPolicy, PasteMode = AllMimeTypes);
264
265     void revealSelectionAfterEditingOperation(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);
266     void changeSelectionAfterCommand(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions);
267     void notifyComponentsOnChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions);
268
269     Node* findEventTargetFromSelection() const;
270
271     PassRefPtrWillBeRawPtr<Range> rangeOfString(const String&, Range*, FindOptions);
272
273     SpellChecker& spellChecker() const;
274
275     bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
276 };
277
278 inline void Editor::setStartNewKillRingSequence(bool flag)
279 {
280     m_shouldStartNewKillRingSequence = flag;
281 }
282
283 inline const VisibleSelection& Editor::mark() const
284 {
285     return m_mark;
286 }
287
288 inline void Editor::setMark(const VisibleSelection& selection)
289 {
290     m_mark = selection;
291 }
292
293 inline bool Editor::markedTextMatchesAreHighlighted() const
294 {
295     return m_areMarkedTextMatchesHighlighted;
296 }
297
298
299 } // namespace WebCore
300
301 #endif // Editor_h