Add event listeners to "prevent" ContextMenu, Key, Mouse and MouseMotion events from being processed.

Bug 128449 r=dcone sr=attinasi a=asa
This commit is contained in:
rods%netscape.com 2002-03-23 18:07:51 +00:00
Родитель d391f38ecb
Коммит e3c71475b1
9 изменённых файлов: 668 добавлений и 2 удалений

Просмотреть файл

@ -60,6 +60,7 @@ REQUIRES = xpcom \
$(NULL)
CPPSRCS = \
nsPrintPreviewListener.cpp \
nsCommentNode.cpp \
nsContentIterator.cpp \
nsContentList.cpp \

Просмотреть файл

@ -60,6 +60,7 @@ REQUIRES = xpcom \
$(NULL)
CPP_OBJS= \
.\$(OBJDIR)\nsPrintPreviewListener.obj \
.\$(OBJDIR)\nsPrintProgress.obj \
.\$(OBJDIR)\nsPrintProgressParams.obj \
.\$(OBJDIR)\nsStyleContext.obj \

Просмотреть файл

@ -131,6 +131,7 @@ static NS_DEFINE_IID(kPrinterEnumeratorCID, NS_PRINTER_ENUMERATOR_CID);
// Printing Events
#include "nsIEventQueue.h"
#include "nsIEventQueueService.h"
#include "nsPrintPreviewListener.h"
// Printing
#include "nsIWebBrowserPrint.h"
@ -421,6 +422,7 @@ public:
nsCOMPtr<nsIPrintSettings> mPrintSettings;
nsCOMPtr<nsIPrintOptions> mPrintOptions;
nsPrintPreviewListener* mPPEventListeners;
#ifdef DEBUG_PRINTING
FILE * mDebugFD;
@ -589,6 +591,7 @@ private:
void InstallNewPresentation();
void ReturnToGalleyPresentation();
void TurnScriptingOn(PRBool aDoTurnOn);
void InstallPrintPreviewListener();
#endif
@ -810,7 +813,7 @@ PrintData::PrintData() :
mIsAborted(PR_FALSE), mPreparingForPrint(PR_FALSE), mDocWasToBeDestroyed(PR_FALSE),
mShrinkToFit(PR_FALSE), mPrintFrameType(nsIPrintSettings::kFramesAsIs),
mNumPrintableDocs(0), mNumDocsPrinted(0), mNumPrintablePages(0), mNumPagesPrinted(0),
mShrinkRatio(1.0), mOrigDCScale(1.0)
mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(NULL)
{
#ifdef DEBUG_PRINTING
mDebugFD = fopen("printing.log", "w");
@ -819,6 +822,12 @@ PrintData::PrintData() :
PrintData::~PrintData()
{
// remove the event listeners
if (mPPEventListeners) {
mPPEventListeners->RemoveListeners();
NS_RELEASE(mPPEventListeners);
}
// Only Send an OnEndPrinting if we have started printing
if (mOnStartSent) {
OnEndPrinting();
@ -3854,6 +3863,11 @@ DocumentViewerImpl::FindXMostFrameInList(nsIPresContext* aPresContext,
rect.x += aX;
rect.y += aY;
nscoord xMost = rect.XMost();
// make sure we have a reasonable value
NS_ASSERTION(xMost < NS_UNCONSTRAINEDSIZE, "Some frame's size is bad.");
if (xMost >= NS_UNCONSTRAINEDSIZE) {
xMost = 0;
}
#ifdef DEBUG_PRINTING // keep this here but leave it turned off
nsAutoString tmp;
@ -5106,6 +5120,24 @@ DocumentViewerImpl::TurnScriptingOn(PRBool aDoTurnOn)
scx->SetScriptsEnabled(aDoTurnOn, PR_TRUE);
}
//-------------------------------------------------------
// Install our event listeners on the document to prevent
// some events from being processed while in PrintPreview
//
// No return code - if this fails, there isn't much we can do
void
DocumentViewerImpl::InstallPrintPreviewListener()
{
if (!mPrt->mPPEventListeners) {
nsCOMPtr<nsIDOMEventReceiver> evRec (do_QueryInterface(mDocument));
mPrt->mPPEventListeners = new nsPrintPreviewListener(evRec);
if (mPrt->mPPEventListeners) {
mPrt->mPPEventListeners->AddListeners();
}
}
}
//----------------------------------------------------------------------
static nsresult GetSeqFrameAndCountPages(PrintObject* aPO,
nsIFrame*& aSeqFrame,
@ -5391,6 +5423,8 @@ DocumentViewerImpl::InstallNewPresentation()
}
#endif
InstallPrintPreviewListener();
// Install the new Presentation
mPresShell = prtObjToDisplay->mPresShell;
mPresContext = prtObjToDisplay->mPresContext;

Просмотреть файл

@ -0,0 +1,188 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
*
*/
#include "nsPrintPreviewListener.h"
#include "nsIDOMKeyEvent.h"
NS_IMPL_ADDREF(nsPrintPreviewListener)
NS_IMPL_RELEASE(nsPrintPreviewListener)
NS_INTERFACE_MAP_BEGIN(nsPrintPreviewListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMContextMenuListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMKeyListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMMouseListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMMouseMotionListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIDOMEventListener, nsIDOMContextMenuListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMContextMenuListener)
NS_INTERFACE_MAP_END
//
// nsPrintPreviewListener ctor
//
nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventReceiver* aEVRec)
: mEventReceiver(aEVRec),
mRegFlags(REG_NONE_LISTENER)
{
NS_INIT_REFCNT();
NS_ADDREF_THIS();
} // ctor
//-------------------------------------------------------
//
// AddListeners
//
// Subscribe to the events that will allow us to track various events.
//
nsresult
nsPrintPreviewListener::AddListeners()
{
if (mRegFlags != REG_NONE_LISTENER) return NS_ERROR_FAILURE;
if (mEventReceiver) {
nsIDOMContextMenuListener *pListener = NS_STATIC_CAST(nsIDOMContextMenuListener *, this);
NS_ASSERTION(pListener, "Cast can't fail!");
nsresult rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMContextMenuListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_CONTEXTMENU_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMKeyListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_KEY_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_MOUSE_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseMotionListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_MOUSEMOTION_LISTENER;
}
return NS_OK;
}
//-------------------------------------------------------
//
// RemoveListeners
//
// Unsubscribe from all the various events that we were listening to.
//
nsresult
nsPrintPreviewListener::RemoveListeners()
{
if (mEventReceiver && mRegFlags != REG_NONE_LISTENER) {
nsIDOMContextMenuListener *pListener = NS_STATIC_CAST(nsIDOMContextMenuListener *, this);
NS_ASSERTION(pListener, "Cast can't fail!");
// ignore return values, so we can try to unregister the other listeners
if (mRegFlags & REG_CONTEXTMENU_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMContextMenuListener));
}
if (mRegFlags & REG_KEY_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMKeyListener));
}
if (mRegFlags & REG_MOUSE_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
}
if (mRegFlags & REG_MOUSEMOTION_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseMotionListener));
}
}
return NS_OK;
}
//-------------------------------------------------------
//
// IsKeyOK
//
// Helper function to let certain key events thru
//
static PRBool IsKeyOK(nsIDOMEvent* aEvent)
{
const PRUint32 kOKKeyCodes[] = {nsIDOMKeyEvent::DOM_VK_PAGE_UP, nsIDOMKeyEvent::DOM_VK_PAGE_DOWN,
nsIDOMKeyEvent::DOM_VK_UP, nsIDOMKeyEvent::DOM_VK_DOWN,
nsIDOMKeyEvent::DOM_VK_HOME, nsIDOMKeyEvent::DOM_VK_END, 0};
nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
if (keyEvent) {
PRBool b;
keyEvent->GetAltKey(&b);
if (b) return PR_FALSE;
keyEvent->GetCtrlKey(&b);
if (b) return PR_FALSE;
keyEvent->GetShiftKey(&b);
if (b) return PR_FALSE;
PRUint32 keyCode;
keyEvent->GetKeyCode(&keyCode);
PRInt32 i = 0;
while (kOKKeyCodes[i] != 0) {
if (keyCode == kOKKeyCodes[i]) {
return PR_TRUE;
}
i++;
}
}
return PR_FALSE;
}
//-------------------------------------------------------
//
// KeyDown
//
NS_IMETHODIMP nsPrintPreviewListener::KeyDown(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}
//-------------------------------------------------------
//
// KeyUp
//
NS_IMETHODIMP nsPrintPreviewListener::KeyUp(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}
//-------------------------------------------------------
//
// KeyPress
//
NS_IMETHODIMP nsPrintPreviewListener::KeyPress(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,95 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
*
*/
#ifndef nsPrintPreviewListener_h__
#define nsPrintPreviewListener_h__
// Interfaces needed to be included
#include "nsIDOMContextMenuListener.h"
#include "nsIDOMKeyListener.h"
#include "nsIDOMMouseListener.h"
#include "nsIDOMMouseMotionListener.h"
// Helper Classes
#include "nsIDOMEventReceiver.h"
#include "nsCOMPtr.h"
#define REG_NONE_LISTENER 0x00
#define REG_CONTEXTMENU_LISTENER 0x01
#define REG_KEY_LISTENER 0x02
#define REG_MOUSE_LISTENER 0x04
#define REG_MOUSEMOTION_LISTENER 0x08
//
// class nsPrintPreviewListener
//
// The class that listens to the chrome events and tells the embedding
// chrome to show context menus, as appropriate. Handles registering itself
// with the DOM with AddChromeListeners() and removing itself with
// RemoveChromeListeners().
//
class nsPrintPreviewListener : public nsIDOMContextMenuListener,
public nsIDOMKeyListener,
public nsIDOMMouseListener,
public nsIDOMMouseMotionListener
{
public:
NS_DECL_ISUPPORTS
nsPrintPreviewListener(nsIDOMEventReceiver* aEVRec);
// nsIDOMContextMenuListener
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) { return NS_OK; }
NS_IMETHOD ContextMenu (nsIDOMEvent* aEvent) { printf("preventing ContextMenu\n"); aEvent->PreventDefault(); return NS_OK; }
// nsIDOMKeyListener
NS_IMETHOD KeyDown(nsIDOMEvent* aKeyEvent);
NS_IMETHOD KeyUp(nsIDOMEvent* aKeyEvent);
NS_IMETHOD KeyPress(nsIDOMEvent* aKeyEvent);
// nsIDOMMouseListener
NS_IMETHOD MouseDown(nsIDOMEvent* aMouseEvent) { printf("preventing MouseDown\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseUp(nsIDOMEvent* aMouseEvent) { printf("preventing MouseUp\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent) { printf("preventing MouseClick\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseDblClick(nsIDOMEvent* aMouseEvent) { printf("preventing MouseDblClick\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseOver(nsIDOMEvent* aMouseEvent) { printf("preventing MouseOver\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseOut(nsIDOMEvent* aMouseEvent) { printf("preventing MouseOut\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
// nsIDOMMouseMotionListener
NS_IMETHOD DragMove(nsIDOMEvent* aMouseEvent) { return NS_OK; };
NS_IMETHOD MouseMove(nsIDOMEvent* aMouseEvent) { aMouseEvent->PreventDefault(); return NS_OK; }
// Add/remove the relevant listeners, based on what interfaces
// the embedding chrome implements.
nsresult AddListeners();
nsresult RemoveListeners();
private:
nsCOMPtr<nsIDOMEventReceiver> mEventReceiver;
PRInt8 mRegFlags;
}; // class nsPrintPreviewListener
#endif /* nsPrintPreviewListener_h__ */

Просмотреть файл

@ -1603,6 +1603,13 @@
<FILEKIND>Text</FILEKIND>
<FILEFLAGS>Debug</FILEFLAGS>
</FILE>
<FILE>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintPreviewListener.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
<FILEKIND>Text</FILEKIND>
<FILEFLAGS>Debug</FILEFLAGS>
</FILE>
<FILE>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintProgress.cpp</PATH>
@ -2938,6 +2945,11 @@
<PATH>nsDocumentViewer.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintPreviewListener.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintProgress.cpp</PATH>
@ -5080,6 +5092,13 @@
<FILEKIND>Text</FILEKIND>
<FILEFLAGS>Debug</FILEFLAGS>
</FILE>
<FILE>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintPreviewListener.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
<FILEKIND>Text</FILEKIND>
<FILEFLAGS>Debug</FILEFLAGS>
</FILE>
<FILE>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintProgress.cpp</PATH>
@ -6415,6 +6434,11 @@
<PATH>nsDocumentViewer.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintPreviewListener.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintProgress.cpp</PATH>
@ -7070,6 +7094,12 @@
<PATH>nsDocumentViewer.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<TARGETNAME>content.shlb</TARGETNAME>
<PATHTYPE>Name</PATHTYPE>
<PATH>nsPrintPreviewListener.cpp</PATH>
<PATHFORMAT>MacOS</PATHFORMAT>
</FILEREF>
<FILEREF>
<TARGETNAME>content.shlb</TARGETNAME>
<PATHTYPE>Name</PATHTYPE>

Просмотреть файл

@ -131,6 +131,7 @@ static NS_DEFINE_IID(kPrinterEnumeratorCID, NS_PRINTER_ENUMERATOR_CID);
// Printing Events
#include "nsIEventQueue.h"
#include "nsIEventQueueService.h"
#include "nsPrintPreviewListener.h"
// Printing
#include "nsIWebBrowserPrint.h"
@ -421,6 +422,7 @@ public:
nsCOMPtr<nsIPrintSettings> mPrintSettings;
nsCOMPtr<nsIPrintOptions> mPrintOptions;
nsPrintPreviewListener* mPPEventListeners;
#ifdef DEBUG_PRINTING
FILE * mDebugFD;
@ -589,6 +591,7 @@ private:
void InstallNewPresentation();
void ReturnToGalleyPresentation();
void TurnScriptingOn(PRBool aDoTurnOn);
void InstallPrintPreviewListener();
#endif
@ -810,7 +813,7 @@ PrintData::PrintData() :
mIsAborted(PR_FALSE), mPreparingForPrint(PR_FALSE), mDocWasToBeDestroyed(PR_FALSE),
mShrinkToFit(PR_FALSE), mPrintFrameType(nsIPrintSettings::kFramesAsIs),
mNumPrintableDocs(0), mNumDocsPrinted(0), mNumPrintablePages(0), mNumPagesPrinted(0),
mShrinkRatio(1.0), mOrigDCScale(1.0)
mShrinkRatio(1.0), mOrigDCScale(1.0), mPPEventListeners(NULL)
{
#ifdef DEBUG_PRINTING
mDebugFD = fopen("printing.log", "w");
@ -819,6 +822,12 @@ PrintData::PrintData() :
PrintData::~PrintData()
{
// remove the event listeners
if (mPPEventListeners) {
mPPEventListeners->RemoveListeners();
NS_RELEASE(mPPEventListeners);
}
// Only Send an OnEndPrinting if we have started printing
if (mOnStartSent) {
OnEndPrinting();
@ -3854,6 +3863,11 @@ DocumentViewerImpl::FindXMostFrameInList(nsIPresContext* aPresContext,
rect.x += aX;
rect.y += aY;
nscoord xMost = rect.XMost();
// make sure we have a reasonable value
NS_ASSERTION(xMost < NS_UNCONSTRAINEDSIZE, "Some frame's size is bad.");
if (xMost >= NS_UNCONSTRAINEDSIZE) {
xMost = 0;
}
#ifdef DEBUG_PRINTING // keep this here but leave it turned off
nsAutoString tmp;
@ -5106,6 +5120,24 @@ DocumentViewerImpl::TurnScriptingOn(PRBool aDoTurnOn)
scx->SetScriptsEnabled(aDoTurnOn, PR_TRUE);
}
//-------------------------------------------------------
// Install our event listeners on the document to prevent
// some events from being processed while in PrintPreview
//
// No return code - if this fails, there isn't much we can do
void
DocumentViewerImpl::InstallPrintPreviewListener()
{
if (!mPrt->mPPEventListeners) {
nsCOMPtr<nsIDOMEventReceiver> evRec (do_QueryInterface(mDocument));
mPrt->mPPEventListeners = new nsPrintPreviewListener(evRec);
if (mPrt->mPPEventListeners) {
mPrt->mPPEventListeners->AddListeners();
}
}
}
//----------------------------------------------------------------------
static nsresult GetSeqFrameAndCountPages(PrintObject* aPO,
nsIFrame*& aSeqFrame,
@ -5391,6 +5423,8 @@ DocumentViewerImpl::InstallNewPresentation()
}
#endif
InstallPrintPreviewListener();
// Install the new Presentation
mPresShell = prtObjToDisplay->mPresShell;
mPresContext = prtObjToDisplay->mPresContext;

Просмотреть файл

@ -0,0 +1,188 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
*
*/
#include "nsPrintPreviewListener.h"
#include "nsIDOMKeyEvent.h"
NS_IMPL_ADDREF(nsPrintPreviewListener)
NS_IMPL_RELEASE(nsPrintPreviewListener)
NS_INTERFACE_MAP_BEGIN(nsPrintPreviewListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMContextMenuListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMKeyListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMMouseListener)
NS_INTERFACE_MAP_ENTRY(nsIDOMMouseMotionListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIDOMEventListener, nsIDOMContextMenuListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMContextMenuListener)
NS_INTERFACE_MAP_END
//
// nsPrintPreviewListener ctor
//
nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventReceiver* aEVRec)
: mEventReceiver(aEVRec),
mRegFlags(REG_NONE_LISTENER)
{
NS_INIT_REFCNT();
NS_ADDREF_THIS();
} // ctor
//-------------------------------------------------------
//
// AddListeners
//
// Subscribe to the events that will allow us to track various events.
//
nsresult
nsPrintPreviewListener::AddListeners()
{
if (mRegFlags != REG_NONE_LISTENER) return NS_ERROR_FAILURE;
if (mEventReceiver) {
nsIDOMContextMenuListener *pListener = NS_STATIC_CAST(nsIDOMContextMenuListener *, this);
NS_ASSERTION(pListener, "Cast can't fail!");
nsresult rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMContextMenuListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_CONTEXTMENU_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMKeyListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_KEY_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_MOUSE_LISTENER;
rv = mEventReceiver->AddEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseMotionListener));
NS_ENSURE_SUCCESS(rv, rv);
mRegFlags |= REG_MOUSEMOTION_LISTENER;
}
return NS_OK;
}
//-------------------------------------------------------
//
// RemoveListeners
//
// Unsubscribe from all the various events that we were listening to.
//
nsresult
nsPrintPreviewListener::RemoveListeners()
{
if (mEventReceiver && mRegFlags != REG_NONE_LISTENER) {
nsIDOMContextMenuListener *pListener = NS_STATIC_CAST(nsIDOMContextMenuListener *, this);
NS_ASSERTION(pListener, "Cast can't fail!");
// ignore return values, so we can try to unregister the other listeners
if (mRegFlags & REG_CONTEXTMENU_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMContextMenuListener));
}
if (mRegFlags & REG_KEY_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMKeyListener));
}
if (mRegFlags & REG_MOUSE_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseListener));
}
if (mRegFlags & REG_MOUSEMOTION_LISTENER) {
mEventReceiver->RemoveEventListenerByIID(pListener, NS_GET_IID(nsIDOMMouseMotionListener));
}
}
return NS_OK;
}
//-------------------------------------------------------
//
// IsKeyOK
//
// Helper function to let certain key events thru
//
static PRBool IsKeyOK(nsIDOMEvent* aEvent)
{
const PRUint32 kOKKeyCodes[] = {nsIDOMKeyEvent::DOM_VK_PAGE_UP, nsIDOMKeyEvent::DOM_VK_PAGE_DOWN,
nsIDOMKeyEvent::DOM_VK_UP, nsIDOMKeyEvent::DOM_VK_DOWN,
nsIDOMKeyEvent::DOM_VK_HOME, nsIDOMKeyEvent::DOM_VK_END, 0};
nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
if (keyEvent) {
PRBool b;
keyEvent->GetAltKey(&b);
if (b) return PR_FALSE;
keyEvent->GetCtrlKey(&b);
if (b) return PR_FALSE;
keyEvent->GetShiftKey(&b);
if (b) return PR_FALSE;
PRUint32 keyCode;
keyEvent->GetKeyCode(&keyCode);
PRInt32 i = 0;
while (kOKKeyCodes[i] != 0) {
if (keyCode == kOKKeyCodes[i]) {
return PR_TRUE;
}
i++;
}
}
return PR_FALSE;
}
//-------------------------------------------------------
//
// KeyDown
//
NS_IMETHODIMP nsPrintPreviewListener::KeyDown(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}
//-------------------------------------------------------
//
// KeyUp
//
NS_IMETHODIMP nsPrintPreviewListener::KeyUp(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}
//-------------------------------------------------------
//
// KeyPress
//
NS_IMETHODIMP nsPrintPreviewListener::KeyPress(nsIDOMEvent* aKeyEvent)
{
if (!IsKeyOK(aKeyEvent)) {
aKeyEvent->PreventDefault();
}
return NS_OK;
}

