1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <vcl/svapp.hxx>
22 #include <vcl/settings.hxx>
23 #include <vcl/mnemonic.hxx>
25 #include <vcl/unohelp.hxx>
26 #include <com/sun/star/i18n/XCharacterClassification.hpp>
27 #include <i18nlangtag/languagetag.hxx>
28 #include <i18nlangtag/mslangid.hxx>
29 #include <rtl/character.hxx>
31 using namespace ::com::sun::star
;
33 MnemonicGenerator::MnemonicGenerator(sal_Unicode cMnemonic
)
34 : m_cMnemonic(cMnemonic
)
36 memset( maMnemonics
, 1, sizeof( maMnemonics
) );
39 sal_uInt16
MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c
)
41 static sal_uInt16
const aImplMnemonicRangeTab
[MNEMONIC_RANGES
*2] =
43 MNEMONIC_RANGE_1_START
, MNEMONIC_RANGE_1_END
,
44 MNEMONIC_RANGE_2_START
, MNEMONIC_RANGE_2_END
,
45 MNEMONIC_RANGE_3_START
, MNEMONIC_RANGE_3_END
,
46 MNEMONIC_RANGE_4_START
, MNEMONIC_RANGE_4_END
49 sal_uInt16 nMnemonicIndex
= 0;
50 for ( sal_uInt16 i
= 0; i
< MNEMONIC_RANGES
; i
++ )
52 if ( (c
>= aImplMnemonicRangeTab
[i
*2]) &&
53 (c
<= aImplMnemonicRangeTab
[i
*2+1]) )
54 return nMnemonicIndex
+c
-aImplMnemonicRangeTab
[i
*2];
56 nMnemonicIndex
+= aImplMnemonicRangeTab
[i
*2+1]-aImplMnemonicRangeTab
[i
*2];
59 return MNEMONIC_INDEX_NOTFOUND
;
62 sal_Unicode
MnemonicGenerator::ImplFindMnemonic( const OUString
& rKey
)
65 while ( (nIndex
= rKey
.indexOf( m_cMnemonic
, nIndex
)) != -1 )
67 sal_Unicode cMnemonic
= rKey
[ nIndex
+1 ];
68 if ( cMnemonic
!= m_cMnemonic
)
76 void MnemonicGenerator::RegisterMnemonic( const OUString
& rKey
)
78 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
80 // Don't crash even when we don't have access to i18n service
81 if ( !xCharClass
.is() )
84 OUString aKey
= xCharClass
->toLower(rKey
, 0, rKey
.getLength(), css::lang::Locale());
86 // If we find a Mnemonic, set the flag. In other case count the
87 // characters, because we need this to set most as possible
89 sal_Unicode cMnemonic
= ImplFindMnemonic( aKey
);
92 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( cMnemonic
);
93 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
94 maMnemonics
[nMnemonicIndex
] = 0;
99 sal_Int32 nLen
= aKey
.getLength();
100 while ( nIndex
< nLen
)
102 sal_Unicode c
= aKey
[ nIndex
];
104 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
105 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
107 if ( maMnemonics
[nMnemonicIndex
] && (maMnemonics
[nMnemonicIndex
] < 0xFF) )
108 maMnemonics
[nMnemonicIndex
]++;
116 OUString
MnemonicGenerator::CreateMnemonic( const OUString
& _rKey
)
118 if ( _rKey
.isEmpty() || ImplFindMnemonic( _rKey
) )
121 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
123 // Don't crash even when we don't have access to i18n service
124 if ( !xCharClass
.is() )
127 OUString aKey
= xCharClass
->toLower(_rKey
, 0, _rKey
.getLength(), css::lang::Locale());
129 bool bChanged
= false;
130 sal_Int32 nLen
= aKey
.getLength();
132 bool bCJK
= MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
134 // #107889# in CJK versions ALL strings (even those that contain latin characters)
135 // will get mnemonics in the form: xyz (M)
136 // thus steps 1) and 2) are skipped for CJK locales
138 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
141 bool bLatinOnly
= true;
142 bool bMnemonicIndexFound
= false;
146 for( nIndex
=0; nIndex
< nLen
; nIndex
++ )
149 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
150 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
155 if( ImplGetMnemonicIndex( c
) != MNEMONIC_INDEX_NOTFOUND
)
156 bMnemonicIndexFound
= true;
158 if( bLatinOnly
&& !bMnemonicIndexFound
)
162 OUString
rKey(_rKey
);
164 sal_uInt16 nMnemonicIndex
;
166 sal_Int32 nIndex
= 0;
169 // 1) first try the first character of a word
176 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
177 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
179 else if ( ((c
>= 0x0030) && (c
<= 0x0039)) || // digits
180 ((c
>= 0x0041) && (c
<= 0x005A)) || // latin capitals
181 ((c
>= 0x0061) && (c
<= 0x007A)) || // latin small
182 ((c
>= 0x0370) && (c
<= 0x037F)) || // greek numeral signs
183 ((c
>= 0x0400) && (c
<= 0x04FF)) ) // cyrillic
187 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
188 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
190 if ( maMnemonics
[nMnemonicIndex
] )
192 maMnemonics
[nMnemonicIndex
] = 0;
193 rKey
= rKey
.replaceAt( nIndex
, 0, OUString(m_cMnemonic
) );
199 // Search for next word
201 while ( nIndex
< nLen
)
210 while ( nIndex
< nLen
);
212 // 2) search for a unique/uncommon character
215 sal_uInt16 nBestCount
= 0xFFFF;
216 sal_uInt16 nBestMnemonicIndex
= 0;
217 sal_Int32 nBestIndex
= 0;
222 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
223 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
225 if ( maMnemonics
[nMnemonicIndex
] )
227 if ( maMnemonics
[nMnemonicIndex
] < nBestCount
)
229 nBestCount
= maMnemonics
[nMnemonicIndex
];
231 nBestMnemonicIndex
= nMnemonicIndex
;
232 if ( nBestCount
== 2 )
240 while ( nIndex
< nLen
);
242 if ( nBestCount
!= 0xFFFF )
244 maMnemonics
[nBestMnemonicIndex
] = 0;
245 rKey
= rKey
.replaceAt( nBestIndex
, 0, OUString(m_cMnemonic
) );
253 // 3) Add English Mnemonic for CJK Text
254 if ( !bChanged
&& (nCJK
== 1) && !rKey
.isEmpty() )
256 // Append Ascii Mnemonic
257 for ( c
= MNEMONIC_RANGE_2_START
; c
<= MNEMONIC_RANGE_2_END
; c
++ )
259 nMnemonicIndex
= ImplGetMnemonicIndex(c
);
260 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
262 if ( maMnemonics
[nMnemonicIndex
] )
264 maMnemonics
[nMnemonicIndex
] = 0;
265 OUString aStr
= OUStringLiteral("(") + OUStringChar(m_cMnemonic
) +
266 OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c
))) +
268 nIndex
= rKey
.getLength();
271 if ( ( rKey
[nIndex
-2] == '>' && rKey
[nIndex
-1] == '>' ) ||
272 ( rKey
[nIndex
-2] == 0xFF1E && rKey
[nIndex
-1] == 0xFF1E ) )
277 if ( ( rKey
[nIndex
-3] == '.' && rKey
[nIndex
-2] == '.' && rKey
[nIndex
-1] == '.' ) ||
278 ( rKey
[nIndex
-3] == 0xFF0E && rKey
[nIndex
-2] == 0xFF0E && rKey
[nIndex
-1] == 0xFF0E ) )
283 sal_Unicode cLastChar
= rKey
[ nIndex
-1 ];
284 if ( (cLastChar
== ':') || (cLastChar
== 0xFF1A) ||
285 (cLastChar
== '.') || (cLastChar
== 0xFF0E) ||
286 (cLastChar
== '?') || (cLastChar
== 0xFF1F) ||
290 rKey
= rKey
.replaceAt( nIndex
, 0, aStr
);
300 uno::Reference
< i18n::XCharacterClassification
> const & MnemonicGenerator::GetCharClass()
302 if ( !mxCharClass
.is() )
303 mxCharClass
= vcl::unohelper::CreateCharacterClassification();
307 OUString
MnemonicGenerator::EraseAllMnemonicChars( const OUString
& rStr
)
309 OUString aStr
= rStr
;
310 sal_Int32 nLen
= aStr
.getLength();
315 if ( aStr
[ i
] == '~' )
317 // check for CJK-style mnemonic
318 if( i
> 0 && (i
+2) < nLen
)
320 sal_Unicode c
= sal_Unicode(rtl::toAsciiUpperCase(aStr
[i
+1]));
321 if( aStr
[ i
-1 ] == '(' &&
322 aStr
[ i
+2 ] == ')' &&
323 c
>= MNEMONIC_RANGE_2_START
&& c
<= MNEMONIC_RANGE_2_END
)
325 aStr
= aStr
.replaceAt( i
-1, 4, "" );
332 // remove standard mnemonics
333 aStr
= aStr
.replaceAt( i
, 1, "" );
343 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */