2007-11-08 Alp Toker <alp@atoker.com>
[WebKit-https.git] / WebKit / gtk / WebCoreSupport / EditorClientGtk.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org> 
3  * Copyright (C) 2006 Zack Rusin <zack@kde.org>
4  * Copyright (C) 2006 Apple Computer, Inc.
5  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "EditorClientGtk.h"
33
34 #include "EditCommand.h"
35 #include "Editor.h"
36 #include "FocusController.h"
37 #include "Frame.h"
38 #include "KeyboardCodes.h"
39 #include "KeyboardEvent.h"
40 #include "NotImplemented.h"
41 #include "Page.h"
42 #include "PlatformKeyboardEvent.h"
43 #include "webkitgtkprivate.h"
44
45 #include <stdio.h>
46
47 using namespace WebCore;
48
49 namespace WebKit {
50
51 bool EditorClient::shouldDeleteRange(Range*)
52 {
53     notImplemented();
54     return true;
55 }
56
57 bool EditorClient::shouldShowDeleteInterface(HTMLElement*)
58 {
59     return false;
60 }
61
62 bool EditorClient::isContinuousSpellCheckingEnabled()
63 {
64     notImplemented();
65     return false;
66 }
67
68 bool EditorClient::isGrammarCheckingEnabled()
69 {
70     notImplemented();
71     return false;
72 }
73
74 int EditorClient::spellCheckerDocumentTag()
75 {
76     notImplemented();
77     return 0;
78 }
79
80 bool EditorClient::shouldBeginEditing(WebCore::Range*)
81 {
82     notImplemented();
83     return true;
84 }
85
86 bool EditorClient::shouldEndEditing(WebCore::Range*)
87 {
88     notImplemented();
89     return true;
90 }
91
92 bool EditorClient::shouldInsertText(String, Range*, EditorInsertAction)
93 {
94     notImplemented();
95     return true;
96 }
97
98 bool EditorClient::shouldChangeSelectedRange(Range*, Range*, EAffinity, bool)
99 {
100     notImplemented();
101     return true;
102 }
103
104 bool EditorClient::shouldApplyStyle(WebCore::CSSStyleDeclaration*,
105                                       WebCore::Range*)
106 {
107     notImplemented();
108     return true;
109 }
110
111 bool EditorClient::shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range*)
112 {
113     notImplemented();
114     return true;
115 }
116
117 void EditorClient::didBeginEditing()
118 {
119     notImplemented();
120 }
121
122 void EditorClient::respondToChangedContents()
123 {
124     notImplemented();
125 }
126
127 void EditorClient::respondToChangedSelection()
128 {
129     notImplemented();
130 }
131
132 void EditorClient::didEndEditing()
133 {
134     notImplemented();
135 }
136
137 void EditorClient::didWriteSelectionToPasteboard()
138 {
139     notImplemented();
140 }
141
142 void EditorClient::didSetSelectionTypesForPasteboard()
143 {
144     notImplemented();
145 }
146
147 bool EditorClient::isEditable()
148 {
149     notImplemented();
150     return false;
151 }
152
153 void EditorClient::registerCommandForUndo(WTF::PassRefPtr<WebCore::EditCommand>)
154 {
155     notImplemented();
156 }
157
158 void EditorClient::registerCommandForRedo(WTF::PassRefPtr<WebCore::EditCommand>)
159 {
160     notImplemented();
161 }
162
163 void EditorClient::clearUndoRedoOperations()
164 {
165     notImplemented();
166 }
167
168 bool EditorClient::canUndo() const
169 {
170     notImplemented();
171     return false;
172 }
173
174 bool EditorClient::canRedo() const
175 {
176     notImplemented();
177     return false;
178 }
179
180 void EditorClient::undo()
181 {
182     notImplemented();
183 }
184
185 void EditorClient::redo()
186 {
187     notImplemented();
188 }
189
190 bool EditorClient::shouldInsertNode(Node*, Range*, EditorInsertAction)
191 {
192     notImplemented();
193     return true;
194 }
195
196 void EditorClient::pageDestroyed()
197 {
198     delete this;
199 }
200
201 bool EditorClient::smartInsertDeleteEnabled()
202 {
203     notImplemented();
204     return false;
205 }
206
207 void EditorClient::toggleContinuousSpellChecking()
208 {
209     notImplemented();
210 }
211
212 void EditorClient::toggleGrammarChecking()
213 {
214 }
215
216 void EditorClient::handleKeypress(KeyboardEvent* event)
217 {
218     Frame* frame = core(m_page)->focusController()->focusedOrMainFrame();
219     if (!frame || !frame->document()->focusedNode())
220         return;
221
222     const PlatformKeyboardEvent* kevent = event->keyEvent();
223     if (!kevent || kevent->isKeyUp())
224         return;
225
226     Node* start = frame->selectionController()->start().node();
227     if (!start)
228         return;
229
230     // FIXME: Use GtkBindingSet instead of this hard-coded switch
231     // http://bugs.webkit.org/show_bug.cgi?id=15911
232
233     if (start->isContentEditable()) {
234         switch(kevent->WindowsKeyCode()) {
235             case VK_BACK:
236                 frame->editor()->deleteWithDirection(SelectionController::BACKWARD,
237                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity, false, true);
238                 break;
239             case VK_DELETE:
240                 frame->editor()->deleteWithDirection(SelectionController::FORWARD,
241                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity, false, true);
242                 break;
243             case VK_LEFT:
244                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
245                         SelectionController::LEFT,
246                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity,
247                         true);
248                 break;
249             case VK_RIGHT:
250                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
251                         SelectionController::RIGHT,
252                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity,
253                         true);
254                 break;
255             case VK_UP:
256                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
257                         SelectionController::BACKWARD,
258                         kevent->ctrlKey() ? ParagraphGranularity : LineGranularity,
259                         true);
260             case VK_DOWN:
261                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
262                         SelectionController::FORWARD,
263                         kevent->ctrlKey() ? ParagraphGranularity : LineGranularity,
264                         true);
265                 break;
266             case VK_PRIOR:  // PageUp
267                 frame->editor()->execCommand("MoveUpByPageAndModifyCaret");
268                 break;
269             case VK_NEXT:  // PageDown
270                 frame->editor()->execCommand("MoveDownByPageAndModifyCaret");
271                 break;
272             case VK_RETURN:
273                 frame->editor()->execCommand("InsertLineBreak");
274                 break;
275             case VK_TAB:
276                 return;
277             default:
278                 if (!kevent->ctrlKey() && !kevent->altKey() && !kevent->text().isEmpty()) {
279                     if (kevent->text().length() == 1) {
280                         UChar ch = kevent->text()[0];
281                         // Don't insert null or control characters as they can result in unexpected behaviour
282                         if (ch < ' ')
283                             break;
284                     }
285                     frame->editor()->insertText(kevent->text(), event);
286                 } else if (kevent->ctrlKey()) {
287                     switch (kevent->WindowsKeyCode()) {
288                         case VK_A:
289                             frame->editor()->execCommand("SelectAll");
290                             break;
291                         case VK_B:
292                             frame->editor()->execCommand("ToggleBold");
293                             break;
294                         case VK_C:
295                             frame->editor()->execCommand("Copy");
296                             break;
297                         case VK_I:
298                             frame->editor()->execCommand("ToggleItalic");
299                             break;
300                         case VK_V:
301                             frame->editor()->execCommand("Paste");
302                             break;
303                         case VK_X:
304                             frame->editor()->execCommand("Cut");
305                             break;
306                         case VK_Y:
307                             frame->editor()->execCommand("Redo");
308                             break;
309                         case VK_Z:
310                             frame->editor()->execCommand("Undo");
311                             break;
312                         default:
313                             return;
314                     }
315                 } else return;
316         }
317     } else {
318         switch (kevent->WindowsKeyCode()) {
319             case VK_UP:
320                 frame->editor()->execCommand("MoveUp");
321                 break;
322             case VK_DOWN:
323                 frame->editor()->execCommand("MoveDown");
324                 break;
325             case VK_PRIOR:  // PageUp
326                 frame->editor()->execCommand("MoveUpByPageAndModifyCaret");
327                 break;
328             case VK_NEXT:  // PageDown
329                 frame->editor()->execCommand("MoveDownByPageAndModifyCaret");
330                 break;
331             case VK_HOME:
332                 if (kevent->ctrlKey())
333                     frame->editor()->execCommand("MoveToBeginningOfDocument");
334                 break;
335             case VK_END:
336                 if (kevent->ctrlKey())
337                     frame->editor()->execCommand("MoveToEndOfDocument");
338                 break;
339             default:
340                 if (kevent->ctrlKey()) {
341                     switch(kevent->WindowsKeyCode()) {
342                         case VK_A:
343                             frame->editor()->execCommand("SelectAll");
344                             break;
345                         case VK_C: case VK_X:
346                             frame->editor()->execCommand("Copy");
347                             break;
348                         default:
349                             return;
350                     }
351                 } else return;
352         }
353     }
354     event->setDefaultHandled();
355 }
356
357
358 void EditorClient::handleInputMethodKeypress(KeyboardEvent*)
359 {
360     notImplemented();
361 }
362
363 EditorClient::EditorClient(WebKitPage* page)
364     : m_page(page)
365 {
366 }
367
368 void EditorClient::textFieldDidBeginEditing(Element*)
369 {
370     notImplemented();
371 }
372
373 void EditorClient::textFieldDidEndEditing(Element*)
374 {
375     notImplemented();
376 }
377
378 void EditorClient::textDidChangeInTextField(Element*)
379 {
380     notImplemented();
381 }
382
383 bool EditorClient::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
384 {
385     notImplemented();
386     return false;
387 }
388
389 void EditorClient::textWillBeDeletedInTextField(Element*)
390 {
391     notImplemented();
392 }
393
394 void EditorClient::textDidChangeInTextArea(Element*)
395 {
396     notImplemented();
397 }
398
399 void EditorClient::ignoreWordInSpellDocument(const String&)
400 {
401     notImplemented();
402 }
403
404 void EditorClient::learnWord(const String&)
405 {
406     notImplemented();
407 }
408
409 void EditorClient::checkSpellingOfString(const UChar*, int, int*, int*)
410 {
411     notImplemented();
412 }
413
414 void EditorClient::checkGrammarOfString(const UChar*, int, Vector<GrammarDetail>&, int*, int*)
415 {
416     notImplemented();
417 }
418
419 void EditorClient::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
420 {
421     notImplemented();
422 }
423
424 void EditorClient::updateSpellingUIWithMisspelledWord(const String&)
425 {
426     notImplemented();
427 }
428
429 void EditorClient::showSpellingUI(bool)
430 {
431     notImplemented();
432 }
433
434 bool EditorClient::spellingUIIsShowing()
435 {
436     notImplemented();
437     return false;
438 }
439
440 void EditorClient::getGuessesForWord(const String&, Vector<String>&)
441 {
442     notImplemented();
443 }
444
445 void EditorClient::setInputMethodState(bool)
446 {
447 }
448
449 }
450
451 // vim: ts=4 sw=4 et