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/mslangid.hxx>
29 using namespace ::com::sun::star
;
31 MnemonicGenerator::MnemonicGenerator()
33 memset( maMnemonics
, 1, sizeof( maMnemonics
) );
36 sal_uInt16
MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c
)
38 static sal_uInt16
const aImplMnemonicRangeTab
[MNEMONIC_RANGES
*2] =
40 MNEMONIC_RANGE_1_START
, MNEMONIC_RANGE_1_END
,
41 MNEMONIC_RANGE_2_START
, MNEMONIC_RANGE_2_END
,
42 MNEMONIC_RANGE_3_START
, MNEMONIC_RANGE_3_END
,
43 MNEMONIC_RANGE_4_START
, MNEMONIC_RANGE_4_END
46 sal_uInt16 nMnemonicIndex
= 0;
47 for ( sal_uInt16 i
= 0; i
< MNEMONIC_RANGES
; i
++ )
49 if ( (c
>= aImplMnemonicRangeTab
[i
*2]) &&
50 (c
<= aImplMnemonicRangeTab
[i
*2+1]) )
51 return nMnemonicIndex
+c
-aImplMnemonicRangeTab
[i
*2];
53 nMnemonicIndex
+= aImplMnemonicRangeTab
[i
*2+1]-aImplMnemonicRangeTab
[i
*2];
56 return MNEMONIC_INDEX_NOTFOUND
;
59 sal_Unicode
MnemonicGenerator::ImplFindMnemonic( const OUString
& rKey
)
62 while ( (nIndex
= rKey
.indexOf( MNEMONIC_CHAR
, nIndex
)) != -1 )
64 sal_Unicode cMnemonic
= rKey
[ nIndex
+1 ];
65 if ( cMnemonic
!= MNEMONIC_CHAR
)
73 void MnemonicGenerator::RegisterMnemonic( const OUString
& rKey
)
75 const css::lang::Locale
& rLocale
= Application::GetSettings().GetUILanguageTag().getLocale();
76 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
78 // Don't crash even when we don't have access to i18n service
79 if ( !xCharClass
.is() )
82 OUString aKey
= xCharClass
->toUpper( rKey
, 0, rKey
.getLength(), rLocale
);
84 // If we find a Mnemonic, set the flag. In other case count the
85 // characters, because we need this to set most as possible
87 sal_Unicode cMnemonic
= ImplFindMnemonic( aKey
);
90 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( cMnemonic
);
91 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
92 maMnemonics
[nMnemonicIndex
] = 0;
97 sal_Int32 nLen
= aKey
.getLength();
98 while ( nIndex
< nLen
)
100 sal_Unicode c
= aKey
[ nIndex
];
102 sal_uInt16 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
103 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
105 if ( maMnemonics
[nMnemonicIndex
] && (maMnemonics
[nMnemonicIndex
] < 0xFF) )
106 maMnemonics
[nMnemonicIndex
]++;
114 OUString
MnemonicGenerator::CreateMnemonic( const OUString
& _rKey
)
116 if ( _rKey
.isEmpty() || ImplFindMnemonic( _rKey
) )
119 const css::lang::Locale
& rLocale
= Application::GetSettings().GetUILanguageTag().getLocale();
120 uno::Reference
< i18n::XCharacterClassification
> xCharClass
= GetCharClass();
122 // Don't crash even when we don't have access to i18n service
123 if ( !xCharClass
.is() )
126 OUString aKey
= xCharClass
->toUpper( _rKey
, 0, _rKey
.getLength(), rLocale
);
128 bool bChanged
= false;
129 sal_Int32 nLen
= aKey
.getLength();
131 bool bCJK
= MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
133 // #107889# in CJK versions ALL strings (even those that contain latin characters)
134 // will get mnemonics in the form: xyz (M)
135 // thus steps 1) and 2) are skipped for CJK locales
137 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
140 bool bLatinOnly
= true;
141 bool bMnemonicIndexFound
= false;
145 for( nIndex
=0; nIndex
< nLen
; nIndex
++ )
148 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
149 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
154 if( ImplGetMnemonicIndex( c
) != MNEMONIC_INDEX_NOTFOUND
)
155 bMnemonicIndexFound
= true;
157 if( bLatinOnly
&& !bMnemonicIndexFound
)
161 OUString
rKey(_rKey
);
163 sal_uInt16 nMnemonicIndex
;
165 sal_Int32 nIndex
= 0;
168 // 1) first try the first character of a word
175 if ( ((c
>= 0x3000) && (c
<= 0xD7FF)) || // cjk
176 ((c
>= 0xFF61) && (c
<= 0xFFDC)) ) // halfwidth forms
178 else if ( ((c
>= 0x0030) && (c
<= 0x0039)) || // digits
179 ((c
>= 0x0041) && (c
<= 0x005A)) || // latin capitals
180 ((c
>= 0x0061) && (c
<= 0x007A)) || // latin small
181 ((c
>= 0x0370) && (c
<= 0x037F)) || // greek numeral signs
182 ((c
>= 0x0400) && (c
<= 0x04FF)) ) // cyrillic
186 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
187 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
189 if ( maMnemonics
[nMnemonicIndex
] )
191 maMnemonics
[nMnemonicIndex
] = 0;
192 rKey
= rKey
.replaceAt( nIndex
, 0, OUString(MNEMONIC_CHAR
) );
198 // Search for next word
200 while ( nIndex
< nLen
)
209 while ( nIndex
< nLen
);
211 // 2) search for a unique/uncommon character
214 sal_uInt16 nBestCount
= 0xFFFF;
215 sal_uInt16 nBestMnemonicIndex
= 0;
216 sal_Int32 nBestIndex
= 0;
221 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
222 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
224 if ( maMnemonics
[nMnemonicIndex
] )
226 if ( maMnemonics
[nMnemonicIndex
] < nBestCount
)
228 nBestCount
= maMnemonics
[nMnemonicIndex
];
230 nBestMnemonicIndex
= nMnemonicIndex
;
231 if ( nBestCount
== 2 )
239 while ( nIndex
< nLen
);
241 if ( nBestCount
!= 0xFFFF )
243 maMnemonics
[nBestMnemonicIndex
] = 0;
244 rKey
= rKey
.replaceAt( nBestIndex
, 0, OUString(MNEMONIC_CHAR
) );
252 // 3) Add English Mnemonic for CJK Text
253 if ( !bChanged
&& (nCJK
== 1) && !rKey
.isEmpty() )
255 // Append Ascii Mnemonic
256 for ( c
= MNEMONIC_RANGE_2_START
; c
<= MNEMONIC_RANGE_2_END
; c
++ )
258 nMnemonicIndex
= ImplGetMnemonicIndex( c
);
259 if ( nMnemonicIndex
!= MNEMONIC_INDEX_NOTFOUND
)
261 if ( maMnemonics
[nMnemonicIndex
] )
263 maMnemonics
[nMnemonicIndex
] = 0;
264 OUString aStr
= OUStringBuffer().
265 append('(').append(MNEMONIC_CHAR
).append(c
).
266 append(')').makeStringAndClear();
267 nIndex
= rKey
.getLength();
270 if ( ( rKey
[nIndex
-2] == '>' && rKey
[nIndex
-1] == '>' ) ||
271 ( rKey
[nIndex
-2] == 0xFF1E && rKey
[nIndex
-1] == 0xFF1E ) )
276 if ( ( rKey
[nIndex
-3] == '.' && rKey
[nIndex
-2] == '.' && rKey
[nIndex
-1] == '.' ) ||
277 ( rKey
[nIndex
-3] == 0xFF0E && rKey
[nIndex
-2] == 0xFF0E && rKey
[nIndex
-1] == 0xFF0E ) )
282 sal_Unicode cLastChar
= rKey
[ nIndex
-1 ];
283 if ( (cLastChar
== ':') || (cLastChar
== 0xFF1A) ||
284 (cLastChar
== '.') || (cLastChar
== 0xFF0E) ||
285 (cLastChar
== '?') || (cLastChar
== 0xFF1F) ||
289 rKey
= rKey
.replaceAt( nIndex
, 0, aStr
);
299 uno::Reference
< i18n::XCharacterClassification
> const & MnemonicGenerator::GetCharClass()
301 if ( !mxCharClass
.is() )
302 mxCharClass
= vcl::unohelper::CreateCharacterClassification();
306 OUString
MnemonicGenerator::EraseAllMnemonicChars( const OUString
& rStr
)
308 OUString aStr
= rStr
;
309 sal_Int32 nLen
= aStr
.getLength();
314 if ( aStr
[ i
] == '~' )
316 // check for CJK-style mnemonic
317 if( i
> 0 && (i
+2) < nLen
)
319 sal_Unicode c
= aStr
[i
+1];
320 if( aStr
[ i
-1 ] == '(' &&
321 aStr
[ i
+2 ] == ')' &&
322 c
>= MNEMONIC_RANGE_2_START
&& c
<= MNEMONIC_RANGE_2_END
)
324 aStr
= aStr
.replaceAt( i
-1, 4, "" );
331 // remove standard mnemonics
332 aStr
= aStr
.replaceAt( i
, 1, "" );
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */