Back to index

lightning-sunbird  0.9+nobinonly
nsDOMKeyboardEvent.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Steve Clark (buster@netscape.com)
00024  *   Ilya Konstantinov (mozilla-code@future.shiny.co.il)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 "nsDOMKeyboardEvent.h"
00041 #include "nsContentUtils.h"
00042 
00043 
00044 nsDOMKeyboardEvent::nsDOMKeyboardEvent(nsPresContext* aPresContext,
00045                                        nsKeyEvent* aEvent)
00046   : nsDOMUIEvent(aPresContext, aEvent ? aEvent :
00047                  new nsKeyEvent(PR_FALSE, 0, nsnull))
00048 {
00049   NS_ASSERTION(mEvent->eventStructType == NS_KEY_EVENT, "event type mismatch");
00050 
00051   if (aEvent) {
00052     mEventIsInternal = PR_FALSE;
00053   }
00054   else {
00055     mEventIsInternal = PR_TRUE;
00056     mEvent->time = PR_Now();
00057   }
00058 }
00059 
00060 NS_IMPL_ADDREF_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
00061 NS_IMPL_RELEASE_INHERITED(nsDOMKeyboardEvent, nsDOMUIEvent)
00062 
00063 NS_INTERFACE_MAP_BEGIN(nsDOMKeyboardEvent)
00064   NS_INTERFACE_MAP_ENTRY(nsIDOMKeyEvent)
00065   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(KeyboardEvent)
00066 NS_INTERFACE_MAP_END_INHERITING(nsDOMUIEvent)
00067 
00068 NS_IMETHODIMP
00069 nsDOMKeyboardEvent::GetAltKey(PRBool* aIsDown)
00070 {
00071   NS_ENSURE_ARG_POINTER(aIsDown);
00072   *aIsDown = ((nsInputEvent*)mEvent)->isAlt;
00073   return NS_OK;
00074 }
00075 
00076 NS_IMETHODIMP
00077 nsDOMKeyboardEvent::GetCtrlKey(PRBool* aIsDown)
00078 {
00079   NS_ENSURE_ARG_POINTER(aIsDown);
00080   *aIsDown = ((nsInputEvent*)mEvent)->isControl;
00081   return NS_OK;
00082 }
00083 
00084 NS_IMETHODIMP
00085 nsDOMKeyboardEvent::GetShiftKey(PRBool* aIsDown)
00086 {
00087   NS_ENSURE_ARG_POINTER(aIsDown);
00088   *aIsDown = ((nsInputEvent*)mEvent)->isShift;
00089   return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP
00093 nsDOMKeyboardEvent::GetMetaKey(PRBool* aIsDown)
00094 {
00095   NS_ENSURE_ARG_POINTER(aIsDown);
00096   *aIsDown = ((nsInputEvent*)mEvent)->isMeta;
00097   return NS_OK;
00098 }
00099 
00100 NS_IMETHODIMP
00101 nsDOMKeyboardEvent::GetCharCode(PRUint32* aCharCode)
00102 {
00103   NS_ENSURE_ARG_POINTER(aCharCode);
00104 
00105   switch (mEvent->message) {
00106   case NS_KEY_UP:
00107   case NS_KEY_DOWN:
00108 #if defined(NS_DEBUG) && defined(DEBUG_brade)
00109     printf("GetCharCode used for wrong key event; should use onkeypress.\n");
00110 #endif
00111     *aCharCode = 0;
00112     break;
00113   case NS_KEY_PRESS:
00114     *aCharCode = ((nsKeyEvent*)mEvent)->charCode;
00115     break;
00116   default:
00117     break;
00118   }
00119   return NS_OK;
00120 }
00121 
00122 NS_IMETHODIMP
00123 nsDOMKeyboardEvent::GetKeyCode(PRUint32* aKeyCode)
00124 {
00125   NS_ENSURE_ARG_POINTER(aKeyCode);
00126 
00127   switch (mEvent->message) {
00128   case NS_KEY_UP:
00129   case NS_KEY_PRESS:
00130   case NS_KEY_DOWN:
00131     *aKeyCode = ((nsKeyEvent*)mEvent)->keyCode;
00132     break;
00133   default:
00134     *aKeyCode = 0;
00135     break;
00136   }
00137 
00138   return NS_OK;
00139 }
00140 
00141 NS_IMETHODIMP
00142 nsDOMKeyboardEvent::GetWhich(PRUint32* aWhich)
00143 {
00144   NS_ENSURE_ARG_POINTER(aWhich);
00145 
00146   switch (mEvent->message) {
00147     case NS_KEY_UP:
00148     case NS_KEY_DOWN:
00149       return GetKeyCode(aWhich);
00150     case NS_KEY_PRESS:
00151       //Special case for 4xp bug 62878.  Try to make value of which
00152       //more closely mirror the values that 4.x gave for RETURN and BACKSPACE
00153       {
00154         PRUint32 keyCode = ((nsKeyEvent*)mEvent)->keyCode;
00155         if (keyCode == NS_VK_RETURN || keyCode == NS_VK_BACK) {
00156           *aWhich = keyCode;
00157           return NS_OK;
00158         }
00159        return GetCharCode(aWhich);
00160       }
00161       break;
00162     default:
00163       *aWhich = 0;
00164       break;
00165   }
00166 
00167   return NS_OK;
00168 }
00169 
00170 NS_IMETHODIMP
00171 nsDOMKeyboardEvent::InitKeyEvent(const nsAString& aType, PRBool aCanBubble, PRBool aCancelable,
00172                                  nsIDOMAbstractView* aView, PRBool aCtrlKey, PRBool aAltKey,
00173                                  PRBool aShiftKey, PRBool aMetaKey,
00174                                  PRUint32 aKeyCode, PRUint32 aCharCode)
00175 {
00176   nsresult rv = nsDOMUIEvent::InitUIEvent(aType, aCanBubble, aCancelable, aView, 0);
00177   NS_ENSURE_SUCCESS(rv, rv);
00178 
00179   nsKeyEvent* keyEvent = NS_STATIC_CAST(nsKeyEvent*, mEvent);
00180   keyEvent->isControl = aCtrlKey;
00181   keyEvent->isAlt = aAltKey;
00182   keyEvent->isShift = aShiftKey;
00183   keyEvent->isMeta = aMetaKey;
00184   keyEvent->keyCode = aKeyCode;
00185   keyEvent->charCode = aCharCode;
00186 
00187   return NS_OK;
00188 }
00189 
00190 nsresult NS_NewDOMKeyboardEvent(nsIDOMEvent** aInstancePtrResult,
00191                                 nsPresContext* aPresContext,
00192                                 nsKeyEvent *aEvent)
00193 {
00194   nsDOMKeyboardEvent* it = new nsDOMKeyboardEvent(aPresContext, aEvent);
00195   if (nsnull == it) {
00196     return NS_ERROR_OUT_OF_MEMORY;
00197   }
00198 
00199   return CallQueryInterface(it, aInstancePtrResult);
00200 }