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
|
|
|
|
|
|
|
#include "mozilla/dom/Element.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 "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;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsPrintPreviewListener, nsIDOMEventListener)
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsPrintPreviewListener ctor
|
|
|
|
//
|
|
|
|
nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventTarget* aTarget)
|
|
|
|
: mEventTarget(aTarget)
|
|
|
|
{
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
} // ctor
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
//
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
//
|
|
|
|
// GetActionForEvent
|
|
|
|
//
|
|
|
|
// Helper function to let certain key events through
|
|
|
|
//
|
|
|
|
enum eEventAction {
|
|
|
|
eEventAction_Tab, eEventAction_ShiftTab,
|
|
|
|
eEventAction_Propagate, eEventAction_Suppress
|
|
|
|
};
|
|
|
|
|
|
|
|
static eEventAction
|
|
|
|
GetActionForEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
|
|
|
|
if (keyEvent) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool b;
|
2007-03-22 10:30:00 -07:00
|
|
|
keyEvent->GetAltKey(&b);
|
|
|
|
if (b) return eEventAction_Suppress;
|
|
|
|
keyEvent->GetCtrlKey(&b);
|
|
|
|
if (b) return eEventAction_Suppress;
|
|
|
|
|
|
|
|
keyEvent->GetShiftKey(&b);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t keyCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
keyEvent->GetKeyCode(&keyCode);
|
|
|
|
if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB)
|
|
|
|
return b ? eEventAction_ShiftTab : eEventAction_Tab;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t charCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
keyEvent->GetCharCode(&charCode);
|
|
|
|
if (charCode == ' ' || keyCode == nsIDOMKeyEvent::DOM_VK_SPACE)
|
|
|
|
return eEventAction_Propagate;
|
|
|
|
|
|
|
|
if (b) return eEventAction_Suppress;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < sizeof(kOKKeyCodes)/sizeof(kOKKeyCodes[0]); ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (keyCode == kOKKeyCodes[i]) {
|
|
|
|
return eEventAction_Propagate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return eEventAction_Suppress;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrintPreviewListener::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
2012-08-04 00:44:00 -07:00
|
|
|
if (aEvent)
|
|
|
|
aEvent->GetOriginalTarget(getter_AddRefs(target));
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(target));
|
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);
|
|
|
|
if (eventString == NS_LITERAL_STRING("keydown")) {
|
|
|
|
// 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;
|
|
|
|
case eEventAction_Propagate:
|
|
|
|
// intentionally empty
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|