2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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"
45 class CompositeEditCommand;
47 class EditCommandComposition;
49 class EditorInternalCommand;
58 class StylePropertySet;
63 enum EditorCommandSource { CommandFromMenuOrKeyBinding, CommandFromDOM, CommandFromDOMWithUserInterface };
64 enum EditorParagraphSeparator { EditorParagraphSeparatorIsDiv, EditorParagraphSeparatorIsP };
66 class Editor FINAL : public NoBaseWillBeGarbageCollectedFinalized<Editor> {
67 WTF_MAKE_NONCOPYABLE(Editor);
69 static PassOwnPtrWillBeRawPtr<Editor> create(LocalFrame&);
72 EditorClient& client() const;
74 LocalFrame& frame() const { return m_frame; }
76 CompositeEditCommand* lastEditCommand() { return m_lastEditCommand.get(); }
78 void handleKeyboardEvent(KeyboardEvent*);
79 bool handleTextEvent(TextEvent*);
82 bool canEditRichly() const;
90 bool canPaste() const;
91 bool canDelete() const;
92 bool canSmartCopyOrDelete() const;
97 void pasteAsPlainText();
100 static void countEvent(ExecutionContext*, const Event*);
101 void copyImage(const HitTestResult&);
107 bool shouldDeleteRange(Range*) const;
109 void respondToChangedContents(const VisibleSelection& endingSelection);
111 bool selectionStartHasStyle(CSSPropertyID, const String& value) const;
112 TriState selectionHasStyle(CSSPropertyID, const String& value) const;
113 String selectionStartCSSPropertyValue(CSSPropertyID);
115 TriState selectionUnorderedListState() const;
116 TriState selectionOrderedListState() const;
118 void removeFormattingAndStyle();
120 void clearLastEditCommand();
122 bool deleteWithDirection(SelectionDirection, TextGranularity, bool killRing, bool isTypingAction);
123 void deleteSelectionWithSmartDelete(bool smartDelete);
125 void applyStyle(StylePropertySet*, EditAction = EditActionUnspecified);
126 void applyParagraphStyle(StylePropertySet*, EditAction = EditActionUnspecified);
127 void applyStyleToSelection(StylePropertySet*, EditAction);
128 void applyParagraphStyleToSelection(StylePropertySet*, EditAction);
130 void appliedEditing(PassRefPtrWillBeRawPtr<CompositeEditCommand>);
131 void unappliedEditing(PassRefPtrWillBeRawPtr<EditCommandComposition>);
132 void reappliedEditing(PassRefPtrWillBeRawPtr<EditCommandComposition>);
134 void setShouldStyleWithCSS(bool flag) { m_shouldStyleWithCSS = flag; }
135 bool shouldStyleWithCSS() const { return m_shouldStyleWithCSS; }
140 Command(const EditorInternalCommand*, EditorCommandSource, PassRefPtr<LocalFrame>);
142 bool execute(const String& parameter = String(), Event* triggeringEvent = 0) const;
143 bool execute(Event* triggeringEvent) const;
145 bool isSupported() const;
146 bool isEnabled(Event* triggeringEvent = 0) const;
148 TriState state(Event* triggeringEvent = 0) const;
149 String value(Event* triggeringEvent = 0) const;
151 bool isTextInsertion() const;
153 // Returns 0 if this Command is not supported.
154 int idForHistogram() const;
156 const EditorInternalCommand* m_command;
157 EditorCommandSource m_source;
158 RefPtr<LocalFrame> m_frame;
160 Command command(const String& commandName); // Command source is CommandFromMenuOrKeyBinding.
161 Command command(const String& commandName, EditorCommandSource);
163 bool insertText(const String&, Event* triggeringEvent);
164 bool insertTextWithoutSendingTextEvent(const String&, bool selectInsertedText, TextEvent* triggeringEvent);
165 bool insertLineBreak();
166 bool insertParagraphSeparator();
168 bool isOverwriteModeEnabled() const { return m_overwriteModeEnabled; }
169 void toggleOverwriteModeEnabled();
176 void setBaseWritingDirection(WritingDirection);
178 // smartInsertDeleteEnabled and selectTrailingWhitespaceEnabled are
179 // mutually exclusive, meaning that enabling one will disable the other.
180 bool smartInsertDeleteEnabled() const;
181 bool isSelectTrailingWhitespaceEnabled() const;
183 bool preventRevealSelection() const { return m_preventRevealSelection; }
185 void setStartNewKillRingSequence(bool);
189 VisibleSelection selectionForCommand(Event*);
191 KillRing& killRing() const { return *m_killRing; }
193 EditingBehavior behavior() const;
195 PassRefPtrWillBeRawPtr<Range> selectedRange();
197 void addToKillRing(Range*, bool prepend);
199 void pasteAsFragment(PassRefPtrWillBeRawPtr<DocumentFragment>, bool smartReplace, bool matchStyle);
200 void pasteAsPlainText(const String&, bool smartReplace);
202 Node* findEventTargetFrom(const VisibleSelection&) const;
204 bool findString(const String&, FindOptions);
205 // FIXME: Switch callers over to the FindOptions version and retire this one.
206 bool findString(const String&, bool forward, bool caseFlag, bool wrapFlag, bool startInSelection);
208 PassRefPtrWillBeRawPtr<Range> findStringAndScrollToVisible(const String&, Range*, FindOptions);
210 const VisibleSelection& mark() const; // Mark, to be used as emacs uses it.
211 void setMark(const VisibleSelection&);
213 void computeAndSetTypingStyle(StylePropertySet* , EditAction = EditActionUnspecified);
215 IntRect firstRectForRange(Range*) const;
217 void respondToChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions);
219 bool markedTextMatchesAreHighlighted() const;
220 void setMarkedTextMatchesAreHighlighted(bool);
222 void replaceSelectionWithFragment(PassRefPtrWillBeRawPtr<DocumentFragment>, bool selectReplacement, bool smartReplace, bool matchStyle);
223 void replaceSelectionWithText(const String&, bool selectReplacement, bool smartReplace);
225 EditorParagraphSeparator defaultParagraphSeparator() const { return m_defaultParagraphSeparator; }
226 void setDefaultParagraphSeparator(EditorParagraphSeparator separator) { m_defaultParagraphSeparator = separator; }
228 class RevealSelectionScope {
229 WTF_MAKE_NONCOPYABLE(RevealSelectionScope);
231 RevealSelectionScope(Editor*);
232 ~RevealSelectionScope();
236 friend class RevealSelectionScope;
238 void trace(Visitor*);
242 RefPtrWillBeMember<CompositeEditCommand> m_lastEditCommand;
243 int m_preventRevealSelection;
244 bool m_shouldStartNewKillRingSequence;
245 bool m_shouldStyleWithCSS;
246 const OwnPtr<KillRing> m_killRing;
247 VisibleSelection m_mark;
248 bool m_areMarkedTextMatchesHighlighted;
249 EditorParagraphSeparator m_defaultParagraphSeparator;
250 bool m_overwriteModeEnabled;
252 explicit Editor(LocalFrame&);
254 bool canDeleteRange(Range*) const;
256 UndoStack* undoStack() const;
260 bool tryDHTMLPaste(PasteMode);
262 bool canSmartReplaceWithPasteboard(Pasteboard*);
263 void pasteAsPlainTextWithPasteboard(Pasteboard*);
264 void pasteWithPasteboard(Pasteboard*);
265 void writeSelectionToPasteboard(Pasteboard*, Range*, const String& plainText);
266 bool dispatchCPPEvent(const AtomicString&, ClipboardAccessPolicy, PasteMode = AllMimeTypes);
268 void revealSelectionAfterEditingOperation(const ScrollAlignment& = ScrollAlignment::alignCenterIfNeeded, RevealExtentOption = DoNotRevealExtent);
269 void changeSelectionAfterCommand(const VisibleSelection& newSelection, FrameSelection::SetSelectionOptions);
270 void notifyComponentsOnChangedSelection(const VisibleSelection& oldSelection, FrameSelection::SetSelectionOptions);
272 Node* findEventTargetFromSelection() const;
274 PassRefPtrWillBeRawPtr<Range> rangeOfString(const String&, Range*, FindOptions);
276 SpellChecker& spellChecker() const;
278 bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
281 inline void Editor::setStartNewKillRingSequence(bool flag)
283 m_shouldStartNewKillRingSequence = flag;
286 inline const VisibleSelection& Editor::mark() const
291 inline void Editor::setMark(const VisibleSelection& selection)
296 inline bool Editor::markedTextMatchesAreHighlighted() const
298 return m_areMarkedTextMatchesHighlighted;
302 } // namespace WebCore