1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Mats Palmgren <mats.palmgren@bredband.net>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsPrintPreviewListener.h"
41 :
42 : #include "mozilla/dom/Element.h"
43 : #include "nsIDOMWindow.h"
44 : #include "nsPIDOMWindow.h"
45 : #include "nsIDOMElement.h"
46 : #include "nsIDOMKeyEvent.h"
47 : #include "nsIDOMNSEvent.h"
48 : #include "nsIDocument.h"
49 : #include "nsIDocShell.h"
50 : #include "nsPresContext.h"
51 : #include "nsFocusManager.h"
52 : #include "nsLiteralString.h"
53 :
54 : using namespace mozilla;
55 :
56 0 : NS_IMPL_ISUPPORTS1(nsPrintPreviewListener, nsIDOMEventListener)
57 :
58 :
59 : //
60 : // nsPrintPreviewListener ctor
61 : //
62 0 : nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventTarget* aTarget)
63 0 : : mEventTarget(aTarget)
64 : {
65 0 : NS_ADDREF_THIS();
66 0 : } // ctor
67 :
68 :
69 : //-------------------------------------------------------
70 : //
71 : // AddListeners
72 : //
73 : // Subscribe to the events that will allow us to track various events.
74 : //
75 : nsresult
76 0 : nsPrintPreviewListener::AddListeners()
77 : {
78 0 : if (mEventTarget) {
79 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("click"), this, true);
80 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
81 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("keydown"), this, true);
82 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("keypress"), this, true);
83 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("keyup"), this, true);
84 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("mousedown"), this, true);
85 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("mousemove"), this, true);
86 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseout"), this, true);
87 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), this, true);
88 0 : mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseup"), this, true);
89 : }
90 :
91 0 : return NS_OK;
92 : }
93 :
94 :
95 : //-------------------------------------------------------
96 : //
97 : // RemoveListeners
98 : //
99 : // Unsubscribe from all the various events that we were listening to.
100 : //
101 : nsresult
102 0 : nsPrintPreviewListener::RemoveListeners()
103 : {
104 0 : if (mEventTarget) {
105 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("click"), this, true);
106 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
107 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keydown"), this, true);
108 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
109 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keyup"), this, true);
110 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
111 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"), this, true);
112 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseout"), this, true);
113 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseover"), this, true);
114 0 : mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseup"), this, true);
115 : }
116 :
117 0 : return NS_OK;
118 : }
119 :
120 : //-------------------------------------------------------
121 : //
122 : // GetActionForEvent
123 : //
124 : // Helper function to let certain key events through
125 : //
126 : enum eEventAction {
127 : eEventAction_Tab, eEventAction_ShiftTab,
128 : eEventAction_Propagate, eEventAction_Suppress
129 : };
130 :
131 : static eEventAction
132 0 : GetActionForEvent(nsIDOMEvent* aEvent)
133 : {
134 : static const PRUint32 kOKKeyCodes[] = {
135 : nsIDOMKeyEvent::DOM_VK_PAGE_UP, nsIDOMKeyEvent::DOM_VK_PAGE_DOWN,
136 : nsIDOMKeyEvent::DOM_VK_UP, nsIDOMKeyEvent::DOM_VK_DOWN,
137 : nsIDOMKeyEvent::DOM_VK_HOME, nsIDOMKeyEvent::DOM_VK_END
138 : };
139 :
140 0 : nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
141 0 : if (keyEvent) {
142 : bool b;
143 0 : keyEvent->GetAltKey(&b);
144 0 : if (b) return eEventAction_Suppress;
145 0 : keyEvent->GetCtrlKey(&b);
146 0 : if (b) return eEventAction_Suppress;
147 :
148 0 : keyEvent->GetShiftKey(&b);
149 :
150 : PRUint32 keyCode;
151 0 : keyEvent->GetKeyCode(&keyCode);
152 0 : if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB)
153 0 : return b ? eEventAction_ShiftTab : eEventAction_Tab;
154 :
155 : PRUint32 charCode;
156 0 : keyEvent->GetCharCode(&charCode);
157 0 : if (charCode == ' ' || keyCode == nsIDOMKeyEvent::DOM_VK_SPACE)
158 0 : return eEventAction_Propagate;
159 :
160 0 : if (b) return eEventAction_Suppress;
161 :
162 0 : for (PRUint32 i = 0; i < sizeof(kOKKeyCodes)/sizeof(kOKKeyCodes[0]); ++i) {
163 0 : if (keyCode == kOKKeyCodes[i]) {
164 0 : return eEventAction_Propagate;
165 : }
166 : }
167 : }
168 0 : return eEventAction_Suppress;
169 : }
170 :
171 : NS_IMETHODIMP
172 0 : nsPrintPreviewListener::HandleEvent(nsIDOMEvent* aEvent)
173 : {
174 0 : nsCOMPtr<nsIDOMEventTarget> target;
175 0 : nsCOMPtr<nsIDOMNSEvent> nsEvent = do_QueryInterface(aEvent);
176 0 : if (nsEvent)
177 0 : nsEvent->GetOriginalTarget(getter_AddRefs(target));
178 0 : nsCOMPtr<nsIContent> content(do_QueryInterface(target));
179 0 : if (content && !content->IsXUL()) {
180 0 : eEventAction action = ::GetActionForEvent(aEvent);
181 0 : switch (action) {
182 : case eEventAction_Tab:
183 : case eEventAction_ShiftTab:
184 : {
185 0 : nsAutoString eventString;
186 0 : aEvent->GetType(eventString);
187 0 : if (eventString == NS_LITERAL_STRING("keydown")) {
188 : // Handle tabbing explicitly here since we don't want focus ending up
189 : // inside the content document, bug 244128.
190 0 : nsIDocument* doc = content->GetCurrentDoc();
191 0 : NS_ASSERTION(doc, "no document");
192 :
193 0 : nsIDocument* parentDoc = doc->GetParentDocument();
194 0 : NS_ASSERTION(parentDoc, "no parent document");
195 :
196 0 : nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(parentDoc->GetWindow());
197 :
198 0 : nsIFocusManager* fm = nsFocusManager::GetFocusManager();
199 0 : if (fm && win) {
200 0 : dom::Element* fromElement = parentDoc->FindContentForSubDocument(doc);
201 0 : nsCOMPtr<nsIDOMElement> from = do_QueryInterface(fromElement);
202 :
203 0 : bool forward = (action == eEventAction_Tab);
204 0 : nsCOMPtr<nsIDOMElement> result;
205 : fm->MoveFocus(win, from,
206 : forward ? nsIFocusManager::MOVEFOCUS_FORWARD :
207 : nsIFocusManager::MOVEFOCUS_BACKWARD,
208 0 : nsIFocusManager::FLAG_BYKEY, getter_AddRefs(result));
209 : }
210 : }
211 : }
212 : // fall-through
213 : case eEventAction_Suppress:
214 0 : aEvent->StopPropagation();
215 0 : aEvent->PreventDefault();
216 0 : break;
217 : case eEventAction_Propagate:
218 : // intentionally empty
219 0 : break;
220 : }
221 : }
222 0 : return NS_OK;
223 : }
|