Back to index

lightning-sunbird  0.9+nobinonly
nsMenuBarListener.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 Communicator client 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  *   Original Author: David W. Hyatt (hyatt@netscape.com)
00024  *   Dean Tessman <dean_tessman@hotmail.com>
00025  *   Mark Hammond <markh@ActiveState.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsMenuBarListener.h"
00042 #include "nsMenuBarFrame.h"
00043 #include "nsIDOMKeyListener.h"
00044 #include "nsIDOMEventReceiver.h"
00045 #include "nsIDOMEventListener.h"
00046 #include "nsIDOMNSUIEvent.h"
00047 #include "nsIDOMNSEvent.h"
00048 #include "nsGUIEvent.h"
00049 
00050 // Drag & Drop, Clipboard
00051 #include "nsIServiceManager.h"
00052 #include "nsWidgetsCID.h"
00053 #include "nsCOMPtr.h"
00054 #include "nsIDOMKeyEvent.h"
00055 #include "nsIContent.h"
00056 #include "nsIDOMNode.h"
00057 #include "nsIDOMElement.h"
00058 #include "nsXULAtoms.h"
00059 
00060 #include "nsIEventStateManager.h"
00061 
00062 #include "nsIViewManager.h"
00063 #include "nsIView.h"
00064 #include "nsISupportsArray.h"
00065 #include "nsContentUtils.h"
00066 
00067 /*
00068  * nsMenuBarListener implementation
00069  */
00070 
00071 NS_IMPL_ADDREF(nsMenuBarListener)
00072 NS_IMPL_RELEASE(nsMenuBarListener)
00073 NS_IMPL_QUERY_INTERFACE3(nsMenuBarListener, nsIDOMKeyListener, nsIDOMFocusListener, nsIDOMMouseListener)
00074 
00075 #define MODIFIER_SHIFT    1
00076 #define MODIFIER_CONTROL  2
00077 #define MODIFIER_ALT      4
00078 #define MODIFIER_META     8
00079 
00081 
00082 PRInt32 nsMenuBarListener::mAccessKey = -1;
00083 PRUint32 nsMenuBarListener::mAccessKeyMask = 0;
00084 PRBool nsMenuBarListener::mAccessKeyFocuses = PR_FALSE;
00085 
00086 nsMenuBarListener::nsMenuBarListener(nsMenuBarFrame* aMenuBar) 
00087   :mAccessKeyDown(PR_FALSE)
00088 {
00089   mMenuBarFrame = aMenuBar;
00090 }
00091 
00093 nsMenuBarListener::~nsMenuBarListener() 
00094 {
00095 }
00096 
00097 nsresult
00098 nsMenuBarListener::GetMenuAccessKey(PRInt32* aAccessKey)
00099 {
00100   if (!aAccessKey)
00101     return NS_ERROR_INVALID_POINTER;
00102   InitAccessKey();
00103   *aAccessKey = mAccessKey;
00104   return NS_OK;
00105 }
00106 
00107 void nsMenuBarListener::InitAccessKey()
00108 {
00109   if (mAccessKey >= 0)
00110     return;
00111 
00112   // Compiled-in defaults, in case we can't get LookAndFeel --
00113   // mac doesn't have menu shortcuts, other platforms use alt.
00114 #if !(defined(XP_MAC) || defined(XP_MACOSX))
00115   mAccessKey = nsIDOMKeyEvent::DOM_VK_ALT;
00116   mAccessKeyMask = MODIFIER_ALT;
00117 #else
00118   mAccessKey = 0;
00119   mAccessKeyMask = 0;
00120 #endif
00121 
00122   // Get the menu access key value from prefs, overriding the default:
00123   mAccessKey = nsContentUtils::GetIntPref("ui.key.menuAccessKey", mAccessKey);
00124   if (mAccessKey == nsIDOMKeyEvent::DOM_VK_SHIFT)
00125     mAccessKeyMask = MODIFIER_SHIFT;
00126   else if (mAccessKey == nsIDOMKeyEvent::DOM_VK_CONTROL)
00127     mAccessKeyMask = MODIFIER_CONTROL;
00128   else if (mAccessKey == nsIDOMKeyEvent::DOM_VK_ALT)
00129     mAccessKeyMask = MODIFIER_ALT;
00130   else if (mAccessKey == nsIDOMKeyEvent::DOM_VK_META)
00131     mAccessKeyMask = MODIFIER_META;
00132 
00133   mAccessKeyFocuses =
00134     nsContentUtils::GetBoolPref("ui.key.menuAccessKeyFocuses");
00135 }
00136 
00138 nsresult
00139 nsMenuBarListener::KeyUp(nsIDOMEvent* aKeyEvent)
00140 {  
00141   InitAccessKey();
00142 
00143   //handlers shouldn't be triggered by non-trusted events.
00144   nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
00145   PRBool trustedEvent = PR_FALSE;
00146 
00147   if (domNSEvent) {
00148     domNSEvent->GetIsTrusted(&trustedEvent);
00149   }
00150 
00151   if (!trustedEvent)
00152     return NS_OK;
00153 
00154   if (mAccessKey && mAccessKeyFocuses)
00155   {
00156     // On a press of the ALT key by itself, we toggle the menu's 
00157     // active/inactive state.
00158     // Get the ascii key code.
00159     nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aKeyEvent);
00160     PRUint32 theChar;
00161     keyEvent->GetKeyCode(&theChar);
00162 
00163     if (mAccessKeyDown && (PRInt32)theChar == mAccessKey)
00164     {
00165       // The access key was down and is now up, and no other
00166       // keys were pressed in between.
00167       mMenuBarFrame->ToggleMenuActiveState();
00168     }
00169     mAccessKeyDown = PR_FALSE; 
00170 
00171     PRBool active = mMenuBarFrame->IsActive();
00172     if (active) {
00173       aKeyEvent->StopPropagation();
00174       aKeyEvent->PreventDefault();
00175       return NS_ERROR_BASE; // I am consuming event
00176     }
00177   }
00178   
00179   return NS_OK; // means I am NOT consuming event
00180 }
00181 
00183 nsresult
00184 nsMenuBarListener::KeyPress(nsIDOMEvent* aKeyEvent)
00185 {
00186   mMenuBarFrame->ClearRecentlyRolledUp();
00187 
00188   // if event has already been handled, bail
00189   nsCOMPtr<nsIDOMNSUIEvent> uiEvent ( do_QueryInterface(aKeyEvent) );
00190   if ( uiEvent ) {
00191     PRBool eventHandled = PR_FALSE;
00192     uiEvent->GetPreventDefault ( &eventHandled );
00193     if ( eventHandled )
00194       return NS_OK;       // don't consume event
00195   }
00196 
00197   //handlers shouldn't be triggered by non-trusted events.
00198   nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
00199   PRBool trustedEvent = PR_FALSE;
00200   if (domNSEvent) {
00201     domNSEvent->GetIsTrusted(&trustedEvent);
00202   }
00203 
00204   if (!trustedEvent)
00205     return NS_OK;
00206 
00207   nsresult retVal = NS_OK;  // default is to not consume event
00208   
00209   InitAccessKey();
00210 
00211   if (mAccessKey)
00212   {
00213     nsCOMPtr<nsIDOMNSUIEvent> nsUIEvent = do_QueryInterface(aKeyEvent);
00214 
00215     PRBool preventDefault;
00216 
00217     nsUIEvent->GetPreventDefault(&preventDefault);
00218     if (!preventDefault) {
00219       nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aKeyEvent);
00220       PRUint32 keyCode, charCode;
00221       keyEvent->GetKeyCode(&keyCode);
00222       keyEvent->GetCharCode(&charCode);
00223 
00224       // Clear the access key flag unless we are pressing the access key.
00225       if (keyCode != (PRUint32)mAccessKey)
00226         mAccessKeyDown = PR_FALSE;
00227 
00228       // If charCode == 0, then it is not a printable character.
00229       // Don't attempt to handle accesskey for non-printable characters.
00230       if (IsAccessKeyPressed(keyEvent) && charCode)
00231       {
00232         // Do shortcut navigation.
00233         // A letter was pressed. We want to see if a shortcut gets matched. If
00234         // so, we'll know the menu got activated.
00235         PRBool active = PR_FALSE;
00236         mMenuBarFrame->ShortcutNavigation(keyEvent, active);
00237 
00238         if (active) {
00239           aKeyEvent->StopPropagation();
00240           aKeyEvent->PreventDefault();
00241 
00242           retVal = NS_ERROR_BASE;       // I am consuming event
00243         }
00244       }    
00245 #if !defined(XP_MAC) && !defined(XP_MACOSX)
00246       // Also need to handle F10 specially on Non-Mac platform.
00247       else if (keyCode == NS_VK_F10) {
00248         if ((GetModifiers(keyEvent) & ~MODIFIER_CONTROL) == 0) {
00249           // The F10 key just went down by itself or with ctrl pressed.
00250           // In Windows, both of these activate the menu bar.
00251           mMenuBarFrame->ToggleMenuActiveState();
00252 
00253           aKeyEvent->StopPropagation();
00254           aKeyEvent->PreventDefault();
00255           return NS_ERROR_BASE; // consume the event
00256         }
00257       }
00258 #endif   // !XP_MAC && !XP_MACOSX
00259     } 
00260   }
00261   return retVal;
00262 }
00263 
00264 PRBool
00265 nsMenuBarListener::IsAccessKeyPressed(nsIDOMKeyEvent* aKeyEvent)
00266 {
00267   InitAccessKey();
00268   // No other modifiers are allowed to be down except for Shift.
00269   PRUint32 modifiers = GetModifiers(aKeyEvent);
00270 
00271   return (mAccessKeyMask != MODIFIER_SHIFT &&
00272           (modifiers & mAccessKeyMask) &&
00273           (modifiers & ~(mAccessKeyMask | MODIFIER_SHIFT)) == 0);
00274 }
00275 
00276 PRUint32
00277 nsMenuBarListener::GetModifiers(nsIDOMKeyEvent* aKeyEvent)
00278 {
00279   PRUint32 modifiers = 0;
00280   PRBool modifier;
00281 
00282   aKeyEvent->GetShiftKey(&modifier);
00283   if (modifier)
00284     modifiers |= MODIFIER_SHIFT;
00285 
00286   aKeyEvent->GetCtrlKey(&modifier);
00287   if (modifier)
00288     modifiers |= MODIFIER_CONTROL;
00289 
00290   aKeyEvent->GetAltKey(&modifier);
00291   if (modifier)
00292     modifiers |= MODIFIER_ALT;
00293 
00294   aKeyEvent->GetMetaKey(&modifier);
00295   if (modifier)
00296     modifiers |= MODIFIER_META;
00297 
00298   return modifiers;
00299 }
00300 
00302 nsresult
00303 nsMenuBarListener::KeyDown(nsIDOMEvent* aKeyEvent)
00304 {
00305   InitAccessKey();
00306 
00307   //handlers shouldn't be triggered by non-trusted events.
00308   nsCOMPtr<nsIDOMNSEvent> domNSEvent = do_QueryInterface(aKeyEvent);
00309   PRBool trustedEvent = PR_FALSE;
00310 
00311   if (domNSEvent) {
00312     domNSEvent->GetIsTrusted(&trustedEvent);
00313   }
00314 
00315   if (!trustedEvent)
00316     return NS_OK;
00317 
00318   if (mAccessKey && mAccessKeyFocuses)
00319   {
00320     nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aKeyEvent);
00321     PRUint32 theChar;
00322     keyEvent->GetKeyCode(&theChar);
00323 
00324     if (theChar == (PRUint32)mAccessKey && (GetModifiers(keyEvent) & ~mAccessKeyMask) == 0) {
00325       // No other modifiers can be down.
00326       // Especially CTRL.  CTRL+ALT == AltGR, and
00327       // we'll fuck up on non-US enhanced 102-key
00328       // keyboards if we don't check this.
00329       mAccessKeyDown = PR_TRUE;
00330     }
00331     else {
00332       // Some key other than the access key just went down,
00333       // so we won't activate the menu bar when the access
00334       // key is released.
00335 
00336       mAccessKeyDown = PR_FALSE;
00337     }
00338   }
00339 
00340   return NS_OK; // means I am NOT consuming event
00341 }
00342 
00344 
00345 nsresult
00346 nsMenuBarListener::Focus(nsIDOMEvent* aEvent)
00347 {
00348   return NS_OK; // means I am NOT consuming event
00349 }
00350 
00352 nsresult
00353 nsMenuBarListener::Blur(nsIDOMEvent* aEvent)
00354 {
00355   if (!mMenuBarFrame->IsOpen() && mMenuBarFrame->IsActive()) {
00356     mMenuBarFrame->ToggleMenuActiveState();
00357     PRBool handled;
00358     mMenuBarFrame->Escape(handled);
00359     mAccessKeyDown = PR_FALSE;
00360   }
00361   return NS_OK; // means I am NOT consuming event
00362 }
00363   
00365 nsresult 
00366 nsMenuBarListener::MouseDown(nsIDOMEvent* aMouseEvent)
00367 {
00368   if (!mMenuBarFrame->IsOpen() && mMenuBarFrame->IsActive()) {
00369     mMenuBarFrame->ToggleMenuActiveState();
00370     PRBool handled;
00371     mMenuBarFrame->Escape(handled);
00372   }
00373 
00374   mAccessKeyDown = PR_FALSE;
00375 
00376   return NS_OK; // means I am NOT consuming event
00377 }
00378 
00380 nsresult 
00381 nsMenuBarListener::MouseUp(nsIDOMEvent* aMouseEvent)
00382 {
00383   mMenuBarFrame->ClearRecentlyRolledUp();
00384 
00385   return NS_OK; // means I am NOT consuming event
00386 }
00387 
00388 nsresult 
00389 nsMenuBarListener::MouseClick(nsIDOMEvent* aMouseEvent)
00390 {
00391   return NS_OK; // means I am NOT consuming event
00392 }
00393 
00395 nsresult 
00396 nsMenuBarListener::MouseDblClick(nsIDOMEvent* aMouseEvent)
00397 {
00398   return NS_OK; // means I am NOT consuming event
00399 }
00400 
00402 nsresult 
00403 nsMenuBarListener::MouseOver(nsIDOMEvent* aMouseEvent)
00404 {
00405   return NS_OK; // means I am NOT consuming event
00406 }
00407 
00409 nsresult 
00410 nsMenuBarListener::MouseOut(nsIDOMEvent* aMouseEvent)
00411 {
00412   return NS_OK; // means I am NOT consuming event
00413 }
00414 
00416 nsresult
00417 nsMenuBarListener::HandleEvent(nsIDOMEvent* aEvent)
00418 {
00419   return NS_OK;
00420 }