2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is IBM code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2007-10-06 05:08:39 -07:00
|
|
|
* IBM.
|
2007-03-22 10:30:00 -07:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Simon Montagu
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "nsBidiKeyboard.h"
|
|
|
|
#include "prmem.h"
|
2008-08-29 14:26:56 -07:00
|
|
|
#include <tchar.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsBidiKeyboard, nsIBidiKeyboard)
|
|
|
|
|
|
|
|
nsBidiKeyboard::nsBidiKeyboard() : nsIBidiKeyboard()
|
|
|
|
{
|
|
|
|
mInitialized = PR_FALSE;
|
|
|
|
mHaveBidiKeyboards = PR_FALSE;
|
|
|
|
mLTRKeyboard[0] = '\0';
|
|
|
|
mRTLKeyboard[0] = '\0';
|
|
|
|
mCurrentLocaleName[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
nsBidiKeyboard::~nsBidiKeyboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsBidiKeyboard::SetLangFromBidiLevel(PRUint8 aLevel)
|
|
|
|
{
|
|
|
|
nsresult result = SetupBidiKeyboards();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
// call LoadKeyboardLayout() only if the target keyboard layout is different from the current
|
2008-08-29 14:26:56 -07:00
|
|
|
PRUnichar currentLocaleName[KL_NAMELENGTH];
|
|
|
|
wcsncpy(currentLocaleName, (aLevel & 1) ? mRTLKeyboard : mLTRKeyboard, KL_NAMELENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
currentLocaleName[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
|
|
|
|
NS_ASSERTION(*currentLocaleName,
|
|
|
|
"currentLocaleName has string length == 0");
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* This implementation of automatic keyboard layout switching is too buggy to be useful
|
|
|
|
and the feature itself is inconsistent with Windows. See Bug 162242 */
|
|
|
|
if (strcmp(mCurrentLocaleName, currentLocaleName)) {
|
|
|
|
if (!::LoadKeyboardLayout(currentLocaleName, KLF_ACTIVATE | KLF_SUBSTITUTE_OK)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsBidiKeyboard::IsLangRTL(PRBool *aIsRTL)
|
|
|
|
{
|
|
|
|
*aIsRTL = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult result = SetupBidiKeyboards();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
HKL currentLocale;
|
|
|
|
|
|
|
|
currentLocale = ::GetKeyboardLayout(0);
|
|
|
|
*aIsRTL = IsRTLLanguage(currentLocale);
|
|
|
|
|
2008-08-29 14:26:56 -07:00
|
|
|
if (!::GetKeyboardLayoutNameW(mCurrentLocaleName))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ASSERTION(*mCurrentLocaleName,
|
|
|
|
"GetKeyboardLayoutName return string length == 0");
|
2008-08-29 14:26:56 -07:00
|
|
|
NS_ASSERTION((wcslen(mCurrentLocaleName) < KL_NAMELENGTH),
|
2007-03-22 10:30:00 -07:00
|
|
|
"GetKeyboardLayoutName return string length >= KL_NAMELENGTH");
|
|
|
|
|
|
|
|
// The language set by the user overrides the default language for that direction
|
|
|
|
if (*aIsRTL) {
|
2008-08-29 14:26:56 -07:00
|
|
|
wcsncpy(mRTLKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
mRTLKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
} else {
|
2008-08-29 14:26:56 -07:00
|
|
|
wcsncpy(mLTRKeyboard, mCurrentLocaleName, KL_NAMELENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
mLTRKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
}
|
|
|
|
|
2008-08-29 14:26:56 -07:00
|
|
|
NS_ASSERTION((wcslen(mRTLKeyboard) < KL_NAMELENGTH),
|
2007-03-22 10:30:00 -07:00
|
|
|
"mLTRKeyboard has string length >= KL_NAMELENGTH");
|
2008-08-29 14:26:56 -07:00
|
|
|
NS_ASSERTION((wcslen(mLTRKeyboard) < KL_NAMELENGTH),
|
2007-03-22 10:30:00 -07:00
|
|
|
"mRTLKeyboard has string length >= KL_NAMELENGTH");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the list of keyboard layouts available in the system
|
|
|
|
// Set mLTRKeyboard to the first LTR keyboard in the list and mRTLKeyboard to the first RTL keyboard in the list
|
|
|
|
// These defaults will be used unless the user explicitly sets something else.
|
|
|
|
nsresult nsBidiKeyboard::SetupBidiKeyboards()
|
|
|
|
{
|
|
|
|
if (mInitialized)
|
|
|
|
return mHaveBidiKeyboards ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
int keyboards;
|
|
|
|
HKL far* buf;
|
|
|
|
HKL locale;
|
2008-08-29 14:26:56 -07:00
|
|
|
PRUnichar localeName[KL_NAMELENGTH];
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool isLTRKeyboardSet = PR_FALSE;
|
|
|
|
PRBool isRTLKeyboardSet = PR_FALSE;
|
|
|
|
|
|
|
|
// GetKeyboardLayoutList with 0 as first parameter returns the number of keyboard layouts available
|
|
|
|
keyboards = ::GetKeyboardLayoutList(0, nsnull);
|
|
|
|
if (!keyboards)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// allocate a buffer to hold the list
|
|
|
|
buf = (HKL far*) PR_Malloc(keyboards * sizeof(HKL));
|
|
|
|
if (!buf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Call again to fill the buffer
|
|
|
|
if (::GetKeyboardLayoutList(keyboards, buf) != keyboards) {
|
|
|
|
PR_Free(buf);
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through the list and pick a default LTR and RTL keyboard layout
|
|
|
|
while (keyboards--) {
|
|
|
|
locale = buf[keyboards];
|
|
|
|
if (IsRTLLanguage(locale)) {
|
2008-08-29 14:26:56 -07:00
|
|
|
_snwprintf(mRTLKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
|
2010-06-25 05:13:17 -07:00
|
|
|
LANGIDFROMLCID((DWORD_PTR)locale));
|
2007-03-22 10:30:00 -07:00
|
|
|
isRTLKeyboardSet = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
2008-08-29 14:26:56 -07:00
|
|
|
_snwprintf(mLTRKeyboard, KL_NAMELENGTH, L"%.*x", KL_NAMELENGTH - 1,
|
2010-06-25 05:13:17 -07:00
|
|
|
LANGIDFROMLCID((DWORD_PTR)locale));
|
2007-03-22 10:30:00 -07:00
|
|
|
isLTRKeyboardSet = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PR_Free(buf);
|
|
|
|
mInitialized = PR_TRUE;
|
|
|
|
|
|
|
|
// If there is not at least one keyboard of each directionality, Bidi
|
|
|
|
// keyboard functionality will be disabled.
|
|
|
|
mHaveBidiKeyboards = (isRTLKeyboardSet && isLTRKeyboardSet);
|
|
|
|
if (!mHaveBidiKeyboards)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Get the current keyboard layout and use it for either mRTLKeyboard or
|
|
|
|
// mLTRKeyboard as appropriate. If the user has many keyboard layouts
|
|
|
|
// installed this prevents us from arbitrarily resetting the current
|
|
|
|
// layout (bug 80274)
|
|
|
|
locale = ::GetKeyboardLayout(0);
|
2008-08-29 14:26:56 -07:00
|
|
|
if (!::GetKeyboardLayoutNameW(localeName))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ASSERTION(*localeName,
|
|
|
|
"GetKeyboardLayoutName return string length == 0");
|
2008-08-29 14:26:56 -07:00
|
|
|
NS_ASSERTION((wcslen(localeName) < KL_NAMELENGTH),
|
2007-03-22 10:30:00 -07:00
|
|
|
"GetKeyboardLayout return string length >= KL_NAMELENGTH");
|
|
|
|
|
|
|
|
if (IsRTLLanguage(locale)) {
|
2008-08-29 14:26:56 -07:00
|
|
|
wcsncpy(mRTLKeyboard, localeName, KL_NAMELENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
mRTLKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
}
|
|
|
|
else {
|
2008-08-29 14:26:56 -07:00
|
|
|
wcsncpy(mLTRKeyboard, localeName, KL_NAMELENGTH);
|
2007-03-22 10:30:00 -07:00
|
|
|
mLTRKeyboard[KL_NAMELENGTH-1] = '\0'; // null terminate
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(*mRTLKeyboard,
|
|
|
|
"mLTRKeyboard has string length == 0");
|
|
|
|
NS_ASSERTION(*mLTRKeyboard,
|
|
|
|
"mLTRKeyboard has string length == 0");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test whether the language represented by this locale identifier is a
|
|
|
|
// right-to-left language, using bit 123 of the Unicode subset bitfield in
|
|
|
|
// the LOCALESIGNATURE
|
|
|
|
// See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_63ub.asp
|
|
|
|
PRBool nsBidiKeyboard::IsRTLLanguage(HKL aLocale)
|
|
|
|
{
|
|
|
|
LOCALESIGNATURE localesig;
|
2010-06-25 05:13:17 -07:00
|
|
|
return (::GetLocaleInfoW(PRIMARYLANGID((DWORD_PTR)aLocale),
|
2007-03-22 10:30:00 -07:00
|
|
|
LOCALE_FONTSIGNATURE,
|
|
|
|
(LPWSTR)&localesig,
|
|
|
|
(sizeof(localesig)/sizeof(WCHAR))) &&
|
|
|
|
(localesig.lsUsb[3] & 0x08000000));
|
|
|
|
}
|