Bug 432389 followup: refactor code to make it more understandable, patch by Karl Tomlinson <mozbugz@karlt.net>, r=roc, a=schrep
authorgavin@gavinsharp.com
Thu, 08 May 2008 18:51:46 -0700
changeset 15079 b833e0c1f7d92cbe4f39b9eef098fa18b3c5b6b7
parent 15078 a3274d6273d402b1ef52da08f15f5befad9da59f
child 15080 48dc2117bf819b7593966522bc40a5e3b6ab5748
push idunknown
push userunknown
push dateunknown
reviewersroc, schrep
bugs432389
milestone1.9pre
Bug 432389 followup: refactor code to make it more understandable, patch by Karl Tomlinson <mozbugz@karlt.net>, r=roc, a=schrep
widget/src/cocoa/nsChildView.mm
--- a/widget/src/cocoa/nsChildView.mm
+++ b/widget/src/cocoa/nsChildView.mm
@@ -4233,41 +4233,41 @@ GetUSLayoutCharFromKeyTranslate(UInt32 a
                                             kTextRegionDontCare,
                                             &kt.mKchr.mEncoding);
           if (err != noErr)
             kt.mKchr.mHandle = nsnull;
         }
       }
 
       UInt32 key = [aKeyEvent keyCode];
-      UInt32 mod;
-
-      UInt32 baseMod = 0;
+
+      // Caps lock and num lock modifier state:
+      UInt32 lockState = 0;
       if ([aKeyEvent modifierFlags] & NSAlphaShiftKeyMask)
-        baseMod |= alphaLock;
+        lockState |= alphaLock;
       if ([aKeyEvent modifierFlags] & NSNumericPadKeyMask)
-        baseMod |= kEventKeyModifierNumLockMask;
+        lockState |= kEventKeyModifierNumLockMask;
 
       // normal chars
-      mod = baseMod;
-      PRUint32 unshiftedChar = GetUniCharFromKeyTranslate(kt, key, mod);
-      mod |= shiftKey;
-      PRUint32 shiftedChar = GetUniCharFromKeyTranslate(kt, key, mod);
-
-      // with Cmd key
-      // XXX we should remove CapsLock state, that changes from Latin to
-      //     Cyrillic characters with Russian layout of 10.4 only when Cmd key
+      PRUint32 unshiftedChar = GetUniCharFromKeyTranslate(kt, key, lockState);
+      PRUint32 shiftLockMod = shiftKey | lockState;
+      PRUint32 shiftedChar = GetUniCharFromKeyTranslate(kt, key, shiftLockMod);
+
+      // characters generated with Cmd key
+      // XXX we should remove CapsLock state, which changes characters from
+      //     Latin to Cyrillic with Russian layout on 10.4 only when Cmd key
       //     is pressed.
-      mod = (baseMod & ~alphaLock);
-      PRUint32 uncmdedChar = GetUniCharFromKeyTranslate(kt, key, mod);
-      PRUint32 uncmdedUSChar = GetUSLayoutCharFromKeyTranslate(key, mod);
-      mod |= cmdKey;
-      PRUint32 cmdedChar = GetUniCharFromKeyTranslate(kt, key, mod);
-      mod |= shiftKey;
-      PRUint32 cmdedShiftChar = GetUniCharFromKeyTranslate(kt, key, mod);
+      PRUint32 numState = (lockState & ~alphaLock); // only num lock state
+      PRUint32 uncmdedChar = GetUniCharFromKeyTranslate(kt, key, numState);
+      PRUint32 uncmdedUSChar = GetUSLayoutCharFromKeyTranslate(key, numState);
+      PRUint32 cmdNumMod = cmdKey | numState;
+      PRUint32 cmdedChar = GetUniCharFromKeyTranslate(kt, key, cmdNumMod);
+      PRUint32 cmdShiftNumMod = shiftKey | cmdNumMod;
+      PRUint32 cmdedShiftChar =
+        GetUniCharFromKeyTranslate(kt, key, cmdShiftNumMod);
 
       // Is the keyboard layout changed by Cmd key?
       // E.g., Arabic, Russian, Hebrew, Greek and Dvorak-QWERTY.
       PRBool isCmdSwitchLayout = uncmdedChar != cmdedChar;
       // Is the keyboard layout for Latin, but Cmd key switches the layout?
       // I.e., Dvorak-QWERTY
       PRBool isDvorakQWERTY = isCmdSwitchLayout && kt.mScript == smRoman;
 
@@ -4277,31 +4277,30 @@ GetUSLayoutCharFromKeyTranslate(UInt32 a
       if ((unshiftedChar || shiftedChar) &&
           (!outGeckoEvent->isMeta || !isDvorakQWERTY)) {
         nsAlternativeCharCode altCharCodes(unshiftedChar, shiftedChar);
         outGeckoEvent->alternativeCharCodes.AppendElement(altCharCodes);
       }
 
       // Cleaning up cmdedShiftChar with CapsLocked characters.
       if (!isCmdSwitchLayout) {
+        if (unshiftedChar)
+          cmdedChar = unshiftedChar;
         // Don't replace if cmdedChar and cmdedShiftChar are not same.
         // E.g., Cmd+Shift+SS(eszett) is '/'. But Shift+SS is '?'. Then,
         // we should send '/' directly and '?' should be sent as alternative.
         if (shiftedChar && cmdedChar == cmdedShiftChar)
           cmdedShiftChar = shiftedChar;
-        if (unshiftedChar)
-          cmdedChar = unshiftedChar;
       } else if (uncmdedUSChar == cmdedChar) {
-        mod = baseMod | shiftKey;
-        PRUint32 ch = GetUSLayoutCharFromKeyTranslate(key, mod);
+        PRUint32 ch = GetUSLayoutCharFromKeyTranslate(key, lockState);
+        if (ch)
+          cmdedChar = ch;
+        ch = GetUSLayoutCharFromKeyTranslate(key, shiftLockMod);
         if (ch && cmdedChar == cmdedShiftChar)
           cmdedShiftChar = ch;
-        ch = GetUSLayoutCharFromKeyTranslate(key, mod & ~shiftKey);
-        if (ch)
-          cmdedChar = ch;
       }
 
       // XXX We should do something similar when Control is down (bug 429510).
       if (outGeckoEvent->isMeta &&
            !(outGeckoEvent->isControl || outGeckoEvent->isAlt)) {
 
         // The character to use for charCode.
         PRUint32 preferredCharCode = 0;