Files
libreoffice/vcl/source/window/mnemonic.cxx

426 lines
13 KiB
C++
Raw Normal View History

2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 16:07:07 +00:00
*
* $RCSfile: mnemonic.cxx,v $
2000-09-18 16:07:07 +00:00
*
* $Revision: 1.19 $
2000-09-18 16:07:07 +00:00
*
* last change: $Author: hr $ $Date: 2006-06-19 19:39:17 $
2000-09-18 16:07:07 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
2000-09-18 16:07:07 +00:00
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
2000-09-18 16:07:07 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
2000-09-18 16:07:07 +00:00
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2000-09-18 16:07:07 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
2000-09-18 16:07:07 +00:00
*
************************************************************************/
#include <string.h>
#include <svapp.hxx>
#include <settings.hxx>
#include <mnemonic.hxx>
#include <unohelp.hxx>
#ifndef _COM_SUN_STAR_I18N_XCHARACTERCLASSIFICATION_HPP_
#include <com/sun/star/i18n/XCharacterClassification.hpp>
2000-09-18 16:07:07 +00:00
#endif
using namespace ::com::sun::star;
// =======================================================================
2002-05-16 10:22:20 +00:00
MnemonicGenerator::MnemonicGenerator()
2000-09-18 16:07:07 +00:00
{
memset( maMnemonics, 1, sizeof( maMnemonics ) );
}
// -----------------------------------------------------------------------
2002-05-16 10:22:20 +00:00
USHORT MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c )
2000-09-18 16:07:07 +00:00
{
static USHORT const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
{
MNEMONIC_RANGE_1_START, MNEMONIC_RANGE_1_END,
MNEMONIC_RANGE_2_START, MNEMONIC_RANGE_2_END,
MNEMONIC_RANGE_3_START, MNEMONIC_RANGE_3_END,
MNEMONIC_RANGE_4_START, MNEMONIC_RANGE_4_END
};
USHORT nMnemonicIndex = 0;
for ( USHORT i = 0; i < MNEMONIC_RANGES; i++ )
{
if ( (c >= aImplMnemonicRangeTab[i*2]) &&
(c <= aImplMnemonicRangeTab[i*2+1]) )
return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
}
return MNEMONIC_INDEX_NOTFOUND;
}
// -----------------------------------------------------------------------
2002-05-16 10:22:20 +00:00
sal_Unicode MnemonicGenerator::ImplFindMnemonic( const XubString& rKey )
2000-09-18 16:07:07 +00:00
{
xub_StrLen nIndex = 0;
while ( (nIndex = rKey.Search( MNEMONIC_CHAR, nIndex )) != STRING_NOTFOUND )
{
sal_Unicode cMnemonic = rKey.GetChar( nIndex+1 );
if ( cMnemonic != MNEMONIC_CHAR )
return cMnemonic;
nIndex += 2;
}
return 0;
}
// -----------------------------------------------------------------------
2002-05-16 10:22:20 +00:00
void MnemonicGenerator::RegisterMnemonic( const XubString& rKey )
2000-09-18 16:07:07 +00:00
{
const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILocale();
uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
2000-09-18 16:07:07 +00:00
// Don't crash even when we don't have access to i18n service
if ( !xCharClass.is() )
return;
2000-09-18 16:07:07 +00:00
XubString aKey = xCharClass->toUpper( rKey, 0, rKey.Len(), rLocale );
// If we find a Mnemonic, set the flag. In other case count the
// characters, because we need this to set most as possible
// Mnemonics
sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
if ( cMnemonic )
{
USHORT nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
maMnemonics[nMnemonicIndex] = 0;
}
else
{
xub_StrLen nIndex = 0;
xub_StrLen nLen = aKey.Len();
while ( nIndex < nLen )
{
sal_Unicode c = aKey.GetChar( nIndex );
USHORT nMnemonicIndex = ImplGetMnemonicIndex( c );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
{
if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
maMnemonics[nMnemonicIndex]++;
}
nIndex++;
}
}
}
// -----------------------------------------------------------------------
2002-05-16 10:22:20 +00:00
BOOL MnemonicGenerator::CreateMnemonic( XubString& rKey )
2000-09-18 16:07:07 +00:00
{
if ( !rKey.Len() || ImplFindMnemonic( rKey ) )
return FALSE;
const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILocale();
uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
2000-09-18 16:07:07 +00:00
// Don't crash even when we don't have access to i18n service
if ( !xCharClass.is() )
return FALSE;
2000-09-18 16:07:07 +00:00
XubString aKey = xCharClass->toUpper( rKey, 0, rKey.Len(), rLocale );
BOOL bChanged = FALSE;
xub_StrLen nLen = aKey.Len();
BOOL bCJK = FALSE;
switch( Application::GetSettings().GetUILanguage() )
{
case LANGUAGE_JAPANESE:
case LANGUAGE_CHINESE_TRADITIONAL:
case LANGUAGE_CHINESE_SIMPLIFIED:
case LANGUAGE_CHINESE_HONGKONG:
case LANGUAGE_CHINESE_SINGAPORE:
case LANGUAGE_CHINESE_MACAU:
case LANGUAGE_KOREAN:
case LANGUAGE_KOREAN_JOHAB:
bCJK = TRUE;
break;
default:
break;
}
// #107889# in CJK versions ALL strings (even those that contain latin characters)
// will get mnemonics in the form: xyz (M)
// thus steps 1) and 2) are skipped for CJK locales
// #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
if( bCJK )
{
BOOL bLatinOnly = TRUE;
BOOL bMnemonicIndexFound = FALSE;
sal_Unicode c;
xub_StrLen nIndex;
for( nIndex=0; nIndex < nLen; nIndex++ )
{
c = aKey.GetChar( nIndex );
if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
{
bLatinOnly = FALSE;
break;
}
if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
bMnemonicIndexFound = TRUE;
}
if( bLatinOnly && !bMnemonicIndexFound )
return FALSE;
}
2001-06-08 12:52:07 +00:00
int nCJK = 0;
2000-09-18 16:07:07 +00:00
USHORT nMnemonicIndex;
sal_Unicode c;
xub_StrLen nIndex = 0;
if( !bCJK )
2000-09-18 16:07:07 +00:00
{
// 1) first try the first character of a word
do
2001-06-08 12:52:07 +00:00
{
c = aKey.GetChar( nIndex );
2001-06-08 12:52:07 +00:00
if ( nCJK != 2 )
2000-09-18 16:07:07 +00:00
{
if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
nCJK = 1;
else if ( ((c >= 0x0030) && (c <= 0x0039)) || // digits
((c >= 0x0041) && (c <= 0x005A)) || // latin capitals
((c >= 0x0061) && (c <= 0x007A)) || // latin small
((c >= 0x0370) && (c <= 0x037F)) || // greek numeral signs
((c >= 0x0400) && (c <= 0x04FF)) ) // cyrillic
nCJK = 2;
2000-09-18 16:07:07 +00:00
}
nMnemonicIndex = ImplGetMnemonicIndex( c );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
{
if ( maMnemonics[nMnemonicIndex] )
{
maMnemonics[nMnemonicIndex] = 0;
rKey.Insert( MNEMONIC_CHAR, nIndex );
bChanged = TRUE;
break;
2000-09-18 16:07:07 +00:00
}
}
// Search for next word
do
{
nIndex++;
c = aKey.GetChar( nIndex );
if ( c == ' ' )
break;
}
while ( nIndex < nLen );
2000-09-18 16:07:07 +00:00
nIndex++;
}
while ( nIndex < nLen );
// 2) search for a unique/uncommon character
if ( !bChanged )
2000-09-18 16:07:07 +00:00
{
USHORT nBestCount = 0xFFFF;
USHORT nBestMnemonicIndex = 0;
xub_StrLen nBestIndex = 0;
nIndex = 0;
do
{
c = aKey.GetChar( nIndex );
nMnemonicIndex = ImplGetMnemonicIndex( c );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
{
if ( maMnemonics[nMnemonicIndex] )
{
if ( maMnemonics[nMnemonicIndex] < nBestCount )
{
nBestCount = maMnemonics[nMnemonicIndex];
nBestIndex = nIndex;
nBestMnemonicIndex = nMnemonicIndex;
if ( nBestCount == 2 )
break;
}
}
}
nIndex++;
}
while ( nIndex < nLen );
if ( nBestCount != 0xFFFF )
{
maMnemonics[nBestMnemonicIndex] = 0;
rKey.Insert( MNEMONIC_CHAR, nBestIndex );
bChanged = TRUE;
}
2000-09-18 16:07:07 +00:00
}
}
else
nCJK = 1;
2000-09-18 16:07:07 +00:00
// 3) Add English Mnemonic for CJK Text
2001-06-08 12:52:07 +00:00
if ( !bChanged && (nCJK == 1) && rKey.Len() )
{
// Append Ascii Mnemonic
for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
{
nMnemonicIndex = ImplGetMnemonicIndex( c );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
{
if ( maMnemonics[nMnemonicIndex] )
{
maMnemonics[nMnemonicIndex] = 0;
UniString aStr( '(' );
aStr += MNEMONIC_CHAR;
aStr += c;
aStr += ')';
nIndex = rKey.Len();
if( nIndex >= 2 )
{
static sal_Unicode cGreaterGreater[] = { 0xFF1E, 0xFF1E };
if ( rKey.EqualsAscii( ">>", nIndex-2, 2 ) ||
rKey.Equals( cGreaterGreater, nIndex-2, 2 ) )
nIndex -= 2;
}
if( nIndex >= 3 )
{
static sal_Unicode cDotDotDot[] = { 0xFF0E, 0xFF0E, 0xFF0E };
if ( rKey.EqualsAscii( "...", nIndex-3, 3 ) ||
rKey.Equals( cDotDotDot, nIndex-3, 3 ) )
nIndex -= 3;
}
if( nIndex >= 1)
{
sal_Unicode cLastChar = rKey.GetChar( nIndex-1 );
if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
(cLastChar == '.') || (cLastChar == 0xFF0E) ||
(cLastChar == '?') || (cLastChar == 0xFF1F) ||
(cLastChar == ' ') )
nIndex--;
}
2001-06-08 12:52:07 +00:00
rKey.Insert( aStr, nIndex );
bChanged = TRUE;
break;
}
}
}
}
2002-03-26 10:58:46 +00:00
if( ! bChanged )
{
2002-03-26 10:58:46 +00:00
/*
* #97809# if all else fails use the first character of a word
* anyway and live with duplicate mnemonics
*/
nIndex = 0;
do
{
c = aKey.GetChar( nIndex );
2002-03-26 10:58:46 +00:00
nMnemonicIndex = ImplGetMnemonicIndex( c );
if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
{
maMnemonics[nMnemonicIndex] = 0;
rKey.Insert( MNEMONIC_CHAR, nIndex );
bChanged = TRUE;
break;
2002-03-26 10:58:46 +00:00
}
// Search for next word
do
{
nIndex++;
c = aKey.GetChar( nIndex );
if ( c == ' ' )
break;
}
while ( nIndex < nLen );
nIndex++;
}
while ( nIndex < nLen );
}
2000-09-18 16:07:07 +00:00
return bChanged;
}
2001-06-08 12:52:07 +00:00
// -----------------------------------------------------------------------
2002-05-16 10:22:20 +00:00
uno::Reference< i18n::XCharacterClassification > MnemonicGenerator::GetCharClass()
2000-09-18 16:07:07 +00:00
{
if ( !mxCharClass.is() )
mxCharClass = vcl::unohelper::CreateCharacterClassification();
return mxCharClass;
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
String MnemonicGenerator::EraseAllMnemonicChars( const String& rStr )
{
String aStr = rStr;
xub_StrLen nLen = aStr.Len();
xub_StrLen i = 0;
while ( i < nLen )
{
if ( aStr.GetChar( i ) == '~' )
{
// check for CJK-style mnemonic
if( i > 0 && (i+2) < nLen )
{
sal_Unicode c = aStr.GetChar(i+1);
if( aStr.GetChar( i-1 ) == '(' &&
aStr.GetChar( i+2 ) == ')' &&
c >= MNEMONIC_RANGE_2_START && c <= MNEMONIC_RANGE_2_END )
{
aStr.Erase( i-1, 4 );
nLen -= 4;
i--;
continue;
}
}
// remove standard mnemonics
aStr.Erase( i, 1 );
nLen--;
}
else
i++;
}
return aStr;
}