Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / printing / nsPrintPreviewListener.cpp
blob9ee525a40992224268a10cef15db9c2e4409f89b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Mats Palmgren <mats.palmgren@bredband.net>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsPrintPreviewListener.h"
41 #include "nsIContent.h"
42 #include "nsIDOMKeyEvent.h"
43 #include "nsIDOMNSEvent.h"
44 #include "nsIDocument.h"
45 #include "nsIPresShell.h"
46 #include "nsIDocShell.h"
47 #include "nsPresContext.h"
48 #include "nsIEventStateManager.h"
49 #include "nsLiteralString.h"
51 NS_IMPL_ISUPPORTS1(nsPrintPreviewListener, nsIDOMEventListener)
55 // nsPrintPreviewListener ctor
57 nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventTarget* aTarget)
58 : mEventTarget(aTarget)
60 NS_ADDREF_THIS();
61 } // ctor
64 //-------------------------------------------------------
66 // AddListeners
68 // Subscribe to the events that will allow us to track various events.
70 nsresult
71 nsPrintPreviewListener::AddListeners()
73 if (mEventTarget) {
74 mEventTarget->AddEventListener(NS_LITERAL_STRING("click"), this, true);
75 mEventTarget->AddEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
76 mEventTarget->AddEventListener(NS_LITERAL_STRING("keydown"), this, true);
77 mEventTarget->AddEventListener(NS_LITERAL_STRING("keypress"), this, true);
78 mEventTarget->AddEventListener(NS_LITERAL_STRING("keyup"), this, true);
79 mEventTarget->AddEventListener(NS_LITERAL_STRING("mousedown"), this, true);
80 mEventTarget->AddEventListener(NS_LITERAL_STRING("mousemove"), this, true);
81 mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseout"), this, true);
82 mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), this, true);
83 mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseup"), this, true);
86 return NS_OK;
90 //-------------------------------------------------------
92 // RemoveListeners
94 // Unsubscribe from all the various events that we were listening to.
96 nsresult
97 nsPrintPreviewListener::RemoveListeners()
99 if (mEventTarget) {
100 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("click"), this, true);
101 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
102 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keydown"), this, true);
103 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
104 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keyup"), this, true);
105 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
106 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"), this, true);
107 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseout"), this, true);
108 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseover"), this, true);
109 mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseup"), this, true);
112 return NS_OK;
115 //-------------------------------------------------------
117 // GetActionForEvent
119 // Helper function to let certain key events through
121 enum eEventAction {
122 eEventAction_Tab, eEventAction_ShiftTab,
123 eEventAction_Propagate, eEventAction_Suppress
126 static eEventAction
127 GetActionForEvent(nsIDOMEvent* aEvent)
129 static const PRUint32 kOKKeyCodes[] = {
130 nsIDOMKeyEvent::DOM_VK_PAGE_UP, nsIDOMKeyEvent::DOM_VK_PAGE_DOWN,
131 nsIDOMKeyEvent::DOM_VK_UP, nsIDOMKeyEvent::DOM_VK_DOWN,
132 nsIDOMKeyEvent::DOM_VK_HOME, nsIDOMKeyEvent::DOM_VK_END
135 nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
136 if (keyEvent) {
137 PRBool b;
138 keyEvent->GetAltKey(&b);
139 if (b) return eEventAction_Suppress;
140 keyEvent->GetCtrlKey(&b);
141 if (b) return eEventAction_Suppress;
143 keyEvent->GetShiftKey(&b);
145 PRUint32 keyCode;
146 keyEvent->GetKeyCode(&keyCode);
147 if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB)
148 return b ? eEventAction_ShiftTab : eEventAction_Tab;
150 PRUint32 charCode;
151 keyEvent->GetCharCode(&charCode);
152 if (charCode == ' ' || keyCode == nsIDOMKeyEvent::DOM_VK_SPACE)
153 return eEventAction_Propagate;
155 if (b) return eEventAction_Suppress;
157 for (PRUint32 i = 0; i < sizeof(kOKKeyCodes)/sizeof(kOKKeyCodes[0]); ++i) {
158 if (keyCode == kOKKeyCodes[i]) {
159 return eEventAction_Propagate;
163 return eEventAction_Suppress;
166 NS_IMETHODIMP
167 nsPrintPreviewListener::HandleEvent(nsIDOMEvent* aEvent)
169 nsCOMPtr<nsIDOMEventTarget> target;
170 nsCOMPtr<nsIDOMNSEvent> nsEvent = do_QueryInterface(aEvent);
171 if (nsEvent)
172 nsEvent->GetOriginalTarget(getter_AddRefs(target));
173 nsCOMPtr<nsIContent> content(do_QueryInterface(target));
174 if (content && !content->IsNodeOfType(nsINode::eXUL)) {
175 eEventAction action = ::GetActionForEvent(aEvent);
176 switch (action) {
177 case eEventAction_Tab:
178 case eEventAction_ShiftTab:
180 nsAutoString eventString;
181 aEvent->GetType(eventString);
182 if (eventString == NS_LITERAL_STRING("keydown")) {
183 // Handle tabbing explicitly here since we don't want focus ending up
184 // inside the content document, bug 244128.
185 nsIDocument* doc = content->GetCurrentDoc();
186 NS_ASSERTION(doc, "no document");
188 nsIDocument* parentDoc = doc->GetParentDocument();
189 NS_ASSERTION(parentDoc, "no parent document");
190 nsIEventStateManager* esm =
191 parentDoc->GetPrimaryShell()->GetPresContext()->EventStateManager();
192 if (esm) {
193 esm->SetContentState(nsnull, NS_EVENT_STATE_FOCUS);
194 PRBool forward = (action == eEventAction_Tab);
195 esm->ShiftFocus(forward,
196 forward ? nsnull : parentDoc->FindContentForSubDocument(doc));
200 // fall-through
201 case eEventAction_Suppress:
202 aEvent->StopPropagation();
203 aEvent->PreventDefault();
204 break;
205 case eEventAction_Propagate:
206 // intentionally empty
207 break;
210 return NS_OK;