Просмотреть файл

@ -0,0 +1,95 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
*
*/
#ifndef nsPrintPreviewListener_h__
#define nsPrintPreviewListener_h__
// Interfaces needed to be included
#include "nsIDOMContextMenuListener.h"
#include "nsIDOMKeyListener.h"
#include "nsIDOMMouseListener.h"
#include "nsIDOMMouseMotionListener.h"
// Helper Classes
#include "nsIDOMEventReceiver.h"
#include "nsCOMPtr.h"
#define REG_NONE_LISTENER 0x00
#define REG_CONTEXTMENU_LISTENER 0x01
#define REG_KEY_LISTENER 0x02
#define REG_MOUSE_LISTENER 0x04
#define REG_MOUSEMOTION_LISTENER 0x08
//
// class nsPrintPreviewListener
//
// The class that listens to the chrome events and tells the embedding
// chrome to show context menus, as appropriate. Handles registering itself
// with the DOM with AddChromeListeners() and removing itself with
// RemoveChromeListeners().
//
class nsPrintPreviewListener : public nsIDOMContextMenuListener,
public nsIDOMKeyListener,
public nsIDOMMouseListener,
public nsIDOMMouseMotionListener
{
public:
NS_DECL_ISUPPORTS
nsPrintPreviewListener(nsIDOMEventReceiver* aEVRec);
// nsIDOMContextMenuListener
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) { return NS_OK; }
NS_IMETHOD ContextMenu (nsIDOMEvent* aEvent) { printf("preventing ContextMenu\n"); aEvent->PreventDefault(); return NS_OK; }
// nsIDOMKeyListener
NS_IMETHOD KeyDown(nsIDOMEvent* aKeyEvent);
NS_IMETHOD KeyUp(nsIDOMEvent* aKeyEvent);
NS_IMETHOD KeyPress(nsIDOMEvent* aKeyEvent);
// nsIDOMMouseListener
NS_IMETHOD MouseDown(nsIDOMEvent* aMouseEvent) { printf("preventing MouseDown\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseUp(nsIDOMEvent* aMouseEvent) { printf("preventing MouseUp\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseClick(nsIDOMEvent* aMouseEvent) { printf("preventing MouseClick\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseDblClick(nsIDOMEvent* aMouseEvent) { printf("preventing MouseDblClick\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseOver(nsIDOMEvent* aMouseEvent) { printf("preventing MouseOver\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
NS_IMETHOD MouseOut(nsIDOMEvent* aMouseEvent) { printf("preventing MouseOut\n"); aMouseEvent->StopPropagation();aMouseEvent->PreventDefault(); return NS_OK; }
// nsIDOMMouseMotionListener
NS_IMETHOD DragMove(nsIDOMEvent* aMouseEvent) { return NS_OK; };
NS_IMETHOD MouseMove(nsIDOMEvent* aMouseEvent) { aMouseEvent->PreventDefault(); return NS_OK; }
// Add/remove the relevant listeners, based on what interfaces
// the embedding chrome implements.
nsresult AddListeners();
nsresult RemoveListeners();
private:
nsCOMPtr<nsIDOMEventReceiver> mEventReceiver;
PRInt8 mRegFlags;
}; // class nsPrintPreviewListener
#endif /* nsPrintPreviewListener_h__ */