tdf#130857 qt weld: Support "Java Start Parameters" dialog
[LibreOffice.git] / vcl / source / window / mnemonic.cxx
blobe4f4cf8cf7cde16d214b0293141ddb32001508fc
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 MnemonicGenerator& MnemonicGenerator::operator=(MnemonicGenerator const &) = default; //MSVC2022 workaround
41 MnemonicGenerator::MnemonicGenerator(MnemonicGenerator const&) = default; //MSVC2022 workaround
43 sal_uInt16 MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c )
45 static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
47 MNEMONIC_RANGE_1_START, MNEMONIC_RANGE_1_END,
48 MNEMONIC_RANGE_2_START, MNEMONIC_RANGE_2_END,
49 MNEMONIC_RANGE_3_START, MNEMONIC_RANGE_3_END,
50 MNEMONIC_RANGE_4_START, MNEMONIC_RANGE_4_END
53 sal_uInt16 nMnemonicIndex = 0;
54 for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
56 if ( (c >= aImplMnemonicRangeTab[i*2]) &&
57 (c <= aImplMnemonicRangeTab[i*2+1]) )
58 return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
60 nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
63 return MNEMONIC_INDEX_NOTFOUND;
66 sal_Unicode MnemonicGenerator::ImplFindMnemonic( const OUString& rKey )
68 sal_Int32 nIndex = 0;
69 while ( (nIndex = rKey.indexOf( m_cMnemonic, nIndex )) != -1 )
71 if (nIndex == rKey.getLength() - 1) {
72 SAL_WARN("vcl", "key \"" << rKey << "\" ends in lone mnemonic prefix");
73 break;
75 sal_Unicode cMnemonic = rKey[ nIndex+1 ];
76 if ( cMnemonic != m_cMnemonic )
77 return cMnemonic;
78 nIndex += 2;
81 return 0;
84 void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
86 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
88 // Don't crash even when we don't have access to i18n service
89 if ( !xCharClass.is() )
90 return;
92 OUString aKey = xCharClass->toLower(rKey, 0, rKey.getLength(), css::lang::Locale());
94 // If we find a Mnemonic, set the flag. In other case count the
95 // characters, because we need this to set most as possible
96 // Mnemonics
97 sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
98 if ( cMnemonic )
100 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
101 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
102 maMnemonics[nMnemonicIndex] = 0;
104 else
106 sal_Int32 nIndex = 0;
107 sal_Int32 nLen = aKey.getLength();
108 while ( nIndex < nLen )
110 sal_Unicode c = aKey[ nIndex ];
112 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
113 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
115 if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
116 maMnemonics[nMnemonicIndex]++;
119 nIndex++;
124 OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
126 if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
127 return _rKey;
129 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
131 // Don't crash even when we don't have access to i18n service
132 if ( !xCharClass.is() )
133 return _rKey;
135 OUString aKey = xCharClass->toLower(_rKey, 0, _rKey.getLength(), css::lang::Locale());
137 bool bChanged = false;
138 sal_Int32 nLen = aKey.getLength();
140 bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
142 // #107889# in CJK versions ALL strings (even those that contain latin characters)
143 // will get mnemonics in the form: xyz (M)
144 // thus steps 1) and 2) are skipped for CJK locales
146 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
147 if( bCJK )
149 bool bLatinOnly = true;
150 bool bMnemonicIndexFound = false;
151 sal_Unicode c;
152 sal_Int32 nIndex;
154 for( nIndex=0; nIndex < nLen; nIndex++ )
156 c = aKey[ nIndex ];
157 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
158 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
160 bLatinOnly = false;
161 break;
163 if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
164 bMnemonicIndexFound = true;
166 if( bLatinOnly && !bMnemonicIndexFound )
167 return _rKey;
170 OUString rKey(_rKey);
171 int nCJK = 0;
172 sal_uInt16 nMnemonicIndex;
173 sal_Unicode c;
174 sal_Int32 nIndex = 0;
175 if( !bCJK )
177 // 1) first try the first character of a word
180 c = aKey[ nIndex ];
182 if ( nCJK != 2 )
184 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
185 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
186 nCJK = 1;
187 else if ( ((c >= 0x0030) && (c <= 0x0039)) || // digits
188 ((c >= 0x0041) && (c <= 0x005A)) || // latin capitals
189 ((c >= 0x0061) && (c <= 0x007A)) || // latin small
190 ((c >= 0x0370) && (c <= 0x037F)) || // greek numeral signs
191 ((c >= 0x0400) && (c <= 0x04FF)) ) // cyrillic
192 nCJK = 2;
195 nMnemonicIndex = ImplGetMnemonicIndex( c );
196 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
198 if ( maMnemonics[nMnemonicIndex] )
200 maMnemonics[nMnemonicIndex] = 0;
201 rKey = rKey.replaceAt( nIndex, 0, rtl::OUStringChar(m_cMnemonic) );
202 bChanged = true;
203 break;
207 // Search for next word
208 nIndex++;
209 while ( nIndex < nLen )
211 c = aKey[ nIndex ];
212 if ( c == ' ' )
213 break;
214 nIndex++;
216 nIndex++;
218 while ( nIndex < nLen );
220 // 2) search for a unique/uncommon character
221 if ( !bChanged )
223 sal_uInt16 nBestCount = 0xFFFF;
224 sal_uInt16 nBestMnemonicIndex = 0;
225 sal_Int32 nBestIndex = 0;
226 nIndex = 0;
229 c = aKey[ nIndex ];
230 nMnemonicIndex = ImplGetMnemonicIndex( c );
231 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
233 if ( maMnemonics[nMnemonicIndex] )
235 if ( maMnemonics[nMnemonicIndex] < nBestCount )
237 nBestCount = maMnemonics[nMnemonicIndex];
238 nBestIndex = nIndex;
239 nBestMnemonicIndex = nMnemonicIndex;
240 if ( nBestCount == 2 )
241 break;
246 nIndex++;
248 while ( nIndex < nLen );
250 if ( nBestCount != 0xFFFF )
252 maMnemonics[nBestMnemonicIndex] = 0;
253 rKey = rKey.replaceAt( nBestIndex, 0, rtl::OUStringChar(m_cMnemonic) );
254 bChanged = true;
258 else
259 nCJK = 1;
261 // 3) Add English Mnemonic for CJK Text
262 if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
264 // Append Ascii Mnemonic
265 for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
267 nMnemonicIndex = ImplGetMnemonicIndex(c);
268 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
270 if ( maMnemonics[nMnemonicIndex] )
272 maMnemonics[nMnemonicIndex] = 0;
273 OUString aStr = OUString::Concat("(") + OUStringChar(m_cMnemonic) +
274 OUStringChar(sal_Unicode(rtl::toAsciiUpperCase(c))) +
275 ")";
276 nIndex = rKey.getLength();
277 if( nIndex >= 2 )
279 if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
280 ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
281 nIndex -= 2;
283 if( nIndex >= 3 )
285 if ( ( rKey[nIndex-3] == '.' && rKey[nIndex-2] == '.' && rKey[nIndex-1] == '.' ) ||
286 ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
287 nIndex -= 3;
289 if( nIndex >= 1)
291 sal_Unicode cLastChar = rKey[ nIndex-1 ];
292 if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
293 (cLastChar == '.') || (cLastChar == 0xFF0E) ||
294 (cLastChar == '?') || (cLastChar == 0xFF1F) ||
295 (cLastChar == ' ') )
296 nIndex--;
298 rKey = rKey.replaceAt( nIndex, 0, aStr );
299 break;
305 return rKey;
308 uno::Reference< i18n::XCharacterClassification > const & MnemonicGenerator::GetCharClass()
310 if ( !mxCharClass.is() )
311 mxCharClass = vcl::unohelper::CreateCharacterClassification();
312 return mxCharClass;
315 OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
317 OUString aStr = rStr;
318 sal_Int32 nLen = aStr.getLength();
319 sal_Int32 i = 0;
321 while ( i < nLen )
323 if ( aStr[ i ] == '~' )
325 // check for CJK-style mnemonic
326 if( i > 0 && (i+2) < nLen )
328 sal_Unicode c = sal_Unicode(rtl::toAsciiLowerCase(aStr[i+1]));
329 if( aStr[ i-1 ] == '(' &&
330 aStr[ i+2 ] == ')' &&
331 c >= MNEMONIC_RANGE_2_START && c <= MNEMONIC_RANGE_2_END )
333 aStr = aStr.replaceAt( i-1, 4, u"" );
334 nLen -= 4;
335 i--;
336 continue;
340 // remove standard mnemonics
341 aStr = aStr.replaceAt( i, 1, u"" );
342 nLen--;
344 else
345 i++;
348 return aStr;
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */