nss: upgrade to release 3.73
[LibreOffice.git] / vcl / source / window / mnemonic.cxx
blob42a3d4ad258e99ba742f6af2e489f5bbd5d4dc9a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <string.h>
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>
30 #include <sal/log.hxx>
32 using namespace ::com::sun::star;
34 MnemonicGenerator::MnemonicGenerator(sal_Unicode cMnemonic)
35 : m_cMnemonic(cMnemonic)
37 memset( maMnemonics, 1, sizeof( maMnemonics ) );
40 sal_uInt16 MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c )
42 static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
44 MNEMONIC_RANGE_1_START, MNEMONIC_RANGE_1_END,
45 MNEMONIC_RANGE_2_START, MNEMONIC_RANGE_2_END,
46 MNEMONIC_RANGE_3_START, MNEMONIC_RANGE_3_END,
47 MNEMONIC_RANGE_4_START, MNEMONIC_RANGE_4_END
50 sal_uInt16 nMnemonicIndex = 0;
51 for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
53 if ( (c >= aImplMnemonicRangeTab[i*2]) &&
54 (c <= aImplMnemonicRangeTab[i*2+1]) )
55 return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
57 nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
60 return MNEMONIC_INDEX_NOTFOUND;
63 sal_Unicode MnemonicGenerator::ImplFindMnemonic( const OUString& rKey )
65 sal_Int32 nIndex = 0;
66 while ( (nIndex = rKey.indexOf( m_cMnemonic, nIndex )) != -1 )
68 if (nIndex == rKey.getLength() - 1) {
69 SAL_WARN("vcl", "key \"" << rKey << "\" ends in lone mnemonic prefix");
70 break;
72 sal_Unicode cMnemonic = rKey[ nIndex+1 ];
73 if ( cMnemonic != m_cMnemonic )
74 return cMnemonic;
75 nIndex += 2;
78 return 0;
81 void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
83 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
85 // Don't crash even when we don't have access to i18n service
86 if ( !xCharClass.is() )
87 return;
89 OUString aKey = xCharClass->toLower(rKey, 0, rKey.getLength(), css::lang::Locale());
91 // If we find a Mnemonic, set the flag. In other case count the
92 // characters, because we need this to set most as possible
93 // Mnemonics
94 sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
95 if ( cMnemonic )
97 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
98 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
99 maMnemonics[nMnemonicIndex] = 0;
101 else
103 sal_Int32 nIndex = 0;
104 sal_Int32 nLen = aKey.getLength();
105 while ( nIndex < nLen )
107 sal_Unicode c = aKey[ nIndex ];
109 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
110 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
112 if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
113 maMnemonics[nMnemonicIndex]++;
116 nIndex++;
121 OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
123 if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
124 return _rKey;
126 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
128 // Don't crash even when we don't have access to i18n service
129 if ( !xCharClass.is() )
130 return _rKey;
132 OUString aKey = xCharClass->toLower(_rKey, 0, _rKey.getLength(), css::lang::Locale());
134 bool bChanged = false;
135 sal_Int32 nLen = aKey.getLength();
137 bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
139 // #107889# in CJK versions ALL strings (even those that contain latin characters)
140 // will get mnemonics in the form: xyz (M)
141 // thus steps 1) and 2) are skipped for CJK locales
143 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
144 if( bCJK )
146 bool bLatinOnly = true;
147 bool bMnemonicIndexFound = false;
148 sal_Unicode c;
149 sal_Int32 nIndex;
151 for( nIndex=0; nIndex < nLen; nIndex++ )
153 c = aKey[ nIndex ];
154 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
155 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
157 bLatinOnly = false;
158 break;
160 if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
161 bMnemonicIndexFound = true;
163 if( bLatinOnly && !bMnemonicIndexFound )
164 return _rKey;
167 OUString rKey(_rKey);
168 int nCJK = 0;
169 sal_uInt16 nMnemonicIndex;
170 sal_Unicode c;
171 sal_Int32 nIndex = 0;
172 if( !bCJK )
174 // 1) first try the first character of a word
177 c = aKey[ nIndex ];
179 if ( nCJK != 2 )
181 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
182 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
183 nCJK = 1;
184 else if ( ((c >= 0x0030) && (c <= 0x0039)) || // digits
185 ((c >= 0x0041) && (c <= 0x005A)) || // latin capitals
186 ((c >= 0x0061) && (c <= 0x007A)) || // latin small
187 ((c >= 0x0370) && (c <= 0x037F)) || // greek numeral signs
188 ((c >= 0x0400) && (c <= 0x04FF)) ) // cyrillic
189 nCJK = 2;
192 nMnemonicIndex = ImplGetMnemonicIndex( c );
193 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
195 if ( maMnemonics[nMnemonicIndex] )
197 maMnemonics[nMnemonicIndex] = 0;
198 rKey = rKey.replaceAt( nIndex, 0, OUString(m_cMnemonic) );
199 bChanged = true;
200 break;
204 // Search for next word
205 nIndex++;
206 while ( nIndex < nLen )
208 c = aKey[ nIndex ];
209 if ( c == ' ' )
210 break;
211 nIndex++;
213 nIndex++;
215 while ( nIndex < nLen );
217 // 2) search for a unique/uncommon character
218 if ( !bChanged )
220 sal_uInt16 nBestCount = 0xFFFF;
221 sal_uInt16 nBestMnemonicIndex = 0;
222 sal_Int32 nBestIndex = 0;
223 nIndex = 0;
226 c = aKey[ nIndex ];
227 nMnemonicIndex = ImplGetMnemonicIndex( c );
228 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
230 if ( maMnemonics[nMnemonicIndex] )
232 if ( maMnemonics[nMnemonicIndex] < nBestCount )
234 nBestCount = maMnemonics[nMnemonicIndex];
235 nBestIndex = nIndex;
236 nBestMnemonicIndex = nMnemonicIndex;
237 if ( nBestCount == 2 )
238 break;
243 nIndex++;
245 while ( nIndex < nLen );
247 if ( nBestCount != 0xFFFF )
249 maMnemonics[nBestMnemonicIndex] = 0;
250 rKey = rKey.replaceAt( nBestIndex, 0, OUString(m_cMnemonic) );
251 bChanged = true;
255 else
256 nCJK = 1;
258 // 3) Add English Mnemonic for CJK Text
259 if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
261 // Append Ascii Mnemonic
262 for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
264 nMnemonicIndex = ImplGetMnemonicIndex(c);
265 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
267 if ( maMnemonics[nMnemonicIndex] )
269 maMnemonics[nMnemonicIndex] = 0;
270 OUString aStr = OUStringLiteral(u"(") + OUStringChar(m_cMnemonic) +
271 OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c))) +
272 ")";
273 nIndex = rKey.getLength();
274 if( nIndex >= 2 )
276 if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
277 ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
278 nIndex -= 2;
280 if( nIndex >= 3 )
282 if ( ( rKey[nIndex-3] == '.' && rKey[nIndex-2] == '.' && rKey[nIndex-1] == '.' ) ||
283 ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
284 nIndex -= 3;
286 if( nIndex >= 1)
288 sal_Unicode cLastChar = rKey[ nIndex-1 ];
289 if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
290 (cLastChar == '.') || (cLastChar == 0xFF0E) ||
291 (cLastChar == '?') || (cLastChar == 0xFF1F) ||
292 (cLastChar == ' ') )
293 nIndex--;
295 rKey = rKey.replaceAt( nIndex, 0, aStr );
296 break;
302 return rKey;
305 uno::Reference< i18n::XCharacterClassification > const & MnemonicGenerator::GetCharClass()
307 if ( !mxCharClass.is() )
308 mxCharClass = vcl::unohelper::CreateCharacterClassification();
309 return mxCharClass;
312 OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
314 OUString aStr = rStr;
315 sal_Int32 nLen = aStr.getLength();
316 sal_Int32 i = 0;
318 while ( i < nLen )
320 if ( aStr[ i ] == '~' )
322 // check for CJK-style mnemonic
323 if( i > 0 && (i+2) < nLen )
325 sal_Unicode c = sal_Unicode(rtl::toAsciiUpperCase(aStr[i+1]));
326 if( aStr[ i-1 ] == '(' &&
327 aStr[ i+2 ] == ')' &&
328 c >= MNEMONIC_RANGE_2_START && c <= MNEMONIC_RANGE_2_END )
330 aStr = aStr.replaceAt( i-1, 4, "" );
331 nLen -= 4;
332 i--;
333 continue;
337 // remove standard mnemonics
338 aStr = aStr.replaceAt( i, 1, "" );
339 nLen--;
341 else
342 i++;
345 return aStr;
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */