📄 webeditorclient.cpp
字号:
/* * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "WebKitDLL.h"#include "WebEditorClient.h"#include "WebKit.h"#include "WebLocalizableStrings.h"#include "WebView.h"#include "DOMCoreClasses.h"#pragma warning(push, 0)#include <WebCore/BString.h>#include <WebCore/Document.h>#include <WebCore/EditCommand.h>#include <WebCore/HTMLElement.h>#include <WebCore/HTMLInputElement.h>#include <WebCore/HTMLNames.h>#include <WebCore/KeyboardEvent.h>#include <WebCore/PlatformKeyboardEvent.h>#include <WebCore/NotImplemented.h>#include <WebCore/Range.h>#pragma warning(pop)using namespace WebCore;using namespace HTMLNames;// {09A11D2B-FAFB-4ca0-A6F7-791EE8932C88}static const GUID IID_IWebUndoCommand = { 0x9a11d2b, 0xfafb, 0x4ca0, { 0xa6, 0xf7, 0x79, 0x1e, 0xe8, 0x93, 0x2c, 0x88 } };class IWebUndoCommand : public IUnknown {public: virtual void execute() = 0;};// WebEditorUndoTarget -------------------------------------------------------------class WebEditorUndoTarget : public IWebUndoTarget{public: WebEditorUndoTarget(); // IUnknown virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject); virtual ULONG STDMETHODCALLTYPE AddRef(void); virtual ULONG STDMETHODCALLTYPE Release(void); // IWebUndoTarget virtual HRESULT STDMETHODCALLTYPE invoke( /* [in] */ BSTR actionName, /* [in] */ IUnknown *obj);private: ULONG m_refCount;};WebEditorUndoTarget::WebEditorUndoTarget(): m_refCount(1){}HRESULT STDMETHODCALLTYPE WebEditorUndoTarget::QueryInterface(REFIID riid, void** ppvObject){ *ppvObject = 0; if (IsEqualGUID(riid, IID_IUnknown)) *ppvObject = static_cast<IWebUndoTarget*>(this); else if (IsEqualGUID(riid, IID_IWebUndoTarget)) *ppvObject = static_cast<IWebUndoTarget*>(this); else return E_NOINTERFACE; AddRef(); return S_OK;}ULONG STDMETHODCALLTYPE WebEditorUndoTarget::AddRef(void){ return ++m_refCount;}ULONG STDMETHODCALLTYPE WebEditorUndoTarget::Release(void){ ULONG newRef = --m_refCount; if (!newRef) delete(this); return newRef;}HRESULT STDMETHODCALLTYPE WebEditorUndoTarget::invoke( /* [in] */ BSTR /*actionName*/, /* [in] */ IUnknown *obj){ IWebUndoCommand* undoCommand = 0; if (SUCCEEDED(obj->QueryInterface(IID_IWebUndoCommand, (void**)&undoCommand))) { undoCommand->execute(); undoCommand->Release(); } return S_OK;}// WebEditorClient ------------------------------------------------------------------WebEditorClient::WebEditorClient(WebView* webView) : m_webView(webView) , m_undoTarget(0){ m_undoTarget = new WebEditorUndoTarget();}WebEditorClient::~WebEditorClient(){ if (m_undoTarget) m_undoTarget->Release();}void WebEditorClient::pageDestroyed(){ delete this;}bool WebEditorClient::isContinuousSpellCheckingEnabled(){ BOOL enabled; if (FAILED(m_webView->isContinuousSpellCheckingEnabled(&enabled))) return false; return !!enabled;}void WebEditorClient::toggleContinuousSpellChecking(){ m_webView->toggleContinuousSpellChecking(0);}bool WebEditorClient::isGrammarCheckingEnabled(){ BOOL enabled; if (FAILED(m_webView->isGrammarCheckingEnabled(&enabled))) return false; return !!enabled;}void WebEditorClient::toggleGrammarChecking(){ m_webView->toggleGrammarChecking(0);}static void initViewSpecificSpelling(IWebViewEditing* viewEditing){ // we just use this as a flag to indicate that we've spell checked the document // and need to close the spell checker out when the view closes. int tag; viewEditing->spellCheckerDocumentTag(&tag);}int WebEditorClient::spellCheckerDocumentTag(){ // we don't use the concept of spelling tags notImplemented(); ASSERT_NOT_REACHED(); return 0;}bool WebEditorClient::shouldBeginEditing(Range*){ notImplemented(); return true;}bool WebEditorClient::shouldEndEditing(Range*){ notImplemented(); return true;}void WebEditorClient::didBeginEditing(){ notImplemented();}void WebEditorClient::respondToChangedContents(){ notImplemented();}void WebEditorClient::respondToChangedSelection(){ m_webView->selectionChanged();}void WebEditorClient::didEndEditing(){ notImplemented();}void WebEditorClient::didWriteSelectionToPasteboard(){ notImplemented();}void WebEditorClient::didSetSelectionTypesForPasteboard(){ notImplemented();}bool WebEditorClient::shouldDeleteRange(Range* /*range*/){ notImplemented(); return true; // FIXME: calling m_webView->editingDelegate() will cause an assertion failure so we don't want to enable this code until that's implemented. //BOOL result = false; //IWebViewEditingDelegate* editingDelegate; //// FIXME: DOMRange needs to be implemented before anything meaningful can be done here //IDOMRange* domRange(0); //if (SUCCEEDED(m_webView->editingDelegate(&editingDelegate))) { // editingDelegate->shouldDeleteDOMRange(m_webView, domRange, &result); // editingDelegate->Release(); //} //return !!result;}bool WebEditorClient::shouldInsertNode(Node* /*node*/, Range* /*replacingRange*/, EditorInsertAction /*givenAction*/){ notImplemented(); return true; }bool WebEditorClient::shouldInsertText(const String& /*str*/, Range* /* replacingRange */, EditorInsertAction /*givenAction*/){ notImplemented(); return true; // FIXME: calling m_webView->editingDelegate() will cause an assertion failure so we don't want to enable this code until that's implemented. //BOOL result = false; //IWebViewEditingDelegate* editingDelegate; //// FIXME: DOMRange needs to be implemented before anything meaningful can be done here //IDOMRange* domRange(0); // make a DOMRange from replacingRange //BString text(str); //if (SUCCEEDED(m_webView->editingDelegate(&editingDelegate))) { // editingDelegate->shouldInsertText(m_webView, text, domRange, (WebViewInsertAction) givenAction, &result); // editingDelegate->Release(); //} //return !!result;}//bool WebEditorClient::shouldChangeSelectedRange(Range *currentRange, Range *toProposedRange, SelectionAffinity selectionAffinity, bool stillSelecting)//{ notImplemented(); return false; }bool WebEditorClient::shouldApplyStyle(CSSStyleDeclaration* /*style*/, Range* /*toElementsInDOMRange*/){ notImplemented(); return true; }bool WebEditorClient::shouldMoveRangeAfterDelete(Range* /*range*/, Range* /*rangeToBeReplaced*/){ notImplemented(); return true; }bool WebEditorClient::shouldChangeTypingStyle(CSSStyleDeclaration* /*currentStyle*/, CSSStyleDeclaration* /*toProposedStyle*/){ notImplemented(); return false; }void WebEditorClient::webViewDidChangeTypingStyle(WebNotification* /*notification*/){ notImplemented(); }void WebEditorClient::webViewDidChangeSelection(WebNotification* /*notification*/){ notImplemented(); }bool WebEditorClient::shouldShowDeleteInterface(HTMLElement* /*element*/){ notImplemented(); return false; }bool WebEditorClient::smartInsertDeleteEnabled(void){ BOOL enabled = FALSE; m_webView->smartInsertDeleteEnabled(&enabled); return !!enabled;}bool WebEditorClient::isSelectTrailingWhitespaceEnabled(void){ BOOL enabled = FALSE; m_webView->isSelectTrailingWhitespaceEnabled(&enabled); return !!enabled;}bool WebEditorClient::shouldChangeSelectedRange(WebCore::Range*, WebCore::Range*, WebCore::EAffinity, bool){ notImplemented(); return true; }void WebEditorClient::textFieldDidBeginEditing(Element* e){ IWebFormDelegate* formDelegate; if (SUCCEEDED(m_webView->formDelegate(&formDelegate)) && formDelegate) { IDOMElement* domElement = DOMElement::createInstance(e); if (domElement) { IDOMHTMLInputElement* domInputElement; if (SUCCEEDED(domElement->QueryInterface(IID_IDOMHTMLInputElement, (void**)&domInputElement))) { formDelegate->textFieldDidBeginEditing(domInputElement, kit(e->document()->frame())); domInputElement->Release(); } domElement->Release(); } formDelegate->Release(); }}void WebEditorClient::textFieldDidEndEditing(Element* e){ IWebFormDelegate* formDelegate; if (SUCCEEDED(m_webView->formDelegate(&formDelegate)) && formDelegate) { IDOMElement* domElement = DOMElement::createInstance(e); if (domElement) { IDOMHTMLInputElement* domInputElement; if (SUCCEEDED(domElement->QueryInterface(IID_IDOMHTMLInputElement, (void**)&domInputElement))) { formDelegate->textFieldDidEndEditing(domInputElement, kit(e->document()->frame())); domInputElement->Release(); } domElement->Release(); } formDelegate->Release(); }}void WebEditorClient::textDidChangeInTextField(Element* e){ IWebFormDelegate* formDelegate; if (SUCCEEDED(m_webView->formDelegate(&formDelegate)) && formDelegate) { IDOMElement* domElement = DOMElement::createInstance(e); if (domElement) { IDOMHTMLInputElement* domInputElement; if (SUCCEEDED(domElement->QueryInterface(IID_IDOMHTMLInputElement, (void**)&domInputElement))) { formDelegate->textDidChangeInTextField(domInputElement, kit(e->document()->frame())); domInputElement->Release(); } domElement->Release(); } formDelegate->Release(); }}bool WebEditorClient::doTextFieldCommandFromEvent(Element* e, KeyboardEvent* ke){ BOOL result = FALSE; IWebFormDelegate* formDelegate; if (SUCCEEDED(m_webView->formDelegate(&formDelegate)) && formDelegate) { IDOMElement* domElement = DOMElement::createInstance(e); if (domElement) { IDOMHTMLInputElement* domInputElement; if (SUCCEEDED(domElement->QueryInterface(IID_IDOMHTMLInputElement, (void**)&domInputElement))) { String command = m_webView->interpretKeyEvent(ke); // We allow empty commands here because the app code actually depends on this being called for all key presses. // We may want to revisit this later because it doesn't really make sense to send an empty command. formDelegate->doPlatformCommand(domInputElement, BString(command), kit(e->document()->frame()), &result); domInputElement->Release(); } domElement->Release(); } formDelegate->Release(); } return !!result;}void WebEditorClient::textWillBeDeletedInTextField(Element* e){ // We're using the deleteBackward command for all deletion operations since the autofill code treats all deletions the same way. IWebFormDelegate* formDelegate; if (SUCCEEDED(m_webView->formDelegate(&formDelegate)) && formDelegate) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -