LibreOffice Module vcl (master) 1
window/mnemonic.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <string.h>
21#include <vcl/svapp.hxx>
22#include <vcl/settings.hxx>
23#include <vcl/mnemonic.hxx>
24
25#include <vcl/unohelp.hxx>
26#include <com/sun/star/i18n/XCharacterClassification.hpp>
29#include <rtl/character.hxx>
30#include <sal/log.hxx>
31
32using namespace ::com::sun::star;
33
35 : m_cMnemonic(cMnemonic)
36{
37 memset( maMnemonics, 1, sizeof( maMnemonics ) );
38}
39
40MnemonicGenerator& MnemonicGenerator::operator=(MnemonicGenerator const &) = default; //MSVC2022 workaround
41MnemonicGenerator::MnemonicGenerator(MnemonicGenerator const&) = default; //MSVC2022 workaround
42
44{
45 static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
46 {
51 };
52
53 sal_uInt16 nMnemonicIndex = 0;
54 for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
55 {
56 if ( (c >= aImplMnemonicRangeTab[i*2]) &&
57 (c <= aImplMnemonicRangeTab[i*2+1]) )
58 return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
59
60 nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
61 }
62
64}
65
67{
68 sal_Int32 nIndex = 0;
69 while ( (nIndex = rKey.indexOf( m_cMnemonic, nIndex )) != -1 )
70 {
71 if (nIndex == rKey.getLength() - 1) {
72 SAL_WARN("vcl", "key \"" << rKey << "\" ends in lone mnemonic prefix");
73 break;
74 }
75 sal_Unicode cMnemonic = rKey[ nIndex+1 ];
76 if ( cMnemonic != m_cMnemonic )
77 return cMnemonic;
78 nIndex += 2;
79 }
80
81 return 0;
82}
83
84void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
85{
86 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
87
88 // Don't crash even when we don't have access to i18n service
89 if ( !xCharClass.is() )
90 return;
91
92 OUString aKey = xCharClass->toLower(rKey, 0, rKey.getLength(), css::lang::Locale());
93
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 )
99 {
100 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
101 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
102 maMnemonics[nMnemonicIndex] = 0;
103 }
104 else
105 {
106 sal_Int32 nIndex = 0;
107 sal_Int32 nLen = aKey.getLength();
108 while ( nIndex < nLen )
109 {
110 sal_Unicode c = aKey[ nIndex ];
111
112 sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
113 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
114 {
115 if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
116 maMnemonics[nMnemonicIndex]++;
117 }
118
119 nIndex++;
120 }
121 }
122}
123
124OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
125{
126 if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
127 return _rKey;
128
129 uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
130
131 // Don't crash even when we don't have access to i18n service
132 if ( !xCharClass.is() )
133 return _rKey;
134
135 OUString aKey = xCharClass->toLower(_rKey, 0, _rKey.getLength(), css::lang::Locale());
136
137 bool bChanged = false;
138 sal_Int32 nLen = aKey.getLength();
139
140 bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
141
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
145
146 // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
147 if( bCJK )
148 {
149 bool bLatinOnly = true;
150 bool bMnemonicIndexFound = false;
151 sal_Unicode c;
152 sal_Int32 nIndex;
153
154 for( nIndex=0; nIndex < nLen; nIndex++ )
155 {
156 c = aKey[ nIndex ];
157 if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
158 ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
159 {
160 bLatinOnly = false;
161 break;
162 }
164 bMnemonicIndexFound = true;
165 }
166 if( bLatinOnly && !bMnemonicIndexFound )
167 return _rKey;
168 }
169
170 OUString rKey(_rKey);
171 int nCJK = 0;
172 sal_uInt16 nMnemonicIndex;
173 sal_Unicode c;
174 sal_Int32 nIndex = 0;
175 if( !bCJK )
176 {
177 // 1) first try the first character of a word
178 do
179 {
180 c = aKey[ nIndex ];
181
182 if ( nCJK != 2 )
183 {
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;
193 }
194
195 nMnemonicIndex = ImplGetMnemonicIndex( c );
196 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
197 {
198 if ( maMnemonics[nMnemonicIndex] )
199 {
200 maMnemonics[nMnemonicIndex] = 0;
201 rKey = rKey.replaceAt( nIndex, 0, rtl::OUStringChar(m_cMnemonic) );
202 bChanged = true;
203 break;
204 }
205 }
206
207 // Search for next word
208 nIndex++;
209 while ( nIndex < nLen )
210 {
211 c = aKey[ nIndex ];
212 if ( c == ' ' )
213 break;
214 nIndex++;
215 }
216 nIndex++;
217 }
218 while ( nIndex < nLen );
219
220 // 2) search for a unique/uncommon character
221 if ( !bChanged )
222 {
223 sal_uInt16 nBestCount = 0xFFFF;
224 sal_uInt16 nBestMnemonicIndex = 0;
225 sal_Int32 nBestIndex = 0;
226 nIndex = 0;
227 do
228 {
229 c = aKey[ nIndex ];
230 nMnemonicIndex = ImplGetMnemonicIndex( c );
231 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
232 {
233 if ( maMnemonics[nMnemonicIndex] )
234 {
235 if ( maMnemonics[nMnemonicIndex] < nBestCount )
236 {
237 nBestCount = maMnemonics[nMnemonicIndex];
238 nBestIndex = nIndex;
239 nBestMnemonicIndex = nMnemonicIndex;
240 if ( nBestCount == 2 )
241 break;
242 }
243 }
244 }
245
246 nIndex++;
247 }
248 while ( nIndex < nLen );
249
250 if ( nBestCount != 0xFFFF )
251 {
252 maMnemonics[nBestMnemonicIndex] = 0;
253 rKey = rKey.replaceAt( nBestIndex, 0, rtl::OUStringChar(m_cMnemonic) );
254 bChanged = true;
255 }
256 }
257 }
258 else
259 nCJK = 1;
260
261 // 3) Add English Mnemonic for CJK Text
262 if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
263 {
264 // Append Ascii Mnemonic
265 for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
266 {
267 nMnemonicIndex = ImplGetMnemonicIndex(c);
268 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
269 {
270 if ( maMnemonics[nMnemonicIndex] )
271 {
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 )
278 {
279 if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
280 ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
281 nIndex -= 2;
282 }
283 if( nIndex >= 3 )
284 {
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;
288 }
289 if( nIndex >= 1)
290 {
291 sal_Unicode cLastChar = rKey[ nIndex-1 ];
292 if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
293 (cLastChar == '.') || (cLastChar == 0xFF0E) ||
294 (cLastChar == '?') || (cLastChar == 0xFF1F) ||
295 (cLastChar == ' ') )
296 nIndex--;
297 }
298 rKey = rKey.replaceAt( nIndex, 0, aStr );
299 break;
300 }
301 }
302 }
303 }
304
305 return rKey;
306}
307
308uno::Reference< i18n::XCharacterClassification > const & MnemonicGenerator::GetCharClass()
309{
310 if ( !mxCharClass.is() )
312 return mxCharClass;
313}
314
315OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
316{
317 OUString aStr = rStr;
318 sal_Int32 nLen = aStr.getLength();
319 sal_Int32 i = 0;
320
321 while ( i < nLen )
322 {
323 if ( aStr[ i ] == '~' )
324 {
325 // check for CJK-style mnemonic
326 if( i > 0 && (i+2) < nLen )
327 {
328 sal_Unicode c = sal_Unicode(rtl::toAsciiLowerCase(aStr[i+1]));
329 if( aStr[ i-1 ] == '(' &&
330 aStr[ i+2 ] == ')' &&
332 {
333 aStr = aStr.replaceAt( i-1, 4, u"" );
334 nLen -= 4;
335 i--;
336 continue;
337 }
338 }
339
340 // remove standard mnemonics
341 aStr = aStr.replaceAt( i, 1, u"" );
342 nLen--;
343 }
344 else
345 i++;
346 }
347
348 return aStr;
349}
350
351/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:761
MnemonicGenerator(sal_Unicode cMnemonic=MNEMONIC_CHAR)
sal_uInt8 maMnemonics[MAX_MNEMONICS]
Definition: mnemonic.hxx:58
css::uno::Reference< css::i18n::XCharacterClassification > const & GetCharClass()
static SAL_DLLPRIVATE sal_uInt16 ImplGetMnemonicIndex(sal_Unicode c)
sal_Unicode m_cMnemonic
Definition: mnemonic.hxx:56
void RegisterMnemonic(const OUString &rKey)
SAL_DLLPRIVATE sal_Unicode ImplFindMnemonic(const OUString &rKey)
static OUString EraseAllMnemonicChars(const OUString &rStr)
css::uno::Reference< css::i18n::XCharacterClassification > mxCharClass
Definition: mnemonic.hxx:59
OUString CreateMnemonic(const OUString &rKey)
MnemonicGenerator & operator=(MnemonicGenerator const &)
static bool isCJK(LanguageType nLang)
float u
sal_Int32 nIndex
#define SAL_WARN(area, stream)
aStr
#define MNEMONIC_RANGE_2_START
Definition: mnemonic.hxx:34
#define MNEMONIC_RANGE_1_START
Definition: mnemonic.hxx:31
#define MNEMONIC_INDEX_NOTFOUND
Definition: mnemonic.hxx:49
#define MNEMONIC_RANGE_3_END
Definition: mnemonic.hxx:38
#define MNEMONIC_RANGE_1_END
Definition: mnemonic.hxx:32
#define MNEMONIC_RANGE_3_START
Definition: mnemonic.hxx:37
#define MNEMONIC_RANGES
Definition: mnemonic.hxx:42
#define MNEMONIC_RANGE_4_START
Definition: mnemonic.hxx:40
#define MNEMONIC_RANGE_2_END
Definition: mnemonic.hxx:35
#define MNEMONIC_RANGE_4_END
Definition: mnemonic.hxx:41
int i
css::uno::Reference< css::i18n::XCharacterClassification > CreateCharacterClassification()
Definition: unohelp.cxx:43
sal_uInt16 sal_Unicode