Back to index

lightning-sunbird  0.9+nobinonly
nsPrintPreviewListener.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Mats Palmgren <mats.palmgren@bredband.net>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsPrintPreviewListener.h"
00041 #include "nsIContent.h"
00042 #include "nsIDOMKeyEvent.h"
00043 #include "nsIDOMNSEvent.h"
00044 #include "nsIDocument.h"
00045 #include "nsIPresShell.h"
00046 #include "nsIDocShell.h"
00047 #include "nsPresContext.h"
00048 #include "nsIEventStateManager.h"
00049 #include "nsLiteralString.h"
00050 
00051 NS_IMPL_ISUPPORTS1(nsPrintPreviewListener, nsIDOMEventListener)
00052 
00053 
00054 //
00055 // nsPrintPreviewListener ctor
00056 //
00057 nsPrintPreviewListener::nsPrintPreviewListener (nsIDOMEventTarget* aTarget)
00058   : mEventTarget(aTarget)
00059 {
00060   NS_ADDREF_THIS();
00061 } // ctor
00062 
00063 
00064 //-------------------------------------------------------
00065 //
00066 // AddListeners
00067 //
00068 // Subscribe to the events that will allow us to track various events. 
00069 //
00070 nsresult
00071 nsPrintPreviewListener::AddListeners()
00072 {
00073   if (mEventTarget) {
00074     mEventTarget->AddEventListener(NS_LITERAL_STRING("click"), this, true);
00075     mEventTarget->AddEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
00076     mEventTarget->AddEventListener(NS_LITERAL_STRING("keydown"), this, true);
00077     mEventTarget->AddEventListener(NS_LITERAL_STRING("keypress"), this, true);
00078     mEventTarget->AddEventListener(NS_LITERAL_STRING("keyup"), this, true);
00079     mEventTarget->AddEventListener(NS_LITERAL_STRING("mousedown"), this, true);
00080     mEventTarget->AddEventListener(NS_LITERAL_STRING("mousemove"), this, true);
00081     mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseout"), this, true);
00082     mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseover"), this, true);
00083     mEventTarget->AddEventListener(NS_LITERAL_STRING("mouseup"), this, true);
00084   }
00085 
00086   return NS_OK;
00087 }
00088 
00089 
00090 //-------------------------------------------------------
00091 //
00092 // RemoveListeners
00093 //
00094 // Unsubscribe from all the various events that we were listening to. 
00095 //
00096 nsresult 
00097 nsPrintPreviewListener::RemoveListeners()
00098 {
00099   if (mEventTarget) {
00100     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("click"), this, true);
00101     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
00102     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keydown"), this, true);
00103     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
00104     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("keyup"), this, true);
00105     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
00106     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mousemove"), this, true);
00107     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseout"), this, true);
00108     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseover"), this, true);
00109     mEventTarget->RemoveEventListener(NS_LITERAL_STRING("mouseup"), this, true);
00110   }
00111 
00112   return NS_OK;
00113 }
00114 
00115 //-------------------------------------------------------
00116 //
00117 // GetActionForEvent
00118 //
00119 // Helper function to let certain key events thru
00120 //
00121 enum eEventAction {
00122   eEventAction_Tab,       eEventAction_ShiftTab,
00123   eEventAction_Propagate, eEventAction_Suppress
00124 };
00125 
00126 static eEventAction
00127 GetActionForEvent(nsIDOMEvent* aEvent)
00128 {
00129   static const PRUint32 kOKKeyCodes[] = {
00130     nsIDOMKeyEvent::DOM_VK_PAGE_UP, nsIDOMKeyEvent::DOM_VK_PAGE_DOWN,
00131     nsIDOMKeyEvent::DOM_VK_UP,      nsIDOMKeyEvent::DOM_VK_DOWN, 
00132     nsIDOMKeyEvent::DOM_VK_HOME,    nsIDOMKeyEvent::DOM_VK_END 
00133   };
00134 
00135   nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
00136   if (keyEvent) {
00137     PRBool b;
00138     keyEvent->GetAltKey(&b);
00139     if (b) return eEventAction_Suppress;
00140     keyEvent->GetCtrlKey(&b);
00141     if (b) return eEventAction_Suppress;
00142 
00143     keyEvent->GetShiftKey(&b);
00144 
00145     PRUint32 keyCode;
00146     keyEvent->GetKeyCode(&keyCode);
00147     if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB)
00148       return b ? eEventAction_ShiftTab : eEventAction_Tab;
00149 
00150     PRUint32 charCode;
00151     keyEvent->GetCharCode(&charCode);
00152     if (charCode == ' ' || keyCode == nsIDOMKeyEvent::DOM_VK_SPACE)
00153       return eEventAction_Propagate;
00154 
00155     if (b) return eEventAction_Suppress;
00156 
00157     for (PRUint32 i = 0; i < sizeof(kOKKeyCodes)/sizeof(kOKKeyCodes[0]); ++i) {
00158       if (keyCode == kOKKeyCodes[i]) {
00159         return eEventAction_Propagate;
00160       }
00161     }
00162   }
00163   return eEventAction_Suppress;
00164 }
00165 
00166 NS_IMETHODIMP
00167 nsPrintPreviewListener::HandleEvent(nsIDOMEvent* aEvent)
00168 {
00169   nsCOMPtr<nsIDOMEventTarget> target;
00170   nsCOMPtr<nsIDOMNSEvent> nsEvent = do_QueryInterface(aEvent);
00171   if (nsEvent)
00172     nsEvent->GetOriginalTarget(getter_AddRefs(target));
00173   nsCOMPtr<nsIContent> content(do_QueryInterface(target));
00174   if (content && !content->IsContentOfType(nsIContent::eXUL)) {
00175     eEventAction action = ::GetActionForEvent(aEvent);
00176     switch (action) {
00177       case eEventAction_Tab:
00178       case eEventAction_ShiftTab:
00179       {
00180         nsAutoString eventString;
00181         aEvent->GetType(eventString);
00182         if (eventString == NS_LITERAL_STRING("keydown")) {
00183           // Handle tabbing explicitly here since we don't want focus ending up
00184           // inside the content document, bug 244128.
00185           nsIDocument* doc = content->GetCurrentDoc();
00186           NS_ASSERTION(doc, "no document");
00187 
00188           nsIDocument* parentDoc = doc->GetParentDocument();
00189           NS_ASSERTION(parentDoc, "no parent document");
00190           nsIEventStateManager* esm =
00191             parentDoc->GetShellAt(0)->GetPresContext()->EventStateManager();
00192           if (esm) {
00193             esm->SetContentState(nsnull, NS_EVENT_STATE_FOCUS);
00194             PRBool forward = (action == eEventAction_Tab);
00195             esm->ShiftFocus(forward,
00196                             forward ? nsnull : parentDoc->FindContentForSubDocument(doc));
00197           }
00198         }
00199       }
00200       // fall-through
00201       case eEventAction_Suppress:
00202         aEvent->StopPropagation();
00203         aEvent->PreventDefault();
00204         break;
00205       case eEventAction_Propagate:
00206         // intentionally empty
00207         break;
00208     }
00209   }
00210   return NS_OK; 
00211 }