Bug 393582, Improve naNavigationDirection code, patch=karunasagark@gmail.com,r=enn,sr=bz

This commit is contained in:
Neil Deakin 2008-12-30 08:30:51 -05:00
parent 36c00fde37
commit a686e28c9e
3 changed files with 36 additions and 42 deletions

View File

@ -41,6 +41,8 @@
#ifndef nsXULPopupManager_h__ #ifndef nsXULPopupManager_h__
#define nsXULPopupManager_h__ #define nsXULPopupManager_h__
#include "prlog.h"
#include "nsGUIEvent.h"
#include "nsIContent.h" #include "nsIContent.h"
#include "nsIWidget.h" #include "nsIWidget.h"
#include "nsIRollupListener.h" #include "nsIRollupListener.h"
@ -51,6 +53,7 @@
#include "nsTArray.h" #include "nsTArray.h"
#include "nsITimer.h" #include "nsITimer.h"
#include "nsThreadUtils.h" #include "nsThreadUtils.h"
#include "nsStyleConsts.h"
/** /**
* There are two types that are used: * There are two types that are used:
@ -126,33 +129,23 @@ enum nsNavigationDirection {
#define NS_DIRECTION_IS_BLOCK_TO_EDGE(dir) (dir == eNavigationDirection_First || \ #define NS_DIRECTION_IS_BLOCK_TO_EDGE(dir) (dir == eNavigationDirection_First || \
dir == eNavigationDirection_Last) dir == eNavigationDirection_Last)
/** PR_STATIC_ASSERT(NS_STYLE_DIRECTION_LTR == 0 && NS_STYLE_DIRECTION_RTL == 1);
* DirectionFromKeyCode_lr_tb: an array that maps keycodes to values of PR_STATIC_ASSERT((NS_VK_HOME == NS_VK_END + 1) &&
* nsNavigationDirection for left-to-right and top-to-bottom flow orientation (NS_VK_LEFT == NS_VK_END + 2) &&
* This is defined in nsXULPopupManager.cpp. (NS_VK_UP == NS_VK_END + 3) &&
*/ (NS_VK_RIGHT == NS_VK_END + 4) &&
extern nsNavigationDirection DirectionFromKeyCode_lr_tb [6]; (NS_VK_DOWN == NS_VK_END + 5));
/** /**
* DirectionFromKeyCode_rl_tb: an array that maps keycodes to values of * DirectionFromKeyCodeTable: two arrays, the first for left-to-right and the
* nsNavigationDirection for right-to-left and top-to-bottom flow orientation * other for right-to-left, that map keycodes to values of
* This is defined in nsXULPopupManager.cpp. * nsNavigationDirection.
*/ */
extern nsNavigationDirection DirectionFromKeyCode_rl_tb [6]; extern const nsNavigationDirection DirectionFromKeyCodeTable[2][6];
#define NS_DIRECTION_FROM_KEY_CODE(frame, direction, keycode) \ #define NS_DIRECTION_FROM_KEY_CODE(frame, keycode) \
NS_ASSERTION(NS_VK_HOME == NS_VK_END + 1, "Broken ordering"); \ (DirectionFromKeyCodeTable[frame->GetStyleVisibility()->mDirection] \
NS_ASSERTION(NS_VK_LEFT == NS_VK_END + 2, "Broken ordering"); \ [keycode - NS_VK_END])
NS_ASSERTION(NS_VK_UP == NS_VK_END + 3, "Broken ordering"); \
NS_ASSERTION(NS_VK_RIGHT == NS_VK_END + 4, "Broken ordering"); \
NS_ASSERTION(NS_VK_DOWN == NS_VK_END + 5, "Broken ordering"); \
NS_ASSERTION(keycode >= NS_VK_END && keycode <= NS_VK_DOWN, \
"Illegal key code"); \
const nsStyleVisibility* vis = frame->GetStyleVisibility(); \
if (vis->mDirection == NS_STYLE_DIRECTION_RTL) \
direction = DirectionFromKeyCode_rl_tb[keycode - NS_VK_END]; \
else \
direction = DirectionFromKeyCode_lr_tb[keycode - NS_VK_END];
// nsMenuChainItem holds info about an open popup. Items are stored in a // nsMenuChainItem holds info about an open popup. Items are stored in a
// doubly linked list. Note that the linked list is stored beginning from // doubly linked list. Note that the linked list is stored beginning from

View File

@ -147,7 +147,7 @@ NS_IMETHODIMP nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent, PRBool*
case NS_VK_END: case NS_VK_END:
{ {
nsNavigationDirection theDirection; nsNavigationDirection theDirection;
NS_DIRECTION_FROM_KEY_CODE(popupFrame, theDirection, keyCode); theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
*aHandledFlag = *aHandledFlag =
pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection); pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
return NS_OK; return NS_OK;

View File

@ -68,23 +68,23 @@
#include "nsIDocument.h" #include "nsIDocument.h"
#include "nsPIDOMWindow.h" #include "nsPIDOMWindow.h"
// See matching definitions in nsXULPopupManager.h const nsNavigationDirection DirectionFromKeyCodeTable[2][6] = {
nsNavigationDirection DirectionFromKeyCode_lr_tb [6] = { {
eNavigationDirection_Last, // NS_VK_END eNavigationDirection_Last, // NS_VK_END
eNavigationDirection_First, // NS_VK_HOME eNavigationDirection_First, // NS_VK_HOME
eNavigationDirection_Start, // NS_VK_LEFT eNavigationDirection_Start, // NS_VK_LEFT
eNavigationDirection_Before, // NS_VK_UP eNavigationDirection_Before, // NS_VK_UP
eNavigationDirection_End, // NS_VK_RIGHT eNavigationDirection_End, // NS_VK_RIGHT
eNavigationDirection_After // NS_VK_DOWN eNavigationDirection_After // NS_VK_DOWN
}; },
{
nsNavigationDirection DirectionFromKeyCode_rl_tb [6] = { eNavigationDirection_Last, // NS_VK_END
eNavigationDirection_Last, // NS_VK_END eNavigationDirection_First, // NS_VK_HOME
eNavigationDirection_First, // NS_VK_HOME eNavigationDirection_End, // NS_VK_LEFT
eNavigationDirection_End, // NS_VK_LEFT eNavigationDirection_Before, // NS_VK_UP
eNavigationDirection_Before, // NS_VK_UP eNavigationDirection_Start, // NS_VK_RIGHT
eNavigationDirection_Start, // NS_VK_RIGHT eNavigationDirection_After // NS_VK_DOWN
eNavigationDirection_After // NS_VK_DOWN }
}; };
nsXULPopupManager* nsXULPopupManager::sInstance = nsnull; nsXULPopupManager* nsXULPopupManager::sInstance = nsnull;
@ -1604,7 +1604,8 @@ nsXULPopupManager::HandleKeyboardNavigation(PRUint32 aKeyCode)
return PR_FALSE; return PR_FALSE;
nsNavigationDirection theDirection; nsNavigationDirection theDirection;
NS_DIRECTION_FROM_KEY_CODE(itemFrame, theDirection, aKeyCode); NS_ASSERTION(aKeyCode >= NS_VK_END && aKeyCode <= NS_VK_DOWN, "Illegal key code");
theDirection = NS_DIRECTION_FROM_KEY_CODE(itemFrame, aKeyCode);
// if a popup is open, first check for navigation within the popup // if a popup is open, first check for navigation within the popup
if (item && HandleKeyboardNavigationInPopup(item, theDirection)) if (item && HandleKeyboardNavigationInPopup(item, theDirection))