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 "ClipboardAccessPolicy.h"
30 #include "Color.h"
31 #include "EditAction.h"
32 #include "EditorDeleteAction.h"
33 #include "EditorInsertAction.h"
34 #include "SelectionController.h"
35
36 namespace WebCore {
37
38 class CSSStyleDeclaration;
39 class Clipboard;
40 class DeleteButtonController;
41 class EditCommand;
42 class EditorClient;
43 class EditorInternalCommand;
44 class HTMLElement;
45 class HitTestResult;
46 class Pasteboard;
47 class SimpleFontData;
48 class Text;
49
50 struct CompositionUnderline {
CompositionUnderlineCompositionUnderline51 CompositionUnderline()
52 : startOffset(0), endOffset(0), thick(false) { }
CompositionUnderlineCompositionUnderline53 CompositionUnderline(unsigned s, unsigned e, const Color& c, bool t)
54 : startOffset(s), endOffset(e), color(c), thick(t) { }
55 unsigned startOffset;
56 unsigned endOffset;
57 Color color;
58 bool thick;
59 };
60
61 enum TriState { FalseTriState, TrueTriState, MixedTriState };
62 enum EditorCommandSource { CommandFromMenuOrKeyBinding, CommandFromDOM, CommandFromDOMWithUserInterface };
63 enum WritingDirection { NaturalWritingDirection, LeftToRightWritingDirection, RightToLeftWritingDirection };
64
65 class Editor {
66 public:
67 Editor(Frame*);
68 ~Editor();
69
70 EditorClient* client() const;
frame()71 Frame* frame() const { return m_frame; }
deleteButtonController()72 DeleteButtonController* deleteButtonController() const { return m_deleteButtonController.get(); }
lastEditCommand()73 EditCommand* lastEditCommand() { return m_lastEditCommand.get(); }
74
75 void handleKeyboardEvent(KeyboardEvent*);
76 void handleInputMethodKeydown(KeyboardEvent*);
77
78 bool canEdit() const;
79 bool canEditRichly() const;
80
81 bool canDHTMLCut();
82 bool canDHTMLCopy();
83 bool canDHTMLPaste();
84 bool tryDHTMLCopy();
85 bool tryDHTMLCut();
86 bool tryDHTMLPaste();
87
88 bool canCut() const;
89 bool canCopy() const;
90 bool canPaste() const;
91 bool canDelete() const;
92 bool canSmartCopyOrDelete();
93
94 void cut();
95 void copy();
96 void paste();
97 void pasteAsPlainText();
98 void performDelete();
99
100 void copyURL(const KURL&, const String&);
101 void copyImage(const HitTestResult&);
102
103 void indent();
104 void outdent();
105 void transpose();
106
107 bool shouldInsertFragment(PassRefPtr<DocumentFragment>, PassRefPtr<Range>, EditorInsertAction);
108 bool shouldInsertText(const String&, Range*, EditorInsertAction) const;
109 bool shouldShowDeleteInterface(HTMLElement*) const;
110 bool shouldDeleteRange(Range*) const;
111 bool shouldApplyStyle(CSSStyleDeclaration*, Range*);
112
113 void respondToChangedSelection(const Selection& oldSelection);
114 void respondToChangedContents(const Selection& endingSelection);
115
116 TriState selectionHasStyle(CSSStyleDeclaration*) const;
117 const SimpleFontData* fontForSelection(bool&) const;
118 WritingDirection textDirectionForSelection(bool&) const;
119
120 TriState selectionUnorderedListState() const;
121 TriState selectionOrderedListState() const;
122 PassRefPtr<Node> insertOrderedList();
123 PassRefPtr<Node> insertUnorderedList();
124 bool canIncreaseSelectionListLevel();
125 bool canDecreaseSelectionListLevel();
126 PassRefPtr<Node> increaseSelectionListLevel();
127 PassRefPtr<Node> increaseSelectionListLevelOrdered();
128 PassRefPtr<Node> increaseSelectionListLevelUnordered();
129 void decreaseSelectionListLevel();
130
131 void removeFormattingAndStyle();
132
133 void clearLastEditCommand();
134
135 bool deleteWithDirection(SelectionController::EDirection, TextGranularity, bool killRing, bool isTypingAction);
136 void deleteSelectionWithSmartDelete(bool smartDelete);
137 bool dispatchCPPEvent(const AtomicString&, ClipboardAccessPolicy);
138
removedAnchor()139 Node* removedAnchor() const { return m_removedAnchor.get(); }
setRemovedAnchor(PassRefPtr<Node> n)140 void setRemovedAnchor(PassRefPtr<Node> n) { m_removedAnchor = n; }
141
142 void applyStyle(CSSStyleDeclaration*, EditAction = EditActionUnspecified);
143 void applyParagraphStyle(CSSStyleDeclaration*, EditAction = EditActionUnspecified);
144 void applyStyleToSelection(CSSStyleDeclaration*, EditAction);
145 void applyParagraphStyleToSelection(CSSStyleDeclaration*, EditAction);
146
147 void appliedEditing(PassRefPtr<EditCommand>);
148 void unappliedEditing(PassRefPtr<EditCommand>);
149 void reappliedEditing(PassRefPtr<EditCommand>);
150
151 bool selectionStartHasStyle(CSSStyleDeclaration*) const;
152
153 bool clientIsEditable() const;
154
155 class Command {
156 public:
157 Command();
158 Command(PassRefPtr<Frame>, const EditorInternalCommand*, EditorCommandSource);
159
160 bool execute(const String& parameter = String(), Event* triggeringEvent = 0) const;
161 bool execute(Event* triggeringEvent) const;
162
163 bool isSupported() const;
164 bool isEnabled(Event* triggeringEvent = 0) const;
165
166 TriState state(Event* triggeringEvent = 0) const;
167 String value(Event* triggeringEvent = 0) const;
168
169 bool isTextInsertion() const;
170
171 private:
172 RefPtr<Frame> m_frame;
173 const EditorInternalCommand* m_command;
174 EditorCommandSource m_source;
175 };
176 Command command(const String& commandName); // Default is CommandFromMenuOrKeyBinding.
177 Command command(const String& commandName, EditorCommandSource);
178
179 bool insertText(const String&, Event* triggeringEvent);
180 bool insertTextWithoutSendingTextEvent(const String&, bool selectInsertedText, Event* triggeringEvent);
181 bool insertLineBreak();
182 bool insertParagraphSeparator();
183
184 bool isContinuousSpellCheckingEnabled();
185 void toggleContinuousSpellChecking();
186 bool isGrammarCheckingEnabled();
187 void toggleGrammarChecking();
188 void ignoreSpelling();
189 void learnSpelling();
190 int spellCheckerDocumentTag();
191 bool isSelectionUngrammatical();
192 bool isSelectionMisspelled();
193 Vector<String> guessesForMisspelledSelection();
194 Vector<String> guessesForUngrammaticalSelection();
195 void markMisspellingsAfterTypingToPosition(const VisiblePosition&);
196 void markMisspellings(const Selection&);
197 void markBadGrammar(const Selection&);
198 void advanceToNextMisspelling(bool startBeforeSelection = false);
199 void showSpellingGuessPanel();
200 bool spellingPanelIsShowing();
201
202 bool shouldBeginEditing(Range*);
203 bool shouldEndEditing(Range*);
204
205 void clearUndoRedoOperations();
206 bool canUndo();
207 void undo();
208 bool canRedo();
209 void redo();
210
211 void didBeginEditing();
212 void didEndEditing();
213 void didWriteSelectionToPasteboard();
214
215 void showFontPanel();
216 void showStylesPanel();
217 void showColorPanel();
218 void toggleBold();
219 void toggleUnderline();
220 void setBaseWritingDirection(WritingDirection);
221
222 // smartInsertDeleteEnabled and selectTrailingWhitespaceEnabled are
223 // mutually exclusive, meaning that enabling one will disable the other.
224 bool smartInsertDeleteEnabled();
225 bool isSelectTrailingWhitespaceEnabled();
226
227 bool hasBidiSelection() const;
228
229 // international text input composition
hasComposition()230 bool hasComposition() const { return m_compositionNode; }
231 void setComposition(const String&, const Vector<CompositionUnderline>&, unsigned selectionStart, unsigned selectionEnd);
232 void confirmComposition();
233 void confirmComposition(const String&); // if no existing composition, replaces selection
234 void confirmCompositionWithoutDisturbingSelection();
235 PassRefPtr<Range> compositionRange() const;
236 bool getCompositionSelection(unsigned& selectionStart, unsigned& selectionEnd) const;
237
238 // getting international text input composition state (for use by InlineTextBox)
compositionNode()239 Text* compositionNode() const { return m_compositionNode.get(); }
compositionStart()240 unsigned compositionStart() const { return m_compositionStart; }
compositionEnd()241 unsigned compositionEnd() const { return m_compositionEnd; }
compositionUsesCustomUnderlines()242 bool compositionUsesCustomUnderlines() const { return !m_customCompositionUnderlines.isEmpty(); }
customCompositionUnderlines()243 const Vector<CompositionUnderline>& customCompositionUnderlines() const { return m_customCompositionUnderlines; }
244
ignoreCompositionSelectionChange()245 bool ignoreCompositionSelectionChange() const { return m_ignoreCompositionSelectionChange; }
246
247 void setStartNewKillRingSequence(bool);
248
249 PassRefPtr<Range> rangeForPoint(const IntPoint& windowPoint);
250
251 void clear();
252
253 Selection selectionForCommand(Event*);
254
255 void appendToKillRing(const String&);
256 void prependToKillRing(const String&);
257 String yankFromKillRing();
258 void startNewKillRingSequence();
259 void setKillRingToYankedState();
260
261 PassRefPtr<Range> selectedRange();
262
263 // We should make these functions private when their callers in Frame are moved over here to Editor
264 bool insideVisibleArea(const IntPoint&) const;
265 bool insideVisibleArea(Range*) const;
266 PassRefPtr<Range> nextVisibleRange(Range*, const String&, bool forward, bool caseFlag, bool wrapFlag);
267
268 void addToKillRing(Range*, bool prepend);
269 private:
270 Frame* m_frame;
271 OwnPtr<DeleteButtonController> m_deleteButtonController;
272 RefPtr<EditCommand> m_lastEditCommand;
273 RefPtr<Node> m_removedAnchor;
274
275 RefPtr<Text> m_compositionNode;
276 unsigned m_compositionStart;
277 unsigned m_compositionEnd;
278 Vector<CompositionUnderline> m_customCompositionUnderlines;
279 bool m_ignoreCompositionSelectionChange;
280 bool m_shouldStartNewKillRingSequence;
281
282 bool canDeleteRange(Range*) const;
283 bool canSmartReplaceWithPasteboard(Pasteboard*);
284 PassRefPtr<Clipboard> newGeneralClipboard(ClipboardAccessPolicy);
285 void pasteAsPlainTextWithPasteboard(Pasteboard*);
286 void pasteWithPasteboard(Pasteboard*, bool allowPlainText);
287 void replaceSelectionWithFragment(PassRefPtr<DocumentFragment>, bool selectReplacement, bool smartReplace, bool matchStyle);
288 void replaceSelectionWithText(const String&, bool selectReplacement, bool smartReplace);
289 void writeSelectionToPasteboard(Pasteboard*);
290 void revealSelectionAfterEditingOperation();
291
292 void selectComposition();
293 void confirmComposition(const String&, bool preserveSelection);
294 void setIgnoreCompositionSelectionChange(bool ignore);
295
296 PassRefPtr<Range> firstVisibleRange(const String&, bool caseFlag);
297 PassRefPtr<Range> lastVisibleRange(const String&, bool caseFlag);
298 };
299
setStartNewKillRingSequence(bool flag)300 inline void Editor::setStartNewKillRingSequence(bool flag)
301 {
302 m_shouldStartNewKillRingSequence = flag;
303 }
304
305 } // namespace WebCore
306
307 #endif // Editor_h
308