2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsPrintPreviewListener.h"
|
2011-10-29 13:22:19 -07:00
|
|
|
|
2013-11-26 16:55:26 -08:00
|
|
|
#include "mozilla/TextEvents.h"
|
2011-10-29 13:22:19 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-03-04 16:37:43 -08:00
|
|
|
#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
2012-08-04 00:44:00 -07:00
|
|
|
#include "nsIDOMEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsPresContext.h"
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
#include "nsFocusManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsLiteralString.h"
|
|
|
|
|
2011-10-29 13:22:19 -07:00
|
|
|
using namespace mozilla;
|
2013-04-19 15:18:32 -07:00
|
|
|
using namespace mozilla::dom;
|
2011-10-29 13:22:19 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsPrintPreviewListener, nsIDOMEventListener)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsPrintPreviewListener ctor
|
|
|
|
//
|
2013-04-19 15:18:32 -07:00
|
|
|
nsPrintPreviewListener::nsPrintPreviewListener(EventTarget* aTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mEventTarget(aTarget)
|
|
|
|
{
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
} // ctor
|
|
|
|
|
2013-08-19 03:15:55 -07:00
|
|
|
nsPrintPreviewListener::~nsPrintPreviewListener()
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
//
|
|
|
|
// AddListeners
|
|
|
|
//
|
|
|
|
// Subscribe to the events that will allow us to track various events.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsPrintPreviewListener::AddListeners()
|
|
|
|
{
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("click"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("keydown"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("keypress"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("keyup"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("mousedown"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("mousemove"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseout"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), this, true);
|
|
|
|
mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseup"), this, true);
|
2013-11-26 16:55:26 -08:00
|
|
|
|
|
|
|
mEventTarget->AddSystemEventListener(NS_LITERAL_STRING("keydown"),
|
|
|
|
this, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
//
|
|
|
|
// RemoveListeners
|
|
|
|
//
|
|
|
|
// Unsubscribe from all the various events that we were listening to.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsPrintPreviewListener::RemoveListeners()
|
|
|
|
{
|
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("click"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keydown"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keyup"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseout"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseover"), this, true);
|
|
|
|
mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseup"), this, true);
|
2013-11-26 16:55:26 -08:00
|
|
|
|
|
|
|
mEventTarget->RemoveSystemEventListener(NS_LITERAL_STRING("keydown"),
|
|
|
|
this, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
//
|
|
|
|
// GetActionForEvent
|
|
|
|
//
|
|
|
|
// Helper function to let certain key events through
|
|
|
|
//
|
|
|
|
enum eEventAction {
|
|
|
|
eEventAction_Tab, eEventAction_ShiftTab,
|
2013-11-26 16:55:26 -08:00
|
|
|
eEventAction_Propagate, eEventAction_Suppress,
|
|
|
|
eEventAction_StopPropagation
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static eEventAction
|
|
|
|
GetActionForEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2013-11-26 16:55:26 -08:00
|
|
|
WidgetKeyboardEvent* keyEvent =
|
|
|
|
aEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
|
|
|
return eEventAction_Suppress;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyEvent->mFlags.mInSystemGroup) {
|
|
|
|
NS_ASSERTION(keyEvent->message == NS_KEY_DOWN,
|
|
|
|
"Assuming we're listening only keydown event in system group");
|
|
|
|
return eEventAction_StopPropagation;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyEvent->IsAlt() || keyEvent->IsControl() || keyEvent->IsMeta()) {
|
|
|
|
// Don't consume keydown event because following keypress event may be
|
|
|
|
// handled as access key or shortcut key.
|
|
|
|
return (keyEvent->message == NS_KEY_DOWN) ? eEventAction_StopPropagation :
|
|
|
|
eEventAction_Suppress;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static const uint32_t kOKKeyCodes[] = {
|
2007-03-22 10:30:00 -07:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2013-11-26 16:55:26 -08:00
|
|
|
if (keyEvent->keyCode == nsIDOMKeyEvent::DOM_VK_TAB) {
|
|
|
|
return keyEvent->IsShift() ? eEventAction_ShiftTab : eEventAction_Tab;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-26 16:55:26 -08:00
|
|
|
if (keyEvent->charCode == ' ' || keyEvent->keyCode == NS_VK_SPACE) {
|
|
|
|
return eEventAction_Propagate;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-26 16:55:26 -08:00
|
|
|
if (keyEvent->IsShift()) {
|
|
|
|
return eEventAction_Suppress;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-26 16:55:26 -08:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(kOKKeyCodes); ++i) {
|
|
|
|
if (keyEvent->keyCode == kOKKeyCodes[i]) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return eEventAction_Propagate;
|
|
|
|
}
|
|
|
|
}
|
2013-11-26 16:55:26 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return eEventAction_Suppress;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrintPreviewListener::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2013-04-19 15:18:32 -07:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(
|
|
|
|
aEvent ? aEvent->InternalDOMEvent()->GetOriginalTarget() : nullptr);
|
2009-08-24 13:02:07 -07:00
|
|
|
if (content && !content->IsXUL()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
eEventAction action = ::GetActionForEvent(aEvent);
|
|
|
|
switch (action) {
|
|
|
|
case eEventAction_Tab:
|
|
|
|
case eEventAction_ShiftTab:
|
|
|
|
{
|
|
|
|
nsAutoString eventString;
|
|
|
|
aEvent->GetType(eventString);
|
2014-05-21 20:48:50 -07:00
|
|
|
if (eventString.EqualsLiteral("keydown")) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Handle tabbing explicitly here since we don't want focus ending up
|
|
|
|
// inside the content document, bug 244128.
|
|
|
|
nsIDocument* doc = content->GetCurrentDoc();
|
|
|
|
NS_ASSERTION(doc, "no document");
|
|
|
|
|
|
|
|
nsIDocument* parentDoc = doc->GetParentDocument();
|
|
|
|
NS_ASSERTION(parentDoc, "no parent document");
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(parentDoc->GetWindow());
|
|
|
|
|
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
if (fm && win) {
|
2011-10-29 13:22:19 -07:00
|
|
|
dom::Element* fromElement = parentDoc->FindContentForSubDocument(doc);
|
|
|
|
nsCOMPtr<nsIDOMElement> from = do_QueryInterface(fromElement);
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool forward = (action == eEventAction_Tab);
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
nsCOMPtr<nsIDOMElement> result;
|
|
|
|
fm->MoveFocus(win, from,
|
|
|
|
forward ? nsIFocusManager::MOVEFOCUS_FORWARD :
|
|
|
|
nsIFocusManager::MOVEFOCUS_BACKWARD,
|
2009-08-26 09:19:41 -07:00
|
|
|
nsIFocusManager::FLAG_BYKEY, getter_AddRefs(result));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// fall-through
|
|
|
|
case eEventAction_Suppress:
|
|
|
|
aEvent->StopPropagation();
|
|
|
|
aEvent->PreventDefault();
|
|
|
|
break;
|
2013-11-26 16:55:26 -08:00
|
|
|
case eEventAction_StopPropagation:
|
|
|
|
aEvent->StopPropagation();
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
case eEventAction_Propagate:
|
|
|
|
// intentionally empty
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 15:18:32 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|