2007-12-04 Luca Bruno <lethalman88@gmail.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 "webkitprivate.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     return webkit_web_view_get_editable(m_page);
150 }
151
152 void EditorClient::registerCommandForUndo(WTF::PassRefPtr<WebCore::EditCommand>)
153 {
154     notImplemented();
155 }
156
157 void EditorClient::registerCommandForRedo(WTF::PassRefPtr<WebCore::EditCommand>)
158 {
159     notImplemented();
160 }
161
162 void EditorClient::clearUndoRedoOperations()
163 {
164     notImplemented();
165 }
166
167 bool EditorClient::canUndo() const
168 {
169     notImplemented();
170     return false;
171 }
172
173 bool EditorClient::canRedo() const
174 {
175     notImplemented();
176     return false;
177 }
178
179 void EditorClient::undo()
180 {
181     notImplemented();
182 }
183
184 void EditorClient::redo()
185 {
186     notImplemented();
187 }
188
189 bool EditorClient::shouldInsertNode(Node*, Range*, EditorInsertAction)
190 {
191     notImplemented();
192     return true;
193 }
194
195 void EditorClient::pageDestroyed()
196 {
197     delete this;
198 }
199
200 bool EditorClient::smartInsertDeleteEnabled()
201 {
202     notImplemented();
203     return false;
204 }
205
206 void EditorClient::toggleContinuousSpellChecking()
207 {
208     notImplemented();
209 }
210
211 void EditorClient::toggleGrammarChecking()
212 {
213 }
214
215 void EditorClient::handleKeypress(KeyboardEvent* event)
216 {
217     Frame* frame = core(m_page)->focusController()->focusedOrMainFrame();
218     if (!frame || !frame->document()->focusedNode())
219         return;
220
221     const PlatformKeyboardEvent* kevent = event->keyEvent();
222     if (!kevent || kevent->isKeyUp())
223         return;
224
225     Node* start = frame->selectionController()->start().node();
226     if (!start)
227         return;
228
229     // FIXME: Use GtkBindingSet instead of this hard-coded switch
230     // http://bugs.webkit.org/show_bug.cgi?id=15911
231
232     if (start->isContentEditable()) {
233         switch(kevent->WindowsKeyCode()) {
234             case VK_BACK:
235                 frame->editor()->deleteWithDirection(SelectionController::BACKWARD,
236                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity, false, true);
237                 break;
238             case VK_DELETE:
239                 frame->editor()->deleteWithDirection(SelectionController::FORWARD,
240                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity, false, true);
241                 break;
242             case VK_LEFT:
243                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
244                         SelectionController::LEFT,
245                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity,
246                         true);
247                 break;
248             case VK_RIGHT:
249                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
250                         SelectionController::RIGHT,
251                         kevent->ctrlKey() ? WordGranularity : CharacterGranularity,
252                         true);
253                 break;
254             case VK_UP:
255                 frame->selectionController()->modify(kevent->shiftKey() ? SelectionController::EXTEND : SelectionController::MOVE,
256                         SelectionController::BACKWARD,
257                         kevent->ctrlKey() ? ParagraphGranularity : LineGranularity,
258                         true);
259                 break;
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_B:
289                             frame->editor()->execCommand("ToggleBold");
290                             break;
291                         case VK_I:
292                             frame->editor()->execCommand("ToggleItalic");
293                             break;
294                         case VK_Y:
295                             frame->editor()->execCommand("Redo");
296                             break;
297                         case VK_Z:
298                             frame->editor()->execCommand("Undo");
299                             break;
300                         default:
301                             return;
302                     }
303                 } else return;
304         }
305     } else {
306         switch (kevent->WindowsKeyCode()) {
307             case VK_UP:
308                 frame->editor()->execCommand("MoveUp");
309                 break;
310             case VK_DOWN:
311                 frame->editor()->execCommand("MoveDown");
312                 break;
313             case VK_PRIOR:  // PageUp
314                 frame->editor()->execCommand("MoveUpByPageAndModifyCaret");
315                 break;
316             case VK_NEXT:  // PageDown
317                 frame->editor()->execCommand("MoveDownByPageAndModifyCaret");
318                 break;
319             case VK_HOME:
320                 if (kevent->ctrlKey())
321                     frame->editor()->execCommand("MoveToBeginningOfDocument");
322                 break;
323             case VK_END:
324                 if (kevent->ctrlKey())
325                     frame->editor()->execCommand("MoveToEndOfDocument");
326                 break;
327             default:
328                 return;
329         }
330     }
331     event->setDefaultHandled();
332 }
333
334
335 void EditorClient::handleInputMethodKeypress(KeyboardEvent*)
336 {
337     notImplemented();
338 }
339
340 EditorClient::EditorClient(WebKitWebView* page)
341     : m_page(page)
342 {
343 }
344
345 void EditorClient::textFieldDidBeginEditing(Element*)
346 {
347     notImplemented();
348 }
349
350 void EditorClient::textFieldDidEndEditing(Element*)
351 {
352     notImplemented();
353 }
354
355 void EditorClient::textDidChangeInTextField(Element*)
356 {
357     notImplemented();
358 }
359
360 bool EditorClient::doTextFieldCommandFromEvent(Element*, KeyboardEvent*)
361 {
362     notImplemented();
363     return false;
364 }
365
366 void EditorClient::textWillBeDeletedInTextField(Element*)
367 {
368     notImplemented();
369 }
370
371 void EditorClient::textDidChangeInTextArea(Element*)
372 {
373     notImplemented();
374 }
375
376 void EditorClient::ignoreWordInSpellDocument(const String&)
377 {
378     notImplemented();
379 }
380
381 void EditorClient::learnWord(const String&)
382 {
383     notImplemented();
384 }
385
386 void EditorClient::checkSpellingOfString(const UChar*, int, int*, int*)
387 {
388     notImplemented();
389 }
390
391 void EditorClient::checkGrammarOfString(const UChar*, int, Vector<GrammarDetail>&, int*, int*)
392 {
393     notImplemented();
394 }
395
396 void EditorClient::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
397 {
398     notImplemented();
399 }
400
401 void EditorClient::updateSpellingUIWithMisspelledWord(const String&)
402 {
403     notImplemented();
404 }
405
406 void EditorClient::showSpellingUI(bool)
407 {
408     notImplemented();
409 }
410
411 bool EditorClient::spellingUIIsShowing()
412 {
413     notImplemented();
414     return false;
415 }
416
417 void EditorClient::getGuessesForWord(const String&, Vector<String>&)
418 {
419     notImplemented();
420 }
421
422 void EditorClient::setInputMethodState(bool)
423 {
424 }
425
426 }
427
428 // vim: ts=4 sw=4 et