build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / window / mnemonic.cxx
blobfe57879d64445b5bb47f668df24855b7fdab4460
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/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 )
61 sal_Int32 nIndex = 0;
62 while ( (nIndex = rKey.indexOf( MNEMONIC_CHAR, nIndex )) != -1 )
64 sal_Unicode cMnemonic = rKey[ nIndex+1 ];
65 if ( cMnemonic != MNEMONIC_CHAR )
66 return cMnemonic;
67 nIndex += 2;
70 return 0;
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() )
80 return;
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
86 // Mnemonics
87 sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
88 if ( cMnemonic )
90 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
91 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
92 maMnemonics[nMnemonicIndex] = 0;
94 else
96 sal_Int32 nIndex = 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]++;
109 nIndex++;
114 OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
116 if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
117 return _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() )
124 return _rKey;
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
138 if( bCJK )
140 bool bLatinOnly = true;
141 bool bMnemonicIndexFound = false;
142 sal_Unicode c;
143 sal_Int32 nIndex;
145 for( nIndex=0; nIndex < nLen; nIndex++ )
147 c = aKey[ nIndex ];
148 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
149 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
151 bLatinOnly = false;
152 break;
154 if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
155 bMnemonicIndexFound = true;
157 if( bLatinOnly && !bMnemonicIndexFound )
158 return _rKey;
161 OUString rKey(_rKey);
162 int nCJK = 0;
163 sal_uInt16 nMnemonicIndex;
164 sal_Unicode c;
165 sal_Int32 nIndex = 0;
166 if( !bCJK )
168 // 1) first try the first character of a word
171 c = aKey[ nIndex ];
173 if ( nCJK != 2 )
175 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
176 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
177 nCJK = 1;
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
183 nCJK = 2;
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) );
193 bChanged = true;
194 break;
198 // Search for next word
199 nIndex++;
200 while ( nIndex < nLen )
202 c = aKey[ nIndex ];
203 if ( c == ' ' )
204 break;
205 nIndex++;
207 nIndex++;
209 while ( nIndex < nLen );
211 // 2) search for a unique/uncommon character
212 if ( !bChanged )
214 sal_uInt16 nBestCount = 0xFFFF;
215 sal_uInt16 nBestMnemonicIndex = 0;
216 sal_Int32 nBestIndex = 0;
217 nIndex = 0;
220 c = aKey[ nIndex ];
221 nMnemonicIndex = ImplGetMnemonicIndex( c );
222 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
224 if ( maMnemonics[nMnemonicIndex] )
226 if ( maMnemonics[nMnemonicIndex] < nBestCount )
228 nBestCount = maMnemonics[nMnemonicIndex];
229 nBestIndex = nIndex;
230 nBestMnemonicIndex = nMnemonicIndex;
231 if ( nBestCount == 2 )
232 break;
237 nIndex++;
239 while ( nIndex < nLen );
241 if ( nBestCount != 0xFFFF )
243 maMnemonics[nBestMnemonicIndex] = 0;
244 rKey = rKey.replaceAt( nBestIndex, 0, OUString(MNEMONIC_CHAR) );
245 bChanged = true;
249 else
250 nCJK = 1;
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();
268 if( nIndex >= 2 )
270 if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
271 ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
272 nIndex -= 2;
274 if( nIndex >= 3 )
276 if ( ( rKey[nIndex-3] == '.' && rKey[nIndex-2] == '.' && rKey[nIndex-1] == '.' ) ||
277 ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
278 nIndex -= 3;
280 if( nIndex >= 1)
282 sal_Unicode cLastChar = rKey[ nIndex-1 ];
283 if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
284 (cLastChar == '.') || (cLastChar == 0xFF0E) ||
285 (cLastChar == '?') || (cLastChar == 0xFF1F) ||
286 (cLastChar == ' ') )
287 nIndex--;
289 rKey = rKey.replaceAt( nIndex, 0, aStr );
290 break;
296 return rKey;
299 uno::Reference< i18n::XCharacterClassification > const & MnemonicGenerator::GetCharClass()
301 if ( !mxCharClass.is() )
302 mxCharClass = vcl::unohelper::CreateCharacterClassification();
303 return mxCharClass;
306 OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
308 OUString aStr = rStr;
309 sal_Int32 nLen = aStr.getLength();
310 sal_Int32 i = 0;
312 while ( i < nLen )
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, "" );
325 nLen -= 4;
326 i--;
327 continue;
331 // remove standard mnemonics
332 aStr = aStr.replaceAt( i, 1, "" );
333 nLen--;
335 else
336 i++;
339 return aStr;
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */