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