Line data Source code
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>
27 : #include <i18nlangtag/mslangid.hxx>
28 :
29 : using namespace ::com::sun::star;
30 :
31 22 : MnemonicGenerator::MnemonicGenerator()
32 : {
33 22 : memset( maMnemonics, 1, sizeof( maMnemonics ) );
34 22 : }
35 :
36 749 : sal_uInt16 MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c )
37 : {
38 : static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
39 : {
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
44 : };
45 :
46 749 : sal_uInt16 nMnemonicIndex = 0;
47 1651 : for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
48 : {
49 2996 : if ( (c >= aImplMnemonicRangeTab[i*2]) &&
50 1396 : (c <= aImplMnemonicRangeTab[i*2+1]) )
51 698 : return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
52 :
53 902 : nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
54 : }
55 :
56 51 : return MNEMONIC_INDEX_NOTFOUND;
57 : }
58 :
59 457 : sal_Unicode MnemonicGenerator::ImplFindMnemonic( const OUString& rKey )
60 : {
61 457 : sal_Int32 nIndex = 0;
62 914 : while ( (nIndex = rKey.indexOf( MNEMONIC_CHAR, nIndex )) != -1 )
63 : {
64 330 : sal_Unicode cMnemonic = rKey[ nIndex+1 ];
65 330 : if ( cMnemonic != MNEMONIC_CHAR )
66 330 : return cMnemonic;
67 0 : nIndex += 2;
68 : }
69 :
70 127 : return 0;
71 : }
72 :
73 248 : void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
74 : {
75 248 : const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
76 248 : uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
77 :
78 : // Don't crash even when we don't have access to i18n service
79 248 : if ( !xCharClass.is() )
80 248 : return;
81 :
82 496 : OUString aKey = xCharClass->toUpper( rKey, 0, rKey.getLength(), rLocale );
83 :
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 248 : sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
88 248 : if ( cMnemonic )
89 : {
90 165 : sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
91 165 : if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
92 160 : maMnemonics[nMnemonicIndex] = 0;
93 : }
94 : else
95 : {
96 83 : sal_Int32 nIndex = 0;
97 83 : sal_Int32 nLen = aKey.getLength();
98 569 : while ( nIndex < nLen )
99 : {
100 403 : sal_Unicode c = aKey[ nIndex ];
101 :
102 403 : sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
103 403 : if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
104 : {
105 366 : if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
106 259 : maMnemonics[nMnemonicIndex]++;
107 : }
108 :
109 403 : nIndex++;
110 : }
111 248 : }
112 : }
113 :
114 241 : OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
115 : {
116 241 : if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
117 197 : return _rKey;
118 :
119 44 : const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
120 44 : uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
121 :
122 : // Don't crash even when we don't have access to i18n service
123 44 : if ( !xCharClass.is() )
124 0 : return _rKey;
125 :
126 88 : OUString aKey = xCharClass->toUpper( _rKey, 0, _rKey.getLength(), rLocale );
127 :
128 44 : bool bChanged = false;
129 44 : sal_Int32 nLen = aKey.getLength();
130 :
131 44 : bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
132 :
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
136 :
137 : // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
138 44 : if( bCJK )
139 : {
140 0 : bool bLatinOnly = true;
141 0 : bool bMnemonicIndexFound = false;
142 : sal_Unicode c;
143 : sal_Int32 nIndex;
144 :
145 0 : for( nIndex=0; nIndex < nLen; nIndex++ )
146 : {
147 0 : c = aKey[ nIndex ];
148 0 : if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
149 0 : ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
150 : {
151 0 : bLatinOnly = false;
152 0 : break;
153 : }
154 0 : if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
155 0 : bMnemonicIndexFound = true;
156 : }
157 0 : if( bLatinOnly && !bMnemonicIndexFound )
158 0 : return _rKey;
159 : }
160 :
161 88 : OUString rKey(_rKey);
162 44 : int nCJK = 0;
163 : sal_uInt16 nMnemonicIndex;
164 : sal_Unicode c;
165 44 : sal_Int32 nIndex = 0;
166 44 : if( !bCJK )
167 : {
168 : // 1) first try the first character of a word
169 20 : do
170 : {
171 48 : c = aKey[ nIndex ];
172 :
173 48 : if ( nCJK != 2 )
174 : {
175 44 : if ( ((c >= 0x3000) && (c <= 0xD7FF)) || // cjk
176 0 : ((c >= 0xFF61) && (c <= 0xFFDC)) ) // halfwidth forms
177 0 : nCJK = 1;
178 44 : else if ( ((c >= 0x0030) && (c <= 0x0039)) || // digits
179 44 : ((c >= 0x0041) && (c <= 0x005A)) || // latin capitals
180 0 : ((c >= 0x0061) && (c <= 0x007A)) || // latin small
181 0 : ((c >= 0x0370) && (c <= 0x037F)) || // greek numeral signs
182 0 : ((c >= 0x0400) && (c <= 0x04FF)) ) // cyrillic
183 44 : nCJK = 2;
184 : }
185 :
186 48 : nMnemonicIndex = ImplGetMnemonicIndex( c );
187 48 : if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
188 : {
189 48 : if ( maMnemonics[nMnemonicIndex] )
190 : {
191 28 : maMnemonics[nMnemonicIndex] = 0;
192 28 : rKey = rKey.replaceAt( nIndex, 0, OUString(MNEMONIC_CHAR) );
193 28 : bChanged = true;
194 28 : break;
195 : }
196 : }
197 :
198 : // Search for next word
199 20 : nIndex++;
200 149 : while ( nIndex < nLen )
201 : {
202 113 : c = aKey[ nIndex ];
203 113 : if ( c == ' ' )
204 4 : break;
205 109 : nIndex++;
206 : }
207 20 : nIndex++;
208 : }
209 : while ( nIndex < nLen );
210 :
211 : // 2) search for a unique/uncommon character
212 44 : if ( !bChanged )
213 : {
214 16 : sal_uInt16 nBestCount = 0xFFFF;
215 16 : sal_uInt16 nBestMnemonicIndex = 0;
216 16 : sal_Int32 nBestIndex = 0;
217 16 : nIndex = 0;
218 133 : do
219 : {
220 133 : c = aKey[ nIndex ];
221 133 : nMnemonicIndex = ImplGetMnemonicIndex( c );
222 133 : if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
223 : {
224 124 : if ( maMnemonics[nMnemonicIndex] )
225 : {
226 18 : if ( maMnemonics[nMnemonicIndex] < nBestCount )
227 : {
228 10 : nBestCount = maMnemonics[nMnemonicIndex];
229 10 : nBestIndex = nIndex;
230 10 : nBestMnemonicIndex = nMnemonicIndex;
231 10 : if ( nBestCount == 2 )
232 0 : break;
233 : }
234 : }
235 : }
236 :
237 133 : nIndex++;
238 : }
239 : while ( nIndex < nLen );
240 :
241 16 : if ( nBestCount != 0xFFFF )
242 : {
243 6 : maMnemonics[nBestMnemonicIndex] = 0;
244 6 : rKey = rKey.replaceAt( nBestIndex, 0, OUString(MNEMONIC_CHAR) );
245 6 : bChanged = true;
246 : }
247 : }
248 : }
249 : else
250 0 : nCJK = 1;
251 :
252 : // 3) Add English Mnemonic for CJK Text
253 44 : if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
254 : {
255 : // Append Ascii Mnemonic
256 0 : for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
257 : {
258 0 : nMnemonicIndex = ImplGetMnemonicIndex( c );
259 0 : if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
260 : {
261 0 : if ( maMnemonics[nMnemonicIndex] )
262 : {
263 0 : maMnemonics[nMnemonicIndex] = 0;
264 : OUString aStr = OUStringBuffer().
265 0 : append('(').append(MNEMONIC_CHAR).append(c).
266 0 : append(')').makeStringAndClear();
267 0 : nIndex = rKey.getLength();
268 0 : if( nIndex >= 2 )
269 : {
270 0 : if ( ( rKey[nIndex-2] == '>' && rKey[nIndex-1] == '>' ) ||
271 0 : ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
272 0 : nIndex -= 2;
273 : }
274 0 : if( nIndex >= 3 )
275 : {
276 0 : if ( ( rKey[nIndex-3] == '.' && rKey[nIndex-2] == '.' && rKey[nIndex-1] == '.' ) ||
277 0 : ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
278 0 : nIndex -= 3;
279 : }
280 0 : if( nIndex >= 1)
281 : {
282 0 : sal_Unicode cLastChar = rKey[ nIndex-1 ];
283 0 : if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
284 0 : (cLastChar == '.') || (cLastChar == 0xFF0E) ||
285 0 : (cLastChar == '?') || (cLastChar == 0xFF1F) ||
286 : (cLastChar == ' ') )
287 0 : nIndex--;
288 : }
289 0 : rKey = rKey.replaceAt( nIndex, 0, aStr );
290 0 : bChanged = true;
291 0 : break;
292 : }
293 : }
294 : }
295 : }
296 :
297 : // #i87415# Duplicates mnemonics are bad for consistent keyboard accessibility
298 : // It's probably better to not have mnemonics for some widgets, than to have ambiguous ones.
299 : // if( ! bChanged )
300 : // {
301 : // /*
302 : // * #97809# if all else fails use the first character of a word
303 : // * anyway and live with duplicate mnemonics
304 : // */
305 : // nIndex = 0;
306 : // do
307 : // {
308 : // c = aKey.GetChar( nIndex );
309 :
310 : // nMnemonicIndex = ImplGetMnemonicIndex( c );
311 : // if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
312 : // {
313 : // maMnemonics[nMnemonicIndex] = 0;
314 : // rKey.Insert( MNEMONIC_CHAR, nIndex );
315 : // bChanged = true;
316 : // break;
317 : // }
318 :
319 : // // Search for next word
320 : // do
321 : // {
322 : // nIndex++;
323 : // c = aKey.GetChar( nIndex );
324 : // if ( c == ' ' )
325 : // break;
326 : // }
327 : // while ( nIndex < nLen );
328 : // nIndex++;
329 : // }
330 : // while ( nIndex < nLen );
331 : // }
332 :
333 88 : return rKey;
334 : }
335 :
336 292 : uno::Reference< i18n::XCharacterClassification > MnemonicGenerator::GetCharClass()
337 : {
338 292 : if ( !mxCharClass.is() )
339 17 : mxCharClass = vcl::unohelper::CreateCharacterClassification();
340 292 : return mxCharClass;
341 : }
342 :
343 115845 : OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
344 : {
345 115845 : OUString aStr = rStr;
346 115845 : sal_Int32 nLen = aStr.getLength();
347 115845 : sal_Int32 i = 0;
348 :
349 1961428 : while ( i < nLen )
350 : {
351 1729738 : if ( aStr[ i ] == '~' )
352 : {
353 : // check for CJK-style mnemonic
354 2614 : if( i > 0 && (i+2) < nLen )
355 : {
356 1480 : sal_Unicode c = aStr[i+1];
357 2960 : if( aStr[ i-1 ] == '(' &&
358 0 : aStr[ i+2 ] == ')' &&
359 1480 : c >= MNEMONIC_RANGE_2_START && c <= MNEMONIC_RANGE_2_END )
360 : {
361 0 : aStr = aStr.replaceAt( i-1, 4, "" );
362 0 : nLen -= 4;
363 0 : i--;
364 0 : continue;
365 : }
366 : }
367 :
368 : // remove standard mnemonics
369 2614 : aStr = aStr.replaceAt( i, 1, "" );
370 2614 : nLen--;
371 : }
372 : else
373 1727124 : i++;
374 : }
375 :
376 115845 : return aStr;
377 : }
378 :
379 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|