source: webkit/trunk/WebCore/dom/KeyboardEvent.cpp @ 19579

Last change on this file since 19579 was 19579, checked in by darin, 17 years ago

WebCore:

Reviewed by Oliver.

This area still needs work because keypress events are being sent for all the
events before they go to the input methods, and it seems that this is not
compatible with what other browsers do and with what sites assume. That's covered
by this bug:

http://bugs.webkit.org/show_bug.cgi?id=10871
<rdar://problem/4823129> REGRESSION: IME key events different in nightly (10871)

which is still open.

  • bindings/objc/ExceptionHandlers.h: Remove selectRange helper that doesn't really belong here, and also isn't needed since the callers don't want to raise ObjC exceptions.
  • bindings/objc/ExceptionHandlers.mm: Ditto. Also removed unneeded external globals that aren't exported anyway and unneeded includes.
  • bridge/EditorClient.h: Added Mac-specific function, markedTextAbandoned. Long term this might move into platform instead, but it's fine to have it on the client for now as long as it doesn't cause trouble for platforms other than Mac.
  • dom/EventTargetNode.cpp: (WebCore::EventTargetNode::defaultEventHandler): Since all the event handling code in this function is intended to run on the node itself, not parent nodes, check and do nothing if this event is bubbling up from another node. Added code to call a function on EventHandler for default handling of text input events.
  • dom/KeyboardEvent.h:
  • dom/KeyboardEvent.cpp: (WebCore::findKeyboardEvent): Added. Just like the findUIEventWithKeyState function, but specific to keyboard events.
  • dom/TextEvent.h: Added isLineBreak and isBackTab flags so that we can carry additional information about what a text input event is trying to do, but still go through the DOM event machinery.
  • dom/TextEvent.cpp: Ditto.
  • editing/Editor.h:
  • editing/Editor.cpp: (WebCore::Editor::handleKeyPress): Removed unneeded check of isCaretOrRange, since isContentEditable already checks that. (WebCore::Editor::canEdit): Ditto. (WebCore::Editor::canEditRichly): Ditto. (WebCore::Editor::canPaste): Changed to call canEdit for brevity. (WebCore::Editor::replaceSelectionWithText): Changed to take a const String&. (WebCore::Editor::shouldInsertText): Ditto. (WebCore::hasEditableSelection): Removed unneeded check of isCaretOrRange, since isContentEditable already checks that. (WebCore::Editor::Editor): Initialize the new m_ignoreMarkedTextSelectionChange flag. Moved here from WebHTMLView. (WebCore::Editor::insertText): Moved most of the code from -[WebHTMLView insertText:] here, since this needs to be done in response to a text input event and we don't want to go back over the WebKit side for that. (WebCore::Editor::insertLineBreak): Added. Moved here from -[WebHTMLView insertLineBreak:] for the same reason -- needs to be done in response to text input events. (WebCore::Editor::insertParagraphSeparator): Ditto. (WebCore::Editor::selectMarkedText): Added. Used by insertText. (WebCore::Editor::discardMarkedText): Added. Used by insertText. (WebCore::Editor::unmarkText): Added. Used by insertText.
  • editing/mac/EditorMac.mm: (WebCore::Editor::unmarkText): Added. Used by insertText.
  • html/HTMLInputElement.cpp: (WebCore::HTMLInputElement::canHaveSelection): Changed to use isTextField to avoid repeating the switch statement. (WebCore::HTMLInputElement::selectionStart): Ditto. (WebCore::HTMLInputElement::selectionEnd): Ditto. (WebCore::HTMLInputElement::setSelectionStart): Ditto. (WebCore::HTMLInputElement::setSelectionEnd): Ditto. (WebCore::HTMLInputElement::select): Ditto. (WebCore::HTMLInputElement::setSelectionRange): Ditto. (WebCore::HTMLInputElement::createRenderer): Resorted cases alphaebtically. (WebCore::HTMLInputElement::defaultEventHandler): Added code to make hitting return cause the form to be submitted when it comes through as a text input event. This is now the normal case on Macintosh, because enter key presses normally turn into this. In addition, change the other event handling to run only after trying the base defaultEventHandler. That makes the key handling done in EventTargetNode take precedence over the event handling here for editable text fields, which is what we want. This gets rid of the need for the hack where we check inputManagerHasMarkedText and suppress the handling of the enter key.
  • page/EventHandler.h: Removed unneeded includes. Moved EventHandlerDragState down into the private part of the class. Changed sendContextMenuEvent to take a const&. Added new handleTextInputEvent and defaultTextInputEventHandler functions. Made eventLoopHandleMouseUp and eventLoopHandleMouseDragged private. Added private defaultTabEventHandler function.
  • page/EventHandler.cpp: (WebCore::EventHandler::sendContextMenuEvent): Changed parameter to be a const&. (WebCore::EventHandler::defaultKeyboardEventHandler): Rearranged things so that the key bindings handling in handleKeyPress comes before special tab event handling. In cases where the tab turns into text to insert, we now handle it in the text input event handler. (WebCore::EventHandler::handleTextInputEvent): Added. Creates and dispatches a text input event. (WebCore::EventHandler::defaultTextInputEventHandler): Added. Inserts text, but has special cases for the tab and newline character cases. (WebCore::EventHandler::defaultTabEventHandler): Added. Shared by the tab-handling code in the keyboard event and text input event handlers.
  • page/mac/EventHandlerMac.mm: Tweak formatting, remove some unused functions.
  • page/FocusController.h: Removed unnneded includes and forwards, and added an overload of advanceFocus that takes a FocusDirection parameter.
  • page/FocusController.cpp: (WebCore::FocusController::advanceFocus): Changed to allow a 0 for the event pointer. Also added a version where the direction is passed explicitly, to be used for cases where we want to tab backward even if the event doesn't have a shift modifier key set.
  • platform/graphics/svg/SVGImageEmptyClients.h: Updated for change to EditorClient. This should really probably just use 0 for the EditorClient.
  • WebCore.exp: Update both for newly used and no-longer-used entry points.
  • page/mac/WebCoreFrameBridge.h:
  • page/mac/WebCoreFrameBridge.mm: Removed two unused methods.

WebKit:

Reviewed by Oliver.

  • WebCoreSupport/WebEditorClient.h:
  • WebCoreSupport/WebEditorClient.mm: (WebEditorClient::markedTextAbandoned): Added.
  • WebView/WebHTMLView.mm: (-[WebHTMLView menuForEvent:]): Added explicit constructor needed now that the function takes a const&. (-[WebHTMLView becomeFirstResponder]): Removed fake event code, no longer needed since advanceFocus now works fine with 0 for a DOM event. (-[WebHTMLView _expandSelectionToGranularity:]): Changed to use the normal selection controller function instead of selectRange. (-[WebHTMLView insertTab:]): Changed to call bottleneck that receives the DOM event. (-[WebHTMLView insertBacktab:]): Ditto. (-[WebHTMLView insertNewline:]): Ditto. (-[WebHTMLView insertLineBreak:]): Ditto. (-[WebHTMLView insertParagraphSeparator:]): Ditto. (-[WebHTMLView insertNewlineIgnoringFieldEditor:]): Ditto. (-[WebHTMLView insertTabIgnoringFieldEditor:]): Ditto. (-[WebHTMLView yank:]): Updated to call Editor directly since the insertText code now works via a text input event which is not what we want for paste-like things such as yank. (-[WebHTMLView yankAndSelect:]): Ditto. (-[WebHTMLView selectToMark:]): Changed to use the normal selection controller function instead of selectRange, which also allows us to remove the ObjC exception handling code. (-[WebHTMLView swapWithMark:]): Ditto. (-[WebHTMLView transpose:]): Ditto. (-[WebHTMLView unmarkText]): Since this is one of the calls back from the input manager, added code to set the "event was handled" flag. Moved the actual work into the Editor class in WebCore and just call that from here. (-[WebHTMLView _selectRangeInMarkedText:]): Changed to use the normal selection controller function instead of selectRange. (-[WebHTMLView setMarkedText:selectedRange:]): Since this is one of the calls back from the input manager, added code to set the "event was handled" flag. Also changed the ignoreMarkedTextSelectionChange to use the flag over on the WebCore side, since we moved it there and to call selectMarkedText over on the WebCore side too. (-[WebHTMLView doCommandBySelector:]): Added special cases for newline and tab selectors so that the event is passed along. These selectors are special because they are ones that turn into text input events. (-[WebHTMLView _discardMarkedText]): Moved the body of this function into the Editor class in WebCore and just call that from here. (-[WebHTMLView insertText:]): Added code to send a text input event instead of calling the editor to do text insertion. The insertion is then done in the default handler for the text input event. (-[WebHTMLView _insertNewlineWithEvent:isLineBreak:]): Added. Sends a text input event. (-[WebHTMLView _insertTabWithEvent:isBackTab:]): Ditto. (-[WebHTMLView _updateSelectionForInputManager]): Changed to use the ignoreMarkedTextSelectionChange flag in Editor now that the one here is gone.
  • WebView/WebHTMLViewInternal.h: Remove ignoreMarkedTextSelectionChange field.
  • WebView/WebView.mm: (-[WebView setSelectedDOMRange:affinity:]): Changed to use the normal selection controller function instead of selectRange.
  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1/**
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003, 2005, 2006, 2007 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#include "config.h"
24#include "KeyboardEvent.h"
25
26#include "EventNames.h"
27#include "PlatformKeyboardEvent.h"
28
29namespace WebCore {
30
31using namespace EventNames;
32
33KeyboardEvent::KeyboardEvent()
34    : m_keyEvent(0)
35    , m_keyLocation(DOM_KEY_LOCATION_STANDARD)
36    , m_altGraphKey(false)
37{
38}
39
40KeyboardEvent::KeyboardEvent(const PlatformKeyboardEvent& key, AbstractView* view)
41    : UIEventWithKeyState(key.isKeyUp() ? keyupEvent : key.isAutoRepeat() ? keypressEvent : keydownEvent,
42                          true, true, view, 0, key.ctrlKey(), key.altKey(), key.shiftKey(), key.metaKey())
43    , m_keyEvent(new PlatformKeyboardEvent(key))
44    , m_keyIdentifier(key.keyIdentifier())
45    , m_keyLocation(key.isKeypad() ? DOM_KEY_LOCATION_NUMPAD : DOM_KEY_LOCATION_STANDARD)
46    , m_altGraphKey(false)
47{
48}
49
50KeyboardEvent::KeyboardEvent(const AtomicString& eventType, bool canBubble, bool cancelable, AbstractView *view,
51                             const String &keyIdentifier,  unsigned keyLocation,
52                             bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
53    : UIEventWithKeyState(eventType, canBubble, cancelable, view, 0, ctrlKey, altKey, shiftKey, metaKey)
54    , m_keyEvent(0)
55    , m_keyIdentifier(keyIdentifier)
56    , m_keyLocation(keyLocation)
57    , m_altGraphKey(altGraphKey)
58{
59}
60
61KeyboardEvent::~KeyboardEvent()
62{
63    delete m_keyEvent;
64}
65
66void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
67                                      const String &keyIdentifier, unsigned keyLocation,
68                                      bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
69{
70    if (dispatched())
71        return;
72
73    initUIEvent(type, canBubble, cancelable, view, 0);
74
75    m_keyIdentifier = keyIdentifier;
76    m_keyLocation = keyLocation;
77    m_ctrlKey = ctrlKey;
78    m_shiftKey = shiftKey;
79    m_altKey = altKey;
80    m_metaKey = metaKey;
81    m_altGraphKey = altGraphKey;
82}
83
84bool KeyboardEvent::getModifierState(const String& keyIdentifier) const
85{
86    if (keyIdentifier == "Control")
87        return ctrlKey();
88    if (keyIdentifier == "Shift")
89        return shiftKey();
90    if (keyIdentifier == "Alt")
91        return altKey();
92    if (keyIdentifier == "Meta")
93        return metaKey();
94    return false;
95}
96
97int KeyboardEvent::keyCode() const
98{
99    if (!m_keyEvent)
100        return 0;
101    if (type() == keydownEvent || type() == keyupEvent)
102        return m_keyEvent->WindowsKeyCode();
103    return charCode();
104}
105
106int KeyboardEvent::charCode() const
107{
108    if (!m_keyEvent)
109        return 0;
110    String text = m_keyEvent->text();
111    if (text.length() != 1)
112        return 0;
113    return text[0];
114}
115
116bool KeyboardEvent::isKeyboardEvent() const
117{
118    return true;
119}
120
121int KeyboardEvent::which() const
122{
123    // Netscape's "which" returns a virtual key code for keydown and keyup, and a character code for keypress.
124    // That's exactly what IE's "keyCode" returns. So they are the same for keyboard events.
125    return keyCode();
126}
127
128KeyboardEvent* findKeyboardEvent(Event* event)
129{
130    for (Event* e = event; e; e = e->underlyingEvent())
131        if (e->isKeyboardEvent())
132            return static_cast<KeyboardEvent*>(e);
133    return 0;
134}
135
136} // namespace WebCore
Note: See TracBrowser for help on using the repository browser